CZT
2023-08-16 2e121f643a6776e6063796c104c95647faa000a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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<DefaultCompany> listCompany = commonService.getCompanyList();
        if (null == listCompany)
            return;
 
        // 获取当天的出入库记录,如果有记录则执行,如果没有,则无需执行
        InoutParam param = new InoutParam();
        param.setEnd(new Date());
        param.setStart(DateUtils.addDays(param.getEnd(), -1));
 
        List<InoutData> 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<InoutData> listRecord, InoutParam param) {
 
        if (null == listRecord || listRecord.isEmpty()) {
            log.info("-----------系统自动同步库存,近期无出入库记录,取消同步仓库库存……{}", companyId);
            return;
        }
 
        // 获取有出入库记录的仓库
        Map<String, InoutRecord> 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);
        }
    }
}