4bc4099e6bbec2c1d5cdd0924d820fc80759ec3f.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. package com.kingdee.shr.customer.gtiit.rpt;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.io.UnsupportedEncodingException;
  5. import java.math.BigDecimal;
  6. import java.net.URLEncoder;
  7. import java.sql.SQLException;
  8. import java.text.ParseException;
  9. import java.util.ArrayList;
  10. import java.util.Calendar;
  11. import java.util.Date;
  12. import java.util.HashMap;
  13. import java.util.LinkedHashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.stream.Collectors;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import org.apache.commons.lang3.ObjectUtils;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.apache.log4j.Logger;
  22. import org.apache.poi.ss.usermodel.DataFormat;
  23. import org.apache.poi.xssf.usermodel.XSSFCell;
  24. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  25. import org.apache.poi.xssf.usermodel.XSSFRow;
  26. import org.apache.poi.xssf.usermodel.XSSFSheet;
  27. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  28. import org.springframework.ui.ModelMap;
  29. import com.alibaba.fastjson.JSON;
  30. import com.alibaba.fastjson.JSONObject;
  31. import com.kingdee.bos.BOSException;
  32. import com.kingdee.bos.Context;
  33. import com.kingdee.eas.hr.base.HRBillStateEnum;
  34. import com.kingdee.eas.util.app.DbUtil;
  35. import com.kingdee.jdbc.rowset.IRowSet;
  36. import com.kingdee.shr.base.syssetting.context.SHRContext;
  37. import com.kingdee.shr.base.syssetting.exception.SHRWebException;
  38. import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
  39. import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
  40. import com.kingdee.shr.customer.gtiit.util.DateTimeUtils;
  41. import com.kingdee.shr.customer.gtiit.util.ExportUtils;
  42. import com.kingdee.shr.customer.gtiit.util.RptUtils;
  43. /**
  44. * 教师缺勤报告
  45. * @author xiaoxin
  46. *
  47. */
  48. public class FacultyAbsenceReportListHandler extends ListHandler {
  49. private static Logger logger =
  50. Logger.getLogger("com.kingdee.shr.customer.gtiit.rpt.FacultyAbsenceReportListHandler");
  51. Context ctx = SHRContext.getInstance().getContext();
  52. private int totalCount;
  53. public FacultyAbsenceReportListHandler() {
  54. }
  55. public String getGridColModelAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
  56. throws SHRWebException, BOSException, SQLException {
  57. List<String> colNames = this.getcolNamesAction();
  58. List<Map<String, Object>> colModel = this.getcolModelAction();
  59. LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
  60. map.put("colNames", colNames);
  61. map.put("colModel", colModel);
  62. JSONUtils.writeJson(response, map);
  63. return null;
  64. }
  65. public void getGridDataAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
  66. throws BOSException, SQLException, SHRWebException, ParseException {
  67. int rows = Integer.parseInt(request.getParameter("rows"));
  68. int page = Integer.parseInt(request.getParameter("page"));
  69. List<Map<String, Object>> maplist = new ArrayList<Map<String, Object>>();
  70. int num = 0;
  71. // 获取报表查询过滤条件
  72. List<String> qfNames = new ArrayList<>();
  73. qfNames.add("queryTime");
  74. String parameter = request.getParameter("fastFilterItems");
  75. JSONObject paramJson = JSON.parseObject(parameter);
  76. Map<String, Object> rptSQLFilterate = RptUtils.getRptSQLFilterate(paramJson, qfNames);
  77. Map<String, Object> contractDateMap = (Map<String, Object>) rptSQLFilterate.get("queryTime");
  78. Date startDate = null;
  79. Date endDate = null;
  80. if(ObjectUtils.isNotEmpty(contractDateMap.get("startDate")) && ObjectUtils.isNotEmpty(contractDateMap.get("endDate"))) {
  81. startDate = DateTimeUtils.parseDate((String)contractDateMap.get("startDate")+" 08:30", "yyyy-MM-dd HH:mm");
  82. endDate = DateTimeUtils.parseDate((String)contractDateMap.get("endDate")+" 17:30", "yyyy-MM-dd HH:mm");
  83. }
  84. // 获取报表数据源
  85. String sql =getSql(rptSQLFilterate);
  86. String searchFilterItems = request.getParameter("searchFilterItems");
  87. if (StringUtils.isNoneBlank(searchFilterItems)) {
  88. String filterItmes = getSearchFilterItems(searchFilterItems);
  89. sql = sql + " and " + filterItmes;
  90. }
  91. IRowSet rs = DbUtil.executeQuery(this.ctx, sql);
  92. while (rs.next()) {
  93. ++num;
  94. if (num >= rows * (page - 1) + 1 && num <= rows * page) {
  95. Map<String, Object> map = new HashMap<String, Object>();
  96. map.put("StaffID", rs.getString("StaffID"));
  97. map.put("Name", rs.getString("Name"));
  98. map.put("PhysicsProgram", rs.getString("PhysicsProgram"));
  99. map.put("Title", rs.getString("Title"));
  100. map.put("Document No.", rs.getString("Document No."));
  101. map.put("Type", rs.getString("Type"));
  102. map.put("Purpose", rs.getString("Purpose"));
  103. map.put("Location", rs.getString("Location"));
  104. map.put("Course", rs.getString("Course"));
  105. map.put("ReplacerNameForCourse", rs.getString("ReplacerNameForCourse"));
  106. map.put("Status", rs.getString("Status"));
  107. Date leaveStartDate = rs.getDate("BusinessTripStartDate");
  108. Date leaveEndDate = rs.getDate("BusinessTripEndDate");
  109. if(startDate != null && endDate != null) {
  110. if(startDate.compareTo(leaveStartDate) > 0) {
  111. leaveStartDate = startDate;
  112. }
  113. if(endDate.compareTo(leaveEndDate) < 0) {
  114. leaveEndDate = endDate;
  115. }
  116. }
  117. map.put("BusinessTripStartDate", leaveStartDate);
  118. map.put("BusinessTripEndDate", leaveEndDate);
  119. maplist.add(map);
  120. }
  121. }
  122. this.totalCount = num;
  123. Map<String, Object> gridDataMap = new LinkedHashMap<String, Object>();
  124. if (this.totalCount % rows == 0) {
  125. gridDataMap.put("total", this.totalCount / rows);
  126. } else {
  127. gridDataMap.put("total", this.totalCount / rows + 1);
  128. }
  129. gridDataMap.put("page", page);
  130. gridDataMap.put("records", this.totalCount);
  131. gridDataMap.put("rows", maplist);
  132. JSONUtils.writeJson(response, gridDataMap);
  133. }
  134. public ArrayList<String> getcolNamesAction() throws SHRWebException {
  135. ArrayList<String> list = new ArrayList<String>();
  136. list.add("StaffID");
  137. list.add("Name");
  138. list.add("PhysicsProgram");
  139. list.add("Title");
  140. list.add("Document No.");
  141. list.add("Type");
  142. list.add("BusinessTripStartDate");
  143. list.add("BusinessTripEndDate");
  144. list.add("Purpose");
  145. list.add("Location");
  146. list.add("Course");
  147. list.add("ReplacerNameForCourse");
  148. list.add("Status");
  149. return list;
  150. }
  151. public List<Map<String, Object>> getcolModelAction() {
  152. List<Map<String, Object>> modelColList = new ArrayList<Map<String, Object>>();
  153. modelColList.add(this.buildColModelMap("StaffID", "工号", 70));
  154. modelColList.add(this.buildColModelMap("Name", "姓", 120));
  155. modelColList.add(this.buildColModelMap("PhysicsProgram", "部门", 170));
  156. modelColList.add(this.buildColModelMap("Title", "岗位", 160));
  157. modelColList.add(this.buildColModelMap("Document No.", "请假单号", 120));
  158. modelColList.add(this.buildColModelMap("Type", "假勤类型", 130));
  159. modelColList.add(this.buildColModelMap("BusinessTripStartDate", "开始时间", 160));
  160. modelColList.add(this.buildColModelMap("BusinessTripEndDate", "结束时间", 160));
  161. modelColList.add(this.buildColModelMap("Purpose", "目的", 120));
  162. modelColList.add(this.buildColModelMap("Location", "位置", 120));
  163. modelColList.add(this.buildColModelMap("Course", "课程", 160));
  164. modelColList.add(this.buildColModelMap("ReplacerNameForCourse", "课程的替代者名称", 120));
  165. modelColList.add(this.buildColModelMap("Status", "单据状态", 90));
  166. return modelColList;
  167. }
  168. private Map<String, Object> buildColModelMap(String index, String label, int width) {
  169. return this.buildColModelMap(index, label, width, false, false, false);
  170. }
  171. private Map<String, Object> buildColModelMap(String index, String label, int width, boolean rowspan, boolean isKey,
  172. boolean isHedden) {
  173. Map<String, Object> gridIdMap = new LinkedHashMap<String, Object>();
  174. gridIdMap.put("index", index);
  175. gridIdMap.put("name", index);
  176. gridIdMap.put("label", label);
  177. gridIdMap.put("width", width);
  178. if (rowspan) {
  179. gridIdMap.put("rowspan", rowspan);
  180. }
  181. if (isKey) {
  182. gridIdMap.put("key", isKey);
  183. }
  184. if (isHedden) {
  185. gridIdMap.put("hidden", isHedden);
  186. }
  187. return gridIdMap;
  188. }
  189. public String toExcelAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
  190. throws BOSException, SQLException, SHRWebException {
  191. List<String> qfNames = new ArrayList<>();
  192. qfNames.add("queryTime");
  193. String parameter = request.getParameter("fastFilterItems");
  194. JSONObject paramJson = JSON.parseObject(parameter);
  195. Map<String, Object> rptSQLFilterate = RptUtils.getRptSQLFilterate(paramJson, qfNames);
  196. // 获取报表数据源
  197. String sql = getSql(rptSQLFilterate);
  198. String searchFilterItems = request.getParameter("searchFilterItems");
  199. if (StringUtils.isNoneBlank(searchFilterItems)) {
  200. String filterItmes = getSearchFilterItems(searchFilterItems);
  201. sql = sql + " and " + filterItmes;
  202. }
  203. IRowSet rs = DbUtil.executeQuery(this.ctx, sql);
  204. LinkedHashMap<String, String> selectFieldsTypeMap = new LinkedHashMap<String, String>();
  205. String stringType = "String_@";
  206. String dateType = "Date_yyyy/MM/dd HH:mm";
  207. selectFieldsTypeMap.put("StaffID", "Number_0_%s");
  208. selectFieldsTypeMap.put("Name", stringType);
  209. selectFieldsTypeMap.put("PhysicsProgram", stringType);
  210. selectFieldsTypeMap.put("Title", stringType);
  211. selectFieldsTypeMap.put("Document No.", stringType);
  212. selectFieldsTypeMap.put("Type", stringType);
  213. selectFieldsTypeMap.put("BusinessTripStartDate", dateType);
  214. selectFieldsTypeMap.put("BusinessTripEndDate", dateType);
  215. selectFieldsTypeMap.put("Purpose", stringType);
  216. selectFieldsTypeMap.put("Location", stringType);
  217. selectFieldsTypeMap.put("Course", stringType);
  218. selectFieldsTypeMap.put("ReplacerNameForCourse", stringType);
  219. selectFieldsTypeMap.put("Status", stringType);
  220. exportData(response, rs, "FacultyAbsenceReport.xlsx", selectFieldsTypeMap, rptSQLFilterate);
  221. return null;
  222. }
  223. public static void exportData(HttpServletResponse response, Object resource, String fileName,
  224. LinkedHashMap<String, String> selectFieldsTypeMap, Map<String, Object> rptSQLFilterate) throws SQLException {
  225. Map<String, Object> contractDateMap = (Map<String, Object>) rptSQLFilterate.get("queryTime");
  226. Date startDate = null;
  227. Date endDate = null;
  228. if(ObjectUtils.isNotEmpty(contractDateMap.get("startDate")) && ObjectUtils.isNotEmpty(contractDateMap.get("endDate"))) {
  229. startDate = DateTimeUtils.parseDate((String)contractDateMap.get("startDate")+" 08:30", "yyyy-MM-dd HH:mm");
  230. endDate = DateTimeUtils.parseDate((String)contractDateMap.get("endDate")+" 17:30", "yyyy-MM-dd HH:mm");
  231. }
  232. // 创建新的Excel 工作簿
  233. XSSFWorkbook workBook = new XSSFWorkbook();
  234. XSSFSheet sheet = workBook.createSheet("Sheet1");
  235. DataFormat format =workBook.createDataFormat();
  236. // 设置数字格式为整数,不保留小数 (_ )空格是必须要切勿格式
  237. if (resource != null) {
  238. if (resource instanceof IRowSet) {
  239. IRowSet rs = (IRowSet) resource;
  240. // 列数
  241. int numColumns = selectFieldsTypeMap.entrySet().size();
  242. // 创建多行多列的数据
  243. int i = 0;
  244. // 创建标题
  245. XSSFRow titleRow = sheet.createRow(i);
  246. List<String> keyList = selectFieldsTypeMap.keySet().stream().collect(Collectors.toList());
  247. if (i == 0) {
  248. for (int j = 0; j < numColumns; j++) {
  249. // 创建列
  250. XSSFCell cell = titleRow.createCell(j);
  251. cell.setCellValue(keyList.get(j));
  252. }
  253. }
  254. i++;
  255. // 创建数据
  256. while (rs.next()) {
  257. try {
  258. XSSFRow dateRow = sheet.createRow(i);
  259. for (int j = 0; j < numColumns; j++) {
  260. XSSFCellStyle style = workBook.createCellStyle();
  261. // 创建列
  262. XSSFCell cell = dateRow.createCell(j);
  263. // 由于开发与测试环境的数据库类型与不一致 导致无法从数据库表读取相关字段类型判断 且有相关计算字段
  264. // 所以采用参数判断
  265. String column = keyList.get(j);
  266. String fieldTypeAndcellStyleType = selectFieldsTypeMap.get(column);
  267. int indexOf = fieldTypeAndcellStyleType.indexOf("_");
  268. // 字段属性类型
  269. String fieldType = fieldTypeAndcellStyleType.substring(0, indexOf);
  270. // 样式类型
  271. String cellStyleType = fieldTypeAndcellStyleType.substring(indexOf + 1, fieldTypeAndcellStyleType.length());
  272. if ("Date".equals(fieldType)) {
  273. // 日期
  274. Date date = rs.getDate(column);
  275. if(startDate != null && endDate != null) {
  276. date = column.equals("BusinessTripStartDate") && startDate.compareTo(date)>0?startDate:date;
  277. date = column.equals("BusinessTripEndDate") && endDate.compareTo(date)<0?endDate:date;
  278. }
  279. style.setDataFormat(format.getFormat(cellStyleType));
  280. cell.setCellStyle(style);
  281. cell.setCellValue(date);
  282. } else if ("Number".equals(fieldType)) {
  283. // 数值 (小数和整数)
  284. style.setDataFormat(format.getFormat(String.format(cellStyleType, " ")));
  285. String value = rs.getString(column);
  286. cell.setCellStyle(style);
  287. if (cellStyleType.contains(".")) {
  288. // 小数
  289. cell.setCellValue(Double.parseDouble(value));
  290. } else {
  291. cell.setCellValue(Integer.parseInt(value));
  292. }
  293. } else if ("String".equals(fieldType)) {
  294. String value = rs.getString(column);
  295. cell.setCellValue(value);
  296. }
  297. }
  298. i++;
  299. } catch (Exception e) {
  300. System.out.println();
  301. logger.info("Excel file was created Error!" + e.getMessage());
  302. }
  303. }
  304. try {
  305. ExportUtils.setResponseHeader(response, fileName);
  306. OutputStream os = response.getOutputStream();
  307. workBook.write(os);
  308. workBook.close();
  309. os.flush();
  310. os.close();
  311. logger.info("Excel file was created successfully!");
  312. } catch (IOException e) {
  313. logger.info("Error while writing to file " + e.getMessage());
  314. e.printStackTrace();
  315. }
  316. }
  317. }
  318. }
  319. private String getSql(Map<String, Object> params) {
  320. Map<String, Object> contractDateMap = (Map<String, Object>) params.get("queryTime");
  321. Object startDate = contractDateMap.get("startDate");
  322. Object endDate = contractDateMap.get("endDate");
  323. StringBuffer sql = new StringBuffer();
  324. sql.append("SELECT a.FNUMBER \"Document No.\",d.fnumber StaffID,d.fname_l1 Name,f.fname_l1 PhysicsProgram,g.fname_l1 Title,c.fname_l1 Type,");
  325. sql.append("b.FREALBEGINTIME BusinessTripStartDate,b.FREALENDTIME BusinessTripEndDate,b.CFPURPOSE Purpose,b.CFCITY Location,");
  326. sql.append("b.CFAlternativeTeacher as ReplacerNameforCourse,b.CFTeachingTask as Course,case when a.FBILLSTATE = 3 then 'Approved' else 'Pending Approval' end Status ");
  327. sql.append(" FROM T_HR_ATS_LeaveBill a ");
  328. sql.append("left join T_HR_ATS_LeaveBillEntry b on a.fid = b.fbillid ");
  329. sql.append("left join T_HR_ATS_HolidayPolicy c on c.fid=b.fpolicyid ");
  330. sql.append("left join t_bd_person d on d.fid = b.FPERSONID ");
  331. sql.append("left join (SELECT emh.fpersonid,emh.fadminorgid,emh.fpositionid,emh.CFWorkercategoryID FROM ");
  332. sql.append(" (select fpersonid,max(fleffdt) maxdate from T_HR_EmpOrgRelation ");
  333. sql.append(" where fassigntype = 1 and fislatestinaday = 1 group by fpersonid) emp ");
  334. sql.append(" left join T_HR_EmpOrgRelation emh on emp.fpersonid = emh.fpersonid and emp.maxdate = emh.fleffdt ");
  335. sql.append(" where emh.fassigntype = 1 and emh.fislatestinaday = 1) e on e.fpersonid = b.FPERSONID ");
  336. sql.append("left join T_ORG_BaseUnit f on f.fid = e.FAdminOrgID ");
  337. sql.append("left join T_ORG_Position g on g.fid = e.fpositionid ");
  338. sql.append("left join CT_MP_WorkerCategory h on h.fid = e.CFWorkercategoryID ");
  339. sql.append("where a.FBILLSTATE in ('1','2','3') and h.fnumber = 'GTIIT_FACULTY' ");
  340. if(ObjectUtils.isNotEmpty(startDate) && ObjectUtils.isNotEmpty(endDate)) {
  341. startDate = startDate + " 08:30";
  342. endDate = endDate + " 17:30";
  343. sql.append(" and b.FREALBEGINTIME <= '"+endDate+"' and b.FREALENDTIME >= '"+startDate+"' ");
  344. }
  345. logger.error("教师缺勤报告SQL:"+sql.toString());
  346. return sql.toString();
  347. }
  348. /**
  349. * 获取serch的过虑条件返回拼接sql
  350. * @param searchFilterItems
  351. * @return
  352. */
  353. public String getSearchFilterItems(String searchFilterItems) {
  354. // 获取serch的过虑条件
  355. String filter = null;
  356. if (StringUtils.isNotBlank(searchFilterItems)) {
  357. // 姓名
  358. if (searchFilterItems.indexOf("name") != -1) {
  359. filter = searchFilterItems.replaceAll("name", "d.fname_l1");
  360. }
  361. // 编码
  362. if (searchFilterItems.indexOf("number") != -1) {
  363. filter = searchFilterItems.replaceAll("number", "d.fnumber");
  364. }
  365. }
  366. if (filter != null) {
  367. return filter.replace("(", "").replace(")", "");
  368. }
  369. return filter;
  370. }
  371. }