mrzhssss
2022-05-06 e67503fd8cc820ff14fa3398a9a7e072d4964444
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
package zy.cloud.wms.common.service.task;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Arith;
import com.core.common.Cools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import zy.cloud.wms.common.model.MatnrDto;
import zy.cloud.wms.common.model.MatnrDto0;
import zy.cloud.wms.common.service.erp.ErpScheduler;
import zy.cloud.wms.common.service.erp.Result;
import zy.cloud.wms.common.service.erp.entity.GetBasisResult;
import zy.cloud.wms.manager.entity.DiffVal;
import zy.cloud.wms.manager.service.DiffValService;
import zy.cloud.wms.manager.utils.HttpHandler;
 
import java.util.*;
 
/**
 * Created by vincent on 2021/11/23
 */
@Slf4j
@Component
public class DiffValScheduler {
 
    @Autowired
    private DiffValService diffValService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    @Transactional
    @Scheduled(cron = "0 */10 * * * ?")
    public void run() {
        List<MatnrDto0> erpQtyList = null;
        Date now = new Date();
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("PUserCode", "N");
            param.put("KUserCode", "N");
            String response = new HttpHandler.Builder()
                    .setUri("http://123.60.34.127:6220/api")
                    .setPath(ErpScheduler.GET_STOCK)
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            if (!Cools.isEmpty(response)) {
                log.info(response);
                Result result = JSON.parseObject(response, Result.class);
                if (result.getCode() != 1) {
                    return;
                }
                erpQtyList = JSON.parseArray(result.getData(), MatnrDto0.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (Cools.isEmpty(erpQtyList)) {
            log.error("获取EPR库存失败");
            return;
        }
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select sum(a.anfme) as count, a.matnr from (\n" +
                "\tselect\n" +
                "\tsum(anfme) as anfme,\n" +
                "\tmatnr\n" +
                "\tfrom asr_loc_detl\n" +
                "\tgroup by matnr\n" +
                "\tunion all\n" +
                "\tselect\n" +
                "\tsum(anfme) as anfme,\n" +
                "\tmatnr\n" +
                "\tfrom man_loc_detl\n" +
                "\tgroup by matnr\n" +
                ") as a\n" +
                "where 1=1\n" +
                "group by a.matnr");
        List<MatnrDto> stockQtyList = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            stockQtyList.add(Cools.conver(map, MatnrDto.class));
        }
 
        for (MatnrDto item : stockQtyList) {
            DiffVal diffVal = diffValService.selectById(item.getMatnr());
            if (diffVal == null) {
                diffVal = new DiffVal(
                        item.getMatnr(),    // 物料编号[非空]
                        item.getCount(),    // WMS库存
                    0.0D,    // ERP库存
                        item.getCount(),    // 差异值
                        now,    // 更新时间
                    null    // 备注
                );
                diffValService.insert(diffVal);
            } else {
                diffVal.setStockQty(item.getCount());
                double erpQty = diffVal.getErpQty();
                diffVal.setVal(Arith.subtract(2, diffVal.getStockQty(), erpQty));
                diffVal.setUpdateTime(now);
                diffValService.updateById(diffVal);
            }
        }
 
        for (MatnrDto0 item : erpQtyList) {
            DiffVal diffVal = diffValService.selectById(item.getPUserCode());
            if (diffVal == null) {
                diffVal = new DiffVal(
                        item.getPUserCode(),    // 物料编号[非空]
                        0.0D,    // WMS库存
                        item.getQty(),    // ERP库存
                        -item.getQty(),    // 差异值
                        now,    // 更新时间
                        null    // 备注
                );
                diffValService.insert(diffVal);
            } else {
                diffVal.setErpQty(item.getQty());
                double stockQty = diffVal.getStockQty();
                diffVal.setVal(Arith.subtract(2, stockQty, diffVal.getErpQty()));
                diffVal.setUpdateTime(now);
                diffValService.updateById(diffVal);
            }
        }
 
 
    }
 
}