ec2156d6e059c980537e2676b1d242baa4bdfc1b.svn-base 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /**
  2. *
  3. */
  4. package com.kingdee.shr.customer.gtiit.rpt;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.io.UnsupportedEncodingException;
  8. import java.math.BigDecimal;
  9. import java.math.RoundingMode;
  10. import java.net.URLEncoder;
  11. import java.sql.SQLException;
  12. import java.text.SimpleDateFormat;
  13. import java.util.ArrayList;
  14. import java.util.Date;
  15. import java.util.HashMap;
  16. import java.util.HashSet;
  17. import java.util.LinkedHashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import java.util.stream.Collectors;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import org.apache.commons.lang3.ObjectUtils;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.apache.log4j.Logger;
  27. import org.apache.poi.ss.usermodel.CellStyle;
  28. import org.apache.poi.ss.usermodel.DataFormat;
  29. import org.apache.poi.ss.usermodel.IndexedColors;
  30. import org.apache.poi.xssf.usermodel.XSSFCell;
  31. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  32. import org.apache.poi.xssf.usermodel.XSSFFont;
  33. import org.apache.poi.xssf.usermodel.XSSFRow;
  34. import org.apache.poi.xssf.usermodel.XSSFSheet;
  35. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  36. import org.json.JSONObject;
  37. import org.springframework.ui.ModelMap;
  38. import com.kingdee.bos.BOSException;
  39. import com.kingdee.bos.Context;
  40. import com.kingdee.eas.base.form.json.JSONException;
  41. import com.kingdee.eas.util.app.DbUtil;
  42. import com.kingdee.jdbc.rowset.IRowSet;
  43. import com.kingdee.shr.base.syssetting.context.SHRContext;
  44. import com.kingdee.shr.base.syssetting.exception.SHRWebException;
  45. import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
  46. import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
  47. import com.kingdee.shr.customer.gtiit.entity.VacationInfo;
  48. /**
  49. * @author ISSUSER FacultyAbsenceSummary报表
  50. */
  51. public class FacultyAbsenceSummaryListHandler extends ListHandler {
  52. private static Logger logger = Logger
  53. .getLogger("com.kingdee.shr.customer.gtiit.rpt.FacultyAbsenceSummaryListHandler");
  54. Context ctx = SHRContext.getInstance().getContext();
  55. private int totalCount;
  56. public String getGridColModelAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
  57. throws SHRWebException, BOSException, SQLException {
  58. List<String> colNames = this.getcolNamesAction();
  59. List<Map<String, Object>> colModel = this.getcolModelAction();
  60. LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
  61. map.put("colNames", colNames);
  62. map.put("colModel", colModel);
  63. JSONUtils.writeJson(response, map);
  64. return null;
  65. }
  66. public void getGridDataAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
  67. throws BOSException, SQLException, SHRWebException, JSONException {
  68. logger.info(">>> FacultyAbsenceSummaryReportHandler...getGridDataAction");
  69. int rows = Integer.parseInt(request.getParameter("rows"));
  70. int page = Integer.parseInt(request.getParameter("page"));
  71. // 获取过滤条件
  72. String fastFilterItems = request.getParameter("fastFilterItems");
  73. JSONObject tofastFilterItems = new JSONObject(fastFilterItems);
  74. // 获取时间的过滤条件
  75. JSONObject queryTime = tofastFilterItems.getJSONObject("queryTime");
  76. String pvalues = queryTime.getString("values");
  77. String startDate = null;
  78. String endDate = null;
  79. if (ObjectUtils.isNotEmpty(pvalues)) {
  80. JSONObject periodEmploymentValues = queryTime.getJSONObject("values");
  81. // 获取日期参数
  82. startDate = periodEmploymentValues.getString("startDate");
  83. endDate = periodEmploymentValues.getString("endDate");
  84. }
  85. // 获取审批通过的sql
  86. String approvedSql = this.approvedSql(startDate, endDate);
  87. // 获取sercrh的过虑
  88. String searchFilterItems = request.getParameter("searchFilterItems");
  89. if (StringUtils.isNoneBlank(searchFilterItems)) {
  90. String filterItmes = getSearchFilterItems(searchFilterItems);
  91. approvedSql = approvedSql + " and " + filterItmes;
  92. }
  93. // 查询审批通过的
  94. IRowSet rs = DbUtil.executeQuery(this.ctx, approvedSql);
  95. // 工作日天数
  96. List<String> workDaysList = workDays(startDate, endDate);
  97. // BigDecimal bigDecimalWork = new BigDecimal(workDaysList.size());
  98. // 获取审批通过计算后的数据
  99. Map<String, VacationInfo> approvedlMap = executeMethod(rs, startDate, endDate, workDaysList);
  100. int num = 0;
  101. List<Map<String, Object>> maplist = new ArrayList<Map<String, Object>>();
  102. rs.beforeFirst();
  103. Set<String> numberSet = new HashSet<String>();
  104. while (rs.next()) {
  105. if (!numberSet.contains(rs.getString("staffid"))) {
  106. ++num;
  107. if (num >= rows * (page - 1) + 1 && num <= rows * page) {
  108. Map<String, Object> map = new HashMap<String, Object>();
  109. VacationInfo vacationInfo = approvedlMap.get(rs.getString("staffid"));
  110. map.put("StaffID", rs.getString("staffid"));
  111. map.put("Name", rs.getString("name"));
  112. map.put("Title", rs.getString("title"));
  113. map.put("AcademicTitle", rs.getString("academictitle"));
  114. map.put("Program", rs.getString("program"));
  115. map.put("AnnualLeave(Days)", vacationInfo.getAnnualLeave());
  116. map.put("BusinessTrip(Days)", vacationInfo.getBusinessTrip());
  117. map.put("OtherLeave(Days)", vacationInfo.getOtherLeave());
  118. map.put("InCampus(Days)", vacationInfo.getInCampus());
  119. map.put("AnnualLeave(%)", vacationInfo.getPercentageAnnualLeave());
  120. map.put("BusinessTrip(%)", vacationInfo.getPercentageBusinessTrip());
  121. map.put("OtherLeave(%)", vacationInfo.getPercentageOtherLeave());
  122. map.put("InCampus(%)", vacationInfo.getPercentageInCampus());
  123. maplist.add(map);
  124. }
  125. }
  126. numberSet.add(rs.getString("staffid"));
  127. }
  128. System.out.print("num是" + num);
  129. this.totalCount = num;
  130. Map<String, Object> gridDataMap = new LinkedHashMap<String, Object>();
  131. if (this.totalCount % rows == 0) {
  132. gridDataMap.put("total", this.totalCount / rows);
  133. } else {
  134. gridDataMap.put("total", this.totalCount / rows + 1);
  135. }
  136. gridDataMap.put("page", page);
  137. gridDataMap.put("records", this.totalCount);
  138. gridDataMap.put("rows", maplist);
  139. JSONUtils.writeJson(response, gridDataMap);
  140. }
  141. public Map<String, VacationInfo> executeMethod(IRowSet rs, String startDate, String endDate, List<String> list) {
  142. // 年假id
  143. Set<String> policySet = getPolicy();
  144. // 出差假id
  145. Set<String> businessIdSet = getBusinessId();
  146. // 其他假期id
  147. Set<String> otherIdSet = otherId();
  148. // 计算假期数据
  149. Map<String, VacationInfo> calcuteVacationMap = calcuteVacation(rs, policySet, otherIdSet, businessIdSet, list);
  150. // List<String> workDaysList = workDays(startDate, endDate);
  151. // BigDecimal bigDecimalWork = new BigDecimal(workDaysList.size());
  152. Map<String, VacationInfo> percentageLeavelMap = percentageLeavel(calcuteVacationMap,
  153. new BigDecimal(list.size()));
  154. return percentageLeavelMap;
  155. }
  156. public ArrayList<String> getcolNamesAction() throws SHRWebException {
  157. ArrayList<String> list = new ArrayList<String>();
  158. list.add("Staff ID");
  159. list.add("Name");
  160. list.add("Title");
  161. list.add("Academic Title");
  162. list.add("Program");
  163. list.add("Annual Leave (Days)");
  164. list.add("Business Trip (Days)");
  165. list.add("Other Leave (Days)");
  166. list.add("In Campus (Days)");
  167. list.add("Annual Leave (%)");
  168. list.add("Business Trip (%)");
  169. list.add("Other Leave (%)");
  170. list.add("In Campus (%)");
  171. return list;
  172. }
  173. public List<Map<String, Object>> getcolModelAction() {
  174. List<Map<String, Object>> modelColList = new ArrayList<Map<String, Object>>();
  175. modelColList.add(this.buildColModelMap("StaffID", "Staff ID", 120));
  176. modelColList.add(this.buildColModelMap("Name", "Name", 120));
  177. modelColList.add(this.buildColModelMap("Title", "Title", 120));
  178. modelColList.add(this.buildColModelMap("AcademicTitle", "Academic Title", 120));
  179. modelColList.add(this.buildColModelMap("Program", "Program", 120));
  180. modelColList.add(this.buildColModelMap("AnnualLeave(Days)", "Annual Leave (Days)", 120));
  181. modelColList.add(this.buildColModelMap("BusinessTrip(Days)", "Business Trip (Days)", 120));
  182. modelColList.add(this.buildColModelMap("OtherLeave(Days)", "Other Leave (Days)", 120));
  183. modelColList.add(this.buildColModelMap("InCampus(Days)", "In Campus (Days)", 120));
  184. modelColList.add(this.buildColModelMap("AnnualLeave(%)", "Annual Leave (%)", 120));
  185. modelColList.add(this.buildColModelMap("BusinessTrip(%)", "Business Trip (%)", 120));
  186. modelColList.add(this.buildColModelMap("OtherLeave(%)", "Other Leave (%)", 120));
  187. modelColList.add(this.buildColModelMap("InCampus(%)", "In Campus (%)", 120));
  188. return modelColList;
  189. }
  190. private Map<String, Object> buildColModelMap(String index, String label, int width) {
  191. return this.buildColModelMap(index, label, width, false, false, false);
  192. }
  193. private Map<String, Object> buildColModelMap(String index, String label, int width, boolean rowspan, boolean isKey,
  194. boolean isHedden) {
  195. Map<String, Object> gridIdMap = new LinkedHashMap<String, Object>();
  196. gridIdMap.put("index", index);
  197. gridIdMap.put("name", index);
  198. gridIdMap.put("label", label);
  199. gridIdMap.put("width", width);
  200. if (rowspan) {
  201. gridIdMap.put("rowspan", rowspan);
  202. }
  203. if (isKey) {
  204. gridIdMap.put("key", isKey);
  205. }
  206. if (isHedden) {
  207. gridIdMap.put("hidden", isHedden);
  208. }
  209. return gridIdMap;
  210. }
  211. /***
  212. * 查询审批通过的假期
  213. */
  214. public String approvedSql(String startDate, String endDate) {
  215. // 获取当前日期
  216. Date nowDate = new Date();
  217. String nowdate = this.getDate(nowDate);
  218. StringBuffer sql = new StringBuffer();
  219. sql.append(
  220. "SELECT g.fnumber as staffid,d.fname_l1 as title,d.cflongname_l1 as AcademicTitle , e.fname_l1 as Program,g.fname_l1 as name,g.fid,j.toleavelenth as leavelenth,j.fpolicyid as cfpolicy,g.fid,j.fleavedate\r\n"
  221. + " FROM T_HR_EmpOrgRelation a \r\n"
  222. + "left join T_ORG_Position d on d.fid = a.fpositionid left join T_ORG_BaseUnit e on e.fid = a.FAdminOrgID left join T_ORG_Job f on f.fid=d.fjobid\r\n"
  223. + "left join t_bd_person g on g.fid=a.fpersonid\r\n"
  224. + "left join T_HR_EmpLaborRelationHis i on i.fpersonid=g.fid\r\n"
  225. + "left join T_HR_BDEmployeeType h on h.fid=i.flaborrelationstateid\r\n"
  226. + "left join (SELECT case when a.fleaveunit=2 then ROUND(a.fleavelength / 7.5, 2) else a.fleavelength end as toleavelenth,a.* FROM T_HR_ATS_LeaveBillDetail a where a.fleavedate >='"
  227. + startDate + "' and a.fleavedate <='" + endDate + "')j\r\n"
  228. + "on j.fproposerid=a.fpersonid\r\n"
  229. + "where f.fnumber='114'and d.fname_l1 not LIKE '%Visiting%'\r\n" + "and i.feffdt<='" + nowdate
  230. + "' and i.fleffdt>='" + nowdate + "'\r\n" + "and a.feffdt<='" + nowdate + "' and a.fleffdt>='"
  231. + nowdate + "'\r\n" + "and h.finservice='1'");
  232. System.out.print("summary审批通过的sql" + sql.toString());
  233. return sql.toString();
  234. }
  235. /***
  236. * 假期类型是年假 cfpolicy
  237. */
  238. public Set<String> getPolicy() {
  239. Set<String> policySet = new HashSet<>();
  240. String sql = "SELECT cfpolicy FROM CT_ATS_AtsLeaveBillDetailSame where cfpolicy in(select fid from T_HR_ATS_HolidayPolicy where fholidaytypeid in (select fid from T_HR_ATS_HolidayType where fnumber in('JQLX000001Y')));";
  241. try {
  242. IRowSet rs = DbUtil.executeQuery(this.ctx, sql);
  243. while (rs.next()) {
  244. policySet.add(rs.getString("cfpolicy"));
  245. }
  246. } catch (BOSException | SQLException e) {
  247. // TODO 自动生成的 catch 块
  248. e.printStackTrace();
  249. }
  250. return policySet;
  251. }
  252. /***
  253. * 内非年假、出差的 假期类型的id
  254. */
  255. public Set<String> otherId() {
  256. Set<String> otherSet = new HashSet<>();
  257. String sql = "SELECT cfpolicy FROM CT_ATS_AtsLeaveBillDetailSame where cfpolicy in(select fid from T_HR_ATS_HolidayPolicy where fholidaytypeid in (select fid from T_HR_ATS_HolidayType where fnumber not in('JQLX000020Y','JQLX000001Y')));";
  258. try {
  259. IRowSet rs = DbUtil.executeQuery(this.ctx, sql);
  260. while (rs.next()) {
  261. otherSet.add(rs.getString("cfpolicy"));
  262. }
  263. } catch (BOSException | SQLException e) {
  264. // TODO 自动生成的 catch 块
  265. e.printStackTrace();
  266. }
  267. return otherSet;
  268. }
  269. /***
  270. * 出差假的id
  271. */
  272. public Set<String> getBusinessId() {
  273. Set<String> businessSet = new HashSet<>();
  274. String sql = "SELECT cfpolicy FROM CT_ATS_AtsLeaveBillDetailSame where cfpolicy in(select fid from T_HR_ATS_HolidayPolicy where fholidaytypeid in (select fid from T_HR_ATS_HolidayType where fnumber in('JQLX000020Y')));";
  275. try {
  276. IRowSet rs = DbUtil.executeQuery(this.ctx, sql);
  277. while (rs.next()) {
  278. businessSet.add(rs.getString("cfpolicy"));
  279. }
  280. } catch (BOSException | SQLException e) {
  281. // TODO 自动生成的 catch 块
  282. e.printStackTrace();
  283. }
  284. return businessSet;
  285. }
  286. /***
  287. * 计算假期数据
  288. *
  289. * @param rs
  290. * 查询的假期数据
  291. * @param policySet
  292. * 假期类型是年假 id
  293. * @param otherSet
  294. * @param days
  295. * 天数 businessSet 出差id dateList 工作日期
  296. */
  297. public Map<String, VacationInfo> calcuteVacation(IRowSet rs, Set<String> policySet, Set<String> otherSet,
  298. Set<String> businessSet, List<String> dateList) {
  299. // 创建map key是工号 value是存储数据的对象
  300. Map<String, VacationInfo> vacationMap = new HashMap<>();
  301. // 将int值转换为BigDecimal
  302. // BigDecimal intAsBigDecimal = new BigDecimal(days);
  303. try {
  304. while (rs.next()) {
  305. VacationInfo vaction = new VacationInfo();
  306. // 工号
  307. String stattId = rs.getString("staffid");
  308. // 请假日期fleavedate
  309. String leavedate = getDate(rs.getDate("fleavedate"));
  310. // 假期长度 leavelenth
  311. BigDecimal leavelenth = rs.getBigDecimal("leavelenth") == null ? new BigDecimal("0.00")
  312. : rs.getBigDecimal("leavelenth").setScale(2, RoundingMode.HALF_UP);
  313. // 假期类型id
  314. String cfpolicy = rs.getString("cfpolicy");
  315. // 判断key 是否存在
  316. if (!vacationMap.containsKey(stattId)) {
  317. // 判断是否是年假 其他字段先设置为0
  318. if (policySet.contains(cfpolicy)) {
  319. vaction.setAnnualLeave(leavelenth);
  320. vaction.setBusinessTrip(new BigDecimal("0.00"));
  321. vaction.setOtherLeave(new BigDecimal("0.00"));
  322. }
  323. // 出差假
  324. if (businessSet.contains(cfpolicy)) {
  325. // 是工作日才计算
  326. if (dateList.contains(leavedate)) {
  327. vaction.setBusinessTrip(leavelenth);
  328. }
  329. vaction.setAnnualLeave(new BigDecimal("0.00"));
  330. vaction.setOtherLeave(new BigDecimal("0.00"));
  331. }
  332. // 其他假期
  333. if (otherSet.contains(cfpolicy)) {
  334. vaction.setOtherLeave(leavelenth);
  335. vaction.setBusinessTrip(new BigDecimal("0.00"));
  336. vaction.setAnnualLeave(new BigDecimal("0.00"));
  337. }
  338. vacationMap.put(stattId, vaction);
  339. } else {
  340. // 存在就把假期要累加起来
  341. VacationInfo vacationInfo = vacationMap.get(stattId);
  342. // 判断是否是年假
  343. if (policySet.contains(cfpolicy)) {
  344. BigDecimal annualLeave = vacationInfo.getAnnualLeave();
  345. if(annualLeave!=null) {
  346. vacationInfo.setAnnualLeave(annualLeave.add(leavelenth));
  347. }else {
  348. vacationInfo.setAnnualLeave(new BigDecimal(0).add(leavelenth));
  349. }
  350. }
  351. // 出差假
  352. if (businessSet.contains(cfpolicy)) {
  353. BigDecimal businessTrip = vacationInfo.getBusinessTrip();
  354. // 是工作日才计算
  355. if (dateList.contains(leavedate)) {
  356. if(businessTrip!=null) {
  357. vacationInfo.setBusinessTrip(businessTrip.add(leavelenth));
  358. }else {
  359. vacationInfo.setBusinessTrip(new BigDecimal(0).add(leavelenth));
  360. }
  361. }
  362. }
  363. // 其他假期
  364. if (otherSet.contains(cfpolicy)) {
  365. BigDecimal otherLeave = vacationInfo.getOtherLeave();
  366. vacationInfo.setOtherLeave(otherLeave.add(leavelenth));
  367. }
  368. vacationMap.put(stattId, vacationInfo);
  369. }
  370. }
  371. } catch (SQLException e) {
  372. // TODO 自动生成的 catch 块
  373. e.printStackTrace();
  374. }
  375. return vacationMap;
  376. }
  377. /***
  378. * 计算假期的百分比
  379. */
  380. public Map<String, VacationInfo> percentageLeavel(Map<String, VacationInfo> vacationMap,
  381. BigDecimal intAsBigDecimal) {
  382. System.out.print("工作日天数" + intAsBigDecimal);
  383. // 创建map key是工号 value是存储数据的对象
  384. Map<String, VacationInfo> peracentageMap = new HashMap<>();
  385. for (String key : vacationMap.keySet()) {
  386. VacationInfo vacationInfo = vacationMap.get(key);
  387. // 年假
  388. BigDecimal annualLeave = vacationInfo.getAnnualLeave() == null ? new BigDecimal("0.00")
  389. : vacationInfo.getAnnualLeave();
  390. // 出差假
  391. BigDecimal businessTrip = vacationInfo.getBusinessTrip() == null ? new BigDecimal("0.00")
  392. : vacationInfo.getBusinessTrip();
  393. // 其他假期
  394. BigDecimal otherLeave = vacationInfo.getOtherLeave() == null ? new BigDecimal("0.00")
  395. : vacationInfo.getOtherLeave();
  396. // 计算工作天数
  397. BigDecimal inCampus = intAsBigDecimal.subtract(annualLeave).subtract(businessTrip).subtract(otherLeave);
  398. vacationInfo.setInCampus(inCampus);
  399. // 年假所占的百分比percentage
  400. BigDecimal percentageAnnualeave = annualLeave.divide(intAsBigDecimal, 4, RoundingMode.HALF_UP);
  401. // 出差假所占的百分比
  402. BigDecimal percentagebusinessTrip = businessTrip.divide(intAsBigDecimal, 4, RoundingMode.HALF_UP);
  403. // 其他假期所占的百分比
  404. BigDecimal percentageotherLeave = otherLeave.divide(intAsBigDecimal, 4, RoundingMode.HALF_UP);
  405. // 工作日期所占的百分比
  406. // BigDecimal percentageinCampus = inCampus.divide(intAsBigDecimal, 2,
  407. // RoundingMode.HALF_UP);
  408. BigDecimal one = new BigDecimal(1);
  409. BigDecimal percentageinCampus = one.subtract(percentageAnnualeave).subtract(percentagebusinessTrip)
  410. .subtract(percentageotherLeave);
  411. vacationInfo.setPercentageAnnualLeave(percentageAnnualeave.multiply(new BigDecimal(100)));
  412. vacationInfo.setPercentageBusinessTrip(percentagebusinessTrip.multiply(new BigDecimal(100)));
  413. vacationInfo.setPercentageOtherLeave(percentageotherLeave.multiply(new BigDecimal(100)));
  414. vacationInfo.setPercentageInCampus(percentageinCampus.multiply(new BigDecimal(100)));
  415. vacationInfo.setAnnualLeave(annualLeave);
  416. vacationInfo.setOtherLeave(otherLeave);
  417. vacationInfo.setBusinessTrip(businessTrip);
  418. peracentageMap.put(key, vacationInfo);
  419. }
  420. return peracentageMap;
  421. }
  422. /***
  423. * 日期范围类工作日的天数
  424. */
  425. public List<String> workDays(String startDate, String endDate) {
  426. List<String> workList = new ArrayList<>();
  427. String sql = "SELECT * FROM T_HR_ATS_WorkCalendarItem where fdaytype='0' and fdate>='" + startDate
  428. + "' and fdate<='" + endDate + "'";
  429. try {
  430. IRowSet executeQuery = DbUtil.executeQuery(this.ctx, sql);
  431. while (executeQuery.next()) {
  432. workList.add(getDate(executeQuery.getDate("fdate")));
  433. }
  434. } catch (BOSException | SQLException e) {
  435. // TODO 自动生成的 catch 块
  436. e.printStackTrace();
  437. }
  438. return workList;
  439. }
  440. // 导出
  441. // 导出数据
  442. public String toExcelAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
  443. throws BOSException, SQLException, SHRWebException {
  444. // 构建参数去查询数据库
  445. // 获取过滤条件
  446. String fastFilterItems = request.getParameter("fastFilterItems");
  447. JSONObject tofastFilterItems = new JSONObject(fastFilterItems);
  448. // 获取时间的过滤条件
  449. JSONObject queryTime = tofastFilterItems.getJSONObject("queryTime");
  450. String pvalues = queryTime.getString("values");
  451. String startDate = null;
  452. String endDate = null;
  453. if (ObjectUtils.isNotEmpty(pvalues)) {
  454. JSONObject periodEmploymentValues = queryTime.getJSONObject("values");
  455. // 获取日期参数
  456. startDate = periodEmploymentValues.getString("startDate");
  457. endDate = periodEmploymentValues.getString("endDate");
  458. }
  459. // 获取审批通过的sql
  460. String approvedSql = this.approvedSql(startDate, endDate);
  461. // 获取sercrh的过虑
  462. String searchFilterItems = request.getParameter("searchFilterItems");
  463. if (StringUtils.isNoneBlank(searchFilterItems)) {
  464. String filterItmes = getSearchFilterItems(searchFilterItems);
  465. approvedSql = approvedSql + " and " + filterItmes;
  466. }
  467. // 查询审批通过的
  468. IRowSet rs = DbUtil.executeQuery(this.ctx, approvedSql);
  469. // 工作日天数
  470. List<String> workDaysList = workDays(startDate, endDate);
  471. // BigDecimal bigDecimalWork = new BigDecimal(workDaysList.size());
  472. // 获取审批通过计算后的数据
  473. Map<String, VacationInfo> approvedlMap = executeMethod(rs, startDate, endDate, workDaysList);
  474. LinkedHashMap<String, String> selectFieldsTypeMap = new LinkedHashMap<String, String>();
  475. String stringType = "String_@";
  476. String numberType = "Number_.";
  477. selectFieldsTypeMap.put("staffid", stringType);
  478. selectFieldsTypeMap.put("name", stringType);
  479. selectFieldsTypeMap.put("title", stringType);
  480. selectFieldsTypeMap.put("academictitle", stringType);
  481. selectFieldsTypeMap.put("Program", stringType);
  482. selectFieldsTypeMap.put("AnnualLeaveDays", numberType);
  483. selectFieldsTypeMap.put("BusinessTripDays", numberType);
  484. selectFieldsTypeMap.put("OtherLeaveDays", numberType);
  485. selectFieldsTypeMap.put("InCampusDays", numberType);
  486. selectFieldsTypeMap.put("AnnualLeave", numberType);
  487. selectFieldsTypeMap.put("BusinessTrip", numberType);
  488. selectFieldsTypeMap.put("OtherLeave", numberType);
  489. selectFieldsTypeMap.put("InCampus", numberType);
  490. ArrayList<String> list = this.getcolNamesAction();
  491. exportData(response, rs, "FacultyAbsenceSummaryReport.xlsx", selectFieldsTypeMap, list, approvedlMap);
  492. return null;
  493. }
  494. // 获取serch的过虑条件返回拼接sql
  495. public String getSearchFilterItems(String searchFilterItems) {
  496. // 获取serch的过虑条件
  497. String filter = null;
  498. if (StringUtils.isNotBlank(searchFilterItems)) {
  499. // 姓名
  500. if (searchFilterItems.indexOf("name") != -1) {
  501. filter = searchFilterItems.replaceAll("name", "g.fname_l1");
  502. }
  503. // 编码
  504. if (searchFilterItems.indexOf("number") != -1) {
  505. filter = searchFilterItems.replaceAll("number", "g.fnumber");
  506. }
  507. }
  508. if (filter != null) {
  509. return filter.replace("(", "").replace(")", "");
  510. }
  511. return filter;
  512. }
  513. /**
  514. *
  515. * @param response
  516. * response
  517. * @param resource
  518. * 需要导出的数据源
  519. * @param fileName
  520. * 导出文件名称
  521. * @param selectFieldsTypeMap
  522. * 存放查询字段对应导出类型和导出样式 key selectField 导出字段 value
  523. * fieldType_cellStyleMap key 导出查询字段 value 导出类型_导出样式 导出样式 key
  524. * DateType 日期格式 value "yyyy-mm-dd" key NumberType 数字 value '0.0_
  525. * '->保留一位小数 '0_ '->整数 key StringType 文本 value @
  526. * @throws SQLException
  527. */
  528. public static void exportData(HttpServletResponse response, Object resource, String fileName,
  529. LinkedHashMap<String, String> selectFieldsTypeMap, List<String> list,
  530. Map<String, VacationInfo> percentageLeavelMap) throws SQLException {
  531. // 创建新的Excel 工作簿
  532. // Excel2003版本(包含2003)以前使用HSSFWorkbook类,扩展名为.xls
  533. // Excel2007版本(包含2007)以后使用XSSFWorkbook类,扩展名为.xlsx
  534. XSSFWorkbook workBook = new XSSFWorkbook();
  535. // 在Excel工作簿中创建新的工作表,名为"Sheet1"
  536. XSSFSheet sheet = workBook.createSheet("Sheet1");
  537. DataFormat format = workBook.createDataFormat();
  538. // 设置数字格式为整数,不保留小数 (_ )空格是必须要切勿格式
  539. if (resource != null) {
  540. if (resource instanceof IRowSet) {
  541. IRowSet rs = (IRowSet) resource;
  542. // 列数
  543. int numColumns = selectFieldsTypeMap.entrySet().size();
  544. // 创建多行多列的数据
  545. int i = 0;
  546. // 创建标题
  547. XSSFRow titleRow = sheet.createRow(i);
  548. // 列名
  549. List<String> keyList = selectFieldsTypeMap.keySet().stream().collect(Collectors.toList());
  550. // 创建字体样式
  551. XSSFFont font = workBook.createFont();
  552. font.setFontName("宋体"); // 设置字体为 Arial
  553. font.setFontHeightInPoints((short) 11); // 设置字体大小为 12 点
  554. font.setColor(IndexedColors.BLACK.getIndex()); // 设置字体颜色为黑色
  555. font.setBold(true);
  556. // 创建一个新的单元格样式,并将字体样式应用于该样式
  557. CellStyle cellStyle = workBook.createCellStyle();
  558. cellStyle.setFont(font);
  559. if (i == 0) {
  560. for (int j = 0; j < numColumns; j++) {
  561. // 创建列
  562. XSSFCell cell = titleRow.createCell(j);
  563. cell.setCellValue(list.get(j));
  564. // 调整列宽
  565. sheet.setColumnWidth(j, 5000);
  566. // 应用字体样式
  567. titleRow.getCell(j).setCellStyle(cellStyle);
  568. }
  569. }
  570. i++;
  571. // 创建数据
  572. Set<String> staffSet = new HashSet<String>();
  573. rs.beforeFirst();
  574. while (rs.next()) {
  575. System.out.print("进来了while循环");
  576. try {
  577. if (!staffSet.contains(rs.getString("staffid"))) {
  578. VacationInfo vacationInfo = percentageLeavelMap.get(rs.getString("staffid"));
  579. XSSFRow dateRow = sheet.createRow(i);
  580. System.out.print("IIIII是" + i);
  581. for (int j = 0; j < numColumns; j++) {
  582. // 创建列
  583. XSSFCell cell = dateRow.createCell(j);
  584. // 由于开发与测试环境的数据库类型与不一致 导致无法从数据库表读取相关字段类型判断 且有相关计算字段
  585. // 所以采用参数判断
  586. String column = keyList.get(j);
  587. String fieldTypeAndcellStyleType = selectFieldsTypeMap.get(column);
  588. int indexOf = fieldTypeAndcellStyleType.indexOf("_");
  589. // 字段属性类型
  590. String fieldType = fieldTypeAndcellStyleType.substring(0, indexOf);
  591. // 样式类型
  592. String cellStyleType = fieldTypeAndcellStyleType.substring(indexOf + 1,
  593. fieldTypeAndcellStyleType.length());
  594. // 判断是否是计算过的数据
  595. if ("AnnualLeaveDays".equals(column)) {
  596. cell.setCellValue(vacationInfo.getAnnualLeave().toString());
  597. } else if ("BusinessTripDays".equals(column)) {
  598. cell.setCellValue(vacationInfo.getBusinessTrip().toString());
  599. } else if ("OtherLeaveDays".equals(column)) {
  600. cell.setCellValue(vacationInfo.getOtherLeave().toString());
  601. } else if ("InCampusDays".equals(column)) {
  602. cell.setCellValue(vacationInfo.getInCampus().toString());
  603. } else if ("AnnualLeave".equals(column)) {
  604. cell.setCellValue(vacationInfo.getPercentageAnnualLeave().setScale(2).toString());
  605. } else if ("BusinessTrip".equals(column)) {
  606. cell.setCellValue(vacationInfo.getPercentageBusinessTrip().setScale(2).toString());
  607. } else if ("OtherLeave".equals(column)) {
  608. cell.setCellValue(vacationInfo.getPercentageOtherLeave().setScale(2).toString());
  609. } else if ("InCampus".equals(column)) {
  610. // setNumerical(workBook, format, key, cell);
  611. cell.setCellValue(vacationInfo.getPercentageInCampus().setScale(2).toString());
  612. } else {
  613. // 不设置格式
  614. cell.setCellValue(rs.getString(column));
  615. }
  616. }
  617. i++;
  618. }
  619. staffSet.add(rs.getString("staffid"));
  620. // i++;
  621. } catch (Exception e) {
  622. logger.info("Excel file was created Error!" + e.getMessage());
  623. }
  624. }
  625. try {
  626. setResponseHeader(response, fileName);
  627. OutputStream os = response.getOutputStream();
  628. workBook.write(os);
  629. workBook.close();
  630. os.flush();
  631. os.close();
  632. logger.info("Excel file was created successfully!");
  633. } catch (IOException e) {
  634. logger.info("Error while writing to file " + e.getMessage());
  635. e.printStackTrace();
  636. }
  637. }
  638. }
  639. }
  640. // /***
  641. // * 设置格式是数值
  642. // */
  643. // public static void setNumerical(XSSFWorkbook workBook,DataFormat
  644. // format,String cellStyleType,XSSFCell cell,BigDecimal value) {
  645. // XSSFCellStyle style = workBook.createCellStyle();
  646. // style.setDataFormat(format.getFormat(String.format(cellStyleType, " ")));
  647. // cell.setCellStyle(style);
  648. // if (cellStyleType.contains(".")) {
  649. // // 小数
  650. // cell.setCellValue(Double.parseDouble(value.toString()));
  651. // } else {
  652. // cell.setCellValue(Integer.parseInt(value.toString()));
  653. // }
  654. // }
  655. //
  656. /***
  657. *
  658. * @param response
  659. * @param fileName
  660. */
  661. public static void setResponseHeader(HttpServletResponse response, String fileName) {
  662. try {
  663. try {
  664. fileName = URLEncoder.encode(fileName, "UTF-8");
  665. } catch (UnsupportedEncodingException e) {
  666. e.printStackTrace();
  667. }
  668. response.setContentType("application/octet-stream;charset=UTF-8");
  669. response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
  670. response.addHeader("Pargam", "no-cache");
  671. response.addHeader("Cache-Control", "no-cache");
  672. } catch (Exception ex) {
  673. ex.printStackTrace();
  674. }
  675. }
  676. /***
  677. * 当前日期转成String 格式
  678. */
  679. public String getDate(Date date) {
  680. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置日期格式
  681. if (date == null) {
  682. return null;
  683. }
  684. return sdf.format(date);
  685. }
  686. }