KwaiJSTServiceImpl.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. package com.ruixuan.isc.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.google.common.collect.Lists;
  5. import com.ruixuan.common.utils.Check;
  6. import com.ruixuan.common.utils.DateUtils;
  7. import com.ruixuan.common.utils.http.HttpUtil;
  8. import com.ruixuan.common.weixin.CorpWexinUtils;
  9. import com.ruixuan.isc.constants.KuaiShouConstants;
  10. import com.ruixuan.isc.entity.*;
  11. import com.ruixuan.isc.mapper.KwaiJSTMapper;
  12. import com.ruixuan.isc.service.IKwaiJSTService;
  13. import com.ruixuan.isc.utils.JSTSignUtils;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import java.math.BigDecimal;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. @Slf4j
  23. @Service
  24. public class KwaiJSTServiceImpl implements IKwaiJSTService {
  25. @Autowired
  26. private KwaiJSTMapper kwaiJSTMapper;
  27. private static String accessTokenUrl = "https://openapi.jushuitan.com/openWeb/auth/refreshToken";
  28. @Override
  29. public JSONObject refreshToken() {
  30. JSONObject returnJson = new JSONObject();
  31. try {
  32. List<JSONObject> list = kwaiJSTMapper.getList();
  33. if (Check.isNull(list)) {
  34. throw new Exception("token信息为空");
  35. }
  36. for (int i = 0; i < list.size(); i++) {
  37. JSONObject jsonObject = list.get(i);
  38. Long id = jsonObject.getLong("id");
  39. String appKey = jsonObject.getString("appKey");
  40. String appSecret = jsonObject.getString("appSecret");
  41. String refreshToken = jsonObject.getString("refreshToken");
  42. Map<String, Object> requestMap = new HashMap<>();
  43. requestMap.put("app_key", appKey);
  44. requestMap.put("grant_type", "refresh_token");
  45. requestMap.put("charset", "utf-8");
  46. long timestamp = System.currentTimeMillis() / 1000;
  47. requestMap.put("timestamp", String.valueOf(timestamp));
  48. // requestMap.put("code", JSTSignUtils.generateSoleName());
  49. requestMap.put("refresh_token", refreshToken);
  50. requestMap.put("scope", "all");
  51. String sign = JSTSignUtils.getSign(appSecret, requestMap);
  52. requestMap.put("sign", sign);
  53. String s = HttpUtil.httpPostFormRequest(accessTokenUrl, requestMap, null);
  54. JSONObject info = JSONObject.parseObject(s);
  55. if (!Check.isNull(info)) {
  56. Integer code = info.getInteger("code");
  57. if (code == 0) {
  58. JSONObject data = info.getJSONObject("data");
  59. if (!Check.isNull(data)) {
  60. String accessToken1 = data.getString("access_token");
  61. String refreshToken1 = data.getString("refresh_token");
  62. kwaiJSTMapper.updateToken(id, accessToken1, refreshToken1);
  63. }
  64. }
  65. }
  66. }
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. returnJson.put("code", 500);
  70. returnJson.put("message", e.getMessage());
  71. }
  72. return returnJson;
  73. }
  74. @Override
  75. public void queryAndInsertSku(String date) {
  76. try {
  77. List<JSONObject> list = kwaiJSTMapper.getList();
  78. if (Check.isNull(list)) {
  79. throw new Exception("token信息为空");
  80. }
  81. for (int i = 0; i < list.size(); i++) {
  82. JSONObject obj = list.get(i);
  83. Map<String, Object> params = new HashMap<>();
  84. params.put("app_key", obj.getString("appKey"));
  85. params.put("access_token", obj.getString("accessToken"));
  86. params.put("charset", "utf-8");
  87. params.put("version", "2");
  88. params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
  89. //普通商品
  90. addSku(params, obj.getString("appSecret"), date, 1);
  91. //组合装
  92. addCombineSku(params, obj.getString("appSecret"), date, 1);
  93. }
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. log.info("{} 完毕", date);
  98. }
  99. private void addSku(Map<String, Object> params, String secret, String date, int pageIndex) throws InterruptedException {
  100. boolean hasNext = false;
  101. JSONObject biz = new JSONObject();
  102. biz.put("page_index", pageIndex);
  103. biz.put("page_size", 50);
  104. biz.put("modified_begin", date + " 00:00:00");
  105. biz.put("modified_end", date + " 23:59:59");
  106. params.put("biz", biz);
  107. String sign = JSTSignUtils.getSign(secret, params);
  108. params.put("sign", sign);
  109. Thread.sleep(500l);
  110. String request = HttpUtil.httpPostFormRequest(KuaiShouConstants.JST_URL + "/open/sku/query", params, null);
  111. JSONObject info = JSONObject.parseObject(request);
  112. if (!Check.isNull(info)) {
  113. Integer code = info.getInteger("code");
  114. if (code == 0) {
  115. JSONObject data = info.getJSONObject("data");
  116. if (!Check.isNull(data)) {
  117. if (data.getBoolean("has_next")) {
  118. hasNext = true;
  119. }
  120. List<KwaixiaodianJst> jstList = new ArrayList<>();
  121. JSONArray datas = data.getJSONArray("datas");
  122. for (int i = 0; i < datas.size(); i++) {
  123. JSONObject skuInfo = datas.getJSONObject(i);
  124. KwaixiaodianJst jst = JSONObject.parseObject(skuInfo.toJSONString(), KwaixiaodianJst.class);
  125. jst.setDataType("1");
  126. jstList.add(jst);
  127. }
  128. if (jstList.size() > 0) {
  129. kwaiJSTMapper.replaceKwaixiaodianJst(jstList);
  130. }
  131. }
  132. } else {
  133. log.error("获取JST普通商品失败,原因:\n{}", info);
  134. }
  135. }
  136. if (hasNext) {
  137. addSku(params, secret, date, pageIndex + 1);
  138. }
  139. }
  140. private void addCombineSku(Map<String, Object> params, String secret, String date, int pageIndex) throws InterruptedException {
  141. boolean hasNext = false;
  142. JSONObject biz = new JSONObject();
  143. biz.put("page_index", pageIndex);
  144. biz.put("page_size", 50);
  145. biz.put("modified_begin", date + " 00:00:00");
  146. biz.put("modified_end", date + " 23:59:59");
  147. params.put("biz", biz);
  148. String sign = JSTSignUtils.getSign(secret, params);
  149. params.put("sign", sign);
  150. Thread.sleep(500l);
  151. String request = HttpUtil.httpPostFormRequest(KuaiShouConstants.JST_URL + "/open/combine/sku/query", params, null);
  152. JSONObject info = JSONObject.parseObject(request);
  153. if (!Check.isNull(info)) {
  154. Integer code = info.getInteger("code");
  155. if (code == 0) {
  156. JSONObject data = info.getJSONObject("data");
  157. if (!Check.isNull(data)) {
  158. if (data.getBoolean("has_next")) {
  159. hasNext = true;
  160. }
  161. List<KwaixiaodianJst> jstList = new ArrayList<>();
  162. JSONArray datas = data.getJSONArray("datas");
  163. for (int i = 0; i < datas.size(); i++) {
  164. JSONObject skuInfo = datas.getJSONObject(i);
  165. KwaixiaodianJst jst = JSONObject.parseObject(skuInfo.toJSONString(), KwaixiaodianJst.class);
  166. jst.setDataType("2");
  167. jstList.add(jst);
  168. if (!Check.isNull(skuInfo.getJSONArray("items"))) {
  169. addCombineSkuItems(jst.getSkuId(), skuInfo.getJSONArray("items"));
  170. }
  171. }
  172. if (jstList.size() > 0) {
  173. kwaiJSTMapper.replaceKwaixiaodianJst(jstList);
  174. }
  175. }
  176. } else {
  177. log.error("获取JST组合装商品失败,原因:\n{}", info);
  178. }
  179. }
  180. if (hasNext) {
  181. addCombineSku(params, secret, date, pageIndex + 1);
  182. }
  183. }
  184. private void addCombineSkuItems(String skuId, JSONArray items) {
  185. List<KwaixiaodianJstItems> itemList = new ArrayList<>();
  186. for (int i = 0; i < items.size(); i++) {
  187. JSONObject it = items.getJSONObject(i);
  188. KwaixiaodianJstItems item = JSONObject.parseObject(it.toJSONString(), KwaixiaodianJstItems.class);
  189. item.setSkuId(skuId);
  190. itemList.add(item);
  191. }
  192. if (itemList.size() > 0) {
  193. kwaiJSTMapper.replaceKwaixiaodianJstItems(itemList);
  194. }
  195. }
  196. @Override
  197. public void queryAndInsertPurchase(String date) {
  198. try {
  199. List<JSONObject> list = kwaiJSTMapper.getList();
  200. if (Check.isNull(list)) {
  201. throw new Exception("token信息为空");
  202. }
  203. for (int i = 0; i < list.size(); i++) {
  204. JSONObject obj = list.get(i);
  205. Map<String, Object> params = new HashMap<>();
  206. params.put("app_key", obj.getString("appKey"));
  207. params.put("access_token", obj.getString("accessToken"));
  208. params.put("charset", "utf-8");
  209. params.put("version", "2");
  210. params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
  211. //采购单查询
  212. addPurchase(params, obj.getString("appSecret"), date, 1);
  213. }
  214. } catch (Exception e) {
  215. e.printStackTrace();
  216. }
  217. log.info("{} 完毕", date);
  218. }
  219. @Override
  220. public JSONObject costDetail(String skuNick, Integer dataType, Long date, Integer count) {
  221. JSONObject returnJSon = new JSONObject();
  222. try {
  223. List<Long> sukNumList = kwaiJSTMapper.getSukNumList(skuNick);
  224. if (Check.isNull(sukNumList)) {
  225. returnJSon.put("code", 200);
  226. returnJSon.put("message", "根据此sku未查询到sukNumList数据");
  227. return returnJSon;
  228. }
  229. List<JSONObject> orderList = new ArrayList<>();
  230. for (int i = 0; i < sukNumList.size(); i++) {
  231. Long skuNumId = sukNumList.get(i);
  232. List<JSONObject> list = kwaiJSTMapper.getOrderListBySkuNumId(skuNumId);
  233. if (!Check.isNull(list)) {
  234. orderList.addAll(list);
  235. }
  236. }
  237. if (Check.isNull(orderList)) {
  238. returnJSon.put("code", 200);
  239. returnJSon.put("message", "根据此sku未查询到数据");
  240. return returnJSon;
  241. }
  242. List<KwaixiaodianCostDetail> adds = new ArrayList<>();
  243. // 单品sku
  244. if (dataType == 1) {
  245. for (int i = 0; i < orderList.size(); i++) {
  246. JSONObject jsonObject = orderList.get(i);
  247. Long companyId = jsonObject.getLong("companyId");
  248. Long oid = jsonObject.getLong("oid");
  249. Long itemId = jsonObject.getLong("itemId");
  250. Long shopId = jsonObject.getLong("shopId");
  251. Integer num = jsonObject.getInteger("num");
  252. Long statDate = jsonObject.getLong("statDate");
  253. Long orderTemp = jsonObject.getLong("orderTemp");
  254. Long sendTime = jsonObject.getLong("sendTime");
  255. for (int j = 0; j < num; j++) {
  256. KwaixiaodianCostDetail addJson = new KwaixiaodianCostDetail();
  257. addJson.setCompanyId(companyId);
  258. addJson.setOid(oid);
  259. addJson.setItemId(itemId);
  260. addJson.setShopId(shopId);
  261. addJson.setSkuId(skuNick);
  262. addJson.setStatDate(statDate);
  263. addJson.setOrderTemp(orderTemp);
  264. addJson.setDataType(dataType);
  265. addJson.setDataLongDate(date);
  266. addJson.setSendTime(sendTime);
  267. adds.add(addJson);
  268. }
  269. }
  270. } else if (dataType == 2) { // 组合sku
  271. List<JSONObject> skuInfo = kwaiJSTMapper.getSkuInfo(skuNick);
  272. if (Check.isNull(skuInfo)) {
  273. returnJSon.put("code", 200);
  274. returnJSon.put("message", "根据此组合sku未查询到sku明细");
  275. return returnJSon;
  276. }
  277. for (int i = 0; i < orderList.size(); i++) { // 循环订单
  278. JSONObject jsonObject = orderList.get(i);
  279. Long companyId = jsonObject.getLong("companyId");
  280. Long oid = jsonObject.getLong("oid");
  281. Long itemId = jsonObject.getLong("itemId");
  282. Long shopId = jsonObject.getLong("shopId");
  283. Integer num = jsonObject.getInteger("num");
  284. Long statDate = jsonObject.getLong("statDate");
  285. Long orderTemp = jsonObject.getLong("orderTemp");
  286. Long sendTime = jsonObject.getLong("sendTime");
  287. for (int j = 0; j < num; j++) { // 循环订单数量
  288. for (int k = 0; k < skuInfo.size(); k++) { // 循环此组合sku下的单品sku
  289. JSONObject skuDetailJson = skuInfo.get(k);
  290. String combinationSkuId = skuDetailJson.getString("combinationSkuId");
  291. String skuId = skuDetailJson.getString("skuId");
  292. Integer qty = skuDetailJson.getInteger("qty");
  293. for (int l = 0; l < qty; l++) { // 循环单品sku数量
  294. KwaixiaodianCostDetail addJson = new KwaixiaodianCostDetail();
  295. addJson.setCompanyId(companyId);
  296. addJson.setOid(oid);
  297. addJson.setItemId(itemId);
  298. addJson.setShopId(shopId);
  299. addJson.setSkuId(skuId);
  300. addJson.setCombinationSkuId(combinationSkuId);
  301. addJson.setStatDate(statDate);
  302. addJson.setOrderTemp(orderTemp);
  303. addJson.setDataType(dataType);
  304. addJson.setDataLongDate(date);
  305. addJson.setSendTime(sendTime);
  306. adds.add(addJson);
  307. }
  308. }
  309. }
  310. }
  311. }
  312. if (!Check.isNull(adds)) {
  313. List<List<KwaixiaodianCostDetail>> partition = Lists.partition(adds, 2000);
  314. for (int i = 0; i < partition.size(); i++) {
  315. List<KwaixiaodianCostDetail> kwaixiaodianCostDetails = partition.get(i);
  316. if (!Check.isNull(kwaixiaodianCostDetails)) {
  317. kwaiJSTMapper.addDpSkuCost(kwaixiaodianCostDetails);
  318. }
  319. }
  320. }
  321. log.info("{},suk数据处理完成", skuNick);
  322. } catch (Exception e) {
  323. e.printStackTrace();
  324. this.send("快手小店成本数据清洗失败,请处理,序号:1,失败次数:" + count + "sukId:" + skuNick + " ," + e.getMessage());
  325. if (count <= 3) {
  326. costDetail(skuNick,dataType, date,count + 1);
  327. }
  328. }
  329. return returnJSon;
  330. }
  331. @Override
  332. public List<JSONObject> getSkuDataList(Long date) {
  333. return kwaiJSTMapper.getSkuDataList(date);
  334. }
  335. @Override
  336. public JSONObject cleanCost(String skuNick, Integer count) {
  337. JSONObject returnJson = new JSONObject();
  338. try {
  339. kwaiJSTMapper.deleteCostDetailEnd(skuNick);
  340. List<JSONObject> ranges = kwaiJSTMapper.getItemsRange(skuNick);
  341. if (Check.isNull(ranges)) { // 如果此sku未查询到采购记录,那么出单成本为0
  342. returnJson.put("code", 200);
  343. returnJson.put("message", "未查询到采购记录");
  344. return returnJson;
  345. }
  346. List<JSONObject> costDetail = kwaiJSTMapper.getCostDetail(skuNick); // 根据此skuId未查询到数据
  347. if (Check.isNull(costDetail)) {
  348. returnJson.put("code", 200);
  349. returnJson.put("message", "未查询到订单记录");
  350. return returnJson;
  351. }
  352. Integer dataSize = costDetail.size() - 1;
  353. Integer sortNum = 0;
  354. Boolean isBreak = false;
  355. List<KwaixiaodianCostDetailEnd> costDetailEnds = new ArrayList<>();
  356. for (int i = 0; i < ranges.size(); i++) {
  357. JSONObject rangeJson = ranges.get(i);
  358. Integer startQty = rangeJson.getInteger("startQty");
  359. Integer endQty = rangeJson.getInteger("endQty");
  360. BigDecimal price = rangeJson.getBigDecimal("price");
  361. Long poiId = rangeJson.getLong("poiId");
  362. Long poId = rangeJson.getLong("poId");
  363. List<JSONObject> details = new ArrayList<>();
  364. if (dataSize >= endQty) { // 如果数据量大于采购
  365. details = costDetail.subList(startQty, endQty + 1);
  366. } else { // 如果数据量小于采购,获取到数据的最后一条
  367. details = costDetail.subList(startQty, dataSize + 1);
  368. isBreak = true;
  369. }
  370. for (int j = 0; j < details.size(); j++) {
  371. sortNum++;
  372. KwaixiaodianCostDetailEnd addCostDetailEnd = new KwaixiaodianCostDetailEnd();
  373. JSONObject info = details.get(j);
  374. Long oid = info.getLong("oid");
  375. Integer dataType = info.getInteger("dataType");
  376. Long companyId = info.getLong("companyId");
  377. Long shopId = info.getLong("shopId");
  378. Long itemId = info.getLong("itemId");
  379. String skuId = info.getString("skuId");
  380. String combinationSkuId = info.getString("combinationSkuId");
  381. Long statDate = info.getLong("statDate");
  382. Long orderTemp = info.getLong("orderTemp");
  383. Long sendTime = info.getLong("sendTime");
  384. addCostDetailEnd.setCompanyId(companyId);
  385. addCostDetailEnd.setOid(oid);
  386. addCostDetailEnd.setItemId(itemId);
  387. addCostDetailEnd.setShopId(shopId);
  388. addCostDetailEnd.setSkuId(skuId);
  389. addCostDetailEnd.setCombinationSkuId(combinationSkuId);
  390. addCostDetailEnd.setStatDate(statDate);
  391. addCostDetailEnd.setOrderTemp(orderTemp);
  392. addCostDetailEnd.setDataType(dataType);
  393. addCostDetailEnd.setSortNum(sortNum);
  394. addCostDetailEnd.setCostPrice(price);
  395. addCostDetailEnd.setPoId(poId);
  396. addCostDetailEnd.setPoiId(poiId);
  397. addCostDetailEnd.setSendTime(sendTime);
  398. costDetailEnds.add(addCostDetailEnd);
  399. }
  400. if (isBreak) {
  401. break;
  402. }
  403. }
  404. if (!Check.isNull(costDetailEnds)) {
  405. List<List<KwaixiaodianCostDetailEnd>> partition = Lists.partition(costDetailEnds, 2000);
  406. for (int i = 0; i < partition.size(); i++) {
  407. List<KwaixiaodianCostDetailEnd> kwaixiaodianCostDetailEnds = partition.get(i);
  408. if (!Check.isNull(kwaixiaodianCostDetailEnds)) {
  409. kwaiJSTMapper.addsCostDetailEnds(kwaixiaodianCostDetailEnds);
  410. }
  411. }
  412. }
  413. log.info("{},suk数据处理最终清洗完成", skuNick);
  414. } catch (Exception e) {
  415. e.printStackTrace();
  416. this.send("快手小店成本数据清洗失败,请处理,序号:2,失败次数:" + count + "sukId:" + skuNick + " ," + e.getMessage());
  417. if (count <= 3) {
  418. cleanCost(skuNick, count + 1);
  419. }
  420. }
  421. return null;
  422. }
  423. @Override
  424. public List<JSONObject> getCleanDataList(Long date) {
  425. return kwaiJSTMapper.getCleanDataList(date);
  426. }
  427. private void addPurchase(Map<String, Object> params, String secret, String date, int pageIndex) throws InterruptedException {
  428. boolean hasNext = false;
  429. JSONObject biz = new JSONObject();
  430. biz.put("page_index", pageIndex);
  431. biz.put("page_size", 50);
  432. biz.put("modified_begin", date + " 00:00:00");
  433. biz.put("modified_end", date + " 23:59:59");
  434. params.put("biz", biz);
  435. String sign = JSTSignUtils.getSign(secret, params);
  436. params.put("sign", sign);
  437. Thread.sleep(500l);
  438. String request = HttpUtil.httpPostFormRequest(KuaiShouConstants.JST_URL + "/open/purchase/query", params, null);
  439. JSONObject info = JSONObject.parseObject(request);
  440. if (!Check.isNull(info)) {
  441. Integer code = info.getInteger("code");
  442. if (code == 0) {
  443. JSONObject data = info.getJSONObject("data");
  444. if (!Check.isNull(data)) {
  445. if (data.getBoolean("has_next")) {
  446. hasNext = true;
  447. }
  448. List<KwaixiaodianJstPurchase> jstList = new ArrayList<>();
  449. JSONArray datas = data.getJSONArray("datas");
  450. if (!Check.isNull(datas)) {
  451. for (int i = 0; i < datas.size(); i++) {
  452. JSONObject skuInfo = datas.getJSONObject(i);
  453. KwaixiaodianJstPurchase jst = JSONObject.parseObject(skuInfo.toJSONString(), KwaixiaodianJstPurchase.class);
  454. jstList.add(jst);
  455. if (!Check.isNull(skuInfo.getJSONArray("items"))) {
  456. addPurchaseItems(skuInfo.getString("po_date"), skuInfo.getJSONArray("items"));
  457. }
  458. }
  459. }
  460. if (jstList.size() > 0) {
  461. kwaiJSTMapper.replacekwaixiaodianJstPurchase(jstList);
  462. }
  463. }
  464. } else {
  465. log.error("查询JST采购单失败,原因:\n{}", info);
  466. }
  467. }
  468. if (hasNext) {
  469. addPurchase(params, secret, date, pageIndex + 1);
  470. }
  471. }
  472. private void addPurchaseItems(String poDate, JSONArray items) {
  473. List<KwaixiaodianJstPurchaseItems> itemList = new ArrayList<>();
  474. for (int i = 0; i < items.size(); i++) {
  475. JSONObject it = items.getJSONObject(i);
  476. KwaixiaodianJstPurchaseItems item = JSONObject.parseObject(it.toJSONString(), KwaixiaodianJstPurchaseItems.class);
  477. item.setPoDate(poDate);
  478. itemList.add(item);
  479. }
  480. if (itemList.size() > 0) {
  481. kwaiJSTMapper.replaceKwaixiaodianJstPurchaseItems(itemList);
  482. }
  483. }
  484. @Autowired
  485. private CorpWexinUtils corpWexinUtils;
  486. private void send(String text) {
  487. JSONObject weChat = new JSONObject();
  488. weChat.put("wexin_id", "YuMeng");
  489. weChat.put("corp_id", "ww24b8a47826f5875f");
  490. corpWexinUtils.sendMessage(weChat, text);
  491. }
  492. }