CZT
2023-10-20 07ff6686da8e9922a6f5a4f276e69d045e12a044
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
package com.fzzy.api.view.pr;
 
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Page;
import com.fzzy.api.data.PushProtocol;
import com.fzzy.api.entity.ApiConfs;
import com.fzzy.api.service.ApiCommonService;
import com.fzzy.api.service.ApiPushManager;
import com.fzzy.api.service.ApiRemoteService;
import com.fzzy.push.gb2022.GBApiRemoteService;
import com.fzzy.api.Constant;
import com.fzzy.api.data.ApiParam;
import com.fzzy.api.dto.ResponseDto;
import com.fzzy.api.entity.Api1202;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.api.view.repository.Api1202Rep;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
 
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 粮食入库信息
 *
 * @author Andy
 */
@Slf4j
@Component
public class Api1202PR {
 
    @Autowired
    private Api1202Rep apiRepository;
    @Autowired
    private ApiCommonService apiCommonService;
    @Autowired
    private ApiPushManager apiPushManager;
 
    /**
     * api1202PR#pageList
     *
     * @return
     */
    @DataProvider
    public void pageList(Page<Api1202> page, ApiParam param) {
 
        //多参数分页查询
        Pageable pageable = PageRequest.of(page.getPageNo() - 1, page.getPageSize(), Sort.Direction.DESC, Api1202.SORT_PROP);
 
        if (null == param) {
            org.springframework.data.domain.Page<Api1202> japPage = apiRepository.findAll(pageable);
            page.setEntityCount((int) japPage.getTotalElements());
            page.setEntities(japPage.getContent());
 
            return;
        }
 
        Specification<Api1202> specification = new Specification<Api1202>() {
            private static final long serialVersionUID = 1L;
 
            public Predicate toPredicate(Root<Api1202> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>(); //所有的断言
 
                if (StringUtils.isNotBlank(param.getKqdm())) {
                    Predicate predicate1 = cb.equal(root.get("kqdm"), param.getKqdm());
                    predicates.add(predicate1);
                }
 
                if (StringUtils.isNotBlank(param.getCzbz())) {
                    Predicate predicate2 = cb.equal(root.get("czbz"), param.getCzbz());
                    predicates.add(predicate2);
                }
                if (null != param.getStart()) {
                    Predicate predicate3 = cb.greaterThan(root.get("ywrq"), ContextUtil.getCurZero(param.getStart()));
                    predicates.add(predicate3);
                }
 
                if (null != param.getEnd()) {
                    Predicate predicate4 = cb.lessThan(root.get("ywrq"), ContextUtil.getNextZero(param.getEnd()));
                    predicates.add(predicate4);
                }
                return cb.and(predicates.toArray(new Predicate[0]));
            }
        };
 
        log.debug("---------分页参数查询----------{}", param.toString());
        org.springframework.data.domain.Page<Api1202> japPage = apiRepository.findAll(specification, pageable);
        page.setEntityCount((int) japPage.getTotalElements());
        page.setEntities(japPage.getContent());
    }
 
 
    /**
     * api1202PR#updateSave
     *
     * @param entity
     */
    @DataResolver
    public void updateSave(Api1202 entity) {
 
        if (StringUtils.isEmpty(entity.getCzbz())) {
            entity.setCzbz(Constant.CZBZ_I);
        }
 
        //手动将doradoEntity对象转换为标准Bean对象
        Api1202 data = new Api1202();
        BeanUtils.copyProperties(entity, data);
 
        apiRepository.save(data);
    }
 
 
    /**
     * api1202PR#delData
     *
     * @param data
     */
    @Expose
    public String delData(Api1202 data) {
 
        apiRepository.deleteById(data.getRkywdh());
 
        return null;
    }
 
    /**
     * 更新删除标记 api1202PR#delUpdate
     *
     * @param entity
     */
    @Expose
    public String delUpdate(Api1202 entity) {
 
        entity.setCzbz(Constant.CZBZ_D);
 
        Api1202 data = new Api1202();
        BeanUtils.copyProperties(entity, data);
 
        apiRepository.save(data);
 
        return null;
    }
 
    /**
     * api1202PR#pushData
     *
     * @param items
     */
    @Expose
    public String pushData(List<Api1202> items) {
 
        String kqdm = items.get(0).getKqdm();
        ApiConfs apiConf = apiCommonService.getConf(kqdm);
 
        if (null == apiConf) return "系统没有获取到当前库区配置信息,执行失败";
 
        //获取实现接口
        ApiRemoteService apiService = apiPushManager.getApiRemoteService(apiConf.getPushProtocol());
        if (null == apiService) return "系统没有当前推送协议配置,执行失败";
 
        return pushDataSingle(items, apiService, apiConf);
    }
 
    /**
     * 单条数据json推送
     *
     * @param items
     * @param apiRemoteService
     * @param apiConf
     * @return
     */
    private String pushDataSingle(List<Api1202> items, ApiRemoteService apiRemoteService, ApiConfs apiConf) {
        ResponseDto responseDto;
        String result = "";
 
        //封装参数
        ApiParam param = new ApiParam(apiConf, Constant.API_CATEGORY_12, Constant.API_CODE_1202);
        Api1202 d;
        for (Api1202 data : items) {
            d = new Api1202();
            BeanUtils.copyProperties(data, d);
            responseDto = apiRemoteService.pushData(param, apiConf, d);
            result += responseDto.toString();
            if (responseDto.getSuccess() == 0) {
                //todo 代表上传成功 ,后期需要保存日志
                if ("i".equals(d.getCzbz())) {
                    apiRepository.updateStatus(d.getRkywdh(), Constant.CZBZ_U);
                }
            }
        }
        return result;
    }
 
    /**
     * 多条数据json推送
     *
     * @param items
     * @param apiRemoteService
     * @param apiConf
     * @return
     */
    private String pushDataList(List<Api1202> items, ApiRemoteService apiRemoteService, ApiConfs apiConf) {
 
        //封装参数
        ApiParam param = new ApiParam(apiConf, Constant.API_CATEGORY_12, Constant.API_CODE_1202);
 
        //推送,数据为集合形式
        ResponseDto responseDto = apiRemoteService.pushData(param, apiConf, items);
        if (responseDto.getSuccess() == 0) {
            //推送成功,更新数据上传状态
            for (Api1202 data : items) {
                if (Constant.CZBZ_I.equals(data.getCzbz())) {
                    //更新状态
                    apiRepository.updateStatus(data.getRkywdh(), Constant.CZBZ_U);
                }
            }
        }
        return responseDto.toString();
    }
 
}