|
@@ -2,16 +2,200 @@ package com.rongwei.bscommon.system.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.rongwei.bscommon.system.dao.QhsePointsUnitDetailsDao;
|
|
import com.rongwei.bscommon.system.dao.QhsePointsUnitDetailsDao;
|
|
|
|
+import com.rongwei.bscommon.system.dao.QhsePointsUnitRecordDao;
|
|
import com.rongwei.bscommon.system.service.QhsePointsUnitDetailsService;
|
|
import com.rongwei.bscommon.system.service.QhsePointsUnitDetailsService;
|
|
|
|
+import com.rongwei.bscommon.system.utils.QHSEUtils;
|
|
import com.rongwei.bsentity.domain.QhsePointsUnitDetailsDo;
|
|
import com.rongwei.bsentity.domain.QhsePointsUnitDetailsDo;
|
|
|
|
+import com.rongwei.bsentity.domain.QhsePointsUnitRecordDo;
|
|
|
|
+import com.rongwei.bsentity.dto.UnitPointsImportValidationResult;
|
|
|
|
+import com.rongwei.bsentity.vo.QhsePointsUnitDetailsVo;
|
|
|
|
+import com.rongwei.bsentity.vo.QhsePointsUnitRecordVo;
|
|
|
|
+import com.rongwei.rwadmincommon.system.vo.SysUserVo;
|
|
import com.rongwei.rwcommon.base.R;
|
|
import com.rongwei.rwcommon.base.R;
|
|
|
|
+import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
public class QhsePointsUnitDetailsServiceImpl extends
|
|
public class QhsePointsUnitDetailsServiceImpl extends
|
|
ServiceImpl<QhsePointsUnitDetailsDao, QhsePointsUnitDetailsDo> implements QhsePointsUnitDetailsService {
|
|
ServiceImpl<QhsePointsUnitDetailsDao, QhsePointsUnitDetailsDo> implements QhsePointsUnitDetailsService {
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private QhsePointsUnitRecordDao qhsePointsUnitRecordDao;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional
|
|
|
|
+ public R ProcessImportedData(List<QhsePointsUnitDetailsVo> parsedData) {
|
|
|
|
+
|
|
|
|
+ if (parsedData.isEmpty()) {
|
|
|
|
+ return R.error("无有效数据需要处理");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ SysUserVo currentUser = QHSEUtils.getCurrentUser();
|
|
|
|
+
|
|
|
|
+ //校验数据
|
|
|
|
+ UnitPointsImportValidationResult unitPointsImportValidationResult= validateImportedData(parsedData,currentUser);
|
|
|
|
+ if (unitPointsImportValidationResult.isValid()){
|
|
|
|
+ return R.error(unitPointsImportValidationResult.getErrorMsg());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //根据加入的子表记录计算应该修改的加扣分信息
|
|
|
|
+ List<QhsePointsUnitRecordDo> qhsePointsUnitRecordDos = calMainTablePoint(unitPointsImportValidationResult.getValidData());
|
|
|
|
+
|
|
|
|
+ //更新子表
|
|
|
|
+ this.getBaseMapper().insertBatchSomeColumn( unitPointsImportValidationResult.getValidData());
|
|
|
|
+
|
|
|
|
+ //更新主表.
|
|
|
|
+ qhsePointsUnitRecordDao.updateScoreInfo(qhsePointsUnitRecordDos,currentUser);
|
|
|
|
+ return R.ok();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<QhsePointsUnitRecordDo> calMainTablePoint(List<QhsePointsUnitDetailsDo> validData) {
|
|
|
|
+
|
|
|
|
+ //获取对应的加分记录数据
|
|
|
|
+ Map<String, List<QhsePointsUnitDetailsDo>> details =validData.stream().collect(Collectors.groupingBy(
|
|
|
|
+ QhsePointsUnitDetailsDo::getPointsunitid
|
|
|
|
+ ));
|
|
|
|
+
|
|
|
|
+ QhsePointsUnitRecordDo qhsePointsUnitRecordDo =null;
|
|
|
|
+ List<QhsePointsUnitRecordDo> mainUpdates =new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ for (String key: details.keySet()){
|
|
|
|
+ qhsePointsUnitRecordDo = new QhsePointsUnitRecordDo();
|
|
|
|
+ qhsePointsUnitRecordDo.setId(key);
|
|
|
|
+ qhsePointsUnitRecordDo.setBonuspoints(0);
|
|
|
|
+ qhsePointsUnitRecordDo.setBonusdescription("");
|
|
|
|
+ qhsePointsUnitRecordDo.setDeductionpoints(0);
|
|
|
|
+ qhsePointsUnitRecordDo.setDeductiondescription("");
|
|
|
|
+ qhsePointsUnitRecordDo.setCumulativescore(0);
|
|
|
|
+ //分值大于0 累加到 加分分值,加分描述追加 ,反之 累加 扣分分值,扣分描述
|
|
|
|
+ for (QhsePointsUnitDetailsDo qhsePointsUnitDetailsDo : details.get(key)) {
|
|
|
|
+ if (qhsePointsUnitDetailsDo.getPoints()>0){
|
|
|
|
+ qhsePointsUnitRecordDo.setBonuspoints(qhsePointsUnitDetailsDo.getPoints()+qhsePointsUnitRecordDo.getBonuspoints());
|
|
|
|
+ }else{
|
|
|
|
+ qhsePointsUnitRecordDo.setDeductionpoints(qhsePointsUnitDetailsDo.getPoints()+qhsePointsUnitRecordDo.getDeductionpoints());
|
|
|
|
+ }
|
|
|
|
+ qhsePointsUnitRecordDo.setCumulativescore(qhsePointsUnitRecordDo.getCumulativescore() + qhsePointsUnitDetailsDo.getPoints());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ mainUpdates.add(qhsePointsUnitRecordDo);
|
|
|
|
+ }
|
|
|
|
+ return mainUpdates;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 校验数据
|
|
|
|
+ *
|
|
|
|
+ * @param parsedData
|
|
|
|
+ * @param currentUser
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private UnitPointsImportValidationResult validateImportedData(List<QhsePointsUnitDetailsVo> parsedData, SysUserVo currentUser) {
|
|
|
|
+
|
|
|
|
+ UnitPointsImportValidationResult unitPointsImportValidationResult = new UnitPointsImportValidationResult();
|
|
|
|
+ List<QhsePointsUnitDetailsDo> insertList = new ArrayList<>();
|
|
|
|
+ QhsePointsUnitDetailsDo detailsDo;
|
|
|
|
+ String recordKey ="",detailKey ="";
|
|
|
|
+
|
|
|
|
+ //1.查询 主表 年度+单位名称记录 分组
|
|
|
|
+ Map<String, List<QhsePointsUnitRecordVo>> recordMap = loadAnnualUnits();
|
|
|
|
+ //2.查询子表已存在的记录 单位名称+ 描述 + 发生时间 分组
|
|
|
|
+ Map<String, List<QhsePointsUnitDetailsVo>> detailMap = loadAnnualDetails();
|
|
|
|
+
|
|
|
|
+ //是否有相同的单位名称,子表是否有相同的记录
|
|
|
|
+ boolean notSameUnitName =false,sameDetail =false;
|
|
|
|
+ StringBuilder errors = new StringBuilder();
|
|
|
|
+ //循环校验
|
|
|
|
+ for (QhsePointsUnitDetailsVo vo:parsedData){
|
|
|
|
+ //校验单位名称 年度 + 单位名称
|
|
|
|
+ recordKey = vo.getPointsyear()+"-"+vo.getUnitname();
|
|
|
|
+ if (recordMap.get(recordKey)==null && !sameDetail){
|
|
|
|
+ errors.append("【").append(vo.getUnitname()).append("】");
|
|
|
|
+ notSameUnitName=true;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //校验子表唯一值 单位+描述 +时间
|
|
|
|
+ detailKey = vo.getUnitname()+"-"+vo.getDescription()+"-"+vo.getHappentimestr();
|
|
|
|
+ if (detailMap.get(detailKey)!=null && !notSameUnitName){
|
|
|
|
+ errors.append("【").append(vo.getUnitname()).append(vo.getDescription()).append(vo.getHappentimestr()).append("】");
|
|
|
|
+ sameDetail =true;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //生成新的实体DO
|
|
|
|
+ detailsDo =new QhsePointsUnitDetailsDo();
|
|
|
|
+ convertToDo(detailsDo,vo,currentUser,recordMap.get(recordKey).get(0).getId());
|
|
|
|
+ insertList.add(detailsDo);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(notSameUnitName || sameDetail){
|
|
|
|
+ unitPointsImportValidationResult.setValid(true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(notSameUnitName ){
|
|
|
|
+ errors.append("单位不在对应年度清单中,请检查再导入");
|
|
|
|
+ unitPointsImportValidationResult.setErrorMsg(errors.toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(sameDetail ){
|
|
|
|
+ errors.append("加扣分说明已存在,请重新维护");
|
|
|
|
+ unitPointsImportValidationResult.setErrorMsg("该年度"+errors);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ unitPointsImportValidationResult.setValidData(insertList);
|
|
|
|
+
|
|
|
|
+ return unitPointsImportValidationResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void convertToDo(QhsePointsUnitDetailsDo detailsDo, QhsePointsUnitDetailsVo vo, SysUserVo currentUser, String pointsunitid) {
|
|
|
|
+ BeanUtils.copyProperties(vo,detailsDo);
|
|
|
|
+ detailsDo.setId(SecurityUtil.getUUID());
|
|
|
|
+ detailsDo.setDeleted("0");
|
|
|
|
+ detailsDo.setSource("1"); //人工导入
|
|
|
|
+ detailsDo.setPointsunitid(pointsunitid);
|
|
|
|
+ detailsDo.setState("1"); //默认生成了 主表信息
|
|
|
|
+ QHSEUtils.initModelGeneralParameters(detailsDo, currentUser);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<String> getValidYears() {
|
|
|
|
+ List<String> years = new ArrayList<>();
|
|
|
|
+ int currentYear = LocalDate.now().getYear();
|
|
|
|
+ years.add(String.valueOf(currentYear));
|
|
|
|
+
|
|
|
|
+ if (LocalDate.now().getMonthValue() == 1) {
|
|
|
|
+ years.add(String.valueOf(currentYear - 1));
|
|
|
|
+ }
|
|
|
|
+ return years;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Map<String, List<QhsePointsUnitRecordVo>> loadAnnualUnits() {
|
|
|
|
+ List<String> years = getValidYears();
|
|
|
|
+ return Optional.ofNullable(qhsePointsUnitRecordDao.getRecordByYear(years))
|
|
|
|
+ .filter(list -> !list.isEmpty())
|
|
|
|
+ .map(list -> list.stream()
|
|
|
|
+ .collect(Collectors.groupingBy(QhsePointsUnitRecordVo::getUniquevalue)))
|
|
|
|
+ .orElse(Collections.emptyMap());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Map<String, List<QhsePointsUnitDetailsVo>> loadAnnualDetails() {
|
|
|
|
+ List<String> years = getValidYears();
|
|
|
|
+ return Optional.ofNullable(qhsePointsUnitRecordDao.getDetailsByYear(years))
|
|
|
|
+ .filter(list -> !list.isEmpty())
|
|
|
|
+ .map(list -> list.stream()
|
|
|
|
+ .collect(Collectors.groupingBy(QhsePointsUnitDetailsVo::getUniquevalue)))
|
|
|
|
+ .orElse(Collections.emptyMap());
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|