YYC
2025-09-22 ee96821fc7990857ad6477690a5758f8c0ebaa66
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.fzzy.async.fzzy61.impl;
 
import com.fzzy.api.Constant;
import com.fzzy.api.entity.Api1202;
import com.fzzy.api.entity.Api1203;
import com.fzzy.api.entity.ApiLog;
import com.fzzy.api.entity.GbCheckItem;
import com.fzzy.api.service.ApiTriggerService;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.api.utils.DateUtil;
import com.fzzy.api.view.repository.Api1202Rep;
import com.fzzy.api.view.repository.Api1203Rep;
import com.fzzy.api.view.repository.ApiLogRep;
import com.fzzy.api.view.repository.GbCheckItemRep;
import com.fzzy.async.fzzy61.entity.Fz61CheckItem;
import com.fzzy.async.fzzy61.entity.Fz61InoutRecord;
import com.fzzy.async.fzzy61.entity.Fz61InoutRecordItem;
import com.fzzy.async.fzzy61.repository.Fzzy61Sync1202Rep;
import com.fzzy.async.fzzy61.repository.Fzzy61Sync1203Rep;
import com.fzzy.async.fzzy61.repository.Fzzy61SyncInoutRecordItemRep;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 粮食入库检验信息
 */
@Slf4j
@Component
public class Fzzy61Sync1203 {
 
    /**
     * 入库类型
     */
    private final static String INOUT_TYPE_IN = "IN";
 
    @Autowired
    private Fzzy61Sync1202Rep fzzySync1202Rep;
    @Autowired
    private Fzzy61SyncInoutRecordItemRep fzzy61SyncInoutRecordItemRep;
    @Autowired
    private Fzzy61Sync1203Rep fzzySync1203Rep;
    @Autowired
    private ApiTriggerService apiTriggerService;
    @Autowired
    private Api1202Rep api1202Rep;
    @Autowired
    private Api1203Rep api1203Rep;
    @Autowired
    private GbCheckItemRep gbCheckItemRep;
    @Autowired
    private ApiLogRep apiLogRep;
 
    /**
     * 粮食入库检验信息
     *
     * @param deptId
     * @param start
     * @param end
     */
    public void syncData(String kqdm, String deptId, Date start, Date end) {
 
        log.info("-------------1203入库检验接口数据开始同步------------------");
 
        //同步数据,只记录失败的信息
        ApiLog apiLog = new ApiLog();
        apiLog.setType(ApiLog.TYPE_SYNC);
        apiLog.setKqdm(deptId);
        apiLog.setUploadTime(new Date());
        apiLog.setInteId(Constant.API_CODE_1203);
        apiLog.setStatus(99);
        apiLog.setId(ContextUtil.getUUID());
        try {
            //获取粮食入库记录信息
            List<Fz61InoutRecord> list = fzzySync1202Rep.listInoutRecord(deptId, INOUT_TYPE_IN, start, end);
 
            if (null == list || list.isEmpty()) {
                log.info("-------------没有获取到粮食入库信息------------------");
                return;
            }
 
            Api1203 apiData;
            List<Fz61CheckItem> fz61CheckItems;
            List<GbCheckItem> gbCheckList;
            String jyxm;
            String jyz;
            String zkj;
            String zkl;
            List<Api1203> api1203List;
            List<Api1202> api1102List;
            List<Fz61InoutRecordItem> listInoutRecordItem;
            List<String> listIds;
            for (Fz61InoutRecord sysData : list) {
                listIds = new ArrayList<>();
                //判断是否为船运
                if (StringUtils.isNotEmpty(sysData.getTransType()) && sysData.getTransType().equals("3")) {
                    listInoutRecordItem = fzzy61SyncInoutRecordItemRep.findDataByRecordId(sysData.getId());
                    if (null != listInoutRecordItem && listInoutRecordItem.size() > 0) {
                        int idNum = Integer.valueOf(sysData.getId().substring(12)) * 100 + 10001;
 
                        for (Fz61InoutRecordItem fz61InoutRecordItem : listInoutRecordItem) {
                            if ("DEL".equals(fz61InoutRecordItem.getRecordStatus())) {
                                continue;
                            }
                            listIds.add(Constant.INOUT_TYPE_14 + sysData.getId().substring(4, 10) + String.valueOf(idNum).substring(1));
                            idNum++;
                        }
                    } else {
                        listIds.add(Constant.INOUT_TYPE_14 + sysData.getId().substring(4));
                    }
                } else {
                    listIds.add(Constant.INOUT_TYPE_14 + sysData.getId().substring(4));
                }
 
                //获取入库质检信息
                fz61CheckItems = fzzySync1203Rep.listInoutCheckItem(sysData.getCheckId(), deptId.substring(0, 4));
                if (null == fz61CheckItems || fz61CheckItems.isEmpty()) {
                    log.info("-------------没有获取到当前入库质检信息--------------");
                    continue;
                }
 
                for (String listId : listIds) {
                    //查询粮食入库信息,若为空则不进行同步上传
                    api1102List = api1202Rep.getDataById(listId);
                    if (null == api1102List || api1102List.isEmpty()) {
                        continue;
                    }
 
                    apiData = new Api1203();
 
                    apiData.setRkjydh(api1102List.get(0).getRkywdh());
                    //货位代码
                    apiData.setHwdm(api1102List.get(0).getHwdm());
                    apiData.setRkywdh(api1102List.get(0).getRkywdh());
                    if (null == sysData.getFullWeightTime()) {
                        sysData.setFullWeightTime(DateUtils.addMinutes(sysData.getRegisterTime(), 5));
                    }
 
                    //判断业务时间与单据号是否匹配,若不匹配,则更改业务时间为单据号所在的时间
                    String time = apiData.getRkjydh().substring(2, 8);
                    if (sysData.getCheckTime() != null) {
                        String isCheckTime = DateFormatUtils.format(sysData.getCheckTime(), "yyMMdd");
                        if (!isCheckTime.equals(time)) {
                            int difDay = DateUtil.difDay1(sysData.getRegisterTime(), sysData.getCheckTime());
                            if (difDay == 0) {
                                apiData.setRkjydh("14" + isCheckTime + "01" + api1102List.get(0).getRkywdh().substring(10));
                            } else {
                                apiData.setRkjydh("14" + isCheckTime + String.format("%02d", difDay) + api1102List.get(0).getRkywdh().substring(10));
                            }
                        }
                    }
 
                    //船运使用入库单据号做质检单号
                    if (StringUtils.isNotEmpty(sysData.getTransType()) && sysData.getTransType().equals("3")) {
                        apiData.setRkjydh(api1102List.get(0).getRkywdh());
                    }
 
                    apiData.setQyrxm(sysData.getCheckUser());
                    //扦样方式 2-智能随机
                    apiData.setQyfs("2");
 
                    //检测项,检测值,增扣价及增扣量
                    jyxm = "";
                    jyz = "";
                    zkj = "";
                    zkl = "";
                    for (Fz61CheckItem fz61CheckItem : fz61CheckItems) {
                        gbCheckList = gbCheckItemRep.findByBizCode(fz61CheckItem.getStandardId());
 
                        if (gbCheckList == null || gbCheckList.isEmpty()) {
                            continue;
                        }
                        if (StringUtils.isEmpty(fz61CheckItem.getValue())) {
                            continue;
                        }
                        jyxm += "," + gbCheckList.get(0).getCode();
                        jyz += "," + fz61CheckItem.getValue();
                        zkj += ",0.0";
                        zkl += ",0.0";
                    }
                    if (jyxm.length() > 0) {
                        jyxm = jyxm.substring(1);
                    }
                    if (jyz.length() > 0) {
                        jyz = jyz.substring(1);
                    }
                    apiData.setJyxm(jyxm);
                    apiData.setJyz(jyz);
                    apiData.setZkj(StringUtils.isEmpty(zkj) ? "" : zkj.substring(1));
                    apiData.setZkl(StringUtils.isEmpty(zkl) ? "" : zkl.substring(1));
                    if ("UNPASS".equals(sysData.getCheckStatus())) {
                        apiData.setJyjg("0");
                    } else {
                        apiData.setJyjg("1");
                    }
                    apiData.setJyrxm(sysData.getCheckUser());
                    if (null == sysData.getCheckTime()) {
                        sysData.setCheckTime(DateUtils.addMinutes(sysData.getFullWeightTime(), -5));
                    }
                    if (sysData.getCheckTime().after(sysData.getEmptyWeightTime())) {
                        sysData.setCheckTime(DateUtils.addMinutes(sysData.getFullWeightTime(), -5));
                    }
                    apiData.setJysj(sysData.getCheckTime());
                    if (null == sysData.getSampleTime()) {
                        sysData.setSampleTime(DateUtils.addMinutes(sysData.getCheckTime(), -1));
                    }
                    if (sysData.getSampleTime().after(sysData.getCheckTime())) {
                        sysData.setSampleTime(DateUtils.addMinutes(sysData.getCheckTime(), -1));
                    }
                    apiData.setQysj(sysData.getSampleTime());
 
                    //粮食品种
                    String mappingCode = apiTriggerService.getMappingCode(Constant.TRIGGER_P_LSPZ, sysData.getFoodVariety());
                    apiData.setLspzdm(mappingCode);
 
                    //保管员复核
                    apiData.setBgyfh(apiData.getJyjg());
                    apiData.setSpaqzbsfhg("1");
 
                    //粮食等级
                    mappingCode = apiTriggerService.getMappingCode(Constant.TRIGGER_P_LSDJ, sysData.getFoodLevel());
                    apiData.setLsdd(mappingCode);
 
                    //操作标志及最后更新时间
                    apiData.setZhgxsj(sysData.getCompleteTime());
 
                    //业务id、库区编码、同步时间
                    apiData.setBizId(sysData.getId());
                    apiData.setKqdm(kqdm);
                    apiData.setSyncTime(new Date());
 
                    api1203List = api1203Rep.getDataById(apiData.getRkjydh());
                    if (null == api1203List || api1203List.isEmpty()) {
                        apiData.setCzbz(Constant.CZBZ_I);
                    } else {
                        apiData.setCzbz(api1203List.get(0).getCzbz());
                    }
 
                    api1203Rep.save(apiData);
                    log.info("1203入库检验---同步数据:{}", apiData.toString());
                }
            }
        } catch (Exception e) {
            log.error("---1203入库检验数据同步失败----{}", e.toString());
            apiLog.setResult("同步失败:" + e.getMessage());
            apiLogRep.save(apiLog);
        }
    }
 
    public static void main(String[] args) {
        //判断业务时间与单据号是否匹配,若不匹配,则更改业务时间为单据号所在的时间
        String id = "142508200001";
        String time = id.substring(2, 8);
 
        String isCheckTime = "250821";
        if (!isCheckTime.equals(time)) {
            int difDay = 2;
            String idd = "14" + isCheckTime + difDay + id.substring(9);
            System.out.println(idd);
        }
 
    }
}