package com.consum.base.service; import com.consum.base.core.CodeGeneratorEnum; import com.consum.base.core.CodeGeneratorService; import com.consum.base.core.utils.MapRowMapper; import com.consum.base.pojo.*; import com.consum.base.core.utils.IdUtil; import com.consum.model.po.*; import com.iplatform.model.po.S_user_core; import com.walker.db.page.GenericPager; import com.walker.infrastructure.utils.DateUtils; import com.walker.infrastructure.utils.StringUtils; import com.walker.jdbc.service.BaseServiceImpl; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @Description 报废单 * @Author 卢庆阳 * @Date 2023/11/1 */ @Service public class LWhFormScrappedServiceImpl extends BaseServiceImpl { @Autowired private CodeGeneratorService codeGeneratorService; @Autowired private BaseWarehouseServiceImpl baseWarehouseService; @Autowired private FinSysTenantDepartmentServiceImpl departmentService; @Autowired private LWhGoodsService lWhGoodsService; @Autowired private LWhFormScrappedGoodsService scrappedGoodsService; @Autowired private SDictDataServiceImpl sDictDataService; @Autowired private BaseGoodsModelsServiceImpl baseGoodsModelsService; @Autowired private BaseGoodsTemplateServiceImpl baseGoodsTemplateService; @Autowired private LWhFormScrappedCoreService lWhFormScrappedCoreService; private static String QUERY_LIST = "SELECT * FROM l_wh_form_scrapped WHERE 1 = 1"; private static final String QUERY_BF_DETAIL_LIST = "select lwfs.BUSINESS_FORM_CODE,\n" + " lwfsg.GOODS_TEMPLATE_NAME,\n" + " lwfsg.BASE_GOODS_MODELS_NAME,\n" + " lwfsg.COUNTS,\n" + " lwfs.AGENCY_NAME,\n" + " lwfs,OPERATOR_NAME,\n" + " lwfs,DEAL_TIME\n" + "from l_wh_form_scrapped lwfs\n" + " inner join l_wh_form_scrapped_goods lwfsg on lwfs.ID = lwfsg.FORM_SCRAPPED_ID "; /** * 新增 * * @param param * @param currentUser 当前登录用户 * @param sysInfo 当前登录用户 * @return */ //1.新增报废单 //2.新增报废单物品 public int add(LWhFormScrappedParam param, S_user_core currentUser, FinSysTenantUser sysInfo) { //1.新增报废单记录 LWhFormScrapped lWhFormScrapped = new LWhFormScrapped(); //报废单id long lWhFormScrappedId = IdUtil.generateId(); lWhFormScrapped.setId(lWhFormScrappedId); lWhFormScrapped.setBusinessFormCode(codeGeneratorService.createBusinessFormCode(CodeGeneratorEnum.Scrapped)); lWhFormScrapped.setWarehouseType(0); Long warehouseId = param.getWarehouseId(); lWhFormScrapped.setWarehouseId(warehouseId); //根据仓库id查询仓库 BaseWarehouse warehouse = this.baseWarehouseService.getById(warehouseId); if (warehouse == null) { log.error("仓库不存在"); return 0; } lWhFormScrapped.setWarehouseName(warehouse.getWarehouseName()); lWhFormScrapped.setAgencyId(Long.valueOf(sysInfo.getTenantId())); lWhFormScrapped.setAgencyName(sysInfo.getTenantName()); lWhFormScrapped.setOperatorId(sysInfo.getSysUserId()); lWhFormScrapped.setOperatorName(sysInfo.getUserName()); lWhFormScrapped.setDealTime(param.getDealTime()); lWhFormScrapped.setStates(0); //根据部门id查询部门 FinSysTenantDepartment department = this.departmentService.getById(sysInfo.getSysDeptId()); if (department != null) { lWhFormScrapped.setDepartmentId(sysInfo.getSysDeptId()); lWhFormScrapped.setDepartmentName(department.getName()); } int flag1 = this.insert(lWhFormScrapped); if (flag1 == 0) { log.error("新增报废单失败"); return 0; } //2.新增报废单物品记录 //根据物品id查询物品 BaseGoodsTemplate goodsTemplate = this.baseGoodsTemplateService.get(new BaseGoodsTemplate(param.getBaseGoodsTemplateId())); if (goodsTemplate == null) { log.error("根据物品id查询物品"); return 0; } List scrappedGoodsParamList = param.getScrappedGoodsList(); if (CollectionUtils.isEmpty(scrappedGoodsParamList)) { log.error("新增报废单物品记录 失败"); return 0; } //获取规格型号id List baseGoodsModelsIdList = scrappedGoodsParamList.stream().map(params -> params.getBaseGoodsModelsId()).collect(Collectors.toList()); //根据规格型号id查询规格型号 List models = this.baseGoodsModelsService.selectByModelsIdList(baseGoodsModelsIdList); Map modelMap = models.stream().collect(Collectors.toMap(model -> model.getId(), model -> model)); //获取报废原因code List scrappedCodeList = scrappedGoodsParamList.stream().map(params -> params.getScrappedCode()).collect(Collectors.toList()); //根据报废原因code查询数据字典 List s_dict_dataList = this.sDictDataService.selectByScrappedCodeList(scrappedCodeList); Map scrappedNameMap = s_dict_dataList.stream().collect(Collectors.toMap(s -> s.getDict_code(), s -> s.getDict_label())); List scrappedGoodsList = new ArrayList<>(); for (LWhFormScrappedGoodsParams params : scrappedGoodsParamList) { LWhFormScrappedGoods scrappedGoods = new LWhFormScrappedGoods(); scrappedGoods.setId(IdUtil.generateId()); //根据物品型号查询物品库存 int goodsNum = this.lWhGoodsService.queryGoodsModelNum(0, warehouseId, scrappedGoods.getBaseGoodsModelsId(), (short) 1, null); if (params.getCounts() > goodsNum) { log.error("报废数量大于库存数量"); return 0; } scrappedGoods.setCounts(params.getCounts()); //报废原因 scrappedGoods.setScrappedCode(params.getScrappedCode()); scrappedGoods.setScrappedName(scrappedNameMap.get(Long.valueOf(params.getScrappedCode()))); scrappedGoods.setFormScrappedId(lWhFormScrappedId); //物品模板 scrappedGoods.setBaseGoodsTemplateId(goodsTemplate.getId()); scrappedGoods.setGoodsTemplateName(goodsTemplate.getGoodsName()); //规格型号 Long baseGoodsModelsId = params.getBaseGoodsModelsId(); BaseGoodsModels baseGoodsModels = modelMap.get(baseGoodsModelsId); if (baseGoodsModels != null) { scrappedGoods.setBaseGoodsModelsId(baseGoodsModelsId); scrappedGoods.setBaseGoodsModelsName(baseGoodsModels.getModelName()); scrappedGoods.setUnit(baseGoodsModels.getUnit()); } scrappedGoodsList.add(scrappedGoods); } int flag2 = this.scrappedGoodsService.insert(scrappedGoodsList); if (flag2 != scrappedGoodsList.size()) { log.error("新增报废单物品记录 失败"); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return 0; } //3.根据报废单报废物品 Long lWarehouseFlowId = this.lWhFormScrappedCoreService.outFormByTransId(lWhFormScrappedId, currentUser, param.getDealTime()); if (lWarehouseFlowId == null) { log.error("根据出库单出库 失败"); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return 0; } return 1; } /** * @Description 列表查询 * @Author 卢庆阳 * @Date 2023/11/2 */ public GenericPager queryList(LWhFormScrappedParam param, FinSysTenantUser sysInfo) { HashMap paramts = new HashMap<>(); StringBuilder sql = new StringBuilder(QUERY_LIST); //报废单号 if (!StringUtils.isEmpty(param.getBusinessFormCode())) { sql.append(" and BUSINESS_FORM_CODE like :businessFormCode "); paramts.put("businessFormCode", StringUtils.CHAR_PERCENT + param.getBusinessFormCode() + StringUtils.CHAR_PERCENT); } //物品名称 if (!StringUtils.isEmpty(param.getGoodsTemplateName())) { sql.append(" AND id IN (SELECT BUSINESS_ID FROM L_WH_PROCURE_MODEL procureModel LEFT JOIN BASE_GOODS_MODELS baseModel ON procureModel.BASE_GOODS_MODELS_ID=baseModel.ID LEFT JOIN BASE_GOODS_TEMPLATE baseTemp ON baseModel.GOODS_TEMPLATES_ID=baseTemp.id WHERE procureModel.BUSINESS_TYPE=1 AND baseTemp.GOODS_NAME LIKE :goodsTemplateName)"); paramts.put("goodsTemplateName", StringUtils.CHAR_PERCENT + param.getGoodsTemplateName() + StringUtils.CHAR_PERCENT); } //机构 sql.append(" and AGENCY_ID like :agencyId "); if (param.getAgencyId() != null){ paramts.put("agencyId", param.getAgencyId() + StringUtils.CHAR_PERCENT); } else { paramts.put("agencyId", sysInfo.getTenantId() + StringUtils.CHAR_PERCENT); } //报废时间 if (param.getDealTimeStart() != null) { sql.append(" and DEAL_TIME >=:dealTimeStart "); paramts.put("dealTimeStart", param.getDealTimeStart() * 1000000); } if (param.getDealTimeEnd() != null) { sql.append(" and DEAL_TIME <:dealTimeEnd "); paramts.put("dealTimeEnd", param.getDealTimeEnd() * 1000000 + 240000); } sql.append(" ORDER BY DEAL_TIME DESC"); GenericPager genericPager = selectSplit(sql.toString(), paramts, new LWhFormScrapped()); return genericPager; } /** * @Description 根据id查询详情 * @Author 卢庆阳 * @Date 2023/11/2 */ public LWhFormScrappedExtend getById(Long id) { LWhFormScrappedExtend scrappedExtend = new LWhFormScrappedExtend(); //1.查询报废单 LWhFormScrapped lWhFormScrapped = this.get(new LWhFormScrapped(id)); if (lWhFormScrapped != null) { BeanUtils.copyProperties(lWhFormScrapped,scrappedExtend); } //2.查询报废单 List scrappedGoodsList = this.scrappedGoodsService.getByFormScrappedId(id); if (!CollectionUtils.isEmpty(scrappedGoodsList)) { scrappedExtend.setScrappedGoodsList(scrappedGoodsList); } return scrappedExtend; } /** * 报废明细 * @param param * @return */ public GenericPager> queryDetailList(LWhFormScrappedParam param, FinSysTenantUser sysInfo) { StringBuilder sql = new StringBuilder(QUERY_BF_DETAIL_LIST); HashMap paramts = new HashMap<>(); //报废单号 if (!StringUtils.isEmpty(param.getBusinessFormCode())) { sql.append(" and BUSINESS_FORM_CODE =:BUSINESS_FORM_CODE "); paramts.put("BUSINESS_FORM_CODE", StringUtils.CHAR_PERCENT + param.getBusinessFormCode() + StringUtils.CHAR_PERCENT); } //物品名称 if (!StringUtils.isEmpty(param.getGoodsTemplateName())) { sql.append(" and GOODS_TEMPLATE_NAME =:GOODS_TEMPLATE_NAME "); paramts.put("GOODS_TEMPLATE_NAME", StringUtils.CHAR_PERCENT + param.getGoodsTemplateName() + StringUtils.CHAR_PERCENT); } //规格型号 if (param.getBaseGoodsModelsId() != null) { sql.append(" and BASE_GOODS_MODELS_ID =:BASE_GOODS_MODELS_ID "); paramts.put("BASE_GOODS_MODELS_ID", param.getBaseGoodsModelsId()); } //机构 sql.append(" and AGENCY_ID like:AGENCY_ID "); if (param.getAgencyId() != null) { paramts.put("AGENCY_ID", param.getAgencyId() + StringUtils.CHAR_PERCENT); } else { paramts.put("AGENCY_ID", sysInfo.getTenantId() + StringUtils.CHAR_PERCENT); } //创建人 if (!StringUtils.isEmpty(param.getOperatorName())) { sql.append(" and OPERATOR_NAME =:OPERATOR_NAME "); paramts.put("OPERATOR_NAME", StringUtils.CHAR_PERCENT + param.getOperatorName() + StringUtils.CHAR_PERCENT); } //结束时间 if (param.getDealTimeStart() != null) { sql.append(" and DEAL_TIME >=:dealTimeStart "); paramts.put("dealTimeStart", param.getDealTimeStart() * 1000000); } if (param.getDealTimeEnd() != null) { sql.append(" and DEAL_TIME <:dealTimeEnd "); paramts.put("dealTimeEnd", param.getDealTimeEnd() * 1000000 + 240000); } sql.append(" ORDER BY DEAL_TIME DESC "); return selectSplit(sql.toString(), paramts, param.getPageNum(), param.getPageSize(), new MapRowMapper()); } /** * @Description 导出报废登记单 * @Author 卢庆阳 * @Date 2023/11/2 */ public LWhFormScrappedExtend export(Long id, FinSysTenantUser sysInfo) { LWhFormScrappedExtend scrappedExtend = new LWhFormScrappedExtend(); //1.查询报废单 LWhFormScrapped lWhFormScrapped = this.get(new LWhFormScrapped(id)); if (lWhFormScrapped != null) { lWhFormScrapped.setDealTime(DateUtils.getDateTimeNumber(System.currentTimeMillis())); lWhFormScrapped.setOperatorName(sysInfo.getUserName()); BeanUtils.copyProperties(lWhFormScrapped,scrappedExtend); } //2.查询报废单物品 List scrappedGoodsList = this.scrappedGoodsService.getByFormScrappedId(id); if (!CollectionUtils.isEmpty(scrappedGoodsList)) { scrappedExtend.setScrappedGoodsList(scrappedGoodsList); } return scrappedExtend; } }