MaterialBatchServiceImpl.java 7.3 KB

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