| | |
| | | package com.product.project.management.service; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | import com.product.core.service.support.AbstractBaseService; |
| | | import com.product.project.management.config.Cmnconst; |
| | | import com.product.project.management.config.SystemCode; |
| | | import com.product.project.management.util.ItemizeUtil; |
| | | import com.product.project.management.util.ProjectItemUtil; |
| | | import com.product.util.BaseUtil; |
| | | |
| | | import cn.hutool.core.lang.UUID; |
| | | |
| | | @Service("projectBudget") |
| | | public class ProjectBudgetService extends AbstractBaseService{ |
| | | |
| | | @Autowired |
| | | BaseDao baseDao; |
| | | |
| | | |
| | | /** |
| | | * 项目预算导入 |
| | | * @param dtParseData 解析数据 |
| | | * @param headerDataList excel标题数据 |
| | | */ |
| | | public DataTableEntity importBudget(DataTableEntity dtParseData, List<List<String>> headerDataList) { |
| | | public DataTableEntity importBudget2(DataTableEntity dtParseData, List<List<String>> headerDataList) { |
| | | try { |
| | | |
| | | |
| | | //判断是否有解析EXCEL数据 |
| | | if (BaseUtil.dataTableIsEmpty(dtParseData)) { |
| | | return null; |
| | | } |
| | | //声明异常集 |
| | | StringBuilder sbError = new StringBuilder(); |
| | | //临时存储集合 |
| | | Map<String, String> itemizeFullNameAndUUID = new HashMap<>(); |
| | | Map<String, String> itemizeFullNameAndCode = new HashMap<>(); |
| | | Map<String, String> itemizeFullNameAndMaxCode = new HashMap<>(); |
| | | DataTableEntity dtNewProjectItem = new DataTableEntity(); |
| | | |
| | | //遍历excel转换数据 |
| | | for (int i = 0; i < dtParseData.getRows(); i++) { |
| | | //获取excel行数据 |
| | | FieldSetEntity fseParseData = dtParseData.getFieldSetEntity(i); |
| | | //生成项目分项 |
| | | String uuid = autoCreateProjectItem(dtNewProjectItem, fseParseData, itemizeFullNameAndUUID, itemizeFullNameAndCode, itemizeFullNameAndMaxCode); |
| | | fseParseData.setValue(Cmnconst.PROJECT_ITEMIZE_UUID, uuid); |
| | | |
| | | //根据解析数据获取项目分项信息 |
| | | List<Object> params = new ArrayList<>(); |
| | | String projectItem = autoCreateFilter(params, fseParseData); |
| | | |
| | | //根据模板项目分项查询数据库项目分项 |
| | | FieldSetEntity fseProjectItem = baseDao.getFieldSetEntityByFilter(Cmnconst.PRODUCT_PROJECT_ITEM, "project_uuid IN (SELECT uuid FROM product_project_qxlw WHERE project_name=?) AND itemize_full_name=?", params.toArray(), false); |
| | | if(fseProjectItem==null) { |
| | | sbError.append(String.format("第 %s 行项目分项 '%s' 不存在于系统中。", fseParseData.getString("~row~"), projectItem)); |
| | | continue; |
| | | } |
| | | //设置关联项目分项 |
| | | fseParseData.setValue("project_itemize_uuid", fseProjectItem.getUUID()); |
| | | |
| | | //获取编码生成材料类型 |
| | | String budgetCode = fseParseData.getString(Cmnconst.BUDGET_CODE); |
| | | Integer materialType = 0; |
| | | if("架立筋".equals(budgetCode)) { |
| | | materialType = 1; |
| | | }else if("声测管".equals(budgetCode)) { |
| | | materialType = 2; |
| | | //生成材料类型 |
| | | String bugetCode = fseParseData.getString(Cmnconst.BUDGET_CODE); |
| | | String materialType = null; |
| | | if ("声测管".equals(bugetCode)) { |
| | | materialType = "2"; |
| | | }else { |
| | | //网片 |
| | | FieldSetEntity materialInfo = baseDao.getFieldSetEntityByFilter(Cmnconst.PRODUCT_PROJECT_BASE_MATERIAL, "material_type=3 AND material_name like ?", new Object[] {fseParseData.getString(Cmnconst.MATERIAL_CODE)+"%"}, false); |
| | | if (materialInfo!=null) { |
| | | materialType = 3; |
| | | String materialCode = fseParseData.getString(Cmnconst.MATERIAL_CODE); |
| | | if (materialCode.contains("架立筋")) { |
| | | materialType = "1"; |
| | | fseParseData.setValue(Cmnconst.MATERIAL_CODE, ItemizeUtil.getChineseOrEnglishOrNumber("ENGLISHNUMBER", materialCode)); |
| | | }else if (materialCode.contains("钢筋网")) { |
| | | materialType = "3"; |
| | | fseParseData.setValue(Cmnconst.MATERIAL_CODE, ItemizeUtil.getChineseOrEnglishOrNumber("ENGLISHNUMBER", materialCode)); |
| | | }else{ |
| | | FieldSetEntity fseMaterial = baseDao.getFieldSetEntityByFilter(Cmnconst.PRODUCT_PROJECT_BASE_MATERIAL,"material_code=?", new Object[] {materialCode}, false); |
| | | if (fseMaterial != null) { |
| | | materialType = fseMaterial.getString(Cmnconst.MATERIAL_TYPE); |
| | | if (materialType.indexOf("0")>-1) { |
| | | materialType = "0"; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | fseParseData.setValue(Cmnconst.MATERIAL_TYPE, materialType); |
| | | } |
| | | //判断是否包含错误信息 |
| | | if (!BaseUtil.strIsNull(sbError.toString())) { |
| | | throw new BaseException(SystemCode.PROJECT_BUDGET_IMPORT_FAIL.getValue(), sbError.toString()); |
| | | if (!BaseUtil.dataTableIsEmpty(dtNewProjectItem)) { |
| | | try { |
| | | baseDao.add(dtNewProjectItem); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return dtParseData; |
| | | } |
| | | |
| | | /** |
| | | * 自动生成查询项目分项filter |
| | | * @param params |
| | | * 通过field1,field2,field3,field4,field5,field6数据自动生成项目分项数据 |
| | | * 如果项目分项已存在数据库或者新生成,将存入map集合中以便使用 |
| | | * 如果项目分项不存在于数据库则生成项目分项数据 |
| | | * @param dtNewProjectItem |
| | | * @param fseParseData |
| | | * @param itemizeFullNameAndUUID |
| | | * @param itemizeFullNameAndCode |
| | | * @param itemizeFullNameAndMaxCode |
| | | * @return |
| | | */ |
| | | public String autoCreateFilter(List<Object> params , FieldSetEntity fseParseData) { |
| | | StringBuilder projectItem = new StringBuilder(); |
| | | for (int i = 1; i < 7; i++) { |
| | | String fieldValue = fseParseData.getString("field"+i); |
| | | if(!BaseUtil.strIsNull(fieldValue)) { |
| | | if (i>2) { |
| | | projectItem.append("/"); |
| | | } |
| | | if(i==1) { |
| | | params.add(fieldValue); |
| | | }else{ |
| | | projectItem.append(fieldValue); |
| | | public String autoCreateProjectItem(DataTableEntity dtNewProjectItem, FieldSetEntity fseParseData, Map<String, String> itemizeFullNameAndUUID, Map<String, String> itemizeFullNameAndCode, Map<String, String> itemizeFullNameAndMaxCode) { |
| | | String itemizFullName = null; |
| | | String finalUUID = null; |
| | | FieldSetEntity fseProject = baseDao.getFieldSetByFilter(Cmnconst.PRODUCT_PROJECT_QXLW, "project_name=?", new Object[] {fseParseData.getString("field0")}, false); |
| | | String projectUUID = fseProject.getUUID(); |
| | | //循环field1~field6字段,逐步生成对应的项目分项数据 |
| | | for (int i = 1; i < 8; i++) { |
| | | |
| | | //获取分项名称,判断是否为空 |
| | | String itemize = fseParseData.getString("field"+i); |
| | | //判断项目分项名称是否为空(如为空:则该预算对应的项目分项生成完毕,预算关联的UUID为上一个field分项对应的UUID |
| | | if (BaseUtil.strIsNull(itemize)) { |
| | | finalUUID = itemizeFullNameAndUUID.get(itemizFullName); |
| | | break; |
| | | } |
| | | |
| | | //获取上级分项和当前分项全称(如当前分项没有上级:则全称等于名称;如有上级:则全称等于上级全称/名称) |
| | | String itemizeParent = null; |
| | | //获取分项全称 |
| | | if (BaseUtil.strIsNull(itemizFullName)) { |
| | | itemizFullName = itemize; |
| | | }else { |
| | | itemizeParent = itemizFullName; |
| | | itemizFullName = itemizFullName + "/" + itemize; |
| | | } |
| | | |
| | | //判断项目分项是否存在于集合中 |
| | | if(!itemizeFullNameAndCode.containsKey(itemizFullName)) { |
| | | //查询分项和上级分项是否存在于数据库中 |
| | | FieldSetEntity fseProjectItem = baseDao.getFieldSetEntityByFilter(Cmnconst.PRODUCT_PROJECT_ITEM, "project_uuid = ? AND itemize_full_name=?", new Object[] {projectUUID,itemizFullName}, false); |
| | | if (fseProjectItem == null) { |
| | | String uuid = UUID.randomUUID().toString(); |
| | | String code; |
| | | |
| | | if (!BaseUtil.strIsNull(itemizeFullNameAndMaxCode.get(itemizeParent))) { |
| | | code = ProjectItemUtil.autoAddOneCode(itemizeFullNameAndMaxCode.get(itemizeParent)); |
| | | }else { |
| | | FieldSetEntity fseMaxProjectItem; |
| | | if(BaseUtil.strIsNull(itemizeParent)) { |
| | | fseMaxProjectItem = baseDao.getFieldSetEntityByFilter(Cmnconst.PRODUCT_PROJECT_ITEM, new String[] {"Max(itemize_code)itemize_code"}, "project_uuid = ? AND itemize_parent_code IS NULL", new Object[] {projectUUID}, false, null); |
| | | }else { |
| | | fseMaxProjectItem = baseDao.getFieldSetEntityByFilter(Cmnconst.PRODUCT_PROJECT_ITEM, new String[] {"Max(itemize_code)itemize_code"}, "project_uuid = ? AND itemize_parent_code = ?", new Object[] {projectUUID, itemizeFullNameAndCode.get(itemizeParent)}, false, null); |
| | | } |
| | | if (fseMaxProjectItem==null || BaseUtil.strIsNull(fseMaxProjectItem.getString(Cmnconst.ITEMIZE_CODE))) { |
| | | code = ProjectItemUtil.autoCreateCode(itemizeFullNameAndCode.get(itemizeParent)); |
| | | }else { |
| | | code = ProjectItemUtil.autoAddOneCode(fseMaxProjectItem.getString(Cmnconst.ITEMIZE_CODE)); |
| | | } |
| | | } |
| | | FieldSetEntity fseNewProject = new FieldSetEntity(Cmnconst.PRODUCT_PROJECT_ITEM); |
| | | fseNewProject.setValue(Cmnconst.UUID, uuid); |
| | | fseNewProject.setValue("~type~", "add"); |
| | | fseNewProject.setValue(Cmnconst.PROJECT_UUID, projectUUID); |
| | | fseNewProject.setValue(Cmnconst.ITEMIZE, itemize); |
| | | fseNewProject.setValue(Cmnconst.ITEMIZE_CODE, code); |
| | | fseNewProject.setValue(Cmnconst.ITEMIZE_PARENT_CODE, itemizeFullNameAndCode.get(itemizeParent)); |
| | | fseNewProject.setValue(Cmnconst.ITEMIZE_FULL_NAME, itemizFullName); |
| | | dtNewProjectItem.addFieldSetEntity(fseNewProject); |
| | | |
| | | itemizeFullNameAndUUID.put(itemizFullName, uuid); |
| | | itemizeFullNameAndCode.put(itemizFullName, code); |
| | | itemizeFullNameAndMaxCode.put(itemizeParent, code); |
| | | }else { |
| | | String uuid = fseProjectItem.getUUID(); |
| | | String code = fseProjectItem.getString(Cmnconst.ITEMIZE_CODE); |
| | | FieldSetEntity fseMaxProjectItem; |
| | | if (BaseUtil.strIsNull(fseProjectItem.getString(Cmnconst.ITEMIZE_PARENT_CODE))) { |
| | | fseMaxProjectItem = baseDao.getFieldSetEntityByFilter(Cmnconst.PRODUCT_PROJECT_ITEM, new String[] {"Max(itemize_code)itemize_code"}, "project_uuid = ? AND itemize_parent_code IS NULL", new Object[] {projectUUID}, false, null); |
| | | }else { |
| | | fseMaxProjectItem = baseDao.getFieldSetEntityByFilter(Cmnconst.PRODUCT_PROJECT_ITEM, new String[] {"Max(itemize_code)itemize_code"}, " project_uuid = ? AND itemize_parent_code=?", new Object[] {projectUUID, fseProjectItem.getString(Cmnconst.ITEMIZE_PARENT_CODE)}, false, null); |
| | | } |
| | | itemizeFullNameAndUUID.put(itemizFullName, uuid); |
| | | itemizeFullNameAndCode.put(itemizFullName, code); |
| | | itemizeFullNameAndMaxCode.put(itemizeParent, fseMaxProjectItem.getString(Cmnconst.ITEMIZE_CODE)); |
| | | } |
| | | } |
| | | } |
| | | params.add(projectItem.toString()); |
| | | return projectItem.toString(); |
| | | return finalUUID; |
| | | } |
| | | |
| | | /** |
| | | * 清除所有预算 |
| | | * @param projectUUID |
| | | * @return |
| | | */ |
| | | public boolean clearBudget(String projectUUID) { |
| | | DataTableEntity dtOutbound = baseDao.listTable(Cmnconst.PRODUCT_PROJECT_OUTBOUND, "project_uuid=?", new Object[] {projectUUID}); |
| | | if (!BaseUtil.dataTableIsEmpty(dtOutbound)) { |
| | | throw new BaseException(SystemCode.PROJeCT_BUDGET_CLEAR_FAIL_HAS_OUTBOUND.getValue(), SystemCode.PROJeCT_BUDGET_CLEAR_FAIL_HAS_OUTBOUND.getText()); |
| | | } |
| | | return baseDao.executeUpdate("DELETE FROM product_project_budget WHERE project_itemize_uuid IN (SELECT uuid FROM product_project_item WHERE project_uuid=?)", new Object[] {projectUUID}); |
| | | } |
| | | } |