CZT
2023-11-29 81f8164227bd019f8fe0233e0bebfe5614bfa644
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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.ApiParam;
import com.fzzy.api.data.PushProtocol;
import com.fzzy.api.service.ApiCommonService;
import com.fzzy.api.service.ApiPushManager;
import com.fzzy.api.service.ApiRemoteService;
import com.fzzy.api.Constant;
import com.fzzy.api.dto.ResponseDto;
import com.fzzy.api.entity.Api1109;
import com.fzzy.api.entity.ApiConfs;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.api.view.repository.Api1109Rep;
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 sun.misc.BASE64Encoder;
 
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 文件信息
 * @author he
 */
@SuppressWarnings("restriction")
@Slf4j
@Component
public class Api1109PR {
 
    @Autowired
    private Api1109Rep api1109Rep;
 
    @Autowired
    private ApiCommonService apiCommonService;
 
    @Autowired
    private ApiPushManager apiPushManager;
 
    /**
     * api1109PR#listAll
     *
     * @return
     */
    @DataProvider
    public void listAll(Page<Api1109> page, ApiParam param) {
        //多参数分页查询
        Pageable pageable = PageRequest.of(page.getPageNo() - 1, page.getPageSize(), Sort.Direction.DESC, Api1109.SORT_PROP);
 
        if (null == param) {
            org.springframework.data.domain.Page<Api1109> japPage = api1109Rep.findAll(pageable);
            page.setEntityCount((int) japPage.getTotalElements());
            page.setEntities(japPage.getContent());
 
            return;
        }
 
        Specification<Api1109> specification = new Specification<Api1109>() {
            private static final long serialVersionUID = 1L;
 
            public Predicate toPredicate(Root<Api1109> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<Predicate>(); //所有的断言
 
                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.<Date>get("qdrq"), ContextUtil.getCurZero(param.getStart()));
                    predicates.add(predicate3);
                }
 
                if (null != param.getEnd()) {
                    Predicate predicate4 = cb.lessThan(root.<Date>get("qdrq"), ContextUtil.getNextZero(param.getEnd()));
                    predicates.add(predicate4);
                }
                return cb.and(predicates.toArray(new Predicate[0]));
            }
        };
 
        log.debug("---------分页参数查询----------{}", param.toString());
        org.springframework.data.domain.Page<Api1109> japPage = api1109Rep.findAll(specification, pageable);
        page.setEntityCount((int) japPage.getTotalElements());
        page.setEntities(japPage.getContent());
    }
 
    /**
     * api1109PR#updateSave
     *
     * @param entity
     */
    @DataResolver
    public void updateSave(Api1109 entity) {
 
        // 手动将doradoEntity对象转换为标准Bean对象
        Api1109 data = new Api1109();
        BeanUtils.copyProperties(entity, data);
        if(StringUtils.isEmpty(data.getId())){
            data.setId(ContextUtil.getUUID());
        }
 
        api1109Rep.save(data);
    }
 
    /**
     * api1109PR#delData
     *
     * @param data
     */
    @Expose
    public String delData(Api1109 data) {
 
        api1109Rep.deleteById(data.getId());
 
        return null;
    }
 
    /**
     *
     * 更新删除标记 api1109PR#delUpdate
     *
     * @param entity
     */
    @Expose
    public String delUpdate(Api1109 entity) {
 
        entity.setCzbz(Constant.CZBZ_D);
 
        Api1109 data = new Api1109();
        BeanUtils.copyProperties(entity, data);
 
        api1109Rep.save(data);
        return null;
    }
 
    /**
     * api1109PR#pushData
     *
     * @param items
     */
    @Expose
    public String pushData(List<Api1109> items) {
 
        //获取配置
        String kqdm = items.get(0).getKqdm();
        ApiConfs apiConf = apiCommonService.getConf(kqdm);
 
        if (null == apiConf) return "系统没有获取到当前库区配置信息,执行失败";
 
        //获取实现接口
        ApiRemoteService apiRemoteService = apiPushManager.getApiRemoteService(apiConf.getPushProtocol());
        if (null == apiRemoteService) return "系统没有当前推送协议配置,执行失败";
 
        return pushDataSingle(items,apiRemoteService,apiConf);
    }
 
    /**
     * 单条数据json推送
     *
     * @param items
     * @param apiRemoteService
     * @param apiConf
     * @return
     */
    private String pushDataSingle(List<Api1109> items, ApiRemoteService apiRemoteService, ApiConfs apiConf) {
        ResponseDto responseDto;
        String result = "";
 
        //封装参数
        ApiParam param = new ApiParam(apiConf, Constant.API_CATEGORY_11, Constant.API_CODE_1109);
        Api1109 d;
        for (Api1109 data : items) {
            d = new Api1109();
            BeanUtils.copyProperties(data, d);
            param.setBizId(data.getBizId());
            responseDto = apiRemoteService.pushData(param, apiConf, d);
 
            result += responseDto.toString();
            if (responseDto.getSuccess() == 0) {
                if (Constant.CZBZ_I.equals(d.getCzbz())) {
                    api1109Rep.updateStatus(data.getId(), Constant.CZBZ_U);
                }
            }
        }
        return result;
    }
 
    /**
     * 多条数据json推送
     *
     * @param items
     * @param apiRemoteService
     * @param apiConf
     * @return
     */
    private String pushDataList(List<Api1109> items, ApiRemoteService apiRemoteService, ApiConfs apiConf) {
 
        //封装参数
        ApiParam param = new ApiParam(apiConf, Constant.API_CATEGORY_11, Constant.API_CODE_1109);
 
        //推送,数据为集合形式
        ResponseDto responseDto = apiRemoteService.pushData(param, apiConf, items);
        if (responseDto.getSuccess() == 0) {
            //推送成功,更新数据上传状态
            for (Api1109 data : items) {
                if (Constant.CZBZ_I.equals(data.getCzbz())) {
                    //更新状态
                    api1109Rep.updateStatus(data.getId(), Constant.CZBZ_U);
                }
            }
        }
        return responseDto.toString();
    }
 
 
    /**
     * imgFile 图片本地存储路径
     */
    public static String getImgFileToBase64(String imgFile) {
        //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        InputStream inputStream = null;
        byte[] buffer = null;
        //读取图片字节数组
        try {
            inputStream = new FileInputStream(imgFile);
            int count = 0;
            while (count == 0) {
                count = inputStream.available();
            }
            buffer = new byte[count];
            inputStream.read(buffer);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        } finally {
            if (inputStream != null) {
                try {
                    // 关闭inputStream流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 对字节数组Base64编码
        //return Base64Utils.encodeToString(buffer);
         return new BASE64Encoder().encode(buffer);
    }
 
    /**
     * 获取图片的二进制流
     * @param imgPath
     * @return
     */
    @SuppressWarnings("resource")
    public String imgToIo(String imgPath){
        //图片转化为二进制
        byte[] imageBytes = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(imgPath));
            imageBytes = new byte[fileInputStream.available()];
            fileInputStream.read(imageBytes);
        } catch (IOException e) {
            System.out.println(e);
            return null;
        }
        return UnicodeByteToStr(imageBytes);
    }
 
    private static String UnicodeByteToStr(byte[] b){
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<b.length;i++) {
            sb.append(String.format("%02x", b[i]));
        }
        return sb.toString();
    }
 
    /**
     * 根据文件路径将文件转为二进制数组
     * @param filePath:文件路径
     * @return
     */
    public static byte[] file2byte(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
 
    /**
     * 将二进制数组转为字符串
     * @param b
     * @return
     */
    public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder();
        String stmp="";
        for (int n=0;n<b.length;n++) {
            stmp=(Integer.toHexString(b[n] & 0XFF));
            if (stmp.length()==1) sb.append("0"+stmp);
            else sb.append(stmp);
        }
        return sb.toString().toUpperCase();
    }
 
 
    public static void main(String[] args) {
        String hex = toHexString(file2byte("E://91511424746940066Y001.jpg"));
        System.out.println(hex);
        saveToImgFile(hex,"E://dd.jpg");
    }
 
 
    public static  void saveToImgFile(String src,String output){
        if(src==null||src.length()==0){
            return;
        }
        try{
            FileOutputStream out = new FileOutputStream(new File(output));
            byte[] bytes = src.getBytes();
            for(int i=0;i<bytes.length;i+=2){
                out.write(charToInt(bytes[i])*16+charToInt(bytes[i+1]));
            }
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    private static int charToInt(byte ch){
        int val = 0;
        if(ch>=0x30&&ch<=0x39){
            val=ch-0x30;
        }else if(ch>=0x41&&ch<=0x46){
            val=ch-0x41+10;
        }
        return val;
    }
}