RequestSTKServiceImpl.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package com.uas.eis.service.Impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.uas.eis.dao.BaseDao;
  4. import com.uas.eis.dao.SqlRowList;
  5. import com.uas.eis.dto.stksto.QueryAccountantProjectDto;
  6. import com.uas.eis.dto.stksto.QueryCashFlowItemsDto;
  7. import com.uas.eis.dto.stksto.QueryTravellingMerchantDto;
  8. import com.uas.eis.dto.stksto.StkCommonDto;
  9. import com.uas.eis.sdk.dto.CustvendDTO;
  10. import com.uas.eis.sdk.dto.SaveOrChangeCustomerDetailDto;
  11. import com.uas.eis.sdk.dto.SaveOrChangeCustomerDto;
  12. import com.uas.eis.service.RequestSTKService;
  13. import com.uas.eis.utils.HuToolUtils;
  14. import com.uas.eis.utils.STKSignUtil;
  15. import com.uas.eis.vo.stkVo.*;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.util.CollectionUtils;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. @Service
  28. @Slf4j
  29. public class RequestSTKServiceImpl implements RequestSTKService {
  30. @Value("${STK.app_id}")
  31. private String STK_APP_ID;
  32. @Value("${STK.app_key}")
  33. private String STK_APP_KEY;
  34. /** 新增修改客商信息接口地址 */
  35. @Value("${STK.save_or_update_url}")
  36. private String SAVE_OR_UPDATE_URL;
  37. /** 自动化查询接口地址 */
  38. @Value("${STK.query_url}")
  39. private String QUERY_URL;
  40. /** 公司组织编码 */
  41. @Value("${STK.s_org_code}")
  42. private String S_ORG_CODE;
  43. /** 新增修改客商信息接口请求编码 */
  44. @Value("${STK.save_or_update_request_code}")
  45. private String SAVE_OR_UPDATE_REQUEST_CODE ;
  46. /** 查询现金流量项目接口编码 */
  47. @Value("${STK.cash_flow_items_request_code}")
  48. private String CASH_FLOW_ITEMS_REQUEST_CODE;
  49. /** 查询会计科目方法编码 */
  50. @Value("${STK.accountant_project_request_code}")
  51. private String ACCOUNTANT_PROJECT_REQUEST_CODE;
  52. @Autowired
  53. private BaseDao baseDao;
  54. /**
  55. * 获取客商相关数据
  56. * */
  57. @Override
  58. public TravellingMerchantVo selectTravellingMerchantList(QueryTravellingMerchantDto dto, String url){
  59. dto.setS_orgcode(S_ORG_CODE);
  60. log.info("请求参数dto,content:"+JSONObject.toJSONString(dto));
  61. //头部请求参数
  62. Map<String,String> headerMap = null;
  63. try {
  64. headerMap = getHeaderMap(dto);
  65. } catch (Exception e) {
  66. log.error("STK加密异常:{}",e.getMessage());
  67. }
  68. //请求数据 (接口地址根据测试环境与正式环境变更)
  69. HttpResultResponse resultResponse = HuToolUtils.post(url,dto,headerMap);
  70. if(!resultResponse.getSuccess()){
  71. log.error(resultResponse.getMessage());
  72. return null;
  73. }
  74. log.info("STK,获取客商信息返回结果:{}",JSONObject.toJSONString(resultResponse));
  75. if(StringUtils.isBlank(resultResponse.getBody())){
  76. return null;
  77. }
  78. //System.out.println("返回结果:"+listArray.size());
  79. return JSONObject.parseObject(resultResponse.getBody(),TravellingMerchantVo.class); //返回结果
  80. }
  81. @Override
  82. public SaveOrChangeCustomerVo saveOrChangeCustomer(List<SaveOrChangeCustomerDetailDto> sourceDataList) {
  83. if(CollectionUtils.isEmpty(sourceDataList)){
  84. log.info("深投控,新增修改客商信息数据为空");
  85. return new SaveOrChangeCustomerVo();
  86. }
  87. //新增修改客商信息对象集合
  88. //List<SaveOrChangeCustomerDetailDto> detailDtos = doSaveOrUpdateCustomerParam(sourceDataList);
  89. //调用深投控新增修改客商信息
  90. SaveOrChangeCustomerDto changeCustomerDto = new SaveOrChangeCustomerDto();
  91. changeCustomerDto.setParam(sourceDataList);
  92. HttpResultResponse resultResponse = HuToolUtils.post(SAVE_OR_UPDATE_URL+SAVE_OR_UPDATE_REQUEST_CODE,changeCustomerDto);
  93. log.info("深投控,新增修改客商信息,返回结果数据{}",JSONObject.toJSONString(resultResponse));
  94. if(!resultResponse.getSuccess()){
  95. log.info("深投控,新增修改客商信息数据异常:{}",resultResponse.getMessage());
  96. }
  97. return JSONObject.parseObject(resultResponse.getBody(), SaveOrChangeCustomerVo.class);
  98. }
  99. @Override
  100. public CashFlowItemsVo getCashFlowItems(StkCommonDto dto) {
  101. /*StkCommonDto stkCommonDto = new StkCommonDto();
  102. stkCommonDto.setParam(dto);*/
  103. //头部请求参数
  104. Map<String,String> headerMap = null;
  105. try {
  106. headerMap = getHeaderMap(dto);
  107. } catch (Exception e) {
  108. log.error("STK加密异常:{}",e.getMessage());
  109. }
  110. HttpResultResponse resultResponse = HuToolUtils.post("https://192.168.2.179/api/oapigw/api/oapisvc/automicApi/89edb885a8cf4412b870233fc89fb381",dto,headerMap);
  111. log.info("深投控,现金流项目,返回结果数据{}",JSONObject.toJSONString(resultResponse));
  112. if(!resultResponse.getSuccess()){
  113. log.info("深投控,现金流项目数据异常:{}",resultResponse.getMessage());
  114. }
  115. return JSONObject.parseObject(resultResponse.getBody(), CashFlowItemsVo.class);
  116. }
  117. @Override
  118. public AccountantProjectVo getAccountantProject(QueryAccountantProjectDto dto) {
  119. StkCommonDto stkCommonDto = new StkCommonDto();
  120. stkCommonDto.setParam(dto);
  121. //头部请求参数
  122. Map<String,String> headerMap = null;
  123. try {
  124. headerMap = getHeaderMap(dto);
  125. } catch (Exception e) {
  126. log.error("STK加密异常:{}",e.getMessage());
  127. }
  128. HttpResultResponse resultResponse = HuToolUtils.post(QUERY_URL+ACCOUNTANT_PROJECT_REQUEST_CODE,stkCommonDto,headerMap);
  129. log.info("深投控,会计项目,返回结果数据{}",JSONObject.toJSONString(resultResponse));
  130. if(!resultResponse.getSuccess()){
  131. log.info("深投控,会计项目数据异常:{}",resultResponse.getMessage());
  132. }
  133. return JSONObject.parseObject(resultResponse.getBody(),AccountantProjectVo.class);
  134. }
  135. /**
  136. * 新增修改客商信息请求参数封装
  137. * @Param 数据库查询数据
  138. * */
  139. private List<SaveOrChangeCustomerDetailDto> doSaveOrUpdateCustomerParam(List<Object> sourceDataList){
  140. if(CollectionUtils.isEmpty(sourceDataList)){
  141. return new ArrayList<>();
  142. }
  143. List<SaveOrChangeCustomerDetailDto> detailDtos = new ArrayList<>();
  144. sourceDataList.stream().forEach(p->{
  145. SaveOrChangeCustomerDetailDto detailDto = new SaveOrChangeCustomerDetailDto();
  146. // todo 数据处理, 业务数据需要将 List<Object>中的Object换成数据库查询出的对象类型
  147. detailDtos.add(detailDto);
  148. });
  149. return detailDtos;
  150. }
  151. @Override
  152. public List<CustvendDTO> queryList() {
  153. List<CustvendDTO> custvendDTO = new ArrayList<CustvendDTO>();
  154. SqlRowList rs = baseDao.queryForRowSet("select * from STK_CUSTVEND_CS");
  155. while (rs.next()) {
  156. CustvendDTO custvend = baseDao.getJdbcTemplate().queryForObject("select * from STK_CUSTVEND_CS where id = " + rs.getInt("id"), new BeanPropertyRowMapper<CustvendDTO>(CustvendDTO.class));
  157. custvendDTO.add(custvend);
  158. }
  159. return custvendDTO;
  160. }
  161. /**
  162. * POST请求头部参数封装
  163. * */
  164. public Map<String,String> getHeaderMap(Object dto) throws Exception {
  165. //获取签名
  166. String sign = getSign(dto);
  167. log.info("STK,加密后的签名:"+sign);
  168. //时间戳
  169. String timestamp = Long.toString(System.currentTimeMillis());
  170. Map<String,String> headerMap = new HashMap<>();
  171. headerMap.put("appid",STK_APP_ID);
  172. headerMap.put("timestamp",timestamp);
  173. headerMap.put("sign",sign);
  174. headerMap.put("Content-Type","application/json");
  175. return headerMap;
  176. }
  177. /**
  178. * 获取深投控加密参数
  179. * */
  180. public String getSign(Object dto) throws Exception {
  181. //时间戳
  182. String timestamp = Long.toString(System.currentTimeMillis());
  183. String content = JSONObject.toJSONString(dto);
  184. //加签方法第一个参数
  185. StringBuilder signBuilder = new StringBuilder("appid").append("=").append(STK_APP_ID).append("&")
  186. .append(content).append("&")
  187. .append("timestamp").append("=").append(timestamp).append("&")
  188. .append("key").append("=").append(STK_APP_KEY);
  189. log.info("STK,加密前参数:"+signBuilder.toString());
  190. //加签
  191. return STKSignUtil.HMACSHA256(signBuilder.toString(),STK_APP_KEY);
  192. }
  193. }