|
- package com.hz.pm.api.collection;
-
- import com.alibaba.fastjson.JSON;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.hz.pm.api.AppTests;
- import com.hz.pm.api.common.model.constant.BizConst;
- import com.hz.pm.api.common.model.constant.RegionConst;
- import com.hz.pm.api.gov.contants.BizProjectContant;
- import com.hz.pm.api.gov.manage.GovProjectCollectionManage;
- import com.hz.pm.api.gov.model.dto.GovBizProjectApplyDTO;
- import com.hz.pm.api.gov.model.dto.GovBizProjectBaseinfoDTO;
- import com.hz.pm.api.gov.model.dto.GovBizProjectSaveDTO;
- import com.hz.pm.api.gov.model.entity.*;
- import com.hz.pm.api.gov.service.*;
- import com.hz.pm.api.projectdeclared.utils.GenerateProjectCodeUtil;
- import com.hz.pm.api.user.security.model.UserInfoDetails;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import org.junit.Test;
- import org.springframework.beans.factory.annotation.Autowired;
-
- import java.io.*;
- import java.time.LocalDateTime;
- import java.util.List;
- import java.util.Objects;
- import java.util.stream.Collectors;
-
- /**
- * @Classname SaveCollectionTest
- * @Description
- * @Date 2023/9/4 17:43
- * @Author PoffyZhang
- */
- public class SaveCollectionTest331125 extends AppTests {//
-
- @Autowired
- private GovProjectCollectionManage manage;
-
- @Autowired
- private GenerateProjectCodeUtil projectCodeUtil;
-
- @Autowired
- private IGovBizProjectBaseinfoService baseinfoService;
- @Autowired
- private IGovBizProjectApplyService applyService;
- @Autowired
- private IGovBizProjectApproveService approveService;
- @Autowired
- private IGovBizProjectCimplementService cimplementService;
- @Autowired
- private IGovBizProjectMimplementService mimplementService;
- @Autowired
- private IGovBizProjectProcureService procureService;
-
- @Test
- public void importData() throws Exception {
- Workbook wb;
- File f = new File("C:\\Users\\PoffyZhang\\Desktop\\丽水开发区(1).xlsx");
- wb = readExcel(new FileInputStream(f),f.getName());
- Row row = null;
- if(wb != null) {
- //获取第一个sheet
- Sheet sheet = wb.getSheetAt(0);
- //获取最大行数
- int rownum = sheet.getPhysicalNumberOfRows();
-
- //获取第一行
- row = sheet.getRow(0);
- //获取最大列数
- for (int i = 1; i < rownum; i++) {
- row = sheet.getRow(i);
- if(Objects.isNull(row) || Objects.isNull(row.getCell(0))){
- break;
- }
- GovBizProjectSaveDTO saveDTO = new GovBizProjectSaveDTO();
-
- GovBizProjectBaseinfoDTO baseinfoDTO = new GovBizProjectBaseinfoDTO();
- //
- String projName = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
- baseinfoDTO.setBaseProjName(projName);
- baseinfoDTO.setBaseAreaCode(RegionConst.LS_KF + BizConst.NINE_AREA_CODE_LAST);
- baseinfoDTO.setBaseAreaName(RegionConst.LS_KF_NAME);
- baseinfoDTO.setAreaCode(RegionConst.RC_LS);
- baseinfoDTO.setIsEffective(BizProjectContant.ProjectCollection.IS_EFFECTIVE);
- baseinfoDTO.setTongTime(LocalDateTime.now());
- baseinfoDTO.setBizTime(LocalDateTime.now());
- baseinfoDTO.setOp("insert");
- String principal = Objects.nonNull(row.getCell(3)) ? row.getCell(3).toString() : StringUtils.EMPTY;
- baseinfoDTO.setBaseProjPrincipal(principal);
- String contacts = Objects.nonNull(row.getCell(4)) ? row.getCell(4).toString() : StringUtils.EMPTY;
- baseinfoDTO.setBaseProjContacts(contacts);
- baseinfoDTO.setBaseProjType("01");
- saveDTO.setBaseinfo(baseinfoDTO);
-
- GovBizProjectApplyDTO applyDTO = new GovBizProjectApplyDTO();
- String baseProjSetYear = Objects.nonNull(row.getCell(20)) ? row.getCell(20).toString() : StringUtils.EMPTY;
- applyDTO.setBaseProjSetYear(baseProjSetYear);
- saveDTO.setApply(applyDTO);
- //生产编号
- String projId = projectCodeUtil.generateProjectCode(saveDTO);
- saveDTO.setBaseProjId(projId);
- saveDTO.getBaseinfo().setBaseProjId(projId);
- saveDTO.getApply().setBaseProjId(projId);
- UserInfoDetails user = new UserInfoDetails();
- user.setRegionCode(RegionConst.LS_KF);
- user.setUsername("系统导入");
- String save = "";
- try{
- GovBizProjectBaseinfo old = baseinfoService.getOne(Wrappers.lambdaQuery(GovBizProjectBaseinfo.class)
- .eq(GovBizProjectBaseinfo::getBaseProjName, projName)
- .last(BizConst.LIMIT_1));
- if(Objects.isNull(old)){
- save = manage.save(saveDTO, user);
- }else{
- System.out.println(projName + ",重复了 要改到经开区里");
- String newProjId = old.getBaseProjId().replace("331100000","331118000");
- baseinfoService.update(Wrappers.lambdaUpdate(GovBizProjectBaseinfo.class)
- .eq(GovBizProjectBaseinfo::getBaseProjName, projName)
- .eq(GovBizProjectBaseinfo::getBaseProjId, old.getBaseProjId())
- .set(GovBizProjectBaseinfo::getBaseAreaCode,RegionConst.LS_KF + BizConst.NINE_AREA_CODE_LAST)
- .set(GovBizProjectBaseinfo::getBaseProjId,newProjId));
- applyService.update(Wrappers.lambdaUpdate(GovBizProjectApply.class)
- .eq(GovBizProjectApply::getBaseProjName, projName)
- .eq(GovBizProjectApply::getBaseProjId, old.getBaseProjId())
- .set(GovBizProjectApply::getBaseProjId,newProjId));
- approveService.update(Wrappers.lambdaUpdate(GovBizProjectApprove.class)
- .eq(GovBizProjectApprove::getBaseProjName, projName)
- .eq(GovBizProjectApprove::getBaseProjId, old.getBaseProjId())
- .set(GovBizProjectApprove::getBaseProjId,newProjId));
- cimplementService.update(Wrappers.lambdaUpdate(GovBizProjectCimplement.class)
- .eq(GovBizProjectCimplement::getBaseProjName, projName)
- .eq(GovBizProjectCimplement::getBaseProjId, old.getBaseProjId())
- .set(GovBizProjectCimplement::getBaseProjId,newProjId));
- mimplementService.update(Wrappers.lambdaUpdate(GovBizProjectMimplement.class)
- .eq(GovBizProjectMimplement::getBaseProjName, projName)
- .eq(GovBizProjectMimplement::getBaseProjId, old.getBaseProjId())
- .set(GovBizProjectMimplement::getBaseProjId,newProjId));
- procureService.update(Wrappers.lambdaUpdate(GovBizProjectProcure.class)
- .eq(GovBizProjectProcure::getBaseProjName, projName)
- .eq(GovBizProjectProcure::getBaseProjId, old.getBaseProjId())
- .set(GovBizProjectProcure::getBaseProjId,newProjId));
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- System.out.println(JSON.toJSONString(save));
- }
- }
-
- System.out.println("导入完成");
- }
-
- @Test
- public void test2() throws FileNotFoundException {
- //先删除 开发区的
- List<GovBizProjectBaseinfo> lss = baseinfoService.list(Wrappers.lambdaQuery(GovBizProjectBaseinfo.class)
- .eq(GovBizProjectBaseinfo::getBaseAreaCode, RegionConst.LS_KF + BizConst.NINE_AREA_CODE_LAST));
- List<String> codes = lss.stream().map(GovBizProjectBaseinfo::getBaseProjId).collect(Collectors.toList());
- applyService.remove(Wrappers.lambdaQuery(GovBizProjectApply.class)
- .in(GovBizProjectApply::getBaseProjId,codes));
- approveService.remove(Wrappers.lambdaQuery(GovBizProjectApprove.class)
- .in(GovBizProjectApprove::getBaseProjId,codes));
- cimplementService.remove(Wrappers.lambdaQuery(GovBizProjectCimplement.class)
- .in(GovBizProjectCimplement::getBaseProjId,codes));
- mimplementService.remove(Wrappers.lambdaQuery(GovBizProjectMimplement.class)
- .in(GovBizProjectMimplement::getBaseProjId,codes));
- procureService.remove(Wrappers.lambdaQuery(GovBizProjectProcure.class)
- .in(GovBizProjectProcure::getBaseProjId,codes));
- baseinfoService.remove(Wrappers.lambdaQuery(GovBizProjectBaseinfo.class)
- .in(GovBizProjectBaseinfo::getBaseProjId,codes));
-
- Workbook wb;
- File f = new File("C:\\Users\\PoffyZhang\\Desktop\\丽水开发区(1).xlsx");
- wb = readExcel(new FileInputStream(f),f.getName());
- Row row = null;
- if(wb != null) {
- //获取第一个sheet
- Sheet sheet = wb.getSheetAt(0);
- //获取最大行数
- int rownum = sheet.getPhysicalNumberOfRows();
-
- //获取第一行
- row = sheet.getRow(0);
- //获取最大列数
- for (int i = 1; i < rownum; i++) {
- row = sheet.getRow(i);
- if (Objects.isNull(row) || Objects.isNull(row.getCell(0))) {
- break;
- }
- GovBizProjectSaveDTO saveDTO = new GovBizProjectSaveDTO();
-
- GovBizProjectBaseinfoDTO baseinfoDTO = new GovBizProjectBaseinfoDTO();
- //
- String projName = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
- baseinfoDTO.setBaseProjName(projName);
- baseinfoDTO.setBaseAreaCode(RegionConst.LS_KF + BizConst.NINE_AREA_CODE_LAST);
- baseinfoDTO.setBaseAreaName(RegionConst.LS_KF_NAME);
- baseinfoDTO.setAreaCode(RegionConst.RC_LS);
- baseinfoDTO.setIsEffective(BizProjectContant.ProjectCollection.IS_EFFECTIVE);
- baseinfoDTO.setTongTime(LocalDateTime.now());
- baseinfoDTO.setBizTime(LocalDateTime.now());
- baseinfoDTO.setOp("insert");
- String principal = Objects.nonNull(row.getCell(3)) ? row.getCell(3).toString() : StringUtils.EMPTY;
- baseinfoDTO.setBaseProjPrincipal(principal);
- String contacts = Objects.nonNull(row.getCell(4)) ? row.getCell(4).toString() : StringUtils.EMPTY;
- baseinfoDTO.setBaseProjContacts(contacts);
- baseinfoDTO.setBaseProjType("01");
- saveDTO.setBaseinfo(baseinfoDTO);
-
- GovBizProjectApplyDTO applyDTO = new GovBizProjectApplyDTO();
- String baseProjSetYear = Objects.nonNull(row.getCell(20)) ? row.getCell(20).toString().substring(0,4) : StringUtils.EMPTY;
- applyDTO.setBaseProjSetYear(baseProjSetYear);
- saveDTO.setApply(applyDTO);
- //生产编号
- String projId = projectCodeUtil.generateProjectCode(saveDTO);
- saveDTO.setBaseProjId(projId);
- saveDTO.getBaseinfo().setBaseProjId(projId);
- saveDTO.getApply().setBaseProjId(projId);
- UserInfoDetails user = new UserInfoDetails();
- user.setRegionCode(RegionConst.LS_KF);
- user.setUsername("系统导入");
- String save = "";
- try {
- save = manage.save(saveDTO, user);
- }catch (Exception e){
- e.printStackTrace();
- }
- System.out.println(JSON.toJSONString(save));
- }
- }
- }
-
- //这个下面是单独函数
- public static Workbook readExcel(InputStream is, String extString){
- Workbook wb = null;
- extString = extString.substring(extString.lastIndexOf("."));
- try {
- if(".xls".equals(extString)){
- return wb = new HSSFWorkbook(is);
- }else if(".xlsx".equals(extString)){
- return wb = new XSSFWorkbook(is);
- }else {
- return wb = null;
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return wb;
- }
- }
|