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 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(); } } }