|
@@ -16,11 +16,28 @@
|
|
*/
|
|
*/
|
|
package org.springblade.meter.service.impl;
|
|
package org.springblade.meter.service.impl;
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
+import org.springblade.common.utils.SnowFlakeUtil;
|
|
|
|
+import org.springblade.core.excel.util.ExcelUtil;
|
|
|
|
+import org.springblade.core.log.exception.ServiceException;
|
|
|
|
+import org.springblade.core.tool.api.R;
|
|
import org.springblade.meter.entity.ContractInventoryForm;
|
|
import org.springblade.meter.entity.ContractInventoryForm;
|
|
import org.springblade.meter.mapper.ContractInventoryFormMapper;
|
|
import org.springblade.meter.mapper.ContractInventoryFormMapper;
|
|
import org.springblade.meter.service.IContractInventoryFormService;
|
|
import org.springblade.meter.service.IContractInventoryFormService;
|
|
import org.springblade.core.mp.base.BaseServiceImpl;
|
|
import org.springblade.core.mp.base.BaseServiceImpl;
|
|
|
|
+import org.springblade.meter.utils.ForestNodeMerger;
|
|
|
|
+import org.springblade.meter.vo.FormTreeVO;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 合同工程清单表 服务实现类
|
|
* 合同工程清单表 服务实现类
|
|
@@ -32,5 +49,169 @@ import org.springframework.stereotype.Service;
|
|
public class ContractInventoryFormServiceImpl extends BaseServiceImpl<ContractInventoryFormMapper, ContractInventoryForm> implements IContractInventoryFormService {
|
|
public class ContractInventoryFormServiceImpl extends BaseServiceImpl<ContractInventoryFormMapper, ContractInventoryForm> implements IContractInventoryFormService {
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 导入excel
|
|
|
|
+ * @param file
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional
|
|
|
|
+ public R<String> importExcel(MultipartFile file,Long projectId, Long contractId) {
|
|
|
|
+ //校验文件类型
|
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
|
+ String fileSuffix = filename.substring(filename.lastIndexOf(".")+1);
|
|
|
|
+ if (!"xls,xlsx".contains(fileSuffix)){
|
|
|
|
+ throw new ServiceException("请传入excel文件");
|
|
|
|
+ }
|
|
|
|
+ List<ContractInventoryForm> excels = ExcelUtil.read(file, ContractInventoryForm.class);
|
|
|
|
+ List<Integer> firstNode = new ArrayList<>();
|
|
|
|
+ //校验必填字段是否为空
|
|
|
|
+ for (int i = 0; i < excels.size(); i++) {
|
|
|
|
+ ContractInventoryForm excel = excels.get(i);
|
|
|
|
+ if (StringUtils.isBlank(excel.getImportNumber()) || StringUtils.isBlank(excel.getFormNumber() ) || StringUtils.isBlank(excel.getFormName())){
|
|
|
|
+ throw new ServiceException("excel中有必填项未填写,请检查后重新导入");
|
|
|
|
+ }
|
|
|
|
+ excel.setProjectId(projectId);
|
|
|
|
+ excel.setContractId(contractId);
|
|
|
|
+ excel.setId(SnowFlakeUtil.getId());
|
|
|
|
+ //保存每个首节点位置
|
|
|
|
+ if (!excel.getImportNumber().contains("-")){
|
|
|
|
+ firstNode.add(i);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ int size = firstNode.size();
|
|
|
|
+ if (size == 0){
|
|
|
|
+ throw new ServiceException("未找到首节点,请检查后重新导入");
|
|
|
|
+ }
|
|
|
|
+ //根据首节点分成多个数组
|
|
|
|
+ List<List<ContractInventoryForm>> lists = new ArrayList<>();
|
|
|
|
+ //只有一个节点,直接单独处理
|
|
|
|
+ if (size == 1){
|
|
|
|
+ lists.add(excels);
|
|
|
|
+ }else {
|
|
|
|
+ for (int i = 0; i < size-1; i++) {
|
|
|
|
+ lists.add(excels.subList(firstNode.get(i),firstNode.get(i+1)));
|
|
|
|
+ }
|
|
|
|
+ lists.add(excels.subList(firstNode.get(size-1),excels.size()));
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ //获取当前合同的首节点
|
|
|
|
+ ContractInventoryForm one = this.getOne(new LambdaQueryWrapper<ContractInventoryForm>()
|
|
|
|
+ .eq(ContractInventoryForm::getContractId, contractId)
|
|
|
|
+ .eq(ContractInventoryForm::getParentId, 0));
|
|
|
|
+ if (one == null){
|
|
|
|
+ throw new ServiceException("未找到当前合同根节点,请联系管理员");
|
|
|
|
+ }
|
|
|
|
+ Long id = one.getId();
|
|
|
|
+ Map<String,ContractInventoryForm> lastMap = new HashMap<>();
|
|
|
|
+ Map<String,ContractInventoryForm> thisMap = new HashMap<>();
|
|
|
|
+ //循环保存
|
|
|
|
+ for (List<ContractInventoryForm> list : lists) {
|
|
|
|
+ ContractInventoryForm form = list.get(0);
|
|
|
|
+ String number = form.getImportNumber();
|
|
|
|
+ form.setAncestors("0,"+id);
|
|
|
|
+ form.setParentId(id);
|
|
|
|
+ lastMap.put(number,form);
|
|
|
|
+ //循环判断层级
|
|
|
|
+ for (ContractInventoryForm fo : list) {
|
|
|
|
+ String num = fo.getImportNumber();
|
|
|
|
+ int i = num.split("-").length - 1;
|
|
|
|
+ fo.setNodeTier(i);
|
|
|
|
+ //判断单位,数量,单价是否存在数据,如果有一个存在数据,则为清单节点
|
|
|
|
+ if (fo.getContractTotal() != null || StringUtils.isNotBlank(fo.getUnit()) || fo.getBidPrice() != null){
|
|
|
|
+ fo.setIsFormNode(1);
|
|
|
|
+ if (fo.getContractTotal() != null){
|
|
|
|
+ fo.setContractTotal(fo.getContractTotal());
|
|
|
|
+ fo.setChangeTotal(fo.getContractTotal());
|
|
|
|
+ }
|
|
|
|
+ if (fo.getBidPrice() != null){
|
|
|
|
+ fo.setCurrentPrice(fo.getBidPrice());
|
|
|
|
+ fo.setChangePrice(fo.getBidPrice());
|
|
|
|
+ }
|
|
|
|
+ if (fo.getContractTotal() != null && fo.getBidPrice() != null){
|
|
|
|
+ fo.setContractMoney(new BigDecimal(fo.getContractTotal()).multiply(fo.getBidPrice()));
|
|
|
|
+ fo.setChangeMoney(fo.getChangeMoney());
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ fo.setIsFormNode(0);
|
|
|
|
+ }
|
|
|
|
+ if (i == 0){
|
|
|
|
+ continue;
|
|
|
|
+ }else if (i == 1){
|
|
|
|
+ fo.setParentNumber(number);
|
|
|
|
+ }else{
|
|
|
|
+ fo.setParentNumber(num.substring(0, num.lastIndexOf("-")));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Map<Integer, List<ContractInventoryForm>> listMap = list.parallelStream().collect(Collectors.groupingBy(ContractInventoryForm::getNodeTier));
|
|
|
|
+ //根据层级循环,为每一层设置数据
|
|
|
|
+ for (int i = 1; i < listMap.size(); i++) {
|
|
|
|
+ List<ContractInventoryForm> forms = listMap.get(i);
|
|
|
|
+ //如果当前层级不存在数据直接跳出
|
|
|
|
+ if (forms == null || forms.size() == 0){
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ for (ContractInventoryForm f : forms) {
|
|
|
|
+ ContractInventoryForm fo = lastMap.get(f.getParentNumber());
|
|
|
|
+ if (fo.getIsFormNode() == 1){
|
|
|
|
+ throw new ServiceException("清单节点:"+fo.getFormName()+"下还有节点,检查后重新上传excel");
|
|
|
|
+ }
|
|
|
|
+ //设置章编号和清单类型,必须根父节点一致
|
|
|
|
+ f.setChapterNumber(fo.getChapterNumber());
|
|
|
|
+ f.setFormType(fo.getFormType());
|
|
|
|
+ //设置父id和祖籍id
|
|
|
|
+ f.setParentId(fo.getId());
|
|
|
|
+ f.setAncestors(fo.getAncestors()+","+fo.getId());
|
|
|
|
+ thisMap.put(f.getImportNumber(),f);
|
|
|
|
+ }
|
|
|
|
+ lastMap = thisMap;
|
|
|
|
+ thisMap = new HashMap<>();
|
|
|
|
+ }
|
|
|
|
+ lastMap.clear();
|
|
|
|
+ }
|
|
|
|
+ //判断是否有节点没有找到上级节点
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ for (ContractInventoryForm excel : excels) {
|
|
|
|
+ if (excel.getParentId() == null){
|
|
|
|
+ sb.append(excel.getFormName()+",");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (sb.length() > 0){
|
|
|
|
+ throw new ServiceException("以下清单名称:"+sb.deleteCharAt(sb.length()-1)+"。未找到上级节点,请修改excel后重新导入");
|
|
|
|
+ }
|
|
|
|
+ //所有节点设置好数据,比对已经存在的节点,然后移除集合,只保存不存在的
|
|
|
|
+
|
|
|
|
+ this.saveBatch(excels);
|
|
|
|
+
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return R.data("导入成功");
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取合同清单树
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public List<FormTreeVO> getFormTree(Long projectId, Long contractId) {
|
|
|
|
+ //获取当前合同下所有节点
|
|
|
|
+ List<FormTreeVO> vos = baseMapper.getAllNode(projectId, contractId);
|
|
|
|
+ //如果当前合同下不存在节点,则创建根节点
|
|
|
|
+ if (vos == null || vos.size() == 0){
|
|
|
|
+ ContractInventoryForm info = new ContractInventoryForm();
|
|
|
|
+ info.setParentId(0L);
|
|
|
|
+ info.setAncestors("0");
|
|
|
|
+ info.setProjectId(projectId);
|
|
|
|
+ info.setContractId(contractId);
|
|
|
|
+ info.setImportNumber("-1");
|
|
|
|
+ info.setFormNumber("");
|
|
|
|
+ info.setFormName("合同工程清单");
|
|
|
|
+ info.setIsFormNode(0);
|
|
|
|
+ baseMapper.insert(info);
|
|
|
|
+ vos = baseMapper.getAllNode(projectId, contractId);
|
|
|
|
+ return vos;
|
|
|
|
+ }
|
|
|
|
+ return ForestNodeMerger.merge(vos);
|
|
|
|
+ }
|
|
}
|
|
}
|