ResignBizBillHrManListHandlerEx.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package com.kingdee.eas.custom.ssPDF.handler;
  2. import com.itextpdf.text.DocumentException;
  3. import com.itextpdf.text.pdf.*;
  4. import com.kingdee.bos.BOSException;
  5. import com.kingdee.bos.Context;
  6. import com.kingdee.eas.base.permission.UserInfo;
  7. import com.kingdee.eas.custom.shengshen.utils.PDFFillInformationUtil;
  8. import com.kingdee.eas.hr.ats.AtsUtil;
  9. import com.kingdee.eas.util.app.ContextUtil;
  10. import com.kingdee.eas.util.app.DbUtil;
  11. import com.kingdee.jdbc.rowset.IRowSet;
  12. import com.kingdee.shr.affair.web.handler.hrman.ResignBizBillHrManListHandler;
  13. import com.kingdee.shr.base.syssetting.context.SHRContext;
  14. import com.kingdee.shr.base.syssetting.exception.SHRWebException;
  15. import com.kingdee.shr.base.syssetting.web.dynamic.util.DynamicUtil;
  16. import com.kingdee.shr.base.syssetting.web.dynamic.util.MD5;
  17. import com.kingdee.shr.base.syssetting.web.util.UserUtil;
  18. import org.springframework.ui.ModelMap;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.servlet.http.HttpSession;
  22. import java.io.*;
  23. import java.sql.SQLException;
  24. import java.text.MessageFormat;
  25. import java.text.SimpleDateFormat;
  26. import java.util.*;
  27. /**
  28. * @author cml
  29. * @date 2024/9/10
  30. * @apiNote
  31. */
  32. public class ResignBizBillHrManListHandlerEx extends ResignBizBillHrManListHandler {
  33. /**
  34. * 离职员工生成离职PDF
  35. * @param request templatePrint
  36. * @param response
  37. * @param modelMap
  38. * @throws IOException
  39. */
  40. public void importPDFAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException, SHRWebException {
  41. generatePDF(request,response,modelMap);
  42. }
  43. public void generatePDF(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
  44. Map<String, String> map = new HashMap<>();
  45. Context ctx = SHRContext.getInstance().getContext();//上下文
  46. HttpSession session = request.getSession();
  47. long time = System.currentTimeMillis();
  48. String tempFilePath1 = UserUtil.getUserTempDirAbsolutePath(session); //存放
  49. File tempFile1 = new File(tempFilePath1);
  50. if (!tempFile1.exists()) {
  51. tempFile1.mkdirs();
  52. }
  53. List<String> list = new ArrayList<String>();
  54. try{
  55. String numbers = request.getParameter("number");
  56. String number = "";
  57. String[] split = numbers.split(",");
  58. for (int i = 0; i < split.length; i++) {
  59. list.add(split[i]);
  60. }
  61. List<Set<String>> listSet = this.splitListIntoSets(list, 20); //超过 20(需要)第二页 pdf
  62. List<File> listFile = new ArrayList<>();
  63. for (int i = 0; i < listSet.size(); i++) {
  64. Set<String> set = listSet.get(i);
  65. Map<String, String> data = getSql(set, ctx, listSet.size(), i);
  66. if (data.containsKey("msgType") && "0".equals(data.get("msgType"))){
  67. this.writeSuccessData(data);
  68. return;
  69. }
  70. number = data.get("number");
  71. PDFFillInformationUtil pdfFillInformationUtil = new PDFFillInformationUtil();
  72. String realPath = session.getServletContext().getRealPath("/") + "addon/custom/html/DSF_Mod.pdf";
  73. File file = new File(realPath);
  74. byte[] pdfBytes = convertPdfToByteArray(file);
  75. File file1 = pdfFillInformationUtil.fillInformation(pdfBytes, data, tempFilePath1, "DSF_Mod"+i+".pdf");
  76. listFile.add(file1);
  77. }
  78. //压缩文件名
  79. String zipName = "员工离职_" + time + ".zip";
  80. String zipNameHash = MD5.md5Hash(zipName);
  81. String realFileName = getEncryRealFileName(ctx,zipNameHash);
  82. String fileStr = zipNameHash;
  83. String zipPath = tempFilePath1 + File.separator + realFileName;
  84. PDFFillInformationUtil.compress(zipPath, listFile);
  85. Map<String, String> param = new HashMap<String, String>();
  86. param.put("method", "tmp");
  87. param.put("file", fileStr);
  88. param.put("filename", zipName);
  89. String link = DynamicUtil.assembleUrl("/shr/downloadfile.do", param).replaceAll("\\+", "%20");
  90. map.put("msg","离职pdf导出:" + split.length + "条数据;成功:" + number + ";失败:" + (split.length - Integer.parseInt(number)));
  91. map.put("msgType","1");
  92. map.put("link",link);
  93. }catch (Exception e){
  94. e.printStackTrace();
  95. map.put("msg",e.getMessage());
  96. map.put("msgType","0");
  97. }
  98. this.writeSuccessData(map);
  99. }
  100. public static String getEncryRealFileName(Context ctx, String realFileName) {
  101. UserInfo currentUserInfo = ContextUtil.getCurrentUserInfo(ctx);
  102. return MessageFormat.format("{0}_{1}", realFileName, MD5.md5Hash(currentUserInfo.getId().toString()));
  103. }
  104. public byte[] convertPdfToByteArray(File file) throws IOException {
  105. FileInputStream fis = new FileInputStream(file);
  106. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  107. byte[] buffer = new byte[1024];
  108. int bytesRead;
  109. while ((bytesRead = fis.read(buffer)) != -1) {
  110. baos.write(buffer, 0, bytesRead);
  111. }
  112. fis.close();
  113. baos.close();
  114. return baos.toByteArray();
  115. }
  116. public Map<String, String> getSql(Set<String> set,Context ctx,int page,int num) throws BOSException, SQLException {
  117. StringBuilder query = new StringBuilder();
  118. query.append("SELECT").append("\n");
  119. query.append("res.FBIZDATE bizDate,").append("\n");
  120. query.append("org.FNAME_l3 orgName,").append("\n");
  121. query.append("org.FPhoneNumber as orgPhone,").append("\n");
  122. query.append("org.FCode as orgCode,").append("\n");
  123. query.append("person.fname_l3 personName,").append("\n");
  124. query.append("person.CFPinyin personPY,").append("\n");
  125. query.append("person.CFOccupationcode personOccTion,").append("\n");
  126. query.append("pal.FCredentialsTypeNO creNumber").append("\n");
  127. // query.append("cre.fname_l3 creName,").append("\n");
  128. // query.append("cre.fname_l3 creNumber").append("\n");
  129. query.append("FROM t_hr_personposition pp").append("\n");
  130. query.append("left join T_BD_person person on pp.FPERSONID = person.Fid").append("\n");
  131. query.append("left join T_Org_admin org on org.fid = pp.FCOMPANYID").append("\n");
  132. query.append("left join T_HR_ResignBizBillEntry res on res.FPERSONID = person.fid").append("\n");
  133. query.append("left join T_HR_personcredentialsType pal on pal.FPERSONID = person.fid").append("\n");
  134. //query.append("left join T_HR_CredentialsType cre on cre.FID = pal.fCredentialsTypeId").append("\n");
  135. query.append("where person.FNUMBER in(").append("\n");
  136. query.append(AtsUtil.convertSetToString(set));
  137. query.append(")").append("\n");
  138. query.append("and pal.FIsSingle='1' and res.FBIZDATE is not null").append("\n");
  139. IRowSet kyIRowSet = DbUtil.executeQuery(ctx, query.toString());
  140. Map<String, String> map = pdfData(kyIRowSet, page, num);
  141. return map;
  142. }
  143. /**
  144. * 按指定大小,分隔集合,将集合按规定个数分为n个部分
  145. * @param inputList 数据
  146. * @param setSize 分割长度
  147. * @return
  148. */
  149. public <T> List<Set<T>> splitListIntoSets(List<T> inputList, int setSize) {
  150. List<Set<T>> sets = new ArrayList<>();
  151. for (int i = 0; i < inputList.size(); i += setSize) {
  152. int endIndex = Math.min(i + setSize, inputList.size());
  153. List<T> sublist = inputList.subList(i, endIndex);
  154. Set<T> set = new HashSet<>(sublist);
  155. sets.add(set);
  156. }
  157. return sets;
  158. }
  159. /**
  160. * 填充模板中的数据
  161. */
  162. public void fillData(AcroFields fields, Map<String, String> data) throws DocumentException, IOException {
  163. System.out.println("fillDatafillData12");
  164. Map<String, AcroFields.Item> fields1 = fields.getFields();
  165. System.out.println(fields1.keySet());
  166. System.out.println(data);
  167. fields.setField("year", "2024");
  168. //fields.setField("empName", "22334");
  169. // fields.setField("taxpayerName0", "234345");
  170. // fields.setField("page", "2");
  171. try {
  172. for (String key : data.keySet()) {
  173. String value = data.get(key);
  174. fields.setField(key, value);
  175. }
  176. } catch (Exception e) {
  177. e.printStackTrace();
  178. }
  179. }
  180. /**
  181. * 填充数据源
  182. * 其中data存放的key值与pdf模板中的文本域值相对应
  183. */
  184. public Map<String, String> pdfData(IRowSet rowSet,int num,int page) throws SQLException {
  185. Map<String, String> data = new HashMap<String, String>();
  186. if (rowSet.size() == 0){
  187. data.put("msg","所选员工未查询到离职员工或还在离职流程中");
  188. data.put("msgType","0");
  189. return data;
  190. }
  191. SimpleDateFormat outputFormatter = new SimpleDateFormat("dd/MM/yyyy");
  192. int index = 2;
  193. int indexAdd = 4;
  194. Date date = new Date();
  195. String dateStr = outputFormatter.format(date);
  196. String sun = dateStr.substring(0, 2);
  197. String month = dateStr.substring(3, 5);
  198. String year = dateStr.substring(6, 10);
  199. while (rowSet.next()) {
  200. Date bizDate = rowSet.getDate("bizDate");//离职日期
  201. String orgName = rowSet.getString("orgName");//所属公司
  202. String orgPhone = rowSet.getString("orgPhone");//联系电话
  203. String orgCode = rowSet.getString("orgCode");//雇主税号
  204. String creNumber = rowSet.getString("creNumber");//证件类型编码
  205. String personName = rowSet.getString("personName");//员工姓名
  206. String personPY = rowSet.getString("personPY") != null ? rowSet.getString("personPY") : "";//外语拼音
  207. String personOccTion = rowSet.getString("personOccTion");//员工信息-职业税编号
  208. String leftStr = outputFormatter.format(bizDate);
  209. if (index == 2){//表头 取第一条数据员工数据
  210. data.put("topmostSubform[0].Page1[0].TextField1[0]", leftStr.substring(6,10)); //第一条员工离职的离职日期年份
  211. data.put("topmostSubform[0].Page1[0].TextField1[1]", orgCode);//雇主税号 (行政组织维护-雇主税号) 1
  212. data.put("topmostSubform[0].Page1[0].TextField1[2]", orgName); //雇主名称(员工信息-所属公司)
  213. data.put("topmostSubform[0].Page1[0].TextField1[3]", orgPhone);//联系电话 (行政组织维护-联系电话) 1
  214. data.put("topmostSubform[0].Page1[0].NumericField1[0]", String.valueOf(page+1)); //当前页数
  215. data.put("topmostSubform[0].Page1[0].NumericField1[1]", String.valueOf(num)); //总页数
  216. data.put("topmostSubform[0].Page1[0].TextField21[1]", orgName); //
  217. }
  218. data.put("topmostSubform[0].Page1[0].TextField1[" + indexAdd + "]", personOccTion); //税务号码(员工信息-职业税编号)
  219. ++indexAdd;
  220. data.put("topmostSubform[0].Page1[0].TextField1[" + indexAdd + "]", personName + " " + personPY); // 纳税人姓名(员工姓名+外语拼音)
  221. ++indexAdd;
  222. if ("62".equals(creNumber) || "64".equals(creNumber)){
  223. creNumber = 15 + "/" + creNumber;
  224. }else {
  225. creNumber = 20 + "/" + creNumber;
  226. }
  227. data.put("topmostSubform[0].Page1[0].TextField1[" + indexAdd + "]", creNumber); //证件类型及编号(员工信息-证件类型和证件编码 澳门永久或非永久身份证为15 其他的都为20)
  228. data.put("topmostSubform[0].Page1[0].TextField" +index+ "[0]", leftStr); //离职日期(离职单-离职日期)
  229. index++;
  230. ++indexAdd;
  231. }
  232. //key要与模板中的别名一一对应
  233. data.put("topmostSubform[0].Page1[0].TextField23[0]", year);
  234. data.put("topmostSubform[0].Page1[0].TextField22[0]", month);
  235. data.put("topmostSubform[0].Page1[0].TextField24[0]", sun);
  236. data.put("number", String.valueOf(rowSet.size()));
  237. return data;
  238. }
  239. }