jiazx0107@163.com
2023-06-08 08cea428e888b6fdd2a3c3783e87e4f828385783
igds-inout/src/main/java/com/ld/igds/timer/InoutScheduled.java
@@ -23,9 +23,7 @@
import java.util.Map;
/**
 * @Desc: 出入库定时任务。
 * 1、通知单完成量统计
 * 2、出入库库存管理统计
 * @Desc: 出入库定时任务。 1、通知单完成量统计 2、出入库库存管理统计
 * @author: Andy
 * @update-time: 2022/11/22
 */
@@ -33,187 +31,181 @@
@Component(InoutScheduled.BEAN_ID)
public class InoutScheduled {
    public static final String BEAN_ID = "basic.inoutScheduled";
   public static final String BEAN_ID = "basic.inoutScheduled";
    @Resource
    private InoutCommonService inoutCommonService;
   @Resource
   private InoutCommonService inoutCommonService;
    @Resource
    private InoutService inoutService;
   @Resource
   private InoutService inoutService;
    @Resource
    private CoreCommonService commonService;
   @Resource
   private CoreCommonService commonService;
   /**
    * 每天晚上11点进行更新未完成出入库通知单的完成量
    */
   @Scheduled(cron = "0 0 23 * * ?")
   public void scheduled1() {
    /**
     * 每天晚上11点进行更新未完成出入库通知单的完成量
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void scheduled1() {
      List<DefaultCompany> listCompany = commonService.getCompanyList();
      if (null == listCompany)
         return;
        List<DefaultCompany> listCompany = commonService.getCompanyList();
        if (null == listCompany) return;
      // 获取3天内的出入库记录,如果有记录则执行,如果没有,则无需执行
      Date start = DateUtil.getNewByDay(null, -3);
      InoutParam param;
      List<InoutData> listRecord;
      for (DefaultCompany company : listCompany) {
         param = new InoutParam();
         param.setStart(start);
         param.setCompanyId(company.getId());
         listRecord = inoutService.listRecordData(param);
        //获取3天内的出入库记录,如果有记录则执行,如果没有,则无需执行
        Date start = DateUtil.getNewByDay(null, -3);
        InoutParam param;
        List<InoutData> listRecord;
        for (DefaultCompany company : listCompany) {
            param = new InoutParam();
            param.setStart(start);
            param.setCompanyId(company.getId());
            listRecord = inoutService.listRecordData(param);
         noticeCompleteExe(company.getId(), listRecord);
            noticeCompleteExe(company.getId(), listRecord);
         sumDepotStoreExe(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();
    /**
     * basic.inoutScheduled#sumDepotStoreExeByHand
     * <p>
     * <p>
     * 手动执行出入库库存统计入口
     *
     * @param start
     * @return
     */
    @Expose
    public String sumDepotStoreExeByHand(Date start) {
      start = DateUtil.getCurZero(start);
        if (null == start) start = new Date();
      // 获取3天内的出入库记录,如果有记录则执行,如果没有,则无需执行
      start = DateUtil.getNewByDay(null, -3);
      String companyId = ContextUtil.getCompanyId();
        start = DateUtil.getCurZero(start);
      InoutParam param = new InoutParam();
      param.setStart(start);
      param.setCompanyId(companyId);
        //获取3天内的出入库记录,如果有记录则执行,如果没有,则无需执行
        start = DateUtil.getNewByDay(null, -3);
        String companyId = ContextUtil.getCompanyId();
      List<InoutData> listRecord = inoutService.listRecordData(param);
        InoutParam param = new InoutParam();
        param.setStart(start);
        param.setCompanyId(companyId);
      sumDepotStoreExe(companyId, listRecord);
        List<InoutData> listRecord = inoutService.listRecordData(param);
      return "SUCCESS";
   }
        sumDepotStoreExe(companyId, listRecord);
   /**
    * 同步仓库库存,根据近期出入库记录,如果没有记录则表示无出入库操作,取消执行
    *
    * @param companyId
    * @param listRecord
    */
   private void sumDepotStoreExe(String companyId, List<InoutData> listRecord) {
        return "SUCCESS";
    }
      if (null == listRecord || listRecord.isEmpty()) {
         log.info("-----------系统自动同步库存,近期无出入库记录,取消同步仓库库存……{}", companyId);
         return;
      }
    /**
     * 同步仓库库存,根据近期出入库记录,如果没有记录则表示无出入库操作,取消执行
     *
     * @param companyId
     * @param listRecord
     */
    private void sumDepotStoreExe(String companyId, List<InoutData> listRecord) {
      // 获取有出入库记录的仓库
      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;
        if (null == listRecord || listRecord.isEmpty()) {
            log.info("-----------系统自动同步库存,近期无出入库记录,取消同步仓库库存……{}", companyId);
            return;
        }
         map.put(key, data);
        //获取有出入库记录的仓库
        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;
         // 根据最后一车进行汇总统计,开始时间是仓库库存最后一个时间截止到当前
         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())) {
            map.put(key, data);
            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());
            //根据最后一车进行汇总统计,开始时间是仓库库存最后一个时间截止到当前
            lastStore = commonService.getLastDepotStore(data.getDepotId());
            if (null == lastStore) {
                log.error("--------出入库定时任务-----没有获取到仓库最后库存信息,取消自动统计,请核对业务逻辑--仓库-{}", data.getDepotId());
                continue;
            }
            // 出入库重量合计
            sumRecordWeight = inoutService.sumRecordWeight(param);
            // 新增一条库存记录
            lastStore.setRemark("系统定时生成记录");
            lastStore.setId(ContextUtil.getUUID());
            lastStore.setUpdateUser(null);
            lastStore.setUpdateDate(new Date());
            // lastStore.setCreateDate(new Date());
            //只有出入库状态的仓库才进行统计,其他状态表示出入库作业完成,不在执行统计
            if (DepotStatus.STATUS_2.getCode().equals(lastStore.getDepotStatus()) ||
                    DepotStatus.STATUS_4.getCode().equals(lastStore.getDepotStatus())) {
            if (InoutConstant.TYPE_IN.equals(data.getType())) {
               lastStore.setStorageReal(lastStore.getStorageReal()
                     + sumRecordWeight);
            }
                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());
            if (InoutConstant.TYPE_OUT.equals(data.getType())) {
               lastStore.setStorageReal(lastStore.getStorageReal()
                     - sumRecordWeight);
            }
                //出入库重量合计
                sumRecordWeight = inoutService.sumRecordWeight(param);
            commonService.addDepotStore(lastStore, true);
         }
                //新增一条库存记录
                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);
                }
   /**
    * 同步通知单完成量,根据近期出入库记录,如果无近期出入库记录,则表示无出入库作业,取消执行
    *
    * @param companyId
    * @param listRecord
    */
   public void noticeCompleteExe(String companyId, List<InoutData> listRecord) {
      log.info("----------系统定时更新出入库通知单完成量----------");
                if (InoutConstant.TYPE_OUT.equals(data.getType())) {
                    lastStore.setStorageReal(lastStore.getStorageReal() - sumRecordWeight);
                }
      if (null == listRecord || listRecord.isEmpty()) {
         log.info("-----------系统自动同步库存,近期无出入库记录,取消同步通知单完成量……{}", companyId);
         return;
      }
      //TODO 待优化
                commonService.addDepotStore(lastStore, true);
            }
      // 获取所有未完成状态的出库通知单
      List<InoutNoticeOut> noticeOutList = inoutCommonService.getUnComNoticeOut(companyId);
        }
    }
      // 获取所有未完成状态的入库通知单
      List<InoutNoticeIn> noticeInList = inoutCommonService.getUnComNoticeIn(companyId);
      String deptId  = noticeInList.get(0).getDeptId();
      // 更新入库通知单完成量
      inoutCommonService.updateSumNoticeIn(deptId,
            noticeInList);
    /**
     * 同步通知单完成量,根据近期出入库记录,如果无近期出入库记录,则表示无出入库作业,取消执行
     *
     * @param companyId
     * @param listRecord
     */
    public void noticeCompleteExe(String companyId, List<InoutData> listRecord) {
        log.info("----------系统定时更新出入库通知单完成量----------");
        if (null == listRecord || listRecord.isEmpty()) {
            log.info("-----------系统自动同步库存,近期无出入库记录,取消同步通知单完成量……{}", companyId);
            return;
        }
        List<InoutSysConf> inoutSysConfList = inoutCommonService.getCacheInoutSysConf(companyId);
        //不配置说明没有启动出入库
        if (null == inoutSysConfList || inoutSysConfList.isEmpty()) {
            return;
        }
        //获取所有未完成状态的出库通知单
        List<InoutNoticeOut> noticeOutList = inoutCommonService.getUnComNoticeOut(companyId);
        //获取所有未完成状态的入库通知单
        List<InoutNoticeIn> noticeInList = inoutCommonService.getUnComNoticeIn(companyId);
        for (InoutSysConf inoutSysConf : inoutSysConfList) {
            //  判断入库通知单是否启用
            //更新入库通知单完成量
            inoutCommonService.updateSumNoticeIn(inoutSysConf.getDeptId(), noticeInList);
            //  判断出库通知单是否启用
            //更新出库通知单完成量
            inoutCommonService.updateSumNoticeOut(inoutSysConf.getDeptId(), noticeOutList);
        }
    }
      // 更新出库通知单完成量
      inoutCommonService.updateSumNoticeOut(deptId,noticeOutList);
   }
}