czt
2024-05-23 bd72ddc43f7d89d0d19e48ea98221fa9b1890e4b
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
package com.fzzy.async.fzzy40.impl;
 
import com.fzzy.api.Constant;
import com.fzzy.api.entity.Api1105;
import com.fzzy.api.entity.Api1303;
import com.fzzy.api.entity.ApiLog;
import com.fzzy.api.service.ApiCommonService;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.api.view.repository.Api1303Rep;
import com.fzzy.api.view.repository.ApiLogRep;
import com.fzzy.async.fzzy40.entity.Fz40Pest;
import com.fzzy.async.fzzy40.repository.Fzzy40Sync1303Rep;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.List;
 
 
/**
 * 虫害检测数据同步
 *
 * @author chen
 * @date 2022-09-08 09:51
 */
@Slf4j
@Component
public class Fzzy40Sync1303 {
 
 
    @Autowired
    private Fzzy40Sync1303Rep fzzy40Sync1303Rep;
    @Autowired
    private ApiCommonService commonService;
    @Autowired
    private Api1303Rep api1303Rep;
    @Autowired
    private ApiLogRep apiLogRep;
 
    /**
     * 同步并封装保存虫害检测数据
     *
     * @param deptId 系统对应库区编码
     * @param start  起始时间
     * @param end    截止时间
     */
    public void syncData(String kqdm, String deptId, Date start, Date end) {
 
        log.info("-------------1303接口数据开始同步------------------");
 
        //同步数据,只记录失败的信息
        ApiLog apiLog = new ApiLog();
        apiLog.setType(ApiLog.TYPE_SYNC);
        apiLog.setKqdm(deptId);
        apiLog.setUploadTime(new Date());
        apiLog.setInteId(Constant.API_CODE_1303);
        apiLog.setStatus(99);
        apiLog.setId(ContextUtil.getUUID());
        try {
 
            List<Fz40Pest> list = fzzy40Sync1303Rep.findPestByReceiveDate(start, end);
            if (null == list || list.isEmpty()) {
                return;
            }
 
            Date syncTime = new Date();
            Api1303 api1303;
            Api1105 api1105;
            List<Api1303> api1303List;
            for (Fz40Pest fz40Pest : list) {
                //获取货位信息
                api1105 = commonService.getApi1105Cache(fz40Pest.getDepotId());
                if (null == api1105) {
                    continue;
                }
 
                api1303 = new Api1303();
                api1303.setHcjcdh(api1105.getHwdm() + fz40Pest.getBatchId());
                api1303.setJcsj(fz40Pest.getReceiveDate());
                api1303.setHwdm(api1105.getHwdm());
 
                //检查害虫方法,0-远程、1-人工、2-其他
                api1303.setJchcff("0");
                //发生部位,坐标填写:x,y,z
                api1303.setFsbw("粮面");//非必填字段
 
                //害虫种类。多个用#隔开#TODO >> 待优化调整
                api1303.setHczl("21212");
 
                //虫口密度值集合
                String ckmdzjh = this.geeCkmdzjh(fz40Pest.getPestMax(), fz40Pest.getPoints());
                api1303.setCkmdzjh(ckmdzjh);
 
                //虫粮等级判定,531-基本无虫粮、532-一般虫粮、533严重虫粮、534危害虫粮
                api1303.setCldjpd("531");
                if (fz40Pest.getPestMax() > 5) {
                    api1303.setCldjpd("532");
                }
                if (fz40Pest.getPestMax() > 30) {
                    api1303.setCldjpd("533");
                }
 
                api1303.setZhgxsj(new Date());
 
                api1303.setBizId(fz40Pest.getBatchId());
                api1303.setKqdm(api1105.getKqdm());
                api1303.setSyncTime(new Date());
                api1303List = api1303Rep.getDataByHcjcdh(api1303.getHcjcdh());
                if (null == api1303List || api1303List.isEmpty()) {
                    api1303.setCzbz(Constant.CZBZ_I);
                } else {
                    api1303.setCzbz(api1303List.get(0).getCzbz());
                }
                api1303Rep.save(api1303);
            }
 
        } catch (Exception e) {
            log.error("---同步失败----{}", e);
            apiLog.setResult("同步失败:" + e.getMessage());
            apiLogRep.save(apiLog);
        }
 
    }
 
    /**
     * 头/kg,指粮食害虫值集合,与
     * 害虫种类顺序对应,用“|”分
     * 隔,多个取样点用英文半角“,”
     * 分隔,按照取样点示意图标识顺
     * 序排列。
     *
     * @param pestMax
     * @param points
     * @return
     */
    private String geeCkmdzjh(int pestMax, String points) {
 
        if (StringUtils.isEmpty(points)) return "0";
 
        String[] attr = points.split(";");
 
        String result = "";
        for (String str : attr) {
            result += str.split(",")[1] + ",";
        }
        return result.substring(0, result.length() - 1);
    }
 
 
}