CZT
2023-08-16 2e121f643a6776e6063796c104c95647faa000a8
igds-inout/src/main/java/com/ld/igds/timer/InoutScheduled.java
@@ -1,26 +1,19 @@
package com.ld.igds.timer;
import com.bstek.bdf2.core.model.DefaultCompany;
import com.bstek.dorado.annotation.Expose;
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.constant.DepotStatus;
import com.ld.igds.inout.InoutConstant;
import com.ld.igds.inout.dto.InoutData;
import com.ld.igds.inout.dto.InoutParam;
import com.ld.igds.inout.service.InoutService;
import com.ld.igds.m.service.InoutCommonService;
import com.ld.igds.models.*;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc: 出入库定时任务。 1、通知单完成量统计 2、出入库库存管理统计
@@ -31,7 +24,7 @@
@Component(InoutScheduled.BEAN_ID)
public class InoutScheduled {
   public static final String BEAN_ID = "basic.inoutScheduled";
   public static final String BEAN_ID = "inout.inoutScheduled";
   @Resource
   private InoutCommonService inoutCommonService;
@@ -62,116 +55,8 @@
         param.setCompanyId(company.getId());
         listRecord = inoutService.listRecordData(param);
         //根据出入库信息同步通知单完成量
         noticeCompleteExe(company.getId(), listRecord);
         sumDepotStoreExe(company.getId(), listRecord);
      }
   }
   /**
    * basic.inoutScheduled#sumDepotStoreExeByHand
    * <p>
    * <p>
    * 手动执行出入库库存统计入口
    *
    * @param start
    * @return
    */
   @Expose
   public String sumDepotStoreExeByHand(Date start) {
      if (null == start)
         start = new Date();
      start = DateUtil.getCurZero(start);
      // 获取3天内的出入库记录,如果有记录则执行,如果没有,则无需执行
      start = DateUtil.getNewByDay(null, -3);
      String companyId = ContextUtil.getCompanyId();
      InoutParam param = new InoutParam();
      param.setStart(start);
      param.setCompanyId(companyId);
      List<InoutData> listRecord = inoutService.listRecordData(param);
      sumDepotStoreExe(companyId, listRecord);
      return "SUCCESS";
   }
   /**
    * 同步仓库库存,根据近期出入库记录,如果没有记录则表示无出入库操作,取消执行
    *
    * @param companyId
    * @param listRecord
    */
   private void sumDepotStoreExe(String companyId, List<InoutData> listRecord) {
      if (null == listRecord || listRecord.isEmpty()) {
         log.info("-----------系统自动同步库存,近期无出入库记录,取消同步仓库库存……{}", companyId);
         return;
      }
      // 获取有出入库记录的仓库
      Map<String, InoutRecord> map = new HashMap<>();
      String key;
      DepotStore lastStore;
      double sumRecordWeight = 0.0;
      InoutParam param;
      for (InoutData data : listRecord) {
         key = data.getDepotId() + "_" + data.getType();
         if (null != map.get(key))
            continue;
         map.put(key, data);
         // 根据最后一车进行汇总统计,开始时间是仓库库存最后一个时间截止到当前
         lastStore = commonService.getLastDepotStore(data.getDepotId());
         if (null == lastStore) {
            log.error(
                  "--------出入库定时任务-----没有获取到仓库最后库存信息,取消自动统计,请核对业务逻辑--仓库-{}",
                  data.getDepotId());
            continue;
         }
         // 只有出入库状态的仓库才进行统计,其他状态表示出入库作业完成,不在执行统计
         if (DepotStatus.STATUS_2.getCode().equals(
               lastStore.getDepotStatus())
               || DepotStatus.STATUS_4.getCode().equals(
                     lastStore.getDepotStatus())) {
            param = new InoutParam();
            // param.setStart(lastStore.getCreateDate());
            param.setEnd(new Date());
            param.setDeptId(data.getDeptId());
            param.setDepotId(data.getDepotId());
            param.setCompanyId(data.getCompanyId());
            param.setType(data.getType());
            // 出入库重量合计
            sumRecordWeight = inoutService.sumRecordWeight(param);
            // 新增一条库存记录
            lastStore.setRemark("系统定时生成记录");
            lastStore.setId(ContextUtil.getUUID());
            lastStore.setUpdateUser(null);
            lastStore.setUpdateDate(new Date());
            // lastStore.setCreateDate(new Date());
            if (InoutConstant.TYPE_IN.equals(data.getType())) {
               lastStore.setStorageReal(lastStore.getStorageReal()
                     + sumRecordWeight);
            }
            if (InoutConstant.TYPE_OUT.equals(data.getType())) {
               lastStore.setStorageReal(lastStore.getStorageReal()
                     - sumRecordWeight);
            }
            commonService.addDepotStore(lastStore, true);
         }
      }
   }