CenterLeaderRelationImportService.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.kingdee.eas.custom.attendanceexception.service;
  2. import com.kingdee.bos.BOSException;
  3. import com.kingdee.bos.Context;
  4. import com.kingdee.bos.metadata.entity.*;
  5. import com.kingdee.eas.basedata.person.IPerson;
  6. import com.kingdee.eas.basedata.person.PersonCollection;
  7. import com.kingdee.eas.basedata.person.PersonFactory;
  8. import com.kingdee.eas.basedata.person.PersonInfo;
  9. import com.kingdee.eas.custom.attendanceexception.*;
  10. import com.kingdee.eas.framework.CoreBaseInfo;
  11. import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseColumnInfo;
  12. import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseImportService;
  13. import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseRowInfo;
  14. import com.kingdee.shr.base.syssetting.app.io.fileImport.ImportException;
  15. import com.kingdee.util.StringUtils;
  16. import org.apache.log4j.Logger;
  17. import org.apache.poi.ss.usermodel.Cell;
  18. import org.apache.poi.ss.usermodel.Row;
  19. import org.apache.poi.xssf.usermodel.XSSFSheet;
  20. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  21. import java.util.*;
  22. /**
  23. * @Description 中心负责人关系表导入业务实现类
  24. * @Date 2025/4/15 18:28
  25. * @Created by Heyuan
  26. */
  27. public class CenterLeaderRelationImportService extends BaseImportService {
  28. private static Logger logger = Logger.getLogger(CenterLeaderRelationImportService.class);
  29. /**
  30. * 设置其他字段
  31. *
  32. * @param baseInfo
  33. * @param row
  34. * @throws ImportException
  35. */
  36. @Override
  37. protected void setOtherFieldInfo(CoreBaseInfo baseInfo, BaseRowInfo row) throws ImportException {
  38. super.setOtherFieldInfo(baseInfo, row);
  39. try {
  40. CenterLeaderRelationInfo relationInfo = (CenterLeaderRelationInfo) baseInfo;
  41. CenterLeaderRelationEntryCollection entrys = relationInfo.getEntry();
  42. entrys.clear();
  43. Map<String, BaseColumnInfo> mapColumnInfo = row.getMapColumnInfo();
  44. Context ctx = this.getContext();
  45. IPerson iPerson = null;
  46. iPerson = PersonFactory.getLocalInstance(ctx);
  47. for (String key : mapColumnInfo.keySet()) {
  48. BaseColumnInfo columnInfo = mapColumnInfo.get(key);
  49. String propName = columnInfo.getPropName();
  50. String name = columnInfo.getName();
  51. if (columnInfo.isCustomField() && propName.startsWith("person")) {
  52. String value = row.getValueOfString(propName);
  53. if (StringUtils.isEmpty(value)) {
  54. continue;
  55. }
  56. String filter = null;
  57. if (value.contains("##")) {
  58. String[] split = value.split("##");
  59. String number = split[0];
  60. filter = "where number = '" + number + "'";
  61. } else {
  62. filter = "where number = '" + value + "' or name = '" + value + "'";
  63. }
  64. PersonCollection personCollection = iPerson.getPersonCollection(filter);
  65. if (personCollection.size() > 0) {
  66. PersonInfo personInfo = personCollection.get(0);
  67. CenterLeaderRelationEntryInfo entry = new CenterLeaderRelationEntryInfo();
  68. entry.setPerson(personInfo);
  69. entrys.add(entry);
  70. } else {
  71. throw new ImportException(name + " " + value + " 未找到,请检查数据是否有误");
  72. }
  73. }
  74. }
  75. } catch (BOSException e) {
  76. logger.error(e.getMessage(), e);
  77. throw new ImportException(e);
  78. }
  79. }
  80. /**
  81. * 模板下载时携带数据
  82. *
  83. * @param columnInfoMap
  84. * @param wb
  85. * @param sheet
  86. */
  87. @Override
  88. public void completeGenerateExcel(Map<String, BaseColumnInfo> columnInfoMap, XSSFWorkbook wb, XSSFSheet sheet) {
  89. Context ctx = this.getContext();
  90. int beginRow = 4;//数据生成开始行数
  91. try {
  92. ICenterLeaderRelation iCenterLeaderRelation = CenterLeaderRelationFactory.getLocalInstance(ctx);
  93. SelectorItemCollection sic = getSelectorItemCollection(columnInfoMap);
  94. SorterItemCollection sortItems = new SorterItemCollection();
  95. sortItems.add(new SorterItemInfo("department.sortCode"));
  96. EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(null, sic, sortItems);
  97. //查询中心负责人关系表
  98. CenterLeaderRelationCollection relationCollection = iCenterLeaderRelation.getCenterLeaderRelationCollection(entityViewInfo);
  99. //获取字段
  100. Map<Integer, String> columnIndexMap = this.getColumnIndexFn(columnInfoMap);
  101. int fillColumnCount = columnInfoMap.size();
  102. for (int j = 0; j < relationCollection.size(); j++) {
  103. Row row = sheet.createRow(beginRow++);
  104. //中心负责人关系表
  105. CenterLeaderRelationInfo relationInfo = relationCollection.get(j);
  106. //分录
  107. CenterLeaderRelationEntryCollection entrys = relationInfo.getEntry();
  108. for (int k = 0; k < fillColumnCount; ++k) {
  109. Object obj = null;
  110. Cell createCell = row.createCell(k);
  111. String columnName = columnIndexMap.get(k);
  112. String value = null;
  113. if (columnName.startsWith("person")) {
  114. String num = columnName.replace("person", "");
  115. if (!StringUtils.isEmpty(num)) {
  116. Integer index = Integer.valueOf(num);
  117. if (index > entrys.size()) {
  118. value = "";
  119. } else {
  120. CenterLeaderRelationEntryInfo entryInfo = entrys.get(index - 1);
  121. obj = entryInfo.getPerson();
  122. }
  123. } else {
  124. continue;
  125. }
  126. } else {
  127. obj = relationInfo.get(columnName);
  128. }
  129. if (obj != null) {
  130. if (obj instanceof CoreBaseInfo) {
  131. CoreBaseInfo coreBaseInfo = (CoreBaseInfo) obj;
  132. value = coreBaseInfo.get("number") + "##" + coreBaseInfo.get("name");
  133. } else if (obj instanceof Boolean) {
  134. value = (Boolean) obj ? "是" : "否";
  135. } else {
  136. value = obj.toString();
  137. }
  138. }
  139. createCell.setCellValue(value);
  140. }
  141. }
  142. } catch (Exception e) {
  143. if (e.getMessage().contains("fetched too much rows")) {
  144. throw new ImportException("查询的数据量超出限制!请联系系统管理员修改配置,或者减少下载的数据量。");
  145. } else {
  146. e.printStackTrace();
  147. throw new ImportException(e.getMessage());
  148. }
  149. }
  150. }
  151. /**
  152. * 将列对象数据转化成Map集合
  153. *
  154. * @param columnInfoMap
  155. * @return
  156. */
  157. protected Map<Integer, String> getColumnIndexFn(Map<String, BaseColumnInfo> columnInfoMap) {
  158. Map<Integer, String> columnIndexMap = new HashMap();
  159. Iterator<String> keys = columnInfoMap.keySet().iterator();
  160. int var4 = 0;
  161. while (keys.hasNext()) {
  162. String key = keys.next();
  163. columnIndexMap.put(var4++, key);
  164. }
  165. return columnIndexMap;
  166. }
  167. /**
  168. * 获取查询字段
  169. *
  170. * @param columnInfoMap
  171. * @return
  172. */
  173. protected SelectorItemCollection getSelectorItemCollection(Map<String, BaseColumnInfo> columnInfoMap) {
  174. SelectorItemCollection sic = new SelectorItemCollection();
  175. for (String key : columnInfoMap.keySet()) {
  176. BaseColumnInfo columnInfo = columnInfoMap.get(key);
  177. if (!columnInfo.isCustomField() && !columnInfo.isDynamicColumn()) {
  178. String supplierEntityName = columnInfo.getSupplierEntityName();
  179. if (DataType.OBJECTVALUE.equals(columnInfo.getPropDataType()) || !StringUtils.isEmpty(supplierEntityName)) {
  180. sic.add(key + ".*");
  181. } else {
  182. sic.add(key);
  183. }
  184. }
  185. }
  186. sic.add("entry.person.number");
  187. sic.add("entry.person.name");
  188. return sic;
  189. }
  190. }