package com.huaheng.pc.shipment.shipmentDetail.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.huaheng.common.constant.QuantityConstant; import com.huaheng.common.exception.service.ServiceException; import com.huaheng.common.support.Convert; import com.huaheng.common.utils.DataUtils; import com.huaheng.common.utils.StringUtils; import com.huaheng.common.utils.security.ShiroUtils; import com.huaheng.framework.web.domain.AjaxResult; import com.huaheng.pc.config.material.domain.Material; import com.huaheng.pc.config.material.service.MaterialService; import com.huaheng.pc.config.shipmentPreference.domain.ShipmentPreference; import com.huaheng.pc.config.shipmentPreference.service.ShipmentPreferenceService; import com.huaheng.pc.config.statusFlow.domain.StatusFlowDetail; import com.huaheng.pc.config.statusFlow.domain.StatusFlowHeader; import com.huaheng.pc.config.statusFlow.service.StatusFlowDetailService; import com.huaheng.pc.config.statusFlow.service.StatusFlowHeaderService; import com.huaheng.pc.config.waveMaster.domain.WaveMaster; import com.huaheng.pc.config.waveMaster.service.WaveMasterService; import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader; import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService; import com.huaheng.pc.shipment.wave.domain.Wave; import com.huaheng.pc.shipment.wave.service.WaveService; import com.huaheng.pc.task.taskDetail.domain.TaskDetail; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Map; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.huaheng.pc.shipment.shipmentDetail.mapper.ShipmentDetailMapper; import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail; import org.springframework.transaction.annotation.Transactional; @Service public class ShipmentDetailServiceImpl extends ServiceImpl<ShipmentDetailMapper, ShipmentDetail> implements ShipmentDetailService{ @Autowired private ShipmentHeaderService shipmentHeaderService; @Autowired private MaterialService materialService; @Resource private ShipmentDetailMapper shipmentDetailMapper; @Autowired private WaveMasterService waveMasterService; @Autowired private WaveService waveService; @Autowired private ShipmentPreferenceService shipmentPreferenceService; @Autowired private StatusFlowHeaderService statusFlowHeaderService; @Autowired private StatusFlowDetailService statusFlowDetailService; /** * 新增出库明细 * * @param shipmentDetail * @return */ @Override public AjaxResult insertDetail(ShipmentDetail shipmentDetail) { //查看主单是否存在 LambdaQueryWrapper<ShipmentHeader> lambdaQueryWrapper = Wrappers.lambdaQuery(); lambdaQueryWrapper.eq(ShipmentHeader::getId,shipmentDetail.getShipmentId()) .eq(ShipmentHeader::getWarehouseCode,ShiroUtils.getWarehouseCode()); ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(lambdaQueryWrapper); if (shipmentHeader == null) { return AjaxResult.error("找不到主单据"); } if (shipmentHeader.getFirstStatus() > QuantityConstant.SHIPMENT_HEADER_POOL) { //表示已经加入了波次 return AjaxResult.error("主单据状态不允许新增明细"); } //查找物料 LambdaQueryWrapper<Material> lam = Wrappers.lambdaQuery(); lam.eq(Material::getCode,shipmentDetail.getMaterialCode()) .eq(Material::getWarehouseCode,ShiroUtils.getWarehouseCode()); Material material = materialService.getOne(lam); if (material == null) { return AjaxResult.error("物料未找到"); } if (StringUtils.isNotEmpty(material.getCompanyCode()) && ShiroUtils.getCompanyCodeList().contains(material.getCompanyCode()) == false) { return AjaxResult.error("物料不属于当前货主!"); } shipmentDetail.setId(null); shipmentDetail.setWarehouseCode(ShiroUtils.getWarehouseCode()); shipmentDetail.setMaterialCode(material.getCode()); shipmentDetail.setMaterialName(material.getName()); shipmentDetail.setReferCode(shipmentHeader.getReferCode()); shipmentDetail.setReferId(shipmentHeader.getReferId()); shipmentDetail.setMaterialSpec(material.getSpec()); shipmentDetail.setMaterialUnit(material.getUnit()); shipmentDetail.setCreated(null); shipmentDetail.setCreatedBy(ShiroUtils.getLoginName()); shipmentDetail.setLastUpdated(null); shipmentDetail.setLastUpdatedBy(ShiroUtils.getLoginName()); if (this.save(shipmentDetail)==true){ //更新单据总行数与总数量 shipmentHeader.setTotalLines(shipmentHeader.getTotalLines() + 1); shipmentHeader.setTotalQty(shipmentHeader.getTotalQty().add(shipmentDetail.getShipQty())); shipmentHeaderService.saveOrUpdate(shipmentHeader); return AjaxResult.success("新增单据明细成功"); } else { return AjaxResult.error("新增单据明细失败"); } } /** * 删除出库单据明细 * * @param id * @return */ @Override @Transactional public AjaxResult deleteDetail(String id) { if (StringUtils.isEmpty(id)) { return AjaxResult.error("id不能为空"); } String[] ids = id.split(","); List<Map<String,Integer>> list = shipmentDetailMapper.SelectFirstStatus(id); if (list.size() < 1) { return AjaxResult.error("找不到主单据!"); } if (list.size() > 1) { return AjaxResult.error("有多个主单据,不能一起删除!"); } if (list.get(0).get("firstStatus") > QuantityConstant.SHIPMENT_HEADER_POOL) { return AjaxResult.error("单据状进入订单池,不允许删除明细"); } Integer result = shipmentDetailMapper.batchDelete(ids); if (result > 0) { Integer headerId=list.get(0).get("id"); Map<String,String> map= shipmentDetailMapper.StatisticalByReceiptId(headerId); if(DataUtils.getInteger(map.get("totalLines")) <= 0) { shipmentHeaderService.removeById(headerId); } else { //更新表头的总行数和总数量统计 ShipmentHeader shipmentHeader = new ShipmentHeader(); shipmentHeader.setId(headerId); shipmentHeader.setTotalLines(DataUtils.getInteger(map.get("totalLines"))); shipmentHeader.setTotalQty(DataUtils.getBigDecimal(map.get("totalQty"))); shipmentHeaderService.saveOrUpdate(shipmentHeader); } return AjaxResult.success("删除单据明细成功"); } else { return AjaxResult.error("删除单据明细失败"); } } @Override public Integer countUnCompleted(Integer shipmentId) { return shipmentDetailMapper.countUnCompleted(shipmentId); } /** * 查看选中的单据状态是否符合出库流程 * 选中的单据加入波次 * 根据选中的主单ID和波次主表的code,判断主单之和是否符合波次的限制条件, * 看此code的波次是否建成未开始执行,如果是则只需修改波次属性,否则创建新的波次, * 修改加入波次的子单,修改子单和主单的状态,并修改子单的waveId */ @Override @Transactional public void saveWave(String ids, String code) { Integer status = QuantityConstant.SHIPMENT_HEADER_POOL; List<ShipmentHeader> shipmentHeaderList =shipmentPreferenceService.checkShipmentProcess(ids,status,code); //找到波次主表,看系统是否有此波次 LambdaQueryWrapper<WaveMaster> lam=Wrappers.lambdaQuery(); lam.eq(WaveMaster::getCode,code) .eq(WaveMaster::getWarehouseCode,ShiroUtils.getWarehouseCode()); WaveMaster waveMaster=waveMasterService.getOne(lam); if(waveMaster == null){ throw new ServiceException("系统没有此波次"); } if(Convert.toIntArray(ids).length >waveMaster.getMaxShipments()){ throw new ServiceException("加入波次的单据数量超过波次的单据限制"); } List<ShipmentDetail> shipmentDetailList=new ArrayList<>(); BigDecimal qty=new BigDecimal(0); //检查出库子表是否有处于波次的 for (Integer id : Convert.toIntArray(ids)) { LambdaQueryWrapper<ShipmentDetail> lamDetail=Wrappers.lambdaQuery(); lamDetail.eq(ShipmentDetail::getWarehouseCode,ShiroUtils.getWarehouseCode()) .eq(ShipmentDetail::getShipmentId,id); List<ShipmentDetail> shipmentDetails=this.list(lamDetail); if(shipmentDetails == null || shipmentDetails.size() == 0){ throw new ServiceException("系统没有主单id为"+id+"的子单"); } //查看是否有单据处于波次中 for(ShipmentDetail shipmentDetail : shipmentDetails) { if (shipmentDetail.getWaveId() != 0) { throw new ServiceException("主单id为" + id + "子单id为" + shipmentDetail.getId() + "的子单已加入波次,不可再加入波次"); } if(shipmentDetail.getShipQty().compareTo(shipmentDetail.getRequestQty())!=0) { shipmentDetailList.add(shipmentDetail); qty = qty.add(shipmentDetail.getShipQty().subtract(shipmentDetail.getRequestQty())); } } } if(shipmentDetailList.size()>waveMaster.getMaxLines()){ throw new ServiceException("加入波次的总行数超过波次的行数限制"); } Boolean flag=false; //查看波次是否建成未执行 LambdaQueryWrapper<Wave> waveLam = Wrappers.lambdaQuery(); waveLam.eq(Wave::getStatus,QuantityConstant.WAVE_STATUS_BUILD) .eq(Wave::getWaveMode,code) .eq(Wave::getWarehouseCode,ShiroUtils.getWarehouseCode()); Wave wave = waveService.getOne(waveLam); if(wave != null && (wave.getTotalShipments()+Convert.toIntArray(ids).length)<= waveMaster.getMaxShipments() && (wave.getTotalLines()+shipmentDetailList.size()<= waveMaster.getMaxLines())){ //修改波次 wave.setTotalShipments(Convert.toIntArray(ids).length + wave.getTotalShipments()); wave.setTotalLines(shipmentDetailList.size() + wave.getTotalLines()); wave.setTotalQty(qty.add(wave.getTotalQty())); wave.setLastUpdatedBy(ShiroUtils.getLoginName()); }else { //创建波次 wave=new Wave(); wave.setWarehouseCode(ShiroUtils.getWarehouseCode()); wave.setMasterCode(code); wave.setWaveName(waveMaster.getName()); wave.setStatus(QuantityConstant.WAVE_STATUS_BUILD); wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_BUILD); wave.setTotalShipments(Convert.toIntArray(ids).length); wave.setTotalLines(shipmentDetailList.size()); wave.setTotalQty(qty); wave.setCreatedBy(ShiroUtils.getLoginName()); flag = waveService.save(wave); if (flag == false) { throw new ServiceException("波次建立失败"); } } //修改出库子单,加入波次ID,并修改状态为波次 for(ShipmentDetail shipmentDetail :shipmentDetailList){ shipmentDetail.setWaveId(wave.getId()); if(shipmentDetail.getStatus()<QuantityConstant.SHIPMENT_HEADER_WAVE) { shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_WAVE); } } flag = this.updateBatchById(shipmentDetailList); if(flag == false){ throw new ServiceException("出库子单加入波次失败"); } for(ShipmentHeader shipmentHeader :shipmentHeaderList){ shipmentHeader.setWaveId(wave.getId()); shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_WAVE); if(shipmentHeader.getLastStatus()<QuantityConstant.SHIPMENT_HEADER_WAVE) { shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_WAVE); } } flag = shipmentHeaderService.updateBatchById(shipmentHeaderList); if(flag == false){ throw new ServiceException("修改主单状态失败"); } } //查看最高状态和最低状态 @Override public Map<String,Integer> selectStatus(Integer id) { return shipmentDetailMapper.selectStatus(id); } @Override public List<TaskDetail> getShipmentQtyLast7Days() { return shipmentDetailMapper.getShipmentQtyLast7Days(); } @Override public List<TaskDetail> getWarehouseShipment() { return shipmentDetailMapper.getWarehouseShipment(); } @Override public List<TaskDetail> getCompanyShipment() { return shipmentDetailMapper.getCompanyShipment(); } @Override public Boolean insertDetails(List<ShipmentDetail> shipmentDetails) { int flag = shipmentDetailMapper.insertDetails(shipmentDetails); if(flag > 0){ return true; }else { return false; } } }