package com.jsh.erp.service.impl; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jsh.erp.datasource.entities.*; import com.jsh.erp.datasource.mappers.MaterialBatchMapper; import com.jsh.erp.datasource.mappers.MaterialCategoryMapperEx; import com.jsh.erp.exception.JshException; import com.jsh.erp.service.*; import com.jsh.erp.utils.DateUtils; import com.jsh.erp.utils.RandomHelper; import com.jsh.erp.utils.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; @Service public class MaterialBatchServiceImpl extends ServiceImpl implements MaterialBatchService { private Logger logger = LoggerFactory.getLogger(MaterialBatchServiceImpl.class); @Resource private MaterialBatchMapper materialBatchMapper; @Resource private UserService userService; @Resource private DepotItemService depotItemService; @Resource private MaterialExtendService materialExtendService; @Resource private InventoryLogService inventoryLogService; @Resource private MaterialCategoryMapperEx materialCategoryMapperEx; @Override public void generateMaterialBatchByDepotItemId(DepotItem depotItem) throws Exception { //获取单据子表信息 //DepotItem depotItem = depotItemService.getDepotItem(diId); //获取条码信息 MaterialExtend materialExtend = materialExtendService.getMaterialExtend(depotItem.getMaterialExtendId()); //创建批次信息 MaterialBatch materialBatch = new MaterialBatch(); //设置单据id materialBatch.setDepotItemId(depotItem.getId()); //设置商品id materialBatch.setMaterialId(depotItem.getMaterialId()); //设置商品单位 materialBatch.setCommodityUnit(depotItem.getMaterialUnit()); User user = userService.getCurrentUser(); //创建人编码 materialBatch.setCreateSerial(user.getLoginName()); //创建日期 materialBatch.setCreateTime(new Date()); //生产日期 materialBatch.setProductionDate(depotItem.getProductionDate()); //保质期天数 materialBatch.setExpiryNum(depotItem.getExpiryNum()); //批次号 String batchNumber = DateUtils.dateTimeNow("yyyyMMdd") + RandomHelper.getRandomStr(6); materialBatch.setBatchNumber(batchNumber); //设置库存(订单基础单位数量) materialBatch.setInventory(depotItem.getBasicNumber()); //仓库id materialBatch.setDepotId(depotItem.getDepotId()); //仓位货架 materialBatch.setPosition(depotItem.getPosition()); materialBatchMapper.insert(materialBatch); } @Override public synchronized void handleMaterialBatchByDepotItemId(Long diId) throws Exception { DepotItem depotItem = depotItemService.getDepotItem(diId); //根据单据商品id查询商品批次数据 List list = materialBatchMapper.getMaterialBatchByMaterialId(depotItem.getMaterialId()); //根据单据子表基础单位数量减去批次库存 BigDecimal basicNumber = depotItem.getBasicNumber(); for (MaterialBatch materialBatch : list) { if (materialBatch.getInventory().compareTo(basicNumber) >= 0){ //批次库存足够,扣除库存,结束循环 BigDecimal inventory = materialBatch.getInventory().subtract(basicNumber); materialBatch.setInventory(inventory); updateInventory("出库",diId,materialBatch); break; }else { //库存不足,扣除当前批次库存,继续循环 basicNumber = basicNumber.subtract(materialBatch.getInventory()); materialBatch.setInventory(BigDecimal.ZERO); updateInventory("出库",diId,materialBatch); } } } @Override public synchronized void updateInventory(String type, Long id, MaterialBatch materialBatch) throws Exception { if (materialBatch.getInventory() != null){ //获取修改前库存 int originalStock = materialBatchMapper.selectOne("id",materialBatch.getId()).getInventory().intValue(); if (originalStock != materialBatch.getInventory().intValue()){ //库存不相同,修改库存 User user = userService.getCurrentUser(); InventoryLog log = new InventoryLog(); log.setUpdateUser(user.getId()); log.setUpdateTime(new Date()); log.setMaterialId(materialBatch.getMaterialId()); log.setMaterialExtendId(materialBatch.getId()); log.setOriginalStock(originalStock); log.setCurrentStock(materialBatch.getInventory().intValue()); log.setItemId(id); log.setType(type); inventoryLogService.save(log); update(new UpdateWrapper().set("inventory",materialBatch.getInventory()).eq("id",materialBatch.getId())); } } } /** * 根据仓库id和商品id查询商品批次库存 * @param depotList 仓库id * @param mid 商品id */ @Override public BigDecimal getInventorySumByDepotAndMid(List depotList, Long mid) { return materialBatchMapper.getInventorySumByDepotAndMid(depotList,mid); } /** * 获取批次号字符 */ @Override public List findBySelectWithBarCode(Long categoryId, String q, String standardOrModel, String color, String brand, String mfrs, String enableSerialNumber, String enableBatchNumber, Integer offset, Integer rows, Long depotId) throws Exception { List list =null; try{ if(StringUtil.isNotEmpty(q)) { q = q.replace("'", ""); q = q.trim(); } list= materialBatchMapper.findBySelectWithBarCode(q, standardOrModel, color, brand, mfrs, enableSerialNumber, enableBatchNumber, offset, rows,depotId); }catch(Exception e){ JshException.readFail(logger, e); } return list; } }