RequestSTKServiceImpl.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. package com.uas.eis.service.Impl;
  2. import cn.hutool.http.HttpRequest;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.uas.eis.dao.BaseDao;
  5. import com.uas.eis.dao.SqlRowList;
  6. import com.uas.eis.dto.stksto.QueryAccountantProjectDto;
  7. import com.uas.eis.dto.stksto.QueryCashFlowItemsDto;
  8. import com.uas.eis.dto.stksto.QueryTravellingMerchantDto;
  9. import com.uas.eis.dto.stksto.StkCommonDto;
  10. import com.uas.eis.sdk.dto.CustvendDTO;
  11. import com.uas.eis.sdk.dto.SaveOrChangeCustomerDetailDto;
  12. import com.uas.eis.sdk.dto.SaveOrChangeCustomerDto;
  13. import com.uas.eis.sdk.dto.UpdateCustVendDTO;
  14. import com.uas.eis.service.RequestSTKService;
  15. import com.uas.eis.utils.HuToolUtils;
  16. import com.uas.eis.utils.STKSignUtil;
  17. import com.uas.eis.vo.stkVo.*;
  18. import lombok.Synchronized;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.commons.lang.StringUtils;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.beans.factory.annotation.Value;
  23. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.util.CollectionUtils;
  26. import java.lang.reflect.Field;
  27. import java.lang.reflect.Method;
  28. import java.time.LocalDate;
  29. import java.time.LocalDateTime;
  30. import java.time.LocalTime;
  31. import java.time.format.DateTimeFormatter;
  32. import java.time.temporal.TemporalAdjusters;
  33. import java.util.ArrayList;
  34. import java.util.HashMap;
  35. import java.util.List;
  36. import java.util.Map;
  37. @Service
  38. @Slf4j
  39. public class RequestSTKServiceImpl implements RequestSTKService {
  40. @Value("${STK.app_id}")
  41. private String STK_APP_ID;
  42. @Value("${STK.app_key}")
  43. private String STK_APP_KEY;
  44. /** 新增修改客商信息接口地址 */
  45. @Value("${STK.save_or_update_url}")
  46. private String SAVE_OR_UPDATE_URL;
  47. /**
  48. * 复杂查询接口请求地址
  49. * */
  50. @Value("${STK.complexity_query_url}")
  51. private String COMPLEXITY_QUERY_URL;
  52. /** 自动化查询接口地址 */
  53. @Value("${STK.query_url}")
  54. private String QUERY_URL;
  55. /** 公司组织编码 */
  56. @Value("${STK.s_org_code}")
  57. private String S_ORG_CODE;
  58. /**
  59. * 获取客商信息接口编码
  60. * */
  61. @Value("${STK.get_travelling_merchant}")
  62. private String GET_TRAVELLING_MERCHANT;
  63. /** 新增修改客商信息接口请求编码 */
  64. @Value("${STK.save_or_update_request_code}")
  65. private String SAVE_OR_UPDATE_REQUEST_CODE ;
  66. /** 查询现金流量项目接口编码 */
  67. @Value("${STK.cash_flow_items_request_code}")
  68. private String CASH_FLOW_ITEMS_REQUEST_CODE;
  69. /** 查询会计科目方法编码 */
  70. @Value("${STK.accountant_project_request_code}")
  71. private String ACCOUNTANT_PROJECT_REQUEST_CODE;
  72. @Autowired
  73. private BaseDao baseDao;
  74. /**
  75. * 获取客商相关数据
  76. * */
  77. @Override
  78. public TravellingMerchantVo selectTravellingMerchantList(QueryTravellingMerchantDto dto, String url){
  79. dto.setS_orgcode(S_ORG_CODE);
  80. log.info("请求参数dto,content:"+JSONObject.toJSONString(dto));
  81. //头部请求参数
  82. Map<String,String> headerMap = null;
  83. try {
  84. headerMap = getHeaderMap(dto);
  85. } catch (Exception e) {
  86. log.error("STK加密异常:{}",e.getMessage());
  87. }
  88. //请求数据 (接口地址根据测试环境与正式环境变更)
  89. HttpResultResponse resultResponse = HuToolUtils.post(url,dto,headerMap);
  90. if(!resultResponse.getSuccess()){
  91. log.error(resultResponse.getMessage());
  92. return null;
  93. }
  94. log.info("STK,获取客商信息返回结果:{}",JSONObject.toJSONString(resultResponse));
  95. if(StringUtils.isBlank(resultResponse.getBody())){
  96. return null;
  97. }
  98. //System.out.println("返回结果:"+listArray.size());
  99. return JSONObject.parseObject(resultResponse.getBody(),TravellingMerchantVo.class); //返回结果
  100. }
  101. @Override
  102. public SaveOrChangeCustomerVo saveOrChangeCustomer(List<SaveOrChangeCustomerDetailDto> sourceDataList) {
  103. if(CollectionUtils.isEmpty(sourceDataList)){
  104. log.info("深投控,新增修改客商信息数据为空");
  105. return new SaveOrChangeCustomerVo();
  106. }
  107. //头部请求参数
  108. Map<String,String> headerMap = null;
  109. try {
  110. headerMap = getHeaderMap(sourceDataList);
  111. } catch (Exception e) {
  112. log.error("STK加密异常:{}",e.getMessage());
  113. }
  114. //新增修改客商信息对象集合
  115. //List<SaveOrChangeCustomerDetailDto> detailDtos = doSaveOrUpdateCustomerParam(sourceDataList);
  116. //调用深投控新增修改客商信息
  117. SaveOrChangeCustomerDto changeCustomerDto = new SaveOrChangeCustomerDto();
  118. changeCustomerDto.setParam(sourceDataList);
  119. HttpResultResponse resultResponse = HuToolUtils.post(SAVE_OR_UPDATE_URL+SAVE_OR_UPDATE_REQUEST_CODE,changeCustomerDto,headerMap);
  120. log.info("深投控,新增修改客商信息,返回结果数据{}",JSONObject.toJSONString(resultResponse));
  121. if(!resultResponse.getSuccess()){
  122. log.info("深投控,新增修改客商信息数据异常:{}",resultResponse.getMessage());
  123. }
  124. return JSONObject.parseObject(resultResponse.getBody(), SaveOrChangeCustomerVo.class);
  125. }
  126. @Override
  127. public CashFlowItemsVo getCashFlowItems(StkCommonDto dto) {
  128. /*StkCommonDto stkCommonDto = new StkCommonDto();
  129. stkCommonDto.setParam(dto);*/
  130. //头部请求参数
  131. Map<String,String> headerMap = null;
  132. try {
  133. headerMap = getHeaderMap(dto);
  134. } catch (Exception e) {
  135. log.error("STK加密异常:{}",e.getMessage());
  136. }
  137. HttpResultResponse resultResponse = HuToolUtils.post(QUERY_URL+CASH_FLOW_ITEMS_REQUEST_CODE,dto,headerMap);
  138. log.info("深投控,现金流项目,返回结果数据{}",JSONObject.toJSONString(resultResponse));
  139. if(!resultResponse.getSuccess()){
  140. log.info("深投控,现金流项目数据异常:{}",resultResponse.getMessage());
  141. }
  142. return JSONObject.parseObject(resultResponse.getBody(), CashFlowItemsVo.class);
  143. }
  144. @Override
  145. public AccountantProjectVo getAccountantProject(QueryAccountantProjectDto dto) {
  146. StkCommonDto stkCommonDto = new StkCommonDto();
  147. stkCommonDto.setParam(dto);
  148. //头部请求参数
  149. Map<String,String> headerMap = null;
  150. try {
  151. headerMap = getHeaderMap(dto);
  152. } catch (Exception e) {
  153. log.error("STK加密异常:{}",e.getMessage());
  154. }
  155. HttpResultResponse resultResponse = HuToolUtils.post(QUERY_URL+ACCOUNTANT_PROJECT_REQUEST_CODE,stkCommonDto,headerMap);
  156. log.info("深投控,会计项目,返回结果数据{}",JSONObject.toJSONString(resultResponse));
  157. if(!resultResponse.getSuccess()){
  158. log.info("深投控,会计项目数据异常:{}",resultResponse.getMessage());
  159. }
  160. return JSONObject.parseObject(resultResponse.getBody(),AccountantProjectVo.class);
  161. }
  162. /**
  163. * 新增修改客商信息请求参数封装
  164. * @Param 数据库查询数据
  165. * */
  166. private List<SaveOrChangeCustomerDetailDto> doSaveOrUpdateCustomerParam(List<Object> sourceDataList){
  167. if(CollectionUtils.isEmpty(sourceDataList)){
  168. return new ArrayList<>();
  169. }
  170. List<SaveOrChangeCustomerDetailDto> detailDtos = new ArrayList<>();
  171. sourceDataList.stream().forEach(p->{
  172. SaveOrChangeCustomerDetailDto detailDto = new SaveOrChangeCustomerDetailDto();
  173. // todo 数据处理, 业务数据需要将 List<Object>中的Object换成数据库查询出的对象类型
  174. detailDtos.add(detailDto);
  175. });
  176. return detailDtos;
  177. }
  178. @Override
  179. public HttpResultResponse buildPostParam(Integer page,Integer size,QueryCashFlowItemsDto dtoParam) throws Exception {
  180. //时间戳
  181. String timestamp = Long.toString(System.currentTimeMillis());
  182. //加签方法的重要参数
  183. JSONObject request=new JSONObject(4);
  184. request.put("page",page);
  185. request.put("size",size);
  186. //传入参数 此处案例仅放置了时间,可自行添加
  187. JSONObject param=new JSONObject();
  188. //param.put("UPDATE_TIME_start","2024-05-09 14:24:06");
  189. //param.put("UPDATE_TIME_end","2024-05-09 15:01:28");
  190. //param.put("TASKNO","P2022032200000041");
  191. //param.put("TASKCODE","202409");
  192. request.put("param",dtoParam);
  193. String content = request.toString();
  194. //加签方法第一个参数
  195. StringBuilder signBuilder = new StringBuilder("appid").append("=").append(STK_APP_ID).append("&")
  196. .append(content).append("&")
  197. .append("timestamp").append("=").append(timestamp).append("&")
  198. .append("key").append("=").append(STK_APP_KEY);
  199. //加签
  200. String sign = STKSignUtil.HMACSHA256(signBuilder.toString(),STK_APP_KEY);
  201. log.info("STK,原始签名:"+signBuilder.toString());
  202. log.info("STK,加密签名:"+sign);
  203. Map<String,String> header = new HashMap<>();
  204. header.put("appid",STK_APP_ID);
  205. header.put("timestamp", timestamp);
  206. header.put("sign", sign);
  207. header.put("Content-Type", "application/json");
  208. HttpResultResponse resultResponse = HuToolUtils.post(QUERY_URL+CASH_FLOW_ITEMS_REQUEST_CODE
  209. ,request, header);
  210. log.info("示例返回:{}",JSONObject.toJSONString(resultResponse));
  211. return resultResponse ;
  212. }
  213. @Override
  214. public HttpResultResponse buildKMPostParam(Integer page, Integer size, QueryAccountantProjectDto dto) throws Exception {
  215. //时间戳
  216. String timestamp = Long.toString(System.currentTimeMillis());
  217. //加签方法的重要参数
  218. JSONObject request=new JSONObject(4);
  219. request.put("page",page);
  220. request.put("size",size);
  221. //传入参数 此处案例仅放置了时间,可自行添加
  222. JSONObject param=new JSONObject();
  223. //param.put("UPDATE_TIME_start","2024-05-09 14:24:06");
  224. //param.put("UPDATE_TIME_end","2024-05-09 15:01:28");
  225. //param.put("TASKNO","P2022032200000041");
  226. //param.put("TASKCODE","202409");
  227. request.put("param",dto);
  228. String content = request.toString();
  229. //加签方法第一个参数
  230. StringBuilder signBuilder = new StringBuilder("appid").append("=").append(STK_APP_ID).append("&")
  231. .append(content).append("&")
  232. .append("timestamp").append("=").append(timestamp).append("&")
  233. .append("key").append("=").append(STK_APP_KEY);
  234. //加签
  235. String sign = STKSignUtil.HMACSHA256(signBuilder.toString(),STK_APP_KEY);
  236. log.info("STK,原始签名:"+signBuilder.toString());
  237. log.info("STK,加密签名:"+sign);
  238. Map<String,String> header = new HashMap<>();
  239. header.put("appid",STK_APP_ID);
  240. header.put("timestamp", timestamp);
  241. header.put("sign", sign);
  242. header.put("Content-Type", "application/json");
  243. HttpResultResponse resultResponse = HuToolUtils.post(QUERY_URL+ACCOUNTANT_PROJECT_REQUEST_CODE
  244. ,request, header);
  245. log.info("示例返回:{}",JSONObject.toJSONString(resultResponse));
  246. return resultResponse ;
  247. }
  248. @Override
  249. public SaveOrChangeCustomerDataVo updateKSPostParam(SaveOrChangeCustomerDetailDto dto) throws Exception {
  250. //时间戳
  251. /*String timestamp = Long.toString(System.currentTimeMillis());
  252. //每一条数据在这里构造,根据实际情况修改
  253. JSONObject request=new JSONObject(4);
  254. *//*Field[] fields = dto.getClass().getDeclaredFields();
  255. for(Field field : fields){
  256. String fieldName = field.getName();
  257. if(getValueByFieldName(fieldName,dto)!=null)
  258. request.put(fieldName, getValueByFieldName(fieldName,dto));
  259. }*//*
  260. request.put("NAME","紫光展讯通信惠州有限公司");
  261. request.put("S_CODE","2024-07-04 17:11:43");
  262. request.put("UID_TYPE","01");
  263. request.put("UID","91441300MA4WY7CN2T");
  264. request.put("CUSTOMER_TYPE","C0201");
  265. request.put("IS_VALID","1");
  266. request.put("CREATED_ORG","HRORGI0000000001");
  267. request.put("CREATED_SYSTEM","ZHONGTAI");
  268. request.put("ISTEMPORARY","0");
  269. request.put("MDM_CODE",null);
  270. request.put("CREAT_AT","2024-07-04 17:11:43");
  271. request.put("UPDATE_AT","2024-07-04 17:11:43");
  272. request.put("SERIAL","23468156486151");
  273. //param参数构造 并传入你构造的每一条数据参数,此处例:1条
  274. JSONObject requestparam=new JSONObject(4);
  275. requestparam.put("param",request);
  276. String content = requestparam.toString();
  277. //加签方法第一个参数
  278. StringBuilder signBuilder = new StringBuilder("appid").append("=").append(STK_APP_ID).append("&")
  279. .append(content).append("&")
  280. .append("timestamp").append("=").append(timestamp).append("&")
  281. .append("key").append("=").append(STK_APP_KEY);
  282. //加签
  283. String sign = STKSignUtil.HMACSHA256(signBuilder.toString(),STK_APP_KEY);
  284. System.out.println("原始:"+signBuilder);
  285. System.out.println("签名:"+sign);
  286. System.out.println("content:"+content);
  287. //请求数据(接口地址根据测试环境与正式环境变更)
  288. String body = HttpRequest.post(SAVE_OR_UPDATE_URL+SAVE_OR_UPDATE_REQUEST_CODE)
  289. .header("appid",STK_APP_ID) //请求头
  290. .header("timestamp", timestamp)
  291. .header("sign", sign)
  292. .header("Content-Type", "application/json")
  293. .body(content) //请求参数
  294. .timeout(20000) //超时时间
  295. .execute().body();
  296. log.info("修改客商返回信息:{}",body);*/
  297. //时间戳
  298. String timestamp = Long.toString(System.currentTimeMillis());
  299. //应用id
  300. String appid = "bc81efc8_b2c6_4670_818e_e2710a45";
  301. //应用key
  302. String appkey = "9a1ae28bc9104deb86f74575acc1c2d6";
  303. //接口代码
  304. String apiCode = "18c60d85eb0646a080341b64310109f1";
  305. //每一条数据在这里构造,根据实际情况修改
  306. JSONObject request=new JSONObject(4);
  307. request.put("NAME","紫光展讯通信惠州有限公司");
  308. request.put("S_CODE","2024-07-04 17:11:43");
  309. request.put("UID_TYPE","01");
  310. request.put("UID","91441300MA4WY7CN2T");
  311. request.put("CUSTOMER_TYPE","C0201");
  312. request.put("IS_VALID","1");
  313. request.put("CREATED_ORG","HRORGI0000000001");
  314. request.put("CREATED_SYSTEM","ZHONGTAI");
  315. request.put("ISTEMPORARY","0");
  316. request.put("MDM_CODE",null);
  317. request.put("CREAT_AT","2024-07-04 17:11:43");
  318. request.put("UPDATE_AT","2024-07-04 17:11:43");
  319. request.put("SERIAL","23468156486151");
  320. //param参数构造 并传入你构造的每一条数据参数,此处例:1条
  321. JSONObject requestparam=new JSONObject(4);
  322. requestparam.put("param",request);
  323. String content = requestparam.toString();
  324. //加签方法第一个参数
  325. StringBuilder signBuilder = new StringBuilder("appid").append("=").append(appid).append("&")
  326. .append(content).append("&")
  327. .append("timestamp").append("=").append(timestamp).append("&")
  328. .append("key").append("=").append(appkey);
  329. //加签
  330. String sign = STKSignUtil.HMACSHA256(signBuilder.toString(),appkey);
  331. System.out.println("原始:"+signBuilder);
  332. System.out.println("签名:"+sign);
  333. System.out.println("content:"+content);
  334. //请求数据(接口地址根据测试环境与正式环境变更)
  335. String body = HttpRequest.post("http://10.67.2.187/api/oapigw/api/oapisvc/automicUpsertApi/"+apiCode)
  336. .header("appid",appid) //请求头
  337. .header("timestamp", timestamp)
  338. .header("sign", sign)
  339. .header("Content-Type", "application/json")
  340. .body(content) //请求参数
  341. .timeout(20000) //超时时间
  342. .execute().body();
  343. System.out.println("返回结果:"+body);
  344. return JSONObject.parseObject(body,SaveOrChangeCustomerDataVo.class);
  345. }
  346. /**
  347. * 根据属性名获取该类此属性的值
  348. * @param fieldName
  349. * @param object
  350. * @return
  351. */
  352. private static Object getValueByFieldName(String fieldName,Object object){
  353. String firstLetter=fieldName.substring(0,1).toUpperCase();
  354. String getter = "get"+firstLetter+fieldName.substring(1);
  355. try {
  356. Method method = object.getClass().getMethod(getter, new Class[]{});
  357. Object value = method.invoke(object, new Object[] {});
  358. return value;
  359. } catch (Exception e) {
  360. return null;
  361. }
  362. }
  363. @Override
  364. public List<CustvendDTO> queryList() {
  365. List<CustvendDTO> custvendDTO = new ArrayList<CustvendDTO>();
  366. SqlRowList rs = baseDao.queryForRowSet("select * from STK_CUSTVEND_CS");
  367. while (rs.next()) {
  368. CustvendDTO custvend = baseDao.getJdbcTemplate().queryForObject("select * from STK_CUSTVEND_CS where id = " + rs.getInt("id"), new BeanPropertyRowMapper<CustvendDTO>(CustvendDTO.class));
  369. custvendDTO.add(custvend);
  370. }
  371. return custvendDTO;
  372. }
  373. @Override
  374. public TravellingMerchantVo queryTravelingMerchantList(QueryTravellingMerchantDto dto) throws Exception {
  375. //时间戳
  376. String timestamp = Long.toString(System.currentTimeMillis());
  377. //加签方法的重要参数
  378. //若该接口需要传入一些参数 如:name、age等等
  379. //则直接在这个位置put即可
  380. JSONObject request=new JSONObject(4);
  381. request.put("code",GET_TRAVELLING_MERCHANT);
  382. request.put("s_orgcode",S_ORG_CODE);//119030009
  383. //request.put("start_time","2022-11-02 09:38:23");
  384. //request.put("end_time","2025-01-06 22:04:47");
  385. //request.put("jslx","合并口径");
  386. //request.put("dwmc","担保集团");
  387. request.put("page",dto.getPage());
  388. request.put("size",dto.getSize());
  389. String content = request.toString();
  390. //加签方法第一个参数
  391. StringBuilder signBuilder = new StringBuilder("appid").append("=").append(STK_APP_ID).append("&")
  392. .append(content).append("&")
  393. .append("timestamp").append("=").append(timestamp).append("&")
  394. .append("key").append("=").append(STK_APP_KEY);
  395. //加签
  396. String sign = STKSignUtil.HMACSHA256(signBuilder.toString(),STK_APP_KEY);
  397. System.out.println("原始:"+signBuilder);
  398. System.out.println("签名:"+sign);
  399. System.out.println("content:"+content);
  400. //请求数据 (接口地址根据测试环境与正式环境变更)
  401. String body = HttpRequest.post(COMPLEXITY_QUERY_URL)
  402. .header("appid",STK_APP_ID) //请求头
  403. .header("timestamp", timestamp)
  404. .header("sign", sign)
  405. .header("Content-Type", "application/json")
  406. .body(content) //请求参数
  407. .timeout(20000) //超时时间
  408. .execute().body();
  409. //JSONObject jsonObject = JSON.parseObject(body);
  410. //JSONObject dataObject = jsonObject.getJSONObject("data");
  411. //JSONArray listArray = dataObject.getJSONArray("list");
  412. System.out.println("返回结果:"+body);
  413. //System.out.println("返回结果:"+listArray.size());
  414. return JSONObject.parseObject(body,TravellingMerchantVo.class); //返回结果
  415. }
  416. /**
  417. * POST请求头部参数封装
  418. * */
  419. public Map<String,String> getHeaderMap(Object dto) throws Exception {
  420. log.info("STK,加密前参数:{}",JSONObject.toJSONString(dto));
  421. //时间戳
  422. String timestamp = Long.toString(System.currentTimeMillis());
  423. //获取签名
  424. String sign = getSign(dto,timestamp);
  425. log.info("STK,加密后的签名:"+sign);
  426. Map<String,String> headerMap = new HashMap<>();
  427. headerMap.put("appid",STK_APP_ID);
  428. headerMap.put("timestamp",timestamp);
  429. headerMap.put("sign",sign);
  430. headerMap.put("Content-Type","application/json");
  431. return headerMap;
  432. }
  433. /**
  434. * 获取深投控加密参数
  435. * */
  436. public String getSign(Object dto,String timestamp) throws Exception {
  437. String content = dto.toString();
  438. //加签方法第一个参数
  439. StringBuilder signBuilder = new StringBuilder("appid").append("=").append(STK_APP_ID).append("&")
  440. .append(content).append("&")
  441. .append("timestamp").append("=").append(timestamp).append("&")
  442. .append("key").append("=").append(STK_APP_KEY);
  443. log.info("STK,加密前参数:"+signBuilder.toString());
  444. //加签
  445. return STKSignUtil.HMACSHA256(signBuilder.toString(),STK_APP_KEY);
  446. }
  447. }