package com.ld.igds.timer; import com.bstek.bdf2.core.model.DefaultCompany; 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.models.DepotStore; import com.ld.igds.models.InoutRecord; import com.ld.igds.util.ContextUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.time.DateUtils; 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条库存数据,并更新库存 * @author: czt * @update-time: 2023/08/14 */ @Slf4j @Component(DepotStoreScheduled.BEAN_ID) public class DepotStoreScheduled { public static final String BEAN_ID = "inout.depotStoreScheduled"; @Resource private InoutService inoutService; @Resource private CoreCommonService commonService; /** * 每天晚上11点25分更新库存数据 */ @Scheduled(cron = "0 10 23 * * ?") public void scheduled1() { List listCompany = commonService.getCompanyList(); if (null == listCompany) return; // 获取当天的出入库记录,如果有记录则执行,如果没有,则无需执行 InoutParam param = new InoutParam(); param.setEnd(new Date()); param.setStart(DateUtils.addDays(param.getEnd(), -1)); List listRecord; for (DefaultCompany company : listCompany) { param.setCompanyId(company.getId()); listRecord = inoutService.listRecordData(param); //根据出入库信息,同步库存信息 sumDepotStoreExe(company.getId(), listRecord, param); } } /** * 同步仓库库存,根据近期出入库记录,如果没有记录则表示无出入库操作,取消执行 * * @param companyId * @param listRecord */ private void sumDepotStoreExe(String companyId, List listRecord, InoutParam param) { if (null == listRecord || listRecord.isEmpty()) { log.info("-----------系统自动同步库存,近期无出入库记录,取消同步仓库库存……{}", companyId); return; } // 获取有出入库记录的仓库 Map map = new HashMap<>(); String key; DepotStore lastStore; double sumRecordWeight = 0.0; 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; } 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("系统管理员"); lastStore.setUpdateDate(new Date()); lastStore.setCreateDate(new Date()); if(InoutConstant.TYPE_IN.equals(data.getType())){ lastStore.setStorageReal(lastStore.getStorageReal() + sumRecordWeight); //设置仓库状态 lastStore.setDepotStatus(DepotStatus.STATUS_2.getCode()); //设置入库时间 if(null == lastStore.getStoreDate()){ lastStore.setStoreDate(data.getCompleteTime()); } } if(InoutConstant.TYPE_OUT.equals(data.getType())){ lastStore.setStorageReal(lastStore.getStorageReal() - sumRecordWeight); lastStore.setDepotStatus(DepotStatus.STATUS_4.getCode()); //设置入库时间 if(null == lastStore.getStoreDate()){ lastStore.setStoreDate(data.getCompleteTime()); } //若出库后数量为负数,则设置库存数为0.0 if(lastStore.getStorageReal() < 0){ lastStore.setStorageReal(0.0); } } lastStore.setStorageSettle(lastStore.getStorageReal()); commonService.addDepotStore(lastStore, true); } } }