| package doumeemes.service.business.impl; | 
|   | 
| import com.alibaba.fastjson.JSON; | 
| import com.alibaba.fastjson.JSONObject; | 
| import doumeemes.core.constants.ResponseStatus; | 
| import doumeemes.core.exception.BusinessException; | 
| import doumeemes.core.model.ApiResponse; | 
| import doumeemes.core.model.LoginUserInfo; | 
| import doumeemes.core.model.PageData; | 
| import doumeemes.core.model.PageWrap; | 
| import doumeemes.core.utils.Constants; | 
| import doumeemes.core.utils.Utils; | 
| import doumeemes.dao.business.BomDetailMapper; | 
| import doumeemes.dao.business.BomMapper; | 
| import doumeemes.dao.business.BomVersionMapper; | 
| import doumeemes.dao.business.MaterialMapper; | 
| import doumeemes.dao.business.model.Bom; | 
| import doumeemes.dao.business.model.BomDetail; | 
| import doumeemes.dao.business.model.BomVersion; | 
| import doumeemes.dao.business.model.Material; | 
| import doumeemes.dao.ext.BomExtMapper; | 
| import doumeemes.dao.ext.MaterialDistributeExtMapper; | 
| import doumeemes.dao.ext.bean.BomBean; | 
| import doumeemes.dao.ext.bean.BomDetailBean; | 
| import doumeemes.dao.ext.vo.BomExtListVO; | 
| import doumeemes.service.business.BomService; | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
| import com.baomidou.mybatisplus.core.metadata.IPage; | 
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
| import io.swagger.annotations.Api; | 
| import org.apache.ibatis.annotations.Param; | 
| import org.apache.ibatis.annotations.Select; | 
| import org.apache.xpath.operations.Bool; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
| import org.springframework.util.CollectionUtils; | 
|   | 
| import java.math.BigDecimal; | 
| import java.util.Date; | 
| import java.util.List; | 
| import java.util.Objects; | 
|   | 
| /** | 
|  * 物料清单信息表Service实现 | 
|  * @author 江蹄蹄 | 
|  * @date 2022/04/20 09:33 | 
|  */ | 
| @Service | 
| public class BomServiceImpl implements BomService { | 
|   | 
|     @Autowired | 
|     private BomMapper bomMapper; | 
|   | 
|     @Autowired | 
|     private BomDetailMapper bomDetailMapper; | 
|   | 
|     @Autowired | 
|     private BomVersionMapper bomVersionMapper; | 
|   | 
|     @Autowired | 
|     private MaterialMapper materialMapper; | 
|   | 
|     @Autowired | 
|     private MaterialDistributeExtMapper materialDistributeExtMapper; | 
|   | 
|     @Autowired | 
|     private BomExtMapper bomExtMapper; | 
|   | 
|     @Override | 
|     public Integer create(Bom bom) { | 
|         bomMapper.insert(bom); | 
|         return bom.getId(); | 
|     } | 
|   | 
|     @Override | 
|     public void deleteById(Integer id) { | 
|         bomMapper.deleteById(id); | 
|     } | 
|   | 
|     @Override | 
|     public void delete(Bom bom) { | 
|         UpdateWrapper<Bom> deleteWrapper = new UpdateWrapper<>(bom); | 
|         bomMapper.delete(deleteWrapper); | 
|     } | 
|   | 
|     @Override | 
|     public void deleteByIdInBatch(List<Integer> ids) { | 
|         if (CollectionUtils.isEmpty(ids)) { | 
|             return; | 
|         } | 
|         bomMapper.deleteBatchIds(ids); | 
|     } | 
|   | 
|     @Override | 
|     public void updateById(Bom bom) { | 
|         bomMapper.updateById(bom); | 
|     } | 
|   | 
|     @Override | 
|     public void updateByIdInBatch(List<Bom> boms) { | 
|         if (CollectionUtils.isEmpty(boms)) { | 
|             return; | 
|         } | 
|         for (Bom bom: boms) { | 
|             this.updateById(bom); | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     public Bom findById(Integer id) { | 
|         return bomMapper.selectById(id); | 
|     } | 
|   | 
|     @Override | 
|     public Bom findOne(Bom bom) { | 
|         QueryWrapper<Bom> wrapper = new QueryWrapper<>(bom); | 
|         return bomMapper.selectOne(wrapper); | 
|     } | 
|   | 
|     @Override | 
|     public List<Bom> findList(Bom bom) { | 
|         QueryWrapper<Bom> wrapper = new QueryWrapper<>(bom); | 
|         return bomMapper.selectList(wrapper); | 
|     } | 
|    | 
|     @Override | 
|     public PageData<Bom> findPage(PageWrap<Bom> pageWrap) { | 
|         IPage<Bom> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         QueryWrapper<Bom> queryWrapper = new QueryWrapper<>(); | 
|         Utils.MP.blankToNull(pageWrap.getModel()); | 
|         if (pageWrap.getModel().getId() != null) { | 
|             queryWrapper.lambda().eq(Bom::getId, pageWrap.getModel().getId()); | 
|         } | 
|         if (pageWrap.getModel().getDeleted() != null) { | 
|             queryWrapper.lambda().eq(Bom::getDeleted, pageWrap.getModel().getDeleted()); | 
|         } | 
|         if (pageWrap.getModel().getCreateUser() != null) { | 
|             queryWrapper.lambda().eq(Bom::getCreateUser, pageWrap.getModel().getCreateUser()); | 
|         } | 
|         if (pageWrap.getModel().getCreateTime() != null) { | 
|             queryWrapper.lambda().ge(Bom::getCreateTime, Utils.Date.getStart(pageWrap.getModel().getCreateTime())); | 
|             queryWrapper.lambda().le(Bom::getCreateTime, Utils.Date.getEnd(pageWrap.getModel().getCreateTime())); | 
|         } | 
|         if (pageWrap.getModel().getUpdateUser() != null) { | 
|             queryWrapper.lambda().eq(Bom::getUpdateUser, pageWrap.getModel().getUpdateUser()); | 
|         } | 
|         if (pageWrap.getModel().getUpdateTime() != null) { | 
|             queryWrapper.lambda().ge(Bom::getUpdateTime, Utils.Date.getStart(pageWrap.getModel().getUpdateTime())); | 
|             queryWrapper.lambda().le(Bom::getUpdateTime, Utils.Date.getEnd(pageWrap.getModel().getUpdateTime())); | 
|         } | 
|         if (pageWrap.getModel().getRemark() != null) { | 
|             queryWrapper.lambda().eq(Bom::getRemark, pageWrap.getModel().getRemark()); | 
|         } | 
|         if (pageWrap.getModel().getRootDepartId() != null) { | 
|             queryWrapper.lambda().eq(Bom::getRootDepartId, pageWrap.getModel().getRootDepartId()); | 
|         } | 
|         if (pageWrap.getModel().getDepartId() != null) { | 
|             queryWrapper.lambda().eq(Bom::getDepartId, pageWrap.getModel().getDepartId()); | 
|         } | 
|         if (pageWrap.getModel().getMaterialId() != null) { | 
|             queryWrapper.lambda().eq(Bom::getMaterialId, pageWrap.getModel().getMaterialId()); | 
|         } | 
|         if (pageWrap.getModel().getVersion() != null) { | 
|             queryWrapper.lambda().eq(Bom::getVersion, pageWrap.getModel().getVersion()); | 
|         } | 
|         if (pageWrap.getModel().getUnitId() != null) { | 
|             queryWrapper.lambda().eq(Bom::getUnitId, pageWrap.getModel().getUnitId()); | 
|         } | 
|         if (pageWrap.getModel().getRouteId() != null) { | 
|             queryWrapper.lambda().eq(Bom::getRouteId, pageWrap.getModel().getRouteId()); | 
|         } | 
|         if (pageWrap.getModel().getStatus() != null) { | 
|             queryWrapper.lambda().eq(Bom::getStatus, pageWrap.getModel().getStatus()); | 
|         } | 
|         if (pageWrap.getModel().getValidTime() != null) { | 
|             queryWrapper.lambda().ge(Bom::getValidTime, Utils.Date.getStart(pageWrap.getModel().getValidTime())); | 
|             queryWrapper.lambda().le(Bom::getValidTime, Utils.Date.getEnd(pageWrap.getModel().getValidTime())); | 
|         } | 
|         if (pageWrap.getModel().getInvalidTime() != null) { | 
|             queryWrapper.lambda().ge(Bom::getInvalidTime, Utils.Date.getStart(pageWrap.getModel().getInvalidTime())); | 
|             queryWrapper.lambda().le(Bom::getInvalidTime, Utils.Date.getEnd(pageWrap.getModel().getInvalidTime())); | 
|         } | 
|         for(PageWrap.SortData sortData: pageWrap.getSorts()) { | 
|             if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { | 
|                 queryWrapper.orderByDesc(sortData.getProperty()); | 
|             } else { | 
|                 queryWrapper.orderByAsc(sortData.getProperty()); | 
|             } | 
|         } | 
|         return PageData.from(bomMapper.selectPage(page, queryWrapper)); | 
|     } | 
|   | 
|     @Override | 
|     public long count(Bom bom) { | 
|         QueryWrapper<Bom> wrapper = new QueryWrapper<>(bom); | 
|         return bomMapper.selectCount(wrapper); | 
|     } | 
|   | 
|   | 
|     @Override | 
|     @Transactional | 
|     public ApiResponse saveBean(BomBean bomBean,LoginUserInfo loginUserInfo){ | 
|         Bom bom = new Bom(); | 
|         BigDecimal bomVersionNum = BigDecimal.ONE.setScale(Constants.TWO); | 
|         if(Objects.isNull(materialDistributeExtMapper.selectById(bomBean.getMaterialId()))){ | 
|             throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "未查询到物料分配信息"); | 
|         }; | 
|         if(null == bomBean.getId() || bomBean.getId() == Constants.ONE){ | 
|             //查询物料是否已创建bom | 
| //            if(bomExtMapper.selectCount(new QueryWrapper<Bom>() | 
| //                    .eq("material_id",bomBean.getMaterialId()))>0){ | 
| //                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "当前物料已存在BOM信息,无法再次建立"); | 
| //            }; | 
|             if(bomExtMapper.checkIsHave(bomBean.getMaterialId(),bomBean.getRouteId())>0){ | 
|                 throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "当前物料于当前工厂下已存在BOM信息,无法再次建立"); | 
|             } | 
|             bom = bomBean.toBom(bom); | 
|             bom.setVersion(bomVersionNum.toString()); | 
|             bom.setCreateTime(new Date()); | 
|             bom.setCreateUser(loginUserInfo.getId()); | 
|             bom.setRootDepartId(loginUserInfo.getRootDepartment().getId()); | 
|             bom.setDepartId(loginUserInfo.getCurComDepartment().getId()); | 
|             bomMapper.insert(bom); | 
|         }else{ | 
|             Bom dbBom = bomMapper.selectById(bomBean.getId()); | 
|             if(2 == dbBom.getStatus()){ | 
|                 throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "已禁用失效,无法编辑保存"); | 
|             } | 
|             if(0 != dbBom.getStatus()){ | 
|                 bomVersionNum = new BigDecimal(dbBom.getVersion()).add(BigDecimal.valueOf(0.1)).setScale(2); | 
|             }else{ | 
|                 BomVersion dbBomVersion = bomVersionMapper.selectOne(new QueryWrapper<BomVersion>().eq("BOM_ID",bomBean.getId())); | 
|                 bomDetailMapper.delete(new QueryWrapper<BomDetail>().eq("BOM_ID",dbBomVersion.getId())); | 
|                 bomVersionMapper.deleteById(dbBomVersion.getId()); | 
|             } | 
|             bom = bomBean.toBom(dbBom); | 
|             bom.setVersion(bomVersionNum.toString()); | 
|             bom.setUpdateTime(new Date()); | 
|             bom.setUpdateUser(loginUserInfo.getId()); | 
|             bomMapper.updateById(bom); | 
|         } | 
|         BomVersion bomVersion = JSON.parseObject(JSONObject.toJSON(bomBean).toString(), BomVersion.class); | 
|         bomVersion.setId(null); | 
|         bomVersion.setBomId(bom.getId()); | 
|         bomVersion.setVersion(bomVersionNum.toString()); | 
|         bomVersion.setRootDepartId(loginUserInfo.getRootDepartment().getId()); | 
|         bomVersion.setDepartId(bom.getDepartId()); | 
|         bomVersionMapper.insert(bomVersion); | 
|         //失效历史版本数据 | 
|         BomVersion updBomVersion = new BomVersion(); | 
|         updBomVersion.setStatus(2); | 
|         updBomVersion.setValidTime(new Date()); | 
|         bomVersionMapper.update(updBomVersion,new UpdateWrapper<BomVersion>() | 
|                 .eq("BOM_ID",bomVersion.getBomId()) | 
|                 .ne("id",bomVersion.getId()) | 
|         ); | 
|         saveBomDetail(bomBean.toBomDetail(),bomVersion.getId()); | 
|         return ApiResponse.success(bom); | 
|     } | 
|   | 
|   | 
|     @Override | 
|     @Transactional | 
|     public ApiResponse openOrClose(Integer id,Integer optType){ | 
|         Bom bom = bomMapper.selectById(id); | 
|         if(Objects.isNull(bom)){ | 
|             throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "未查询到对象信息"); | 
|         } | 
|         if( 1 == optType){ | 
|             if(1 == bom.getStatus()){ | 
|                 throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "已启用生效,请勿重复操作"); | 
|             }else if(2 == bom.getStatus()){ | 
|                 throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "已禁用失效,无法启用"); | 
|             }else{ | 
|                 bom.setValidTime(new Date()); | 
|             } | 
|         }else if(2 == optType){ | 
|             //查询当前记录下是否存在被占用的物料 | 
|             List<Material> materialList = materialMapper.selectList( | 
|                     new QueryWrapper<Material>() | 
|                             .eq("id", bom.getMaterialId()) | 
|                             .apply(" id in ( select b2.MATERIAL_ID from  bom_version b1 inner join bom_detail b2  on b1.id = b2.bom_id  where b1.`STATUS` = 1  ) ") | 
|             ); | 
|             if(materialList.size()>0){ | 
|                 StringBuffer useName = new StringBuffer(); | 
|                 for (Material material:materialList) { | 
|                     useName.append(","+material.getName()); | 
|                 } | 
|                 throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "物料使用中,无法禁用失效,占用者:"+ useName.toString().substring(Constants.ONE,useName.length())); | 
|             } | 
|             if(0 == bom.getStatus()){ | 
|                 throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "未启用生效,无法禁用失效"); | 
|             }else  if(2 == bom.getStatus()){ | 
|                 throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "已禁用失效,请勿重复操作"); | 
|             }else{ | 
|                 bom.setInvalidTime(new Date()); | 
|             } | 
|         } | 
|         bom.setStatus(optType); | 
|         bomMapper.updateById(bom); | 
|         BomVersion bomVersion = new BomVersion(); | 
|         bomVersion.setStatus(optType); | 
|         bomVersion.setValidTime(bom.getValidTime()); | 
|         bomVersion.setInvalidTime(bom.getInvalidTime()); | 
|         bomVersionMapper.update(bomVersion,new UpdateWrapper<BomVersion>().eq("BOM_ID",id)); | 
|         return ApiResponse.success(bom); | 
|     } | 
|   | 
|     public void saveBomDetail(List<BomDetail> bomDetailList,Integer parentId){ | 
|         bomDetailList.forEach(i->{ | 
|             i.setBomId(parentId); | 
|             bomDetailMapper.insert(i); | 
|         }); | 
|     } | 
|   | 
|   | 
| } |