| package doumeemes.service.ext.impl; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| 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.dao.business.WTransferDetailMapper; | 
| import doumeemes.dao.business.WarehouseLocationMapper; | 
| import doumeemes.dao.business.dto.WStockImportDTO; | 
| import doumeemes.dao.business.model.*; | 
| import doumeemes.dao.ext.*; | 
| import doumeemes.dao.ext.beanDto.QueryWStockDto; | 
| import doumeemes.dao.ext.beanDto.QueryWStockHeadNewDTO; | 
| import doumeemes.dao.ext.beanDto.QueryWStockNewDTO; | 
| import doumeemes.dao.ext.dto.*; | 
| import doumeemes.dao.ext.vo.*; | 
| import doumeemes.service.ext.ProceduresExtService; | 
| import doumeemes.service.ext.WStockExtService; | 
| import com.github.pagehelper.PageHelper; | 
| import com.github.pagehelper.PageInfo; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.apache.shiro.SecurityUtils; | 
| import org.apache.shiro.util.CollectionUtils; | 
| import org.springframework.beans.BeanUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| 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.List; | 
| import java.util.Objects; | 
|   | 
| /** | 
|  * 物料库存余额信息Service实现 | 
|  * @author 江蹄蹄 | 
|  * @date 2022/04/20 10:56 | 
|  */ | 
| @Service | 
| public class WStockExtServiceImpl implements WStockExtService { | 
|   | 
|     @Autowired | 
|     private WStockExtMapper wStockExtMapper; | 
|     @Autowired | 
|     private MaterialDistributeExtMapper materialDistributeExtMapper; | 
|     @Autowired | 
|     private WarehouseExtMapper warehouseExtMapper; | 
|     @Autowired | 
|     private WarehouseLocationMapper warehouseLocationMapper; | 
|     @Autowired | 
|     private AppliancesExtMapper appliancesExtMapper; | 
|   | 
|     @Autowired | 
|     private BomDetailExtMapper bomDetailExtMapper; | 
|     @Autowired | 
|     private BomExtMapper bomExtMapper; | 
|     @Autowired | 
|     private ProceduresExtService proceduresExtService; | 
|     @Autowired | 
|     private WTransferDetailExtMapper wTransferDetailExtMapper; | 
|     @Override | 
|     public PageData<WStockExtListVO> findPage(PageWrap<QueryWStockExtDTO> pageWrap) { | 
|         PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         List<WStockExtListVO> result = wStockExtMapper.selectListAll(pageWrap.getModel()); | 
|         return PageData.from(new PageInfo<>(result)); | 
|     } | 
|   | 
|     @Override | 
|     public List<WStockExtListVO> getListBynum(QueryWStockExtDTO pageWrap) { | 
|         List<WStockExtListVO> result = wStockExtMapper.selectListAll(pageWrap); | 
|         return result; | 
|     } | 
|   | 
|     @Override | 
|     public PageData<WStockExtListVO> findListPage(PageWrap<QueryWStockDto> 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()); | 
| //        } | 
|         PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         List<WStockExtListVO> result = wStockExtMapper.selectAdminList(pageWrap.getModel()); | 
|         return PageData.from(new PageInfo<>(result)); | 
|     } | 
|   | 
|   | 
|   | 
|     @Override | 
|     public PageData<WStockExtListVO> choiceStockListPage(PageWrap<QueryWStockDto> pageWrap) { | 
|         PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         if(!Objects.isNull(pageWrap.getModel().getWarehouseQualityType())){ | 
|             Constants.wareHouse_QualityType w = Constants.wareHouse_QualityType.checkEnum(pageWrap.getModel().getWarehouseQualityType()); | 
|             if(w.getType()==3){ | 
|                 pageWrap.getModel().setWarehouseQualityType(null); | 
|             }else{ | 
|                 pageWrap.getModel().setWarehouseQualityType(w.getType().toString()); | 
|             } | 
|         } | 
|         List<WStockExtListVO> result = wStockExtMapper.choiceStockList(pageWrap.getModel()); | 
|         return PageData.from(new PageInfo<>(result)); | 
|     } | 
|   | 
|   | 
|   | 
|     @Override | 
|     public PageData<WStockExtListVO> choiceStockPageByTransfer(PageWrap<QueryWStockHeadNewDTO> pageWrap) { | 
|         List<WTransferDetail> wTransferDetailList = wTransferDetailExtMapper.wTransferDetailListById(pageWrap.getModel().getTransferId()); | 
|         if(Objects.isNull(wTransferDetailList)||wTransferDetailList.size()==Constants.ZERO){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未查询到转库单明细信息"); | 
|         } | 
|         List<QueryWStockNewDTO> stockNewDTOList = new ArrayList<>(); | 
|         for (WTransferDetail wTransferDetail:wTransferDetailList) { | 
|             QueryWStockNewDTO queryWStockNewDTO = new QueryWStockNewDTO(); | 
|             BeanUtils.copyProperties(wTransferDetail,queryWStockNewDTO); | 
|             stockNewDTOList.add(queryWStockNewDTO); | 
|         } | 
|         pageWrap.getModel().setStockNewDTOList(stockNewDTOList); | 
|         PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         List<WStockExtListVO> result = wStockExtMapper.choiceStockPageByTransfer(pageWrap.getModel()); | 
|         return PageData.from(new PageInfo<>(result)); | 
|     } | 
|   | 
|     /** | 
|      * 库存是否满足计划 | 
|      * @param  p | 
|      * @return | 
|      */ | 
|     @Override | 
|     public   Integer isStockForPlan(PlansExtListVO p){ | 
|         LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         QueryBomDetailExtDTO bdparam = initBomParam(user,p); | 
|         ProceduresExtListVO pModel = proceduresExtService.getById(user.getCompany().getId(),p.getProcedureId()); | 
|         if(pModel==null){ | 
|             return 0; | 
|         } | 
|         //待分配(计划数量-已分配未报工数量-合格数量-不良数量) | 
|         int tNum = p.getNum() - p.getDistributNum(); | 
|         QueryWStockExtDTO stock = new QueryWStockExtDTO(); | 
|         stock.setDeleted(Constants.ZERO); | 
|         stock.setRootDepartId(p.getRootDepartId()); | 
|         stock.setWarehouseId(pModel.getPickingWarehouseId()); | 
|         if(bdparam==null){ | 
|             //如果没有配置BOM,则投料本身 | 
|             stock.setMaterialId(p.getMaterialId()); | 
|             //查询库存记录 | 
|           List<WStockExtListVO>  stockList = wStockExtMapper.selectAllList( stock); | 
|             if(stockList!=null && stockList.size()>0 &&Constants.formatBigdecimal(stockList.get(0).getNum()).compareTo(new BigDecimal(tNum)) >=0){ | 
|                 //如果库存不足 | 
|                 return 1; | 
|             } | 
|         }else{ | 
|             //查询bom明细 | 
|             List<BomDetailExtListVO> bomDetailList = bomDetailExtMapper.selectList(bdparam); | 
|             if(!CollectionUtils.isEmpty(bomDetailList) ){ | 
|                stock.setMaterialIds(getMaterialIds(bomDetailList)); | 
|             } | 
|             //查询库存记录 | 
|             List<WStockExtListVO>  stockList = wStockExtMapper.selectAllList( stock); | 
|             boolean isFull = true; | 
|             for (BomDetailExtListVO i : bomDetailList) { | 
|                 //遍历查询库存是否满足 | 
|                 BigDecimal  sNum =getStockByMaterialId(i.getMaterialId(),stockList); | 
|                 //所需要库存量 | 
|                 BigDecimal needNum = Constants.formatBigdecimal(i.getNum()).multiply(new BigDecimal(tNum)); | 
|                 if( sNum.compareTo(needNum) <0){ | 
|                     //如果库存不足 | 
|                     isFull =false; | 
|                     break; | 
|                 } | 
|             } | 
|             return  isFull?1:0; | 
|         } | 
|         return 0; | 
|   | 
|     } | 
|   | 
|     private BigDecimal getStockByMaterialId(Integer materialId, List<WStockExtListVO> stockList) { | 
|         BigDecimal num = new BigDecimal(0); | 
|         if(stockList!=null){ | 
|             for(WStockExtListVO s : stockList){ | 
|                 if(Constants.equalsInteger(s.getMaterialId(),materialId)){ | 
|                     num.add(Constants.formatBigdecimal(s.getNum())); | 
|                 } | 
|             } | 
|         } | 
|         return num; | 
|     } | 
|   | 
|     private List<Integer> getMaterialIds(List<BomDetailExtListVO> bomDetailList) { | 
|         if(CollectionUtils.isEmpty(bomDetailList) ){ | 
|             return  null; | 
|         } | 
|         List<Integer> list = new ArrayList<>(); | 
|         bomDetailList. forEach(i->{ | 
|             //判断当前状态,根据状态获取明细记录 若是已取消业务,查询 0 | 
|          list.add(i.getMaterialId()); | 
|         }); | 
|         return  list; | 
|     } | 
|   | 
|   | 
|     private QueryBomDetailExtDTO initBomParam(LoginUserInfo user, PlansExtListVO mp)  throws BusinessException{ | 
|         QueryBomExtDTO bb = new QueryBomExtDTO(); | 
|         bb.setDepartId(mp.getDepartId()); | 
|         bb.setDeleted(Constants.ZERO); | 
|         bb.setRootDepartId(user.getRootDepartment().getId()); | 
|         bb.setMaterialId(mp.getMaterialId()); | 
|         bb.setProcedureId(mp.getProcedureId()); | 
|         BomExtListVO versionBom = bomExtMapper.selectByModel( bb); | 
|         if(versionBom == null || StringUtils.isBlank(versionBom.getVersion()) || versionBom.getBomVersionId() == null){ | 
|             return  null; | 
|         } | 
|         QueryBomDetailExtDTO  bom = new QueryBomDetailExtDTO(); | 
|         bom.setDeleted(Constants.ZERO); | 
|         bom.setBomId(versionBom.getBomVersionId()); | 
|         bom.setWorkorderId(mp.getId()); | 
|         return bom; | 
|     } | 
|   | 
|   | 
|   | 
|   | 
|   | 
|     @Transactional(rollbackFor = {BusinessException.class,Exception.class}) | 
|     @Override | 
|     public String importStock(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<WStockImportDTO> list = EasyExcelUtil.importExcel(file, 1, 1, WStockImportDTO.class); | 
|         if(list == null || list.size()==0){ | 
|             throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容有误!"); | 
|         } | 
|         String msg = ""; | 
|         int index = 1; | 
|         for(WStockImportDTO p : list){ | 
|             WStock stock = new WStock(); | 
|             stock.setDeleted(Constants.ZERO); | 
|             stock.setRootDepartId(user.getRootDepartment().getId()); | 
|             stock.setBatch(p.getBatch()); | 
|             stock.setNum(p.getNum()); | 
|   | 
|             Appliances appliances = new Appliances(); | 
|             appliances.setUpdateTime(DateUtil.getCurrentDate()); | 
|             appliances.setUpdateUser(user.getId()); | 
|             appliances.setNum(p.getNum()); | 
|             appliances.setBatch(p.getBatch()); | 
|             //检查数据有效性 | 
|             String code =  checkData(p,stock,appliances,index,user); | 
|             if(StringUtils.isNotBlank(code)){ | 
|                 //如果有篮筐不为空,提示信息有修改 | 
|                 msg += "【"+code+"】"; | 
|             } | 
|             if(stock.getId()!=null){ | 
|                 stock.setUpdateTime(DateUtil.getCurrentDate()); | 
|                 stock.setUpdateUser(user.getId()); | 
|                 wStockExtMapper.updateById(stock); | 
|             }else{ | 
|                 stock.setCreateTime(DateUtil.getCurrentDate()); | 
|                 stock.setCreateUser(user.getId()); | 
|                 wStockExtMapper.insert(stock); | 
|             } | 
|             appliancesExtMapper.updateById(appliances); | 
|   | 
|             index++; | 
|         } | 
|         if(StringUtils.isNotBlank(msg)){ | 
|             msg = "你好,部分数据导入成功!其中篮筐"+msg+"非空篮筐,导入不成功!"; | 
|         }else{ | 
|             msg = "全部导入成功!"; | 
|         } | 
|         return msg; | 
|   | 
|     } | 
|   | 
|     /** | 
|      * 数据有效性检查 | 
|      * @param p | 
|      * @param index | 
|      * @param user | 
|      * @throws BusinessException | 
|      */ | 
|     private String checkData(WStockImportDTO p, WStock stock, Appliances appliances ,int index,LoginUserInfo user) throws BusinessException{ | 
|         if(Constants.formatBigdecimal(p.getNum()).compareTo(new BigDecimal(0)) != 1){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【物料数量】数据错误!"); | 
|         } | 
|         if(p.getApplianceCode() == null){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【物料编码】数据错误"); | 
|         } | 
|         if(StringUtils.isBlank(p.getMaterialCode())){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【物料编码】数据错误!"); | 
|         } | 
|         if(StringUtils.isBlank(p.getWarehouse())){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【仓库名称】数据错误!"); | 
|         } | 
|   | 
|         p.setApplianceCode(p.getApplianceCode().trim()); | 
|         p.setMaterialCode(p.getMaterialCode().trim()); | 
|         p.setWarehouse(p.getWarehouse().trim()); | 
|         Appliances a = new Appliances(); | 
|         a.setDeleted(Constants.ZERO); | 
|         a.setCode(p.getApplianceCode()); | 
|         a.setRootDepartId(user.getRootDepartment().getId()); | 
|         a.setDepartId(user.getCurComDepartment().getId()); | 
|         //查询篮筐信息 | 
|         a = appliancesExtMapper.selectOne(new QueryWrapper<>(a)); | 
|         if(a == null){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【篮筐编码】数据不存在!"); | 
|         } | 
|         if(!Constants.equalsInteger(a.getStatus(),Constants.APPLIANCES_STATUS.empty)){ | 
|             return p.getApplianceCode() ; | 
|         } | 
|   | 
|         Warehouse w = new Warehouse(); | 
|         w.setDeleted(Constants.ZERO); | 
|         w.setName(p.getWarehouse()); | 
|         w.setRootDepartId(user.getRootDepartment().getId()); | 
|         w.setDepartId(user.getCurComDepartment().getId()); | 
|         //查询篮筐信息 | 
|         w = warehouseExtMapper.selectOne(new QueryWrapper<>(w)); | 
|         if(w == null){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【仓库名称】数据不存在!"); | 
|         } | 
|         stock.setWarehouseId(w.getId()); | 
|   | 
|         if(StringUtils.isNotBlank(p.getLocation())){ | 
|             p.setLocation(p.getLocation().trim()); | 
|             WarehouseLocation l = new WarehouseLocation(); | 
|             l.setDeleted(Constants.ZERO); | 
|             l.setWarehouseId(w.getId()); | 
|             l.setUnionName(p.getLocation()); | 
|             l.setRootDepartId(user.getRootDepartment().getId()); | 
|             //查询篮筐信息 | 
|             l = warehouseLocationMapper.selectOne(new QueryWrapper<>(l)); | 
|             if(l == null || !Constants.equalsInteger(l.getWarehouseId(),w.getId())){ | 
|                 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【库位名称】数据不存在!!"); | 
|             } | 
|             stock.setLocationId(l.getId()); | 
|             appliances.setLocationId(l.getId()); | 
|         } | 
|   | 
|   | 
|         QueryMaterialDistributeExtDTO d = new QueryMaterialDistributeExtDTO(); | 
|         d.setDeleted(Constants.ZERO); | 
|         d.setMmodelCode(p.getMaterialCode()); | 
|         d.setRootDepartId(user.getRootDepartment().getId()); | 
|         d.setDepartId(user.getCurComDepartment().getId()); | 
|         //查询产品信息 | 
|         MaterialDistributeExtListVO mm = materialDistributeExtMapper.selectByModel(d); | 
|         if(mm == null){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品编码】数据不存在!"); | 
|         } | 
|         //存储物料分配表主键 | 
|         stock.setMaterialId(mm.getId()); | 
|         //单位编码 | 
|         stock.setUnitId(mm.getUnitId()); | 
|   | 
|         appliances.setMaterialId(mm.getId()); | 
|         appliances.setUnitId(mm.getUnitId()); | 
|         appliances.setPositionType(Constants.APPLIANCES_POSITION.warehouse); | 
|         appliances.setStatus(Constants.APPLIANCES_STATUS.full); | 
|         appliances.setId(a.getId()); | 
|         appliances.setWarehouseId(w.getId()); | 
|   | 
|         WStock s1 = new WStock(); | 
|         s1.setLocationId(stock.getLocationId()); | 
|         s1.setWarehouseId(stock.getWarehouseId()); | 
|         s1.setDeleted(Constants.ZERO); | 
|         s1.setMaterialId(stock.getMaterialId()); | 
|         s1.setBatch(stock.getBatch()); | 
|         s1 = wStockExtMapper.selectOne(new QueryWrapper<>(s1)); | 
|         if(s1 !=null){ | 
|             stock.setId(s1.getId()); | 
|             stock.setNum(Constants.formatBigdecimal(s1.getNum()).add(stock.getNum())); | 
|         } | 
|   | 
|         return null; | 
|     } | 
| } |