Commit f23ce0cf authored by wangqiang's avatar wangqiang

采购计划导入功能开发(已测试ok)

parent 13c8d106
...@@ -407,4 +407,5 @@ public class Constants { ...@@ -407,4 +407,5 @@ public class Constants {
public static final String PURCHASE_PLAN_EXCEL_NAME = "\\采购计划列表.xlsx"; public static final String PURCHASE_PLAN_EXCEL_NAME = "\\采购计划列表.xlsx";
public static final String PURCHASE_PLAN_TEMP_FILE_CODE_NAME ="采购计划001模板"; public static final String PURCHASE_PLAN_TEMP_FILE_CODE_NAME ="采购计划001模板";
} }
package com.yonde.dcs.plan.core.listener; package com.yonde.dcs.plan.core.listener;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.yonde.dcs.document.common.entity.vo.DxDocumentVO;
import com.yonde.dcs.plan.common.constants.Constants; import com.yonde.dcs.plan.common.constants.Constants;
import com.yonde.dcs.plan.common.utils.SearchUtil; import com.yonde.dcs.plan.common.utils.SearchUtil;
import com.yonde.dcs.plan.common.vo.ExtPuchasePlanAttributeVO; import com.yonde.dcs.plan.common.vo.ExtPuchasePlanAttributeVO;
import com.yonde.dcs.plan.common.vo.ExtPuchasePlanExcelVO; import com.yonde.dcs.plan.common.vo.ExtPuchasePlanExcelVO;
import com.yonde.dcs.plan.core.service.ExtDistributeRecordService;
import com.yonde.dcs.plan.core.service.ExtPuchasePlanAttributeService; import com.yonde.dcs.plan.core.service.ExtPuchasePlanAttributeService;
import com.yonde.dcs.plan.core.util.CommonUtils;
import com.yonde.dcs.plan.entity.po.ExtPuchasePlanAttribute;
import com.yonde.dex.basedata.data.search.SearchItem; import com.yonde.dex.basedata.data.search.SearchItem;
import com.yonde.dex.basedata.data.search.SearchItems;
import com.yonde.dex.basedata.data.search.SearchQueryCondition;
import com.yonde.dex.basedata.entity.data.DxPageImpl; import com.yonde.dex.basedata.entity.data.DxPageImpl;
import com.yonde.dex.basedata.entity.data.OperatorType; import com.yonde.dex.basedata.entity.data.OperatorType;
import com.yonde.dex.basedata.exception.DxBusinessException; import com.yonde.dex.basedata.exception.DxBusinessException;
import com.yonde.dex.dao.service.util.ApplicationContextUtil;
import com.yonde.dex.dao.service.util.DxPageUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import java.io.File; import javax.servlet.ServletContextListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Component @Component
@Slf4j @Slf4j
public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuchasePlanExcelVO> { public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuchasePlanExcelVO> implements ServletContextListener {
@Autowired @Autowired
private ExtPuchasePlanAttributeService extPuchasePlanAttributeService; private ExtPuchasePlanAttributeService extPuchasePlanAttributeService;
...@@ -43,6 +45,9 @@ public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuc ...@@ -43,6 +45,9 @@ public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuc
*/ */
private List<ExtPuchasePlanExcelVO> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); private List<ExtPuchasePlanExcelVO> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
public PurchasePlanExcelReadListenner() {
this.extPuchasePlanAttributeService = ApplicationContextUtil.getBean(ExtPuchasePlanAttributeService.class);
}
@Override @Override
public void invoke(ExtPuchasePlanExcelVO extPuchasePlanExcelVO, AnalysisContext analysisContext) { public void invoke(ExtPuchasePlanExcelVO extPuchasePlanExcelVO, AnalysisContext analysisContext) {
...@@ -64,18 +69,22 @@ public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuc ...@@ -64,18 +69,22 @@ public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuc
} }
for (ExtPuchasePlanExcelVO extPuchasePlanExcelVO : cachedDataList) { for (ExtPuchasePlanExcelVO extPuchasePlanExcelVO : cachedDataList) {
ExtPuchasePlanAttributeVO extPuchasePlanAttributeVO = new ExtPuchasePlanAttributeVO(); ExtPuchasePlanAttributeVO extPuchasePlanAttributeVO = new ExtPuchasePlanAttributeVO();
//excel数据转换成VO对象 //excel数据转换成VO对象
transformObject(extPuchasePlanAttributeVO, extPuchasePlanExcelVO); transformObject(extPuchasePlanAttributeVO, extPuchasePlanExcelVO);
//根据excle中操作符执行 新增、更新、删除 //根据excle中操作符执行 新增、更新、删除
if (Constants.EXCEL_ADD.equals(extPuchasePlanExcelVO.getOperation())) { if (Constants.EXCEL_ADD.equals(extPuchasePlanExcelVO.getOperation())) {
extPuchasePlanAttributeVO.setOperator(OperatorType.ADD); extPuchasePlanAttributeVO.setOperator(OperatorType.ADD);
extPuchasePlanAttributeService.saveRecursion(transformObject(extPuchasePlanAttributeVO, extPuchasePlanExcelVO)); extPuchasePlanAttributeService.saveRecursion(extPuchasePlanAttributeVO);
} else if (Constants.EXCEL_UPDATE.equals(extPuchasePlanExcelVO.getOperation())) { } else if (Constants.EXCEL_UPDATE.equals(extPuchasePlanExcelVO.getOperation())) { //更新
extPuchasePlanAttributeVO.setOperator(OperatorType.MODIFY); //根据计划编码获取采购计划
ExtPuchasePlanAttributeVO purPlanForDB = getPurchasePlanByPlanCode(extPuchasePlanExcelVO.getPlanCode());
purPlanForDB.setOperator(OperatorType.MODIFY);
} else if (Constants.EXCEL_DELETE.equals(extPuchasePlanExcelVO.getOperation())) { extPuchasePlanAttributeService.saveRecursion(transformObject(purPlanForDB,extPuchasePlanExcelVO));
extPuchasePlanAttributeVO.setOperator(OperatorType.REMOVE); } else if (Constants.EXCEL_DELETE.equals(extPuchasePlanExcelVO.getOperation())) { //删除;
//根据计划编码获取采购计划
ExtPuchasePlanAttributeVO purPlanForDB = getPurchasePlanByPlanCode(extPuchasePlanExcelVO.getPlanCode());
purPlanForDB.setOperator(OperatorType.REMOVE);
extPuchasePlanAttributeService.remove(purPlanForDB.getId());
} else { } else {
log.info("sheet={} ,操作符={}无效!", analysisContext.readSheetHolder().getSheetName(), extPuchasePlanExcelVO.getOperation()); log.info("sheet={} ,操作符={}无效!", analysisContext.readSheetHolder().getSheetName(), extPuchasePlanExcelVO.getOperation());
} }
...@@ -107,7 +116,9 @@ public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuc ...@@ -107,7 +116,9 @@ public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuc
extPuchasePlanAttributeVO.setContractDeliveryTime(extPuchasePlanExcelVO.getContractActualDeliveryTime()); extPuchasePlanAttributeVO.setContractDeliveryTime(extPuchasePlanExcelVO.getContractActualDeliveryTime());
extPuchasePlanAttributeVO.setPurDesignFileManager(extPuchasePlanExcelVO.getPurDesignFileManager()); extPuchasePlanAttributeVO.setPurDesignFileManager(extPuchasePlanExcelVO.getPurDesignFileManager());
extPuchasePlanAttributeVO.setPurchaseBudget(extPuchasePlanExcelVO.getPurchaseBudget()); extPuchasePlanAttributeVO.setPurchaseBudget(extPuchasePlanExcelVO.getPurchaseBudget());
extPuchasePlanAttributeVO.setSecretCode(extPuchasePlanExcelVO.getSecretCode()); //获取密级code
String secretCode = CommonUtils.searchDictDataByDictCode(Constants.SECRET_CODE, extPuchasePlanExcelVO.getSecretCode());
extPuchasePlanAttributeVO.setSecretCode(secretCode);
extPuchasePlanAttributeVO.setPlanCode(extPuchasePlanExcelVO.getPlanCode()); extPuchasePlanAttributeVO.setPlanCode(extPuchasePlanExcelVO.getPlanCode());
extPuchasePlanAttributeVO.setNote(extPuchasePlanExcelVO.getNote()); extPuchasePlanAttributeVO.setNote(extPuchasePlanExcelVO.getNote());
//todo wq:excel中的项目代号对应项目属性中什么? //todo wq:excel中的项目代号对应项目属性中什么?
...@@ -124,17 +135,18 @@ public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuc ...@@ -124,17 +135,18 @@ public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuc
if (cachedDataList.size() > 0) { if (cachedDataList.size() > 0) {
for (ExtPuchasePlanExcelVO extPuchasePlanExcelVO : cachedDataList) { for (ExtPuchasePlanExcelVO extPuchasePlanExcelVO : cachedDataList) {
StringBuffer errString = new StringBuffer(); StringBuffer errString = new StringBuffer();
if (StringUtils.isEmpty(extPuchasePlanExcelVO.getPlanCode())) { if(Constants.EXCEL_ADD.equals(extPuchasePlanExcelVO.getOperation()) && StringUtils.isNotEmpty(extPuchasePlanExcelVO.getPlanCode())){
errString.append("项目代号为:" + extPuchasePlanExcelVO.getProjectCode() + "的计划编码不能为空"); ExtPuchasePlanAttributeVO extPuchasePlanAttributeVO = getPurchasePlanByPlanCode(extPuchasePlanExcelVO.getPlanCode());
if(!ObjectUtils.isEmpty(extPuchasePlanAttributeVO)){
errString.append("新增计划编码:" + extPuchasePlanExcelVO.getPlanCode() + "已存在,不能重复导入!!");
errorList.add(errString); errorList.add(errString);
}else {
if(Constants.EXCEL_ADD.equals(extPuchasePlanExcelVO.getOperation())){
DxPageImpl dxPage = extPuchasePlanAttributeService.findRecursion(SearchUtil.buildQuery("planCode", SearchItem.Operator.EQ, extPuchasePlanExcelVO.getPlanCode()));
if(dxPage.getTotalElements() > 0){
errString.append("计划编码:" + extPuchasePlanExcelVO.getPlanCode() + "已存在,不能重复导入!!");
errorList.add(errString);
}
} }
}else if(Constants.EXCEL_UPDATE.equals(extPuchasePlanExcelVO.getOperation()) && StringUtils.isEmpty(extPuchasePlanExcelVO.getPlanCode())){
errString.append("更新计划编码:项目代号为" + extPuchasePlanExcelVO.getProjectCode() + "的计划编码不能为空");
errorList.add(errString);
}else if(Constants.EXCEL_DELETE.equals(extPuchasePlanExcelVO.getOperation()) && StringUtils.isEmpty(extPuchasePlanExcelVO.getPlanCode())){
errString.append("删除计划编码:项目代号为" + extPuchasePlanExcelVO.getProjectCode() + "的计划编码不能为空");
errorList.add(errString);
} }
} }
} }
...@@ -142,12 +154,21 @@ public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuc ...@@ -142,12 +154,21 @@ public class PurchasePlanExcelReadListenner extends AnalysisEventListener<ExtPuc
return errorList; return errorList;
} }
// public static void main(String[] args) { /**
// File file = new File("F:\\yangyi\\项目\\清华核研院\\核研院设计文档及原型\\采购计划导入模板333.xlsx"); * 根据计划编码查询计划
// EasyExcel.read("F:\\yangyi\\项目\\清华核研院\\核研院设计文档及原型\\采购计划导入模板333.xlsx", ExtPuchasePlanExcelVO.class, * @param planCode
// new PurchasePlanExcelReadListenner()) * @return
// .sheet() */
// .doRead(); public ExtPuchasePlanAttributeVO getPurchasePlanByPlanCode(String planCode){
// } //根据计划编码获取数据库中采购计划数据
DxPageImpl<ExtPuchasePlanAttributeVO> dxPuchasePlanPage = extPuchasePlanAttributeService.findRecursion(SearchUtil.buildQuery("planCode", SearchItem.Operator.EQ, planCode));
if (!CollectionUtils.isEmpty(dxPuchasePlanPage.getContent())) {
ExtPuchasePlanAttributeVO extPurPlanForDB = DxPageUtils.getFirst(dxPuchasePlanPage);
return extPurPlanForDB;
}else {
log.info("根据计划编码:{},未查到相关计划",planCode);
}
return null;
}
} }
package com.yonde.dcs.plan.core.util;
import com.yonde.dex.dao.service.util.ApplicationContextUtil;
import com.yonde.dex.dict.feign.DictDataFeignService;
import com.yonde.dex.dict.service.vo.DictDataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.List;
@Slf4j
@Component
public class CommonUtils {
private static DictDataFeignService dictDataFeignService;
public CommonUtils() {
this.dictDataFeignService = ApplicationContextUtil.getBean(DictDataFeignService.class);
}
/**
*根据字典code获取字典code和字典项的值获取key
* @param dictCode
* @param dictValue
* @return
*/
public static String searchDictDataByDictCode(String dictCode, String dictValue) {
if (!StringUtils.isEmpty(dictValue)) {
//字典type 项目代号 ProjectCode
List<DictDataVO> dictDatasByDictCode = dictDataFeignService.getDictDatas(dictCode);
if (CollectionUtils.isEmpty(dictDatasByDictCode)) {
log.error("所查字典为空!字典code为:" + dictCode);
return "";
} else {
for (DictDataVO dictDataVO : dictDatasByDictCode) {
if (dictValue.equals(dictDataVO.getDictValue())) {
return dictDataVO.getDictKey();
}
}
log.error("所查字典数据项为空!字典code为:" + dictCode);
return "";
}
}
log.error("所查字典Key为空!字典code为:" + dictCode);
return "";
}
}
package com.yonde.dcs.plan.core.util;
import com.yonde.dex.basedata.data.search.SearchItem;
import com.yonde.dex.basedata.data.search.SearchQueryBuilder;
import com.yonde.dex.basedata.data.search.SearchQueryCondition;
public class SearchUtil {
/**
* 构建查询条件
*
* @param key
* @param value
* @return
*/
public static SearchQueryCondition buildQuery(String key, SearchItem.Operator operator, Object value) {
return SearchQueryBuilder.openBuild()
.openFilterBuilder()
.setItem(key, operator, value)
.builder().build();
}
/**
* 构建带扩展属性的查询条件
*
* @param key
* @param value
* @param openAttr
* @return
*/
public static SearchQueryCondition buildQueryWithOpenAttr(String key, SearchItem.Operator operator, Object value, String openAttr) {
return SearchQueryBuilder.openBuild()
.setPropName(openAttr)
.openFilterBuilder()
.setItem(key, operator, value)
.builder().build();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment