zouyu
2023-11-15 c9db22f6ccb6a6d66e38dec09807e88b160f29e8
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
package com.chinaztt.mes.plan.util;
 
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.chinaztt.mes.plan.entity.CustomerOrder;
import com.chinaztt.mes.plan.entity.CustomerOrderForJointStockCompany;
import com.chinaztt.mes.plan.mapper.CustomerOrderForJointStockCompanyMapper;
import com.chinaztt.mes.plan.mapper.CustomerOrderMapper;
import com.chinaztt.ztt.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author ZTT
 * 获取附件
 */
@Service
@AllArgsConstructor
public class CustomerOrderUtil {
 
    private CustomerOrderClientConfigure customerOrderClientConfigure;
    private CustomerOrderMapper customerOrderMapper;
    private CustomerOrderForJointStockCompanyMapper customerOrderForJointStockCompanyMapper;
 
    /**
     * OTC 获取token
     *
     * @return
     */
    public String getToken() {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("username", customerOrderClientConfigure.getOtcUsername());
            map.put("password", customerOrderClientConfigure.getOtcPassword());
            map.put("scope", "server");
            map.put("grant_type", "password");
            String result = HttpRequest.post(customerOrderClientConfigure.getOtcHost() + customerOrderClientConfigure.getOtcTokenUrl())
                    .contentType("application/x-www-form-urlencoded")
                    .header("Authorization", "Basic cGlnOnBpZw==").form(map).execute().body();
            JSONObject jsonObject = JSONObject.parseObject(result);
            return jsonObject.getString("access_token");
        } catch (Exception e) {
            throw new RuntimeException("token获取异常");
        }
    }
 
    /**
     * OTC 附件获取
     *
     * @param orderNo
     */
    public R getOtcCustomerOrderFileList(String orderNo) {
        try {
            String token = getToken();
            String encode = URLEncoder.encode(orderNo);
            System.out.println("url编码--------------------------"+encode);
            JSONObject result = JSONObject.parseObject(HttpRequest.get(customerOrderClientConfigure.getOtcHost() + customerOrderClientConfigure.getOtcFileUrl() + orderNo)
                    .header("Authorization", "Bearer " + token).execute().body());
            if (result.getInteger("code") == 0) {
                JSONArray jsonArray = result.getJSONArray("data");
                return R.ok(jsonArray);
            } else {
                return R.failed("获取失败," + result.getString("msg"));
            }
        } catch (Exception e) {
            return R.failed("获取异常:"+ e.getMessage());
        }
    }
 
    public CustomerOrder updateCustomerOrderLine(CustomerOrder customerOrder) {
        if (StringUtils.isBlank(customerOrder.getOtcLineNo())) {
            throw new RuntimeException("缺少OTC行号");
        }
        String token = getToken();
        Map map = new HashMap();
        map.put("orderNo", customerOrder.getCustomerOrderNo());
        map.put("lineNo", customerOrder.getOtcLineNo());
        map.put("selectTime", "1899-12-31 23:59:59");
        String str = HttpRequest.get(customerOrderClientConfigure.getOtcHost() + customerOrderClientConfigure.getOtcCustomerOrderUrl())
                .contentType("application/json")
                .header("Authorization", "Bearer " + token).form(map).execute().body();
        JSONObject result = JSONObject.parseObject(str);
        if (result.getInteger("code") != 0) {
            throw new RuntimeException(customerOrder.getCustomerOrderNo() + "  OTC合同行同步失败:错误编号" + result.getInteger("code"));
        }
        JSONArray jsonArray = result.getJSONArray("data");
        if (jsonArray.size() != 1) {
            throw new RuntimeException(customerOrder.getCustomerOrderNo() + "  OTC行项号 " + customerOrder.getOtcLineNo() + "无对应数据或对应多条数据");
        }
        JSONObject customerOrderJSONObject = JSONObject.parseObject(jsonArray.get(0).toString());
        Integer version = customerOrderJSONObject.getInteger("version");
        customerOrder.setVersion(version);
        customerOrderMapper.updateById(customerOrder);
        return customerOrder;
    }
 
    public CustomerOrderForJointStockCompany updateCustomerOrderLineForJointStockCompany(CustomerOrderForJointStockCompany customerOrder) {
        if (StringUtils.isBlank(customerOrder.getOtcLineNo())) {
            throw new RuntimeException("缺少OTC行号");
        }
        String token = getToken();
        Map map = new HashMap();
        map.put("orderNo", customerOrder.getCustomerOrderNo());
        map.put("lineNo", customerOrder.getOtcLineNo());
        map.put("selectTime", "1899-12-31 23:59:59");
        String str = HttpRequest.get(customerOrderClientConfigure.getOtcHost() + customerOrderClientConfigure.getOtcCustomerOrderUrl())
                .contentType("application/json")
                .header("Authorization", "Bearer " + token).form(map).execute().body();
        JSONObject result = JSONObject.parseObject(str);
        if (result.getInteger("code") != 0) {
            throw new RuntimeException(customerOrder.getCustomerOrderNo() + "  OTC合同行同步失败:错误编号" + result.getInteger("code"));
        }
        JSONArray jsonArray = result.getJSONArray("data");
        if (jsonArray.size() != 1) {
            throw new RuntimeException(customerOrder.getCustomerOrderNo() + "  OTC行项号 " + customerOrder.getOtcLineNo() + "无对应数据或对应多条数据");
        }
        JSONObject customerOrderJSONObject = JSONObject.parseObject(jsonArray.get(0).toString());
        Integer version = customerOrderJSONObject.getInteger("version");
        customerOrder.setVersion(version);
        customerOrderForJointStockCompanyMapper.updateById(customerOrder);
        return customerOrder;
    }
 
    /**
     * OTC 附件下载
     *
     * @param id
     * @param response
     */
    public void otcDownLoadFiles(Long id, HttpServletResponse response) {
        InputStream is = null;
        BufferedInputStream bis = null;
        try {
            String token = getToken();
            String url = customerOrderClientConfigure.getOtcHost() + customerOrderClientConfigure.getOtcFileDownLoadUrl() + "/" + id;
            is = HttpRequest.get(url)
                    .contentType("application/json")
                    .header("Authorization", "Bearer " + token).execute().bodyStream();
            bis = new BufferedInputStream(is);
            OutputStream out = response.getOutputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = bis.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}