杭州市委办项管
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

257 satır
13KB

  1. package com.hz.pm.api.collection;
  2. import com.alibaba.fastjson.JSON;
  3. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  4. import com.hz.pm.api.AppTests;
  5. import com.hz.pm.api.common.model.constant.BizConst;
  6. import com.hz.pm.api.common.model.constant.RegionConst;
  7. import com.hz.pm.api.gov.contants.BizProjectContant;
  8. import com.hz.pm.api.gov.manage.GovProjectCollectionManage;
  9. import com.hz.pm.api.gov.model.dto.GovBizProjectApplyDTO;
  10. import com.hz.pm.api.gov.model.dto.GovBizProjectBaseinfoDTO;
  11. import com.hz.pm.api.gov.model.dto.GovBizProjectSaveDTO;
  12. import com.hz.pm.api.gov.model.entity.*;
  13. import com.hz.pm.api.gov.service.*;
  14. import com.hz.pm.api.projectdeclared.utils.GenerateProjectCodeUtil;
  15. import com.hz.pm.api.user.security.model.UserInfoDetails;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  18. import org.apache.poi.ss.usermodel.Row;
  19. import org.apache.poi.ss.usermodel.Sheet;
  20. import org.apache.poi.ss.usermodel.Workbook;
  21. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  22. import org.junit.Test;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import java.io.*;
  25. import java.time.LocalDateTime;
  26. import java.util.List;
  27. import java.util.Objects;
  28. import java.util.stream.Collectors;
  29. /**
  30. * @Classname SaveCollectionTest
  31. * @Description
  32. * @Date 2023/9/4 17:43
  33. * @Author PoffyZhang
  34. */
  35. public class SaveCollectionTest331125 extends AppTests {//
  36. @Autowired
  37. private GovProjectCollectionManage manage;
  38. @Autowired
  39. private GenerateProjectCodeUtil projectCodeUtil;
  40. @Autowired
  41. private IGovBizProjectBaseinfoService baseinfoService;
  42. @Autowired
  43. private IGovBizProjectApplyService applyService;
  44. @Autowired
  45. private IGovBizProjectApproveService approveService;
  46. @Autowired
  47. private IGovBizProjectCimplementService cimplementService;
  48. @Autowired
  49. private IGovBizProjectMimplementService mimplementService;
  50. @Autowired
  51. private IGovBizProjectProcureService procureService;
  52. @Test
  53. public void importData() throws Exception {
  54. Workbook wb;
  55. File f = new File("C:\\Users\\PoffyZhang\\Desktop\\丽水开发区(1).xlsx");
  56. wb = readExcel(new FileInputStream(f),f.getName());
  57. Row row = null;
  58. if(wb != null) {
  59. //获取第一个sheet
  60. Sheet sheet = wb.getSheetAt(0);
  61. //获取最大行数
  62. int rownum = sheet.getPhysicalNumberOfRows();
  63. //获取第一行
  64. row = sheet.getRow(0);
  65. //获取最大列数
  66. for (int i = 1; i < rownum; i++) {
  67. row = sheet.getRow(i);
  68. if(Objects.isNull(row) || Objects.isNull(row.getCell(0))){
  69. break;
  70. }
  71. GovBizProjectSaveDTO saveDTO = new GovBizProjectSaveDTO();
  72. GovBizProjectBaseinfoDTO baseinfoDTO = new GovBizProjectBaseinfoDTO();
  73. //
  74. String projName = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
  75. baseinfoDTO.setBaseProjName(projName);
  76. baseinfoDTO.setBaseAreaCode(RegionConst.LS_KF + BizConst.NINE_AREA_CODE_LAST);
  77. baseinfoDTO.setBaseAreaName(RegionConst.LS_KF_NAME);
  78. baseinfoDTO.setAreaCode(RegionConst.RC_LS);
  79. baseinfoDTO.setIsEffective(BizProjectContant.ProjectCollection.IS_EFFECTIVE);
  80. baseinfoDTO.setTongTime(LocalDateTime.now());
  81. baseinfoDTO.setBizTime(LocalDateTime.now());
  82. baseinfoDTO.setOp("insert");
  83. String principal = Objects.nonNull(row.getCell(3)) ? row.getCell(3).toString() : StringUtils.EMPTY;
  84. baseinfoDTO.setBaseProjPrincipal(principal);
  85. String contacts = Objects.nonNull(row.getCell(4)) ? row.getCell(4).toString() : StringUtils.EMPTY;
  86. baseinfoDTO.setBaseProjContacts(contacts);
  87. baseinfoDTO.setBaseProjType("01");
  88. saveDTO.setBaseinfo(baseinfoDTO);
  89. GovBizProjectApplyDTO applyDTO = new GovBizProjectApplyDTO();
  90. String baseProjSetYear = Objects.nonNull(row.getCell(20)) ? row.getCell(20).toString() : StringUtils.EMPTY;
  91. applyDTO.setBaseProjSetYear(baseProjSetYear);
  92. saveDTO.setApply(applyDTO);
  93. //生产编号
  94. String projId = projectCodeUtil.generateProjectCode(saveDTO);
  95. saveDTO.setBaseProjId(projId);
  96. saveDTO.getBaseinfo().setBaseProjId(projId);
  97. saveDTO.getApply().setBaseProjId(projId);
  98. UserInfoDetails user = new UserInfoDetails();
  99. user.setRegionCode(RegionConst.LS_KF);
  100. user.setUsername("系统导入");
  101. String save = "";
  102. try{
  103. GovBizProjectBaseinfo old = baseinfoService.getOne(Wrappers.lambdaQuery(GovBizProjectBaseinfo.class)
  104. .eq(GovBizProjectBaseinfo::getBaseProjName, projName)
  105. .last(BizConst.LIMIT_1));
  106. if(Objects.isNull(old)){
  107. save = manage.save(saveDTO, user);
  108. }else{
  109. System.out.println(projName + ",重复了 要改到经开区里");
  110. String newProjId = old.getBaseProjId().replace("331100000","331118000");
  111. baseinfoService.update(Wrappers.lambdaUpdate(GovBizProjectBaseinfo.class)
  112. .eq(GovBizProjectBaseinfo::getBaseProjName, projName)
  113. .eq(GovBizProjectBaseinfo::getBaseProjId, old.getBaseProjId())
  114. .set(GovBizProjectBaseinfo::getBaseAreaCode,RegionConst.LS_KF + BizConst.NINE_AREA_CODE_LAST)
  115. .set(GovBizProjectBaseinfo::getBaseProjId,newProjId));
  116. applyService.update(Wrappers.lambdaUpdate(GovBizProjectApply.class)
  117. .eq(GovBizProjectApply::getBaseProjName, projName)
  118. .eq(GovBizProjectApply::getBaseProjId, old.getBaseProjId())
  119. .set(GovBizProjectApply::getBaseProjId,newProjId));
  120. approveService.update(Wrappers.lambdaUpdate(GovBizProjectApprove.class)
  121. .eq(GovBizProjectApprove::getBaseProjName, projName)
  122. .eq(GovBizProjectApprove::getBaseProjId, old.getBaseProjId())
  123. .set(GovBizProjectApprove::getBaseProjId,newProjId));
  124. cimplementService.update(Wrappers.lambdaUpdate(GovBizProjectCimplement.class)
  125. .eq(GovBizProjectCimplement::getBaseProjName, projName)
  126. .eq(GovBizProjectCimplement::getBaseProjId, old.getBaseProjId())
  127. .set(GovBizProjectCimplement::getBaseProjId,newProjId));
  128. mimplementService.update(Wrappers.lambdaUpdate(GovBizProjectMimplement.class)
  129. .eq(GovBizProjectMimplement::getBaseProjName, projName)
  130. .eq(GovBizProjectMimplement::getBaseProjId, old.getBaseProjId())
  131. .set(GovBizProjectMimplement::getBaseProjId,newProjId));
  132. procureService.update(Wrappers.lambdaUpdate(GovBizProjectProcure.class)
  133. .eq(GovBizProjectProcure::getBaseProjName, projName)
  134. .eq(GovBizProjectProcure::getBaseProjId, old.getBaseProjId())
  135. .set(GovBizProjectProcure::getBaseProjId,newProjId));
  136. }
  137. }catch (Exception e){
  138. e.printStackTrace();
  139. }
  140. System.out.println(JSON.toJSONString(save));
  141. }
  142. }
  143. System.out.println("导入完成");
  144. }
  145. @Test
  146. public void test2() throws FileNotFoundException {
  147. //先删除 开发区的
  148. List<GovBizProjectBaseinfo> lss = baseinfoService.list(Wrappers.lambdaQuery(GovBizProjectBaseinfo.class)
  149. .eq(GovBizProjectBaseinfo::getBaseAreaCode, RegionConst.LS_KF + BizConst.NINE_AREA_CODE_LAST));
  150. List<String> codes = lss.stream().map(GovBizProjectBaseinfo::getBaseProjId).collect(Collectors.toList());
  151. applyService.remove(Wrappers.lambdaQuery(GovBizProjectApply.class)
  152. .in(GovBizProjectApply::getBaseProjId,codes));
  153. approveService.remove(Wrappers.lambdaQuery(GovBizProjectApprove.class)
  154. .in(GovBizProjectApprove::getBaseProjId,codes));
  155. cimplementService.remove(Wrappers.lambdaQuery(GovBizProjectCimplement.class)
  156. .in(GovBizProjectCimplement::getBaseProjId,codes));
  157. mimplementService.remove(Wrappers.lambdaQuery(GovBizProjectMimplement.class)
  158. .in(GovBizProjectMimplement::getBaseProjId,codes));
  159. procureService.remove(Wrappers.lambdaQuery(GovBizProjectProcure.class)
  160. .in(GovBizProjectProcure::getBaseProjId,codes));
  161. baseinfoService.remove(Wrappers.lambdaQuery(GovBizProjectBaseinfo.class)
  162. .in(GovBizProjectBaseinfo::getBaseProjId,codes));
  163. Workbook wb;
  164. File f = new File("C:\\Users\\PoffyZhang\\Desktop\\丽水开发区(1).xlsx");
  165. wb = readExcel(new FileInputStream(f),f.getName());
  166. Row row = null;
  167. if(wb != null) {
  168. //获取第一个sheet
  169. Sheet sheet = wb.getSheetAt(0);
  170. //获取最大行数
  171. int rownum = sheet.getPhysicalNumberOfRows();
  172. //获取第一行
  173. row = sheet.getRow(0);
  174. //获取最大列数
  175. for (int i = 1; i < rownum; i++) {
  176. row = sheet.getRow(i);
  177. if (Objects.isNull(row) || Objects.isNull(row.getCell(0))) {
  178. break;
  179. }
  180. GovBizProjectSaveDTO saveDTO = new GovBizProjectSaveDTO();
  181. GovBizProjectBaseinfoDTO baseinfoDTO = new GovBizProjectBaseinfoDTO();
  182. //
  183. String projName = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
  184. baseinfoDTO.setBaseProjName(projName);
  185. baseinfoDTO.setBaseAreaCode(RegionConst.LS_KF + BizConst.NINE_AREA_CODE_LAST);
  186. baseinfoDTO.setBaseAreaName(RegionConst.LS_KF_NAME);
  187. baseinfoDTO.setAreaCode(RegionConst.RC_LS);
  188. baseinfoDTO.setIsEffective(BizProjectContant.ProjectCollection.IS_EFFECTIVE);
  189. baseinfoDTO.setTongTime(LocalDateTime.now());
  190. baseinfoDTO.setBizTime(LocalDateTime.now());
  191. baseinfoDTO.setOp("insert");
  192. String principal = Objects.nonNull(row.getCell(3)) ? row.getCell(3).toString() : StringUtils.EMPTY;
  193. baseinfoDTO.setBaseProjPrincipal(principal);
  194. String contacts = Objects.nonNull(row.getCell(4)) ? row.getCell(4).toString() : StringUtils.EMPTY;
  195. baseinfoDTO.setBaseProjContacts(contacts);
  196. baseinfoDTO.setBaseProjType("01");
  197. saveDTO.setBaseinfo(baseinfoDTO);
  198. GovBizProjectApplyDTO applyDTO = new GovBizProjectApplyDTO();
  199. String baseProjSetYear = Objects.nonNull(row.getCell(20)) ? row.getCell(20).toString().substring(0,4) : StringUtils.EMPTY;
  200. applyDTO.setBaseProjSetYear(baseProjSetYear);
  201. saveDTO.setApply(applyDTO);
  202. //生产编号
  203. String projId = projectCodeUtil.generateProjectCode(saveDTO);
  204. saveDTO.setBaseProjId(projId);
  205. saveDTO.getBaseinfo().setBaseProjId(projId);
  206. saveDTO.getApply().setBaseProjId(projId);
  207. UserInfoDetails user = new UserInfoDetails();
  208. user.setRegionCode(RegionConst.LS_KF);
  209. user.setUsername("系统导入");
  210. String save = "";
  211. try {
  212. save = manage.save(saveDTO, user);
  213. }catch (Exception e){
  214. e.printStackTrace();
  215. }
  216. System.out.println(JSON.toJSONString(save));
  217. }
  218. }
  219. }
  220. //这个下面是单独函数
  221. public static Workbook readExcel(InputStream is, String extString){
  222. Workbook wb = null;
  223. extString = extString.substring(extString.lastIndexOf("."));
  224. try {
  225. if(".xls".equals(extString)){
  226. return wb = new HSSFWorkbook(is);
  227. }else if(".xlsx".equals(extString)){
  228. return wb = new XSSFWorkbook(is);
  229. }else {
  230. return wb = null;
  231. }
  232. } catch (FileNotFoundException e) {
  233. e.printStackTrace();
  234. } catch (IOException e) {
  235. e.printStackTrace();
  236. }
  237. return wb;
  238. }
  239. }