MaterialBatchServiceImpl.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.jsh.erp.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import com.jsh.erp.datasource.entities.*;
  5. import com.jsh.erp.datasource.mappers.MaterialBatchMapper;
  6. import com.jsh.erp.datasource.mappers.MaterialCategoryMapperEx;
  7. import com.jsh.erp.exception.JshException;
  8. import com.jsh.erp.service.*;
  9. import com.jsh.erp.utils.DateUtils;
  10. import com.jsh.erp.utils.RandomHelper;
  11. import com.jsh.erp.utils.StringUtil;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.stereotype.Service;
  15. import javax.annotation.Resource;
  16. import java.math.BigDecimal;
  17. import java.util.ArrayList;
  18. import java.util.Date;
  19. import java.util.List;
  20. @Service
  21. public class MaterialBatchServiceImpl extends ServiceImpl<MaterialBatchMapper,MaterialBatch> implements MaterialBatchService {
  22. private Logger logger = LoggerFactory.getLogger(MaterialBatchServiceImpl.class);
  23. @Resource
  24. private MaterialBatchMapper materialBatchMapper;
  25. @Resource
  26. private UserService userService;
  27. @Resource
  28. private DepotItemService depotItemService;
  29. @Resource
  30. private MaterialExtendService materialExtendService;
  31. @Resource
  32. private InventoryLogService inventoryLogService;
  33. @Resource
  34. private MaterialCategoryMapperEx materialCategoryMapperEx;
  35. @Override
  36. public void generateMaterialBatchByDepotItemId(DepotItem depotItem) throws Exception {
  37. //获取单据子表信息
  38. //DepotItem depotItem = depotItemService.getDepotItem(diId);
  39. //获取条码信息
  40. MaterialExtend materialExtend = materialExtendService.getMaterialExtend(depotItem.getMaterialExtendId());
  41. //创建批次信息
  42. MaterialBatch materialBatch = new MaterialBatch();
  43. //设置单据id
  44. materialBatch.setDepotItemId(depotItem.getId());
  45. //设置商品id
  46. materialBatch.setMaterialId(depotItem.getMaterialId());
  47. //设置商品单位
  48. materialBatch.setCommodityUnit(depotItem.getMaterialUnit());
  49. User user = userService.getCurrentUser();
  50. //创建人编码
  51. materialBatch.setCreateSerial(user.getLoginName());
  52. //创建日期
  53. materialBatch.setCreateTime(new Date());
  54. //生产日期
  55. materialBatch.setProductionDate(depotItem.getProductionDate());
  56. //保质期天数
  57. materialBatch.setExpiryNum(depotItem.getExpiryNum());
  58. //批次号
  59. String batchNumber = DateUtils.dateTimeNow("yyyyMMdd") + RandomHelper.getRandomStr(6);
  60. materialBatch.setBatchNumber(batchNumber);
  61. //设置库存(订单基础单位数量)
  62. materialBatch.setInventory(depotItem.getBasicNumber());
  63. //仓库id
  64. materialBatch.setDepotId(depotItem.getDepotId());
  65. //仓位货架
  66. materialBatch.setPosition(depotItem.getPosition());
  67. materialBatchMapper.insert(materialBatch);
  68. }
  69. @Override
  70. public synchronized void handleMaterialBatchByDepotItemId(Long diId) throws Exception {
  71. DepotItem depotItem = depotItemService.getDepotItem(diId);
  72. //根据单据商品id查询商品批次数据
  73. List<MaterialBatch> list = materialBatchMapper.getMaterialBatchByMaterialId(depotItem.getMaterialId());
  74. //根据单据子表基础单位数量减去批次库存
  75. BigDecimal basicNumber = depotItem.getBasicNumber();
  76. for (MaterialBatch materialBatch : list) {
  77. if (materialBatch.getInventory().compareTo(basicNumber) >= 0){
  78. //批次库存足够,扣除库存,结束循环
  79. BigDecimal inventory = materialBatch.getInventory().subtract(basicNumber);
  80. materialBatch.setInventory(inventory);
  81. updateInventory("出库",diId,materialBatch);
  82. break;
  83. }else {
  84. //库存不足,扣除当前批次库存,继续循环
  85. basicNumber = basicNumber.subtract(materialBatch.getInventory());
  86. materialBatch.setInventory(BigDecimal.ZERO);
  87. updateInventory("出库",diId,materialBatch);
  88. }
  89. }
  90. }
  91. @Override
  92. public synchronized void updateInventory(String type, Long id, MaterialBatch materialBatch) throws Exception {
  93. if (materialBatch.getInventory() != null){
  94. //获取修改前库存
  95. int originalStock = materialBatchMapper.selectOne("id",materialBatch.getId()).getInventory().intValue();
  96. if (originalStock != materialBatch.getInventory().intValue()){
  97. //库存不相同,修改库存
  98. User user = userService.getCurrentUser();
  99. InventoryLog log = new InventoryLog();
  100. log.setUpdateUser(user.getId());
  101. log.setUpdateTime(new Date());
  102. log.setMaterialId(materialBatch.getMaterialId());
  103. log.setMaterialExtendId(materialBatch.getId());
  104. log.setOriginalStock(originalStock);
  105. log.setCurrentStock(materialBatch.getInventory().intValue());
  106. log.setItemId(id);
  107. log.setType(type);
  108. inventoryLogService.save(log);
  109. update(new UpdateWrapper<MaterialBatch>().set("inventory",materialBatch.getInventory()).eq("id",materialBatch.getId()));
  110. }
  111. }
  112. }
  113. /**
  114. * 根据仓库id和商品id查询商品批次库存
  115. * @param depotList 仓库id
  116. * @param mid 商品id
  117. */
  118. @Override
  119. public BigDecimal getInventorySumByDepotAndMid(List<Long> depotList, Long mid) {
  120. return materialBatchMapper.getInventorySumByDepotAndMid(depotList,mid);
  121. }
  122. /**
  123. * 获取批次号字符
  124. */
  125. @Override
  126. public List<MaterialVo4Unit> 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 {
  127. List<MaterialVo4Unit> list =null;
  128. try{
  129. if(StringUtil.isNotEmpty(q)) {
  130. q = q.replace("'", "");
  131. q = q.trim();
  132. }
  133. list= materialBatchMapper.findBySelectWithBarCode(q, standardOrModel, color, brand, mfrs,
  134. enableSerialNumber, enableBatchNumber, offset, rows,depotId);
  135. }catch(Exception e){
  136. JshException.readFail(logger, e);
  137. }
  138. return list;
  139. }
  140. }