Explorar o código

合同计量单元,导入模板1

qianxb hai 9 meses
pai
achega
33a7ceacff

+ 3 - 0
blade-service-api/blade-meter-api/src/main/java/org/springblade/meter/vo/InventoryFormMeterVO.java

@@ -37,4 +37,7 @@ public class InventoryFormMeterVO extends InventoryFormMeter {
 	@ApiModelProperty(value = "当前部位当前清单当前计量总和")
 	private BigDecimal allMeterMoney;
 
+	@ApiModelProperty(value = "清单编号")
+	private String formNumber;
+
 }

+ 0 - 1
blade-service/blade-meter/src/main/java/org/springblade/meter/controller/MeterTreeController.java

@@ -877,7 +877,6 @@ public class MeterTreeController extends BladeController {
                return meterTreeContractService.contractTreeNodeImport1(id, file);
             }else if (type == 2) {
                return meterTreeContractService.contractTreeNodeImport2(id, file);
-
             }
         }
         return R.fail("操作失败");

+ 5 - 1
blade-service/blade-meter/src/main/java/org/springblade/meter/mapper/InventoryFormMeterMapper.java

@@ -16,10 +16,12 @@
  */
 package org.springblade.meter.mapper;
 
-import feign.Param;
+
+import org.apache.ibatis.annotations.Param;
 import org.springblade.meter.entity.ContractInventoryForm;
 import org.springblade.meter.entity.InventoryFormMeter;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springblade.meter.vo.InventoryFormMeterVO;
 
 import java.util.List;
 
@@ -32,8 +34,10 @@ import java.util.List;
 public interface InventoryFormMeterMapper extends BaseMapper<InventoryFormMeter> {
 
     List<Long> getNodeAllForm(@Param("nodeId") Long nodeId);
+
     ContractInventoryForm dadainfo(@Param("forid") long forid);
 
     void batchInsert(List<InventoryFormMeter> inventoryFormMeters);
 
+    List<InventoryFormMeterVO> getFormByNodeIds(@Param("contractId") Long contractId,@Param("ids") List<Long> nodeIds);
 }

+ 8 - 0
blade-service/blade-meter/src/main/java/org/springblade/meter/mapper/InventoryFormMeterMapper.xml

@@ -43,5 +43,13 @@
         from s_contract_inventory_form
         where id = #{forid};
     </select>
+    <select id="getFormByNodeIds" resultType="org.springblade.meter.vo.InventoryFormMeterVO">
+        select *,(select form_number from s_contract_inventory_form where id = ifm.contract_form_id) as formNumber
+        from s_inventory_form_meter ifm
+        where contract_id = #{contractId} and is_deleted = 0 and contract_meter_id in
+        <foreach collection="ids" item="id" open="(" separator="," close=")">
+            #{id}
+        </foreach>
+    </select>
 
 </mapper>

+ 4 - 0
blade-service/blade-meter/src/main/java/org/springblade/meter/mapper/MeterTreeContractMapper.java

@@ -3,8 +3,10 @@ package org.springblade.meter.mapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
+import org.springblade.meter.entity.InventoryFormApply;
 import org.springblade.meter.entity.InventoryFormMeter;
 import org.springblade.meter.entity.MeterTreeContract;
+import org.springblade.meter.entity.MiddleMeterApply;
 
 import java.math.BigDecimal;
 import java.util.List;
@@ -25,4 +27,6 @@ public interface MeterTreeContractMapper extends BaseMapper<MeterTreeContract> {
     List<String> getAllEqualsNumber(@Param("contractId") Long contractId,@Param("numbers") List<String> collect);
 
     void batchUpdateFormByNumber(@Param("contractId") Long contractId,@Param("map") Map<String, BigDecimal> map);
+
+    List<InventoryFormApply> getMeterInfo(@Param("meterId") Long aLong, @Param("formId") Long id);
 }

+ 4 - 0
blade-service/blade-meter/src/main/java/org/springblade/meter/mapper/MeterTreeContractMapper.xml

@@ -42,4 +42,8 @@
             #{number}
         </foreach>
     </select>
+    <select id="getMeterInfo" resultType="org.springblade.meter.entity.InventoryFormApply">
+        select * from s_inventory_form_apply where contract_meter_id = #{meterId} and contract_form_id = #{formId} and is_deleted = 0
+    </select>
+
 </mapper>

+ 273 - 5
blade-service/blade-meter/src/main/java/org/springblade/meter/service/impl/MeterTreeContractServiceImpl.java

@@ -10,6 +10,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springblade.common.utils.SnowFlakeUtil;
 import org.springblade.core.log.exception.ServiceException;
 import org.springblade.core.mp.base.BaseServiceImpl;
+import org.springblade.core.secure.utils.AuthUtil;
 import org.springblade.core.secure.utils.SecureUtil;
 import org.springblade.core.tool.api.R;
 import org.springblade.core.tool.utils.BeanUtil;
@@ -24,6 +25,7 @@ import org.springblade.meter.mapper.MeterTreeProjectMapper;
 import org.springblade.meter.service.MeterTreeContractService;
 import org.springblade.meter.vo.ChangeNodeVO;
 import org.springblade.meter.vo.ContractFromVO;
+import org.springblade.meter.vo.InventoryFormMeterVO;
 import org.springblade.meter.vo.MeterTreeContractVO;
 import org.springblade.system.entity.Dict;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
@@ -39,6 +41,7 @@ import java.math.BigDecimal;
 import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 @Service
 @AllArgsConstructor
@@ -1511,10 +1514,12 @@ public class MeterTreeContractServiceImpl extends BaseServiceImpl<MeterTreeContr
             }
         }
         //修改合同工程清单的变更后数量
-        Map<String, BigDecimal> map = InventoryFormListUpdate.stream()
-                .collect(Collectors.groupingBy(l -> l.getFormNumber(),
-                        Collectors.mapping(ContractInventoryForm::getChangeTotal, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
-        baseMapper.batchUpdateFormByNumber(meterTreeContract.getContractId(),map);
+        if (InventoryFormListUpdate.size() > 0) {
+            Map<String, BigDecimal> map = InventoryFormListUpdate.stream()
+                    .collect(Collectors.groupingBy(l -> l.getFormNumber(),
+                            Collectors.mapping(ContractInventoryForm::getChangeTotal, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
+            baseMapper.batchUpdateFormByNumber(meterTreeContract.getContractId(), map);
+        }
         return R.success("成功导入:节点("+addNode+")个,"+"新增清单("+addForm+")条,"+"修改清单("+updateForm+")条.");
     }
 
@@ -1525,10 +1530,273 @@ public class MeterTreeContractServiceImpl extends BaseServiceImpl<MeterTreeContr
      * @return
      */
     @Override
+    @Transactional
     public R<String> contractTreeNodeImport1(String id, MultipartFile file) {
-        return null;
+        //获取当前节点
+        MeterTreeContract meterTreeContract = baseMapper.selectById(id);
+        if (meterTreeContract == null){
+            throw new ServiceException("未找到当前导入节点信息");
+        }
+        //查看当前节点是否挂载表单,如果存在表单,则提示
+        List<InventoryFormMeter> meterList = inventoryFormMeterMapper.selectList(new LambdaQueryWrapper<InventoryFormMeter>()
+                .eq(InventoryFormMeter::getContractId, meterTreeContract.getContractId())
+                .eq(InventoryFormMeter::getContractMeterId, id));
+        if (meterList.size() > 0){
+            throw new ServiceException("当前节点已经挂载清单,不能导入");
+        }
+        /*解析excel*/
+        List<Map<String, String>> parseExcelFileToList;
+        try {
+            parseExcelFileToList = this.parseExcelFile(file);
+        }catch (Exception e){
+            throw new ServiceException("excel解析失败,请检查格式:"+e.getMessage());
+        }
+        if (parseExcelFileToList.size() == 0){
+            throw new ServiceException("excel中未发现数据,请检查后再导入");
+        }
+        //过滤掉桩号支付编号为空的
+        parseExcelFileToList = parseExcelFileToList.stream().filter(l->StringUtils.isNotBlank(l.get("桩号支付编号")) && !l.get("桩号支付编号").contains("清单编号需手动填写")).collect(Collectors.toList());
+        /*节点新增*/
+        List<MeterTreeContract> resultNodeListAdd = new LinkedList<>();
+        /*节点金额修改Ids*/
+        Set<Long> updateNodeSet = new HashSet<>();
+        /*清单中间新增*/
+        List<InventoryFormMeter> resultFormListAdd = new LinkedList<>();
+        /*清单中间数量修改*/
+        List<InventoryFormMeter> resultFormListUpdate = new LinkedList<>();
+        /*合同清单变更后数量修改*/
+        List<ContractInventoryForm> InventoryFormListUpdate = new LinkedList<>();
+        /*末尾校验是否被计量过*/
+        Map<Long,Long> checkMeter = new HashMap<>();
+        //是否有子节点
+        Boolean hasChild = false;
+        //是否子节点有表单
+        Boolean hasForm = false;
+        //1 获取当前节点下一层的所有节点
+        List<MeterTreeContract> childNode = this.list(new LambdaQueryWrapper<MeterTreeContract>()
+                .eq(MeterTreeContract::getParentId, meterTreeContract.getId())
+                .eq(MeterTreeContract::getContractId, meterTreeContract.getContractId()));
+        if (childNode.size() > 0){
+            hasChild = true;
+        }
+        //key节点名称,value节点id
+        Map<String, Long> nameMap = childNode.stream().collect(Collectors.toMap(l -> l.getNodeName(), l -> l.getId()));
+        Map<Long, List<InventoryFormMeterVO>> formMap = new HashMap<>();
+        if (hasChild) {
+            List<Long> nodeIds = childNode.stream().map(l -> l.getId()).collect(Collectors.toList());
+            //2 获取节点下所有挂载表单,按照节点id分组
+            List<InventoryFormMeterVO> formMeters = inventoryFormMeterMapper.getFormByNodeIds(meterTreeContract.getContractId(),nodeIds);
+            if (formMeters.size() > 0) {
+                hasForm = true;
+                formMap = formMeters.stream().collect(Collectors.groupingBy(InventoryFormMeterVO::getContractMeterId));
+            }
+        }
+        //获取合同段所有的清单
+        List<ContractInventoryForm> formList = contractInventoryFormMapper.selectList(new LambdaQueryWrapper<ContractInventoryForm>()
+                .eq(ContractInventoryForm::getContractId, meterTreeContract.getContractId())
+                .eq(ContractInventoryForm::getIsFormNode, 1));
+        if (formList.size() == 0){
+            throw new ServiceException("导入失败,当前合同段没有合同工程清单");
+        }
+        List<String> numberList = formList.stream().map(l -> l.getFormNumber()).collect(Collectors.toList());
+        Map<String, ContractInventoryForm> formNumberMap = formList.stream().collect(Collectors.toMap(l -> l.getFormNumber(), l -> l));
+        //3 循环导入数据,查询当前节点下是否存在
+        for (Map<String, String> stringMap : parseExcelFileToList) {
+            String name = stringMap.get("桩号支付编号");
+            //获取所有清单,过滤掉不是清单的数据,过滤掉清单编号不存在的数据,过滤掉没有数量的数据,只留下清单存在,并且有数量的
+            List<String> allForm = stringMap.entrySet().stream()
+                    .filter(l->l.getKey().contains("-") && numberList.indexOf(l.getKey()) != -1 && StringUtils.isNotBlank(l.getValue()))
+                    .map(l->l.getKey()).collect(Collectors.toList());
+            // 查询当前节点下是否存在
+            if (hasChild && nameMap.get(name) != null){
+                updateNodeSet.add(nameMap.get(name));
+                // 循环需要挂载的清单
+                for (String fo : allForm) {
+                    //获取清单信息
+                    ContractInventoryForm form = formNumberMap.get(fo);
+                    //根据节点名称获取节点下的表单
+                    if (hasForm && formMap.get(nameMap.get(name)) != null){
+                        //存在挂载的表单,重新计算中间表
+                        List<InventoryFormMeterVO> list = formMap.get(nameMap.get(name));
+                        Map<String, InventoryFormMeterVO> map = list.stream().collect(Collectors.toMap(l -> l.getFormNumber(), l -> l));
+                        InventoryFormMeterVO vo = map.get(fo);
+                        if (vo == null){
+                            //没挂载过,组装中间表
+                            InventoryFormMeter middleAdd = this.buildInventoryFormMeter(form, nameMap.get(name), stringMap.get(fo));
+                            resultFormListAdd.add(middleAdd);
+                        }else {
+                            //存在节点,并且挂载过,则添加到校验集合
+                            //checkMeter.put(nameMap.get(name),vo.getId());
+                            //变更过直接跳过
+                            if (vo.getBuildPictureTotal().compareTo(vo.getChangeBuildPictureTotal()) != 0){
+                                continue;
+                            }
+                            //目前直接查询,以后优化
+                            List<InventoryFormApply> apply = baseMapper.getMeterInfo(nameMap.get(name),vo.getContractFormId());
+                            if (apply.size() > 0){
+                                continue;
+                            }
+
+                            //挂载过,重新计算中间表
+                            InventoryFormMeter middleUpdate = this.updateInventoryFormMeter(vo, form, stringMap.get(fo));
+                            resultFormListUpdate.add(middleUpdate);
+                        }
+                    }else {
+                        //不存在挂载的表单,组装中间表
+                        InventoryFormMeter middleAdd = this.buildInventoryFormMeter(form, nameMap.get(name), stringMap.get(fo));
+                        resultFormListAdd.add(middleAdd);
+                    }
+                    //设置清单增量
+                    ContractInventoryForm form2 = new ContractInventoryForm();
+                    form2.setFormNumber(form.getFormNumber());
+                    form2.setChangeTotal(this.parseString(stringMap.get(fo)));
+                    InventoryFormListUpdate.add(form2);
+                }
+            }else {
+                //节点不存在,则先新增节点,再直接新增清单
+                MeterTreeContract obj = new MeterTreeContract();
+                Long aLong = SnowFlakeUtil.getId();
+                obj.setId(aLong);
+                obj.setProjectId(meterTreeContract.getProjectId());
+                obj.setContractId(meterTreeContract.getContractId());
+                obj.setParentId(meterTreeContract.getId());
+                obj.setAncestor(meterTreeContract.getAncestor()+","+meterTreeContract.getId());
+                obj.setNodeName(name);
+                obj.setNodeType(6);
+                obj.setDataSourceType(4);
+                obj.setUpdateStatus(0);
+                obj.setRemarks(stringMap.get("备注"));
+                obj.setTenantId(AuthUtil.getTenantId());
+                obj.setIsSupplement(0);
+                obj.setIsResolveForm(1);
+                obj.setIsLock(0);
+                resultNodeListAdd.add(obj);
+                updateNodeSet.add(aLong);
+                for (String fo : allForm) {
+                    //获取清单信息
+                    ContractInventoryForm form = formNumberMap.get(fo);
+                    //设置清单增量
+                    ContractInventoryForm form2 = new ContractInventoryForm();
+                    form2.setFormNumber(form.getFormNumber());
+                    form2.setChangeTotal(this.parseString(stringMap.get(fo)));
+                    InventoryFormListUpdate.add(form2);
+                    //组装中间表
+                    InventoryFormMeter middleAdd = this.buildInventoryFormMeter(form, aLong, stringMap.get(fo));
+                    resultFormListAdd.add(middleAdd);
+                }
+            }
+        }
+
+        int addNode = 0;
+        int addForm = 0;
+        int updateForm = 0;
+        /*节点*/
+        if (resultNodeListAdd.size() > 0) {
+            this.saveBatch(resultNodeListAdd, 1000);
+            addNode = resultNodeListAdd.size();
+        }
+
+        /*清单中间新增*/
+        if (resultFormListAdd.size() > 0) {
+            inventoryFormMeterMapper.batchInsert(resultFormListAdd);
+            addForm = resultFormListAdd.size();
+        }
+
+        /*清单中间数量修改*/
+        if (resultFormListUpdate.size() > 0) {
+            for (InventoryFormMeter inventoryFormMeter : resultFormListUpdate) {
+                String sql = "UPDATE s_inventory_form_meter " +
+                        "SET build_picture_total = ? ," +
+                        "change_build_picture_total = ? ," +
+                        "build_picture_money = ? ," +
+                        "change_build_picture_money = ? WHERE id = ?";
+                jdbcTemplate.update(sql,
+                        inventoryFormMeter.getBuildPictureTotal(),
+                        inventoryFormMeter.getChangeBuildPictureTotal(),
+                        inventoryFormMeter.getBuildPictureMoney(),
+                        inventoryFormMeter.getChangeBuildPictureMoney(),
+                        inventoryFormMeter.getId());
+            }
+            updateForm = resultFormListUpdate.size();
+        }
+
+        /*节点金额修改*/
+        if (updateNodeSet.size() > 0) {
+            for (Long treeContractId : updateNodeSet) {
+                /*重新计算节点下的清单数量金额等*/
+                List<ContractFromVO> decompositionList = contractInventoryFormMapper.getNodeResolveForm(meterTreeContract.getContractId(), treeContractId);
+                if (ObjectUtil.isNotEmpty(decompositionList) && decompositionList.size() > 0) {
+                    BigDecimal moneyAll = BigDecimal.ZERO;
+                    BigDecimal moneyChangeAll = BigDecimal.ZERO;
+
+                    for (ContractFromVO contractFromVO : decompositionList) {
+                        BigDecimal buildPictureTotal = contractFromVO.getBuildPictureTotal(); //变更前数量
+                        BigDecimal changeBuildPictureTotal = contractFromVO.getChangeBuildPictureTotal(); //变更后数量
+                        BigDecimal currentPrice = contractFromVO.getCurrentPrice(); //单价
+
+                        BigDecimal buildPictureMoney = buildPictureTotal.multiply(currentPrice); //变更前金额
+                        BigDecimal changeBuildPictureMoney = changeBuildPictureTotal.multiply(currentPrice); //变更后金额
+
+                        moneyAll = moneyAll.add(buildPictureMoney);
+                        moneyChangeAll = moneyChangeAll.add(changeBuildPictureMoney);
+                    }
+
+                    String sql = "UPDATE s_meter_tree_contract " +
+                            "SET build_picture_money = ?, " +
+                            "change_money = ? WHERE id = ?";
+                    jdbcTemplate.update(sql,
+                            moneyAll,
+                            moneyChangeAll,
+                            treeContractId);
+                }
+            }
+        }
+        //修改合同工程清单的变更后数量
+        if (InventoryFormListUpdate.size() > 0) {
+            Map<String, BigDecimal> map = InventoryFormListUpdate.stream()
+                    .collect(Collectors.groupingBy(l -> l.getFormNumber(),
+                            Collectors.mapping(ContractInventoryForm::getChangeTotal, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
+            baseMapper.batchUpdateFormByNumber(meterTreeContract.getContractId(), map);
+        }
+        return R.success("成功导入:节点("+addNode+")个,"+"新增清单("+addForm+")条,"+"修改清单("+updateForm+")条.");
     }
 
+    /**
+     *  通过清单信息和合同计量单元节点,施工图数量,返回组装好的中间表对象
+     */
+    private InventoryFormMeter buildInventoryFormMeter(ContractInventoryForm form, Long nodeId, String buildTotal){
+        InventoryFormMeter meter = new InventoryFormMeter();
+        meter.setProjectId(form.getProjectId());
+        meter.setContractId(form.getContractId());
+        meter.setContractMeterId(nodeId);
+        meter.setContractFormId(form.getId());
+        try {
+            meter.setBuildPictureTotal(new BigDecimal(buildTotal));
+        }catch (Exception e){
+            throw new ServiceException("导入失败,施工图数量只能包含数字:清单编号:"+form.getFormNumber());
+        }
+        meter.setChangeBuildPictureTotal(new BigDecimal(buildTotal));
+        meter.setBuildPictureMoney(form.getBidPrice().multiply(meter.getBuildPictureTotal()));
+        meter.setChangeBuildPictureMoney(meter.getBuildPictureMoney());
+        return meter;
+    }
+
+    /**
+     *  更新中间表信息
+     */
+    private InventoryFormMeter updateInventoryFormMeter(InventoryFormMeter meter,ContractInventoryForm form,String buildTotal){
+        try {
+            meter.setBuildPictureTotal(meter.getBuildPictureTotal().add(new BigDecimal(buildTotal)));
+        }catch (Exception e){
+            throw new ServiceException("导入失败,施工图数量只能包含数字:清单编号:"+form.getFormNumber());
+        }
+        meter.setChangeBuildPictureTotal(meter.getChangeBuildPictureTotal().add(new BigDecimal(buildTotal)));
+        meter.setBuildPictureMoney(meter.getBuildPictureTotal().multiply(form.getBidPrice()));
+        meter.setChangeBuildPictureMoney(meter.getChangeBuildPictureTotal().multiply(form.getBidPrice()));
+        return meter;
+    }
+
+
     /**
      * 获取对应的父级的ancestor字段
      *