CZT
2023-12-12 a82631013be641db3979e11013b2cf6e937a3e7b
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.fzzy.async.fzzy40.impl;
 
import com.fzzy.api.Constant;
import com.fzzy.api.entity.*;
import com.fzzy.api.service.ApiCommonService;
import com.fzzy.api.service.ApiTriggerService;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.api.view.repository.Api1102Rep;
import com.fzzy.api.view.repository.Api1208Rep;
import com.fzzy.api.view.repository.ApiLogRep;
import com.fzzy.async.fzzy40.entity.Fz40DepotStore;
import com.fzzy.async.fzzy40.repository.Fzzy40Sync1208Rep;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.List;
 
/**
 * 粮食库存
 */
@Slf4j
@Component
public class Fzzy40Sync1208 {
 
    @Autowired
    private Fzzy40Sync1208Rep fzzySync1208Rep;
    @Autowired
    private ApiTriggerService apiTriggerService;
    @Autowired
    private Api1208Rep api1208Rep;
    @Autowired
    private ApiCommonService commonService;
    @Autowired
    private Api1102Rep api1102Rep;
    @Autowired
    private ApiLogRep apiLogRep;
 
    /**
     * 同步粮食库存
     *
     * @param deptId
     * @param start
     * @param end
     */
    public void syncData(String kqdm, String deptId, Date start, Date end) {
 
        log.info("-------------1208接口数据开始同步------------------");
 
        //同步数据,只记录失败的信息
        ApiLog apiLog = new ApiLog();
        apiLog.setType(ApiLog.TYPE_SYNC);
        apiLog.setKqdm(deptId);
        apiLog.setUploadTime(new Date());
        apiLog.setInteId(Constant.API_CODE_1208);
        apiLog.setStatus(99);
        apiLog.setId(ContextUtil.getUUID());
        try {
            List<Fz40DepotStore> list = fzzySync1208Rep.listDepot(deptId, start, end);
 
            if (null == list || list.isEmpty()) {
 
                log.info("-------------没有获取到粮食库存信息------------------");
 
                return;
            }
 
            //根据库区代码获取单位代码
            Api1102 api1102 = api1102Rep.findById(kqdm).get();
 
            Api1208 apiData;
            Api1105 api1105;
            List<Api1208> api1208List;
            for (Fz40DepotStore fz40DepotStore : list) {
 
                //获取货位信息
                api1105 = commonService.getApi1105Cache(fz40DepotStore.getDepotId());
                if (null == api1105) {
                    continue;
                }
 
                apiData = new Api1208();
                apiData.setId(fz40DepotStore.getId());
                apiData.setHwdm(api1105.getHwdm());
 
                //粮食品种
                String mappingCode = apiTriggerService.getMappingCode(Constant.TRIGGER_P_LSPZ, fz40DepotStore.getFoodVariety().substring(0, 3));
                apiData.setLspzdm(mappingCode);
 
                //粮食性质
                mappingCode = apiTriggerService.getMappingCode(Constant.TRIGGER_P_LSXZ, fz40DepotStore.getFoodType());
                apiData.setLsxzdm(mappingCode);
 
                //粮食等级
                mappingCode = apiTriggerService.getMappingCode(Constant.TRIGGER_P_LSDJ, fz40DepotStore.getFoodLevel());
                apiData.setLsdjdm(mappingCode);
 
                apiData.setShnd(fz40DepotStore.getFoodYear());
 
                //产地代码
                mappingCode = commonService.getFoodLocationIdFromCache(fz40DepotStore.getFoodLocation());
                if (StringUtils.isEmpty(mappingCode)) {
                    mappingCode = apiData.getHwdm().substring(2, 8);
                }
                apiData.setCd(mappingCode);
 
                apiData.setBgy(StringUtils.isEmpty(api1105.getBgy()) ? "保管员" : api1105.getBgy());
                //粮权归属单位
                if (StringUtils.isEmpty(fz40DepotStore.getLqgsdwdm())) {
                    apiData.setLqgsdwdm(api1102.getDwdm());
                } else {
                    apiData.setLqgsdwdm(fz40DepotStore.getLqgsdwdm());
                }
                //粮权行政区划代码
                apiData.setLqxzqhdm(api1102.getXzqhdm());
                //管理方式-默认直储
                if (StringUtils.isEmpty(fz40DepotStore.getManageType())) {
                    apiData.setGlfs("01");
                } else {
                    apiData.setGlfs(fz40DepotStore.getManageType());
                }
 
                //收储地点-默认库内
                apiData.setScdd("1");
                //储粮方式
                apiData.setClfs(fz40DepotStore.getStoreType());
 
                //货位状态
                mappingCode = apiTriggerService.getMappingCode(Constant.TRIGGER_P_HWZT, fz40DepotStore.getDepotStatus());
                apiData.setHwzt(mappingCode);
                if (null == fz40DepotStore.getStoreDate()) {
                    fz40DepotStore.setStoreDate(new Date());
                }
                //入仓时间
                apiData.setRcsj(fz40DepotStore.getStoreDate());
                //封仓日期
                if ("3".equals(apiData.getHwzt()) || "4".equals(apiData.getHwzt())) {
                    if(null == fz40DepotStore.getFullDate()){
                        fz40DepotStore.setFullDate(DateUtils.addDays(fz40DepotStore.getStoreDate(), 30));
                    }
                    apiData.setFcrq(fz40DepotStore.getFullDate());
                }
 
                if ("1".equals(apiData.getHwzt())) {
                    //出仓完成时间
                    apiData.setCcwcsj(DateUtils.addDays(fz40DepotStore.getStoreDate(), 5));
                    //清仓时间
                    apiData.setQcsj(DateUtils.addDays(apiData.getCcwcsj(), 1));
                }
                //国别
                apiData.setGb("156");
                apiData.setSjsl(fz40DepotStore.getStorageReal());
                apiData.setJjsl(fz40DepotStore.getStorageReal());
                apiData.setSjzlxg(6);
                apiData.setLdtj(10000);
 
                apiData.setZhgxsj(fz40DepotStore.getUpdateDate());
 
                //业务id、库区编码、同步时间
                apiData.setBizId(fz40DepotStore.getId());
                apiData.setKqdm(kqdm);
                apiData.setSyncTime(new Date());
 
                //库存每次新增
                api1208List = api1208Rep.getDataById(apiData.getId());
                if (null == api1208List || api1208List.isEmpty()) {
                    apiData.setCzbz(Constant.CZBZ_I);
                } else {
                    apiData.setCzbz(api1208List.get(0).getCzbz());
                }
 
                //保存数据
                api1208Rep.save(apiData);
            }
        } catch (Exception e) {
            log.error("---同步失败----{}", e);
            apiLog.setResult("同步失败:" + e.getMessage());
            apiLogRep.save(apiLog);
        }
    }
}