|
|
@@ -1,35 +1,32 @@
|
|
|
package com.lc.ibps.components.reagent.service;
|
|
|
|
|
|
-import com.lc.ibps.api.base.query.QueryFilter;
|
|
|
-import com.lc.ibps.base.core.util.string.StringUtil;
|
|
|
-import com.lc.ibps.base.db.model.DefaultQueryFilter;
|
|
|
-import com.lc.ibps.base.framework.table.ICommonDao;
|
|
|
-import com.lc.ibps.cloud.entity.APIResult;
|
|
|
+import com.lc.ibps.base.core.util.BeanUtils;
|
|
|
+import com.lc.ibps.base.core.util.Collections;
|
|
|
+import com.lc.ibps.cloud.provider.GenericProvider;
|
|
|
import com.lc.ibps.components.reagent.domain.ReagentConsumablesInventory;
|
|
|
import com.lc.ibps.components.reagent.domain.ReagentConsumablesTransaction;
|
|
|
import com.lc.ibps.components.reagent.dto.InventoryDTO;
|
|
|
+import com.lc.ibps.components.reagent.dto.KcDto;
|
|
|
+import com.lc.ibps.components.reagent.dto.StockDto;
|
|
|
import com.lc.ibps.components.reagent.persistence.entity.ReagentConsumablesInventoryPo;
|
|
|
+import com.lc.ibps.components.reagent.persistence.entity.ReagentConsumablesTransactionPo;
|
|
|
import com.lc.ibps.components.reagent.repository.ReagentConsumablesInventoryRepository;
|
|
|
-import com.lc.ibps.components.reagent.repository.ReagentConsumablesItemRepository;
|
|
|
import com.lc.ibps.components.reagent.repository.ReagentConsumablesTransactionRepository;
|
|
|
+import com.lc.ibps.untils.StrUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
-public class InventoryService {
|
|
|
-
|
|
|
- @Resource
|
|
|
- private ReagentConsumablesItemRepository reagentConsumablesItemRepository;
|
|
|
+public class InventoryService extends GenericProvider {
|
|
|
|
|
|
@Resource
|
|
|
private ReagentConsumablesInventoryRepository reagentConsumablesInventoryRepository;
|
|
|
@Resource
|
|
|
private ReagentConsumablesTransactionRepository reagentConsumablesTransactionRepository;
|
|
|
- @Resource
|
|
|
- private ICommonDao commonDao;
|
|
|
|
|
|
@Autowired
|
|
|
private StoreService storeService;
|
|
|
@@ -37,42 +34,242 @@ public class InventoryService {
|
|
|
@Autowired
|
|
|
private LinYongService linYongService;
|
|
|
|
|
|
- public boolean sync(String id, String action) {
|
|
|
+ @Autowired
|
|
|
+ private ReturnService returnService;
|
|
|
+
|
|
|
+ public boolean sync(String id, String action) throws Exception {
|
|
|
+ logger.warn("com.lc.ibps.components.reagent.service.InventoryService.sync()--->id={},action={}", id,action);
|
|
|
OperationService service = getService(action);
|
|
|
- if(service == null) throw new IllegalArgumentException();
|
|
|
for (InventoryDTO item : service.getItems(id)) {
|
|
|
ReagentConsumablesInventoryPo inventory = item.getInventory();
|
|
|
- if(StringUtil.isNotBlank(inventory.getId())){
|
|
|
- //出库操作,根据库存id计算数量
|
|
|
- ReagentConsumablesInventoryPo inventoryPo = reagentConsumablesInventoryRepository.get(inventory.getId());
|
|
|
- inventory.setReagentCode(inventoryPo.getReagentCode());
|
|
|
- inventory.setExpDate(inventoryPo.getExpDate());
|
|
|
- item.getTransaction().setReagentCode(inventoryPo.getReagentCode());
|
|
|
- item.getTransaction().setExpDate(inventoryPo.getExpDate());
|
|
|
- calcQuantity(inventory, inventoryPo.getQuantity(),service.getPolicy());
|
|
|
- }else{
|
|
|
- //入库,根据试剂id+过期时间判断是否在库
|
|
|
- List<ReagentConsumablesInventoryPo> query = reagentConsumablesInventoryRepository.findByMainId(inventory.getReagentCode());
|
|
|
- for (ReagentConsumablesInventoryPo inventoryPo: query) {
|
|
|
- if(inventoryPo.getExpDate().equalsIgnoreCase(inventory.getExpDate())){
|
|
|
- inventory.setId(inventoryPo.getId());
|
|
|
- calcQuantity(inventory, inventoryPo.getQuantity(),service.getPolicy());
|
|
|
- break;
|
|
|
- }
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ map.put("batch_num",inventory.getBatchNum());
|
|
|
+ map.put("reagent_code",inventory.getReagentCode());
|
|
|
+ map.put("position",inventory.getPosition());
|
|
|
+ List<Map<String, Object>> inv = storeService.queryInventoryByRuKu(map);
|
|
|
+ if(Collections.isNotEmpty(inv)){
|
|
|
+ if (inv.get(0).get("").equals("否")) {
|
|
|
+ throw new Exception("编码为:"+inventory.getReagentCode()+"的试剂耗材正在盘点,请等待盘点完成后再操作!");
|
|
|
}
|
|
|
+ inventory.setId(inv.get(0).get("id_").toString());
|
|
|
+ calcQuantity(inventory, Long.parseLong(inv.get(0).get("quantity").toString()),service.getPolicy());
|
|
|
+ }else {
|
|
|
+ // 新库存
|
|
|
+ inventory.setStatus("入库");
|
|
|
+ inventory.setEnable("是");
|
|
|
+ inventory.setStatus("未验证");
|
|
|
}
|
|
|
+
|
|
|
ReagentConsumablesInventory domain = reagentConsumablesInventoryRepository.newInstance(inventory);
|
|
|
domain.save();
|
|
|
|
|
|
ReagentConsumablesTransaction transaction = reagentConsumablesTransactionRepository.newInstance();
|
|
|
+ item.getTransaction().setInventoryId(inventory.getId());
|
|
|
+ item.getTransaction().setFlow("not");
|
|
|
transaction.setData(item.getTransaction());
|
|
|
transaction.save();
|
|
|
-
|
|
|
}
|
|
|
- service.updateStatus(id);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ public void issue(String id, String type, String status) throws Exception {
|
|
|
+ status = StrUtil.str(status);
|
|
|
+ if ("return".equals(type)) {
|
|
|
+ returnStock(id, status,"退货");
|
|
|
+ } else if ("scrap".equals(type)) {
|
|
|
+ scrap(id, status,"报废");
|
|
|
+ } else if ("withdraw".equals(type)) {
|
|
|
+ decrementStock(id, status,"领用");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void scrap(String id, String status,String type) throws Exception {
|
|
|
+ logger.warn("com.lc.ibps.components.reagent.service.InventoryService.scrap()--->id={},status={},type={}", id, status, type);
|
|
|
+ handleOperation(status, type, storeService.queryBfRecord(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void returnStock(String id, String status,String type) throws Exception {
|
|
|
+ logger.warn("com.lc.ibps.components.reagent.service.InventoryService.returnStock()--->id={},status={},type={}", id, status, type);
|
|
|
+ handleOperation(status, type, storeService.queryThRecord(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void decrementStock(String id, String status,String type) throws Exception {
|
|
|
+ logger.warn("com.lc.ibps.components.reagent.service.InventoryService.decrementStock()--->id={},status={},type={}", id, status, type);
|
|
|
+ handleOperation(status, type, storeService.queryLingYongRecord(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void handleOperation(String status, String operationType, List<Map<String, Object>> records) throws Exception {
|
|
|
+ if (records == null || records.isEmpty()) {
|
|
|
+ throw new Exception("未查询到对应的" + operationType + "记录,操作失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> ids = records.stream().map(record -> record.get("id_").toString()).collect(Collectors.toList());
|
|
|
+ returnService.updateBillAndInventory(String.join(",",ids),operationType);
|
|
|
+
|
|
|
+ for (Map<String, Object> record : records) {
|
|
|
+ record.put("type", operationType);
|
|
|
+ record.put("status", status);
|
|
|
+ String name1 = StrUtil.str(record.get("ming_cheng_"));
|
|
|
+ String name2 = StrUtil.str(record.get("shi_ji_ming_cheng"));
|
|
|
+ String reagentName = !name1.isEmpty() ? name1 : !name2.isEmpty() ? name2 : "";
|
|
|
+ record.put("ming_cheng_", reagentName);
|
|
|
+ String batchNum = StrUtil.str(record.get("pi_hao_"));
|
|
|
+ String reagentCode = StrUtil.str(record.get("bian_ma_"));
|
|
|
+ String position = operationType.equals("领用") ? StrUtil.str(record.get("cang_ku_id_")) : "";
|
|
|
+ String kcId = StrUtil.str(record.get("inventory_id"));
|
|
|
+
|
|
|
+ List<Map<String, Object>> inventoryList;
|
|
|
+ if (BeanUtils.isNotEmpty(kcId)) {
|
|
|
+ inventoryList = storeService.queryInventoryById(kcId);
|
|
|
+ }else if (operationType.equals("领用")) {
|
|
|
+ Map<String, Object> queryParams = new HashMap<>();
|
|
|
+ queryParams.put("batch_num", batchNum);
|
|
|
+ queryParams.put("reagent_code", reagentCode);
|
|
|
+ queryParams.put("position", position);
|
|
|
+ inventoryList = storeService.queryInventoryByLingYong(queryParams);
|
|
|
+ } else {
|
|
|
+ inventoryList = storeService.queryInventoryByPhAndCode(batchNum, reagentCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ checkEnable(inventoryList, reagentName);
|
|
|
+
|
|
|
+ if (inventoryList.size() == 1) {
|
|
|
+ handleSingleInventory(inventoryList.get(0), record);
|
|
|
+ } else {
|
|
|
+ handleMultipleInventories(inventoryList, record);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleSingleInventory(Map<String, Object> inventory, Map<String, Object> record) throws Exception {
|
|
|
+ long quantity = Long.parseLong(record.get("shu_liang_").toString());
|
|
|
+ long currentQuantity = StrUtil.parseLongSafe(inventory.get("quantity"));
|
|
|
+ long currentWithhold = StrUtil.parseLongSafe(inventory.get("withhold"));
|
|
|
+ long available = currentQuantity - currentWithhold;
|
|
|
+
|
|
|
+ if (available < quantity) {
|
|
|
+ throw new Exception(record.get("ming_cheng_") + " 可用量不足!可用量:" + available + ",需要:" + quantity);
|
|
|
+ }
|
|
|
+
|
|
|
+ String inventoryId = StrUtil.str(inventory.get("id_"));
|
|
|
+ String status = StrUtil.str(record.get("status"));
|
|
|
+ if ("start".equals(status)) {
|
|
|
+ linYongService.updateWithhold(inventoryId, currentWithhold + quantity);
|
|
|
+ } else if ("end".equals(status)) {
|
|
|
+ linYongService.updateWithholdAndInventory(inventoryId, currentQuantity - quantity, currentWithhold - quantity);
|
|
|
+ } else {
|
|
|
+ linYongService.updateInventory(inventoryId, currentQuantity - quantity);
|
|
|
+ }
|
|
|
+ record.put("inventory_id", inventoryId);
|
|
|
+ record.put("quantity", quantity);
|
|
|
+ record.put("flow", status.isEmpty()?"not":status);
|
|
|
+ record.put("position", StrUtil.str(inventory.get("position")));
|
|
|
+ addTrans(record);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleMultipleInventories(List<Map<String, Object>> inventoryList, Map<String, Object> record) throws Exception {
|
|
|
+ long quantity = Long.parseLong(record.get("shu_liang_").toString());
|
|
|
+ long totalAvailable = inventoryList.stream()
|
|
|
+ .mapToLong(inv -> {
|
|
|
+ long qty = StrUtil.parseLongSafe(inv.get("quantity"));
|
|
|
+ long wh = StrUtil.parseLongSafe(inv.get("withhold"));
|
|
|
+ return qty - wh;
|
|
|
+ })
|
|
|
+ .sum();
|
|
|
+
|
|
|
+ if (totalAvailable < quantity) {
|
|
|
+ throw new Exception(record.get("ming_cheng_") + " 总可用量不足!总可用量:" + totalAvailable + ",需要:" + quantity);
|
|
|
+ }
|
|
|
+
|
|
|
+ long remainingQuantity = quantity;
|
|
|
+ String status = StrUtil.str(record.get("status"));
|
|
|
+ for (Map<String, Object> inventory : inventoryList) {
|
|
|
+ if (remainingQuantity <= 0) break;
|
|
|
+
|
|
|
+ String inventoryId = StrUtil.str(inventory.get("id_"));
|
|
|
+ long currentQuantity = StrUtil.parseLongSafe(inventory.get("quantity"));
|
|
|
+ long currentWithhold = StrUtil.parseLongSafe(inventory.get("withhold"));
|
|
|
+ long available = currentQuantity - currentWithhold;
|
|
|
+
|
|
|
+ if (available <= 0) continue;
|
|
|
+
|
|
|
+ long actualDeduction;
|
|
|
+ if ("start".equals(status)) {
|
|
|
+ actualDeduction = Math.min(available, remainingQuantity);
|
|
|
+ linYongService.updateWithhold(inventoryId, currentWithhold + actualDeduction);
|
|
|
+ } else if ("end".equals(status)) {
|
|
|
+ actualDeduction = Math.min(available, remainingQuantity);
|
|
|
+ linYongService.updateWithholdAndInventory(inventoryId, currentQuantity - actualDeduction, currentWithhold - actualDeduction);
|
|
|
+ } else {
|
|
|
+ actualDeduction = Math.min(available, remainingQuantity);
|
|
|
+ linYongService.updateInventory(inventoryId, currentQuantity - actualDeduction);
|
|
|
+ }
|
|
|
+
|
|
|
+ remainingQuantity -= actualDeduction;
|
|
|
+
|
|
|
+ record.put("inventory_id", inventoryId);
|
|
|
+ record.put("quantity", actualDeduction);
|
|
|
+ record.put("flow", status.isEmpty()?"not":status);
|
|
|
+ record.put("position", StrUtil.str(inventory.get("position")));
|
|
|
+ addTrans(record);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkEnable(List<Map<String, Object>> inventoryList, String reagentName) {
|
|
|
+ for (Map<String, Object> inventory : inventoryList) {
|
|
|
+ if ("否".equals(inventory.get("enable_"))) {
|
|
|
+ throw new RuntimeException(reagentName + " 正在盘点,请等待完成!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addTrans(Map<String, Object> map) {
|
|
|
+ ReagentConsumablesTransactionPo tran = new ReagentConsumablesTransactionPo();
|
|
|
+ tran.setReagentCode(StrUtil.str(map.get("bian_ma_")));
|
|
|
+ tran.setQuantity(Long.parseLong(StrUtil.str(map.get("quantity"))));
|
|
|
+ String value1 = StrUtil.str(map.get("you_xiao_qi_"));
|
|
|
+ String value2 = StrUtil.str(map.get("you_xiao_qi_zhi_"));
|
|
|
+ String date = !value1.isEmpty() ? value1 : !value2.isEmpty() ? value2 : "";
|
|
|
+ tran.setExpDate(date);
|
|
|
+ tran.setPosition(StrUtil.str(map.get("position")));
|
|
|
+ tran.setStatus(StrUtil.str(map.get("type")));
|
|
|
+ tran.setType(Long.valueOf("-1"));
|
|
|
+ tran.setBoId(StrUtil.str(map.get("id_")));
|
|
|
+ tran.setDiDian(StrUtil.str(map.get("di_dian_")));
|
|
|
+ tran.setBatchNum(StrUtil.str(map.get("pi_hao_")));
|
|
|
+ tran.setInventoryId(StrUtil.str(map.get("inventory_id")));
|
|
|
+ tran.setFlow(StrUtil.str(map.get("flow")));
|
|
|
+
|
|
|
+ ReagentConsumablesTransaction transaction = reagentConsumablesTransactionRepository.newInstance();
|
|
|
+ transaction.setData(tran);
|
|
|
+ transaction.save();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void updateStock(StockDto stockDto) throws Exception {
|
|
|
+ logger.warn("com.lc.ibps.components.reagent.service.InventoryService.updateStock()--->stockDto={}", stockDto);
|
|
|
+ if (Collections.isEmpty(stockDto.getKcList())){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (KcDto kcDto : stockDto.getKcList()){
|
|
|
+ ReagentConsumablesInventoryPo inventory = reagentConsumablesInventoryRepository.get(kcDto.getId());
|
|
|
+ if (BeanUtils.isEmpty(inventory)){
|
|
|
+ throw new Exception("未查询到对应库存,请核对后再修改!");
|
|
|
+ }
|
|
|
+ if ("both".equals(stockDto.getType()) && "否".equals(inventory.getEnable())) {
|
|
|
+ throw new RuntimeException("试剂耗材编码为:" +inventory.getReagentCode() + " 正在盘点,请等待完成!");
|
|
|
+ }
|
|
|
+ if (BeanUtils.isNotEmpty(inventory.getWithhold()) && kcDto.getQuantity() < inventory.getWithhold()){
|
|
|
+ throw new Exception("修改失败!当前库存的预扣量为:"+inventory.getWithhold()+",修改后的库存不能小于预扣量!");
|
|
|
+ }
|
|
|
+ if ("both".equals(stockDto.getType())){
|
|
|
+ linYongService.updateInventoryAndStatus(kcDto.getId(),kcDto.getQuantity());
|
|
|
+ }else {
|
|
|
+ linYongService.updateInventory(kcDto.getId(),kcDto.getQuantity());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private static void calcQuantity( ReagentConsumablesInventoryPo inventory, Long inventoryQuantity,OperationService.Policy policy) {
|
|
|
switch (policy) {
|
|
|
case plus:
|