package doumeemes.service.ext.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import doumeemes.core.constants.ResponseStatus; import doumeemes.core.exception.BusinessException; import doumeemes.core.model.LoginUserInfo; import doumeemes.core.model.PageData; import doumeemes.core.model.PageWrap; import doumeemes.core.utils.Constants; import doumeemes.core.utils.DateUtil; import doumeemes.core.utils.excel.EasyExcelUtil; import doumeemes.core.utils.redis.RedisUtil; import doumeemes.dao.business.model.*; import doumeemes.dao.business.vo.WorkPlansVO; import doumeemes.dao.ext.*; import doumeemes.dao.ext.dto.*; import doumeemes.dao.ext.vo.*; import doumeemes.service.ext.DepartmentExtService; import doumeemes.service.ext.PlansExtService; import doumeemes.service.ext.ProceduresExtService; import doumeemes.service.ext.WorkPlansExtService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; 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.Date; import java.util.List; import java.util.Objects; /** * Service实现 * @author 江蹄蹄 * @since 2023/07/27 13:53 */ @Service public class WorkPlansExtServiceImpl implements WorkPlansExtService { @Autowired private RedisTemplate redisTemplate; @Autowired private WorkPlansExtMapper workPlansExtMapper; @Autowired private PlansExtService plansExtService; @Autowired private PlanImportExtMapper planImportExtMapper; @Autowired private PlanHistoryExtMapper planHistoryExtMapper; @Autowired private MaterialDistributeExtMapper materialDistributeExtMapper; @Autowired private BomExtMapper bomExtMapper; @Autowired private RouteExtMapper routeExtMapper; @Autowired private ProceduresExtService proceduresExtService; @Autowired private DepartmentExtService departmentExtService; @Override public PageData findPage(PageWrap pageWrap) { PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity()); List result = workPlansExtMapper.selectList(pageWrap.getModel()); return PageData.from(new PageInfo<>(result)); } /****************************************************************************************************************************************************************************/ @Override public Integer saveOrUpdate(WorkPlansSaveDTO workPlansSaveDTO) { LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); } if(Objects.isNull(workPlansSaveDTO) || StringUtils.isBlank(workPlansSaveDTO.getBatch()) || Objects.isNull(workPlansSaveDTO.getPlanDate()) || Objects.isNull(workPlansSaveDTO.getNum()) || Objects.isNull(workPlansSaveDTO.getFactoryId()) || Objects.isNull(workPlansSaveDTO.getUnitId()) || Objects.isNull(workPlansSaveDTO.getMaterialId())){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } WorkPlans workPlans = new WorkPlans(); if(!Objects.isNull(workPlansSaveDTO.getWorkPlansId())){ workPlans = workPlansExtMapper.selectById(workPlansSaveDTO.getWorkPlansId()); if(Objects.isNull(workPlans)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } if (!workPlans.getStatus().equals(Constants.WORKORDER_HISTORY_STATUS.create)){ throw new BusinessException(ResponseStatus.ERR_STATUS); } BeanUtils.copyProperties(workPlansSaveDTO,workPlans); workPlans.setUpdateTime(DateUtil.getCurrentDate()); workPlans.setUpdateUser(user.getId()); workPlansExtMapper.updateById(workPlans); }else{ BeanUtils.copyProperties(workPlansSaveDTO,workPlans); workPlans.setCreateTime(DateUtil.getCurrentDate()); workPlans.setCreateUser(user.getId()); workPlans.setDeleted(Constants.ZERO); workPlans.setDepartId(user.getCurComDepartment().getId()); workPlans.setRootDepartId(user.getRootDepartment().getId()); workPlans.setUserId(user.getId()); workPlans.setOrigin(Constants.PLAS_ORIGIN.user); workPlans.setStatus(Constants.PLAN_STATUS.create); workPlans.setPlanCode(this.getNextCode(user.getCompany().getId())); workPlansExtMapper.insert(workPlans); } return workPlans.getId(); } @Override public PageData findWorkPlansVOPage(PageWrap pageWrap) { LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); } //只能查看当前根组织的数据 pageWrap.getModel().setRootDepartId(user.getRootDepartment().getId()); // if(!Constants.equalsInteger(user.getCurComDepartment().getId(),user.getRootDepartment().getId())){ //如果当前选择的公司级组织非根组织信息,只能查看当前选择公司级组织数据 pageWrap.getModel().setDepartId(user.getCurComDepartment().getId()); // } //数据权限 List dataPermission = user.getDepartPermissionList(); if(dataPermission!=null){ //只能看自己的 // pageWrap.getModel().setCreateUser(user.getId()); // //否则走数据权限 // pageWrap.getModel().setDepartIds(dataPermission); /* if(dataPermission.size() == 0){ //只能看自己的 pageWrap.getModel().setCreateUser(user.getId()); }else{ }*/ } // if(user.getProcedureIds()!=null){ // pageWrap.getModel().setProcedureIds(user.getProcedureIds()); // }else{ // // pageWrap.getModel().setUserId(user.getId()); // pageWrap.getModel().setCreateUser(user.getId()); // } PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity()); List result = workPlansExtMapper.getWorkPlansVOPage(pageWrap.getModel()); if(!Objects.isNull(result)&&result.size()>Constants.ZERO){ result.forEach(i->{ List plansExtListVOList = plansExtService.getListByWorkPlan(i.getId()); if(!Objects.isNull(plansExtListVOList)&&plansExtListVOList.size()>Constants.ZERO){ PlansExtListVO plansExtListVO = plansExtListVOList.get(plansExtListVOList.size()-Constants.ONE); i.setFinishQualifiedNum(Objects.isNull(plansExtListVO.getQulifiedNum())?Constants.ZERO:plansExtListVO.getQulifiedNum()); i.setFinishUnQualifiedNum(Objects.isNull(plansExtListVO.getUnqulifiedNum())?Constants.ZERO:plansExtListVO.getUnqulifiedNum()); i.setFinishNum(Objects.isNull(plansExtListVO.getDoneNum())?Constants.ZERO:plansExtListVO.getDoneNum()); } }); } return PageData.from(new PageInfo<>(result)); } @Transactional(rollbackFor = {BusinessException.class,Exception.class}) @Override public Integer deleteById(Integer id) { LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); WorkPlans workPlans = workPlansExtMapper.selectById(id); if(Objects.isNull(workPlans)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } if(!workPlans.getStatus().equals(Constants.WORKPLANHISTORY_TYPE.create)){ throw new BusinessException(ResponseStatus.ERR_STATUS); } workPlans.setDeleted(Constants.ONE); workPlans.setUpdateUser(user.getId()); workPlans.setUpdateTime(new Date()); return workPlansExtMapper.updateById(workPlans); } @Transactional(rollbackFor = {BusinessException.class,Exception.class}) @Override public void paused(Integer id){ LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); } WorkPlans workPlans = workPlansExtMapper.selectById(id); if(Objects.isNull(workPlans)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } //已暂停的计划不需重复操作 if(Constants.equalsInteger(workPlans.getPaused(),Constants.ONE) ){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,该计划已暂停,无需重复操作!"); } workPlans.setUpdateTime(DateUtil.getCurrentDate()); workPlans.setUpdateUser(user.getId()); workPlans.setPaused(Constants.ONE); workPlansExtMapper.updateById(workPlans); //历史数据,关闭 planHistoryExtMapper.insert(initHistoryByModel(workPlans,user.getId(),Constants.PLANHISTORY_TYPE.pause)); List plansList = plansExtService.getPlansList(new QueryWrapper() .in("STATUS",0,1,4) .eq("PAUSED",Constants.ZERO) .eq("DELETED",Constants.ZERO) .eq("WORK_PLANS_ID",workPlans.getId())); plansList.forEach(i->{ plansExtService.pauseByIdForStandard(i,user); }); } @Transactional(rollbackFor = {BusinessException.class,Exception.class}) @Override public void regain(Integer id){ LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); } WorkPlans workPlans = workPlansExtMapper.selectById(id); if(Objects.isNull(workPlans)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } //已暂停的计划不需重复操作 if(Constants.equalsInteger(workPlans.getPaused(),Constants.ZERO) ){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,该计划已恢复,无需重复操作!"); } workPlans.setUpdateTime(DateUtil.getCurrentDate()); workPlans.setUpdateUser(user.getId()); workPlans.setPaused(Constants.ZERO); workPlansExtMapper.updateById(workPlans); //历史数据,关闭 planHistoryExtMapper.insert(initHistoryByModel(workPlans,user.getId(),Constants.PLANHISTORY_TYPE.pause)); List plansList = plansExtService.getPlansList(new QueryWrapper() .in("STATUS",0,1,4) .eq("PAUSED",Constants.ONE) .eq("DELETED",Constants.ZERO) .eq("WORK_PLANS_ID",workPlans.getId())); plansList.forEach(i->{ plansExtService.regainByIdForStandard(i,user); }); } @Transactional(rollbackFor = {BusinessException.class,Exception.class}) @Override public void close(Integer id){ LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); } WorkPlans workPlans = workPlansExtMapper.selectById(id); if(Objects.isNull(workPlans)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } if(Constants.equalsInteger(workPlans.getStatus(),Constants.PLAN_STATUS.close) ){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,该计划已关闭,无需重复操作!"); } workPlans.setUpdateTime(DateUtil.getCurrentDate()); workPlans.setUpdateUser(user.getId()); workPlans.setStatus(Constants.PLAN_STATUS.close); workPlansExtMapper.updateById(workPlans); List plansList = plansExtService.getPlansList(new QueryWrapper() .in("STATUS",0,1,4) .eq("DELETED",Constants.ZERO) .eq("WORK_PLANS_ID",workPlans.getId())); plansList.forEach(i->{ i.setUpdateTime(DateUtil.getCurrentDate()); i.setUpdateUser(user.getId()); i.setStatus(Constants.PLAN_STATUS.close); plansExtService.updateForPlan(i); }); } public static PlanHistory initHistoryByModel(WorkPlans p, Integer userId, int status ) { PlanHistory h = new PlanHistory(); h.setDeleted(Constants.ZERO); h.setCreateTime(DateUtil.getCurrentDate()); h.setCreateUser(userId); h.setDepartId(p.getDepartId()); h.setRootDepartId(p.getRootDepartId()); h.setPlanId(p.getId()); h.setRootDepartId(p.getRootDepartId()); h.setDepartId(p.getDepartId()); h.setTitle(Constants.WORKPLANHISTORY_TYPE.getTitleByStatus(p,status)); h.setType(status); h.setInfo(Constants.WORKPLANHISTORY_TYPE.getInfoByStatus(p,status)); h.setType(status); return h; } @Transactional(rollbackFor = {BusinessException.class,Exception.class}) @Override public void importPlans(MultipartFile file) { LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); } //解析excel List plansList = EasyExcelUtil.importExcel(file, 1, 1, WorkPlans.class); if(plansList == null || plansList.size()==0){ throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容有误!"); } PlanImport ip = new PlanImport(); ip.setDeleted(Constants.ZERO); ip.setCreateTime(DateUtil.getCurrentDate()); ip.setCreateUser(user.getId()); ip.setFileName(file.getOriginalFilename()); ip.setRootDepartId(user.getRootDepartment().getId()); ip.setDepartId(user.getComDepartment().getId()); //计划文件上传记录 planImportExtMapper.insert(ip); int index = 1; List historyList = new ArrayList<>(); for(WorkPlans p : plansList){ p.setCreateTime(DateUtil.getCurrentDate()); p.setCreateUser(user.getId()); p.setDeleted(Constants.ZERO); p.setDepartId(user.getCurComDepartment().getId()); p.setRootDepartId(ip.getRootDepartId()); p.setImportId(ip.getId()); p.setStatus(Constants.PLAN_STATUS.create); p.setOrigin(Constants.PLAS_ORIGIN.imports); p.setUserId(user.getId()); p.setPlanCode(this.getNextCode(user.getCompany().getId())); // p.setFactoryId(user.getDepartment().getId()); //检查数据有效性 checkData(p,index,user); workPlansExtMapper.insert(p); historyList.add(initHistoryByModel(p,user.getId(),Constants.WORKPLANHISTORY_TYPE.create)); index++; } //批量导入计划数据 planHistoryExtMapper.insertBatch(historyList); } /** * 数据有效性检查 * @param p * @param index * @param user * @throws BusinessException */ private void checkData(WorkPlans p, int index,LoginUserInfo user) throws BusinessException{ if(Constants.formatIntegerNum(p.getNum())<=0){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【计划生产数量】数据错误!"); } if(p.getPlanDate() == null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【计划日期】数据错误,正确格式为:yyyy-MM-dd(如2022-06-07)!"); } if(StringUtils.isBlank(p.getMaterialCode())){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品编码】数据错误!"); } if(p.getFactoryName() == null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【生产工厂】数据错误!"); } if(p.getBatch() == null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【生产批次】数据错误!"); } Date pDate = DateUtil.getDateFromString(DateUtil.getShortTime(p.getPlanDate()) +" 00:00:00"); Date nDate = DateUtil.getDateFromString(DateUtil.getShortTime(DateUtil.getCurrentDate()) +" 00:00:00"); if( nDate.getTime() > pDate.getTime()){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,计划生产日期必须为今天以后的日期!"); } p.setMaterialCode(p.getMaterialCode().trim()); p.setFactoryName(p.getFactoryName().trim()); p.setBatch(p.getBatch().trim()); QueryMaterialDistributeExtDTO d = new QueryMaterialDistributeExtDTO(); d.setDeleted(Constants.ZERO); d.setMmodelCode(p.getMaterialCode()); d.setRootDepartId(p.getRootDepartId()); d.setDepartId(p.getDepartId()); //查询产品信息 MaterialDistributeExtListVO mm = materialDistributeExtMapper.selectByModel(d); if(mm == null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品编码】数据不存在!"); } if(Constants.equalsInteger(mm.getStatus(),Constants.ZERO)){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品】已失效,无法进行生产!"); } //存储物料分配表主键 p.setMaterialId(mm.getId()); //单位编码 p.setUnitId(mm.getUnitId()); if(Constants.equalsInteger(mm.getStatus(),Constants.ZERO)){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品编码】不能安排生产计划!"); } Bom bom = new Bom(); bom.setDeleted(Constants.ZERO); bom.setMaterialId(mm.getId()); bom.setRootDepartId(mm.getRootDepartId()); bom = bomExtMapper.selectOne(new QueryWrapper<>(bom).last(" limit 1")); if(bom == null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品】BOM未配置数据!"); } if(!Constants.equalsInteger(bom.getStatus(),Constants.ONE)){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品】BOM已失效,无法进行生产!"); } DepartmentExtListVO f = departmentExtService.getModelByName(user.getCompany().getId(),p.getFactoryName()); if(f==null || !Constants.equalsInteger(f.getType(),Constants.DEPART_TYPE.factory) ){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【生产工厂】数据非工厂组织,请检查!"); } if(Constants.equalsInteger(f.getStatus(),Constants.ONE) ){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【生产工厂】当前状态不正确!"); } if(!Constants.equalsInteger(p.getDepartId(),departmentExtService.getComDepartId(f))){ //如果工厂与公司级组织不对应 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【生产工厂】数据非当前公司组织,请检查!"); } p.setFactoryId(f.getId()); } @Override public WorkPlansVO getWorkPlansDetail(Integer id){ if(Objects.isNull(id)){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } QueryWorkPlansDTO dto = new QueryWorkPlansDTO(); dto.setWorkPlanId(id); WorkPlansVO workPlansVO = workPlansExtMapper.getWorkPlansVODetail(dto); if(Objects.isNull(workPlansVO)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } List plansExtListVOList = plansExtService.getListByWorkPlan(id); if(!Objects.isNull(plansExtListVOList)&&plansExtListVOList.size()>Constants.ZERO){ workPlansVO.setPlansExtListVOList(plansExtListVOList); PlansExtListVO plansExtListVO = plansExtListVOList.get(plansExtListVOList.size()-Constants.ONE); workPlansVO.setFinishQualifiedNum(Objects.isNull(plansExtListVO.getQulifiedNum())?Constants.ZERO:plansExtListVO.getQulifiedNum()); workPlansVO.setFinishUnQualifiedNum(Objects.isNull(plansExtListVO.getUnqulifiedNum())?Constants.ZERO:plansExtListVO.getUnqulifiedNum()); workPlansVO.setFinishNum(Objects.isNull(plansExtListVO.getDoneNum())?Constants.ZERO:plansExtListVO.getDoneNum()); } return workPlansVO; } public synchronized String getNextCode(Integer comId ){ String prefix = "WP-" + DateUtil.getDate(new Date(),"yyyyMMdd") +"-"; Integer countNum = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_WORK_PLAN_KEY+comId,Integer.class); countNum = Constants.formatIntegerNum(countNum)+1; //更新缓存 RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_WORK_PLAN_KEY+comId,countNum); String nextIndex =Integer.toString( countNum); return prefix + StringUtils.leftPad(nextIndex,4,"0"); } @Transactional(rollbackFor = {BusinessException.class,Exception.class}) @Override public void release(Integer id){ LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); } WorkPlans workPlans = workPlansExtMapper.selectById(id); if(Objects.isNull(workPlans)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } if(!Constants.equalsInteger(workPlans.getStatus(),Constants.ZERO) ){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,计划状态已流转,无法进行发布!"); } workPlans.setUpdateTime(DateUtil.getCurrentDate()); workPlans.setUpdateUser(user.getId()); workPlans.setPublishDate(new Date()); workPlans.setStatus(Constants.ONE); workPlansExtMapper.updateById(workPlans); planHistoryExtMapper.insert(initHistoryByModel(workPlans,user.getId(),Constants.WORKPLANHISTORY_TYPE.publish)); //查询物料BOM工序信息 Bom bom = bomExtMapper.selectOne(new QueryWrapper().eq("MATERIAL_ID",workPlans.getMaterialId()).eq("DELETED",Constants.ZERO).last(" limit 1 ")); if(Objects.isNull(bom)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,未查询到BOM信息,无法进行发布!"); } List list= proceduresExtService.getGYListByCondition(bom.getRouteId()); if(Objects.isNull(list)||list.size()==Constants.ZERO){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,未查询到BOM工序数据信息!"); } list.forEach(i->{ Plans plans = new Plans(); BeanUtils.copyProperties(workPlans,plans); plans.setId(null); plans.setCreateTime(new Date()); plans.setCreateUser(user.getId()); plans.setOrigin(Constants.TWO); plans.setProcedureId(i.getId()); plans.setUnitId(workPlans.getUnitId()); plans.setStatus(Constants.PLAN_STATUS.publish); plans.setPublishDate(new Date()); plans.setWorkPlansId(workPlans.getId()); plansExtService.insertForWorkPlan(plans); }); } }