123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- package com.ruixuan.isc.service.impl;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.google.common.collect.Lists;
- import com.ruixuan.common.utils.Check;
- import com.ruixuan.common.utils.DateUtils;
- import com.ruixuan.common.utils.http.HttpUtil;
- import com.ruixuan.common.weixin.CorpWexinUtils;
- import com.ruixuan.isc.constants.KuaiShouConstants;
- import com.ruixuan.isc.entity.*;
- import com.ruixuan.isc.mapper.KwaiJSTMapper;
- import com.ruixuan.isc.service.IKwaiJSTService;
- import com.ruixuan.isc.utils.JSTSignUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Slf4j
- @Service
- public class KwaiJSTServiceImpl implements IKwaiJSTService {
- @Autowired
- private KwaiJSTMapper kwaiJSTMapper;
- private static String accessTokenUrl = "https://openapi.jushuitan.com/openWeb/auth/refreshToken";
- @Override
- public JSONObject refreshToken() {
- JSONObject returnJson = new JSONObject();
- try {
- List<JSONObject> list = kwaiJSTMapper.getList();
- if (Check.isNull(list)) {
- throw new Exception("token信息为空");
- }
- for (int i = 0; i < list.size(); i++) {
- JSONObject jsonObject = list.get(i);
- Long id = jsonObject.getLong("id");
- String appKey = jsonObject.getString("appKey");
- String appSecret = jsonObject.getString("appSecret");
- String refreshToken = jsonObject.getString("refreshToken");
- Map<String, Object> requestMap = new HashMap<>();
- requestMap.put("app_key", appKey);
- requestMap.put("grant_type", "refresh_token");
- requestMap.put("charset", "utf-8");
- long timestamp = System.currentTimeMillis() / 1000;
- requestMap.put("timestamp", String.valueOf(timestamp));
- // requestMap.put("code", JSTSignUtils.generateSoleName());
- requestMap.put("refresh_token", refreshToken);
- requestMap.put("scope", "all");
- String sign = JSTSignUtils.getSign(appSecret, requestMap);
- requestMap.put("sign", sign);
- String s = HttpUtil.httpPostFormRequest(accessTokenUrl, requestMap, null);
- JSONObject info = JSONObject.parseObject(s);
- if (!Check.isNull(info)) {
- Integer code = info.getInteger("code");
- if (code == 0) {
- JSONObject data = info.getJSONObject("data");
- if (!Check.isNull(data)) {
- String accessToken1 = data.getString("access_token");
- String refreshToken1 = data.getString("refresh_token");
- kwaiJSTMapper.updateToken(id, accessToken1, refreshToken1);
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- returnJson.put("code", 500);
- returnJson.put("message", e.getMessage());
- }
- return returnJson;
- }
- @Override
- public void queryAndInsertSku(String date) {
- try {
- List<JSONObject> list = kwaiJSTMapper.getList();
- if (Check.isNull(list)) {
- throw new Exception("token信息为空");
- }
- for (int i = 0; i < list.size(); i++) {
- JSONObject obj = list.get(i);
- Map<String, Object> params = new HashMap<>();
- params.put("app_key", obj.getString("appKey"));
- params.put("access_token", obj.getString("accessToken"));
- params.put("charset", "utf-8");
- params.put("version", "2");
- params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
- //普通商品
- addSku(params, obj.getString("appSecret"), date, 1);
- //组合装
- addCombineSku(params, obj.getString("appSecret"), date, 1);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- log.info("{} 完毕", date);
- }
- private void addSku(Map<String, Object> params, String secret, String date, int pageIndex) throws InterruptedException {
- boolean hasNext = false;
- JSONObject biz = new JSONObject();
- biz.put("page_index", pageIndex);
- biz.put("page_size", 50);
- biz.put("modified_begin", date + " 00:00:00");
- biz.put("modified_end", date + " 23:59:59");
- params.put("biz", biz);
- String sign = JSTSignUtils.getSign(secret, params);
- params.put("sign", sign);
- Thread.sleep(500l);
- String request = HttpUtil.httpPostFormRequest(KuaiShouConstants.JST_URL + "/open/sku/query", params, null);
- JSONObject info = JSONObject.parseObject(request);
- if (!Check.isNull(info)) {
- Integer code = info.getInteger("code");
- if (code == 0) {
- JSONObject data = info.getJSONObject("data");
- if (!Check.isNull(data)) {
- if (data.getBoolean("has_next")) {
- hasNext = true;
- }
- List<KwaixiaodianJst> jstList = new ArrayList<>();
- JSONArray datas = data.getJSONArray("datas");
- for (int i = 0; i < datas.size(); i++) {
- JSONObject skuInfo = datas.getJSONObject(i);
- KwaixiaodianJst jst = JSONObject.parseObject(skuInfo.toJSONString(), KwaixiaodianJst.class);
- jst.setDataType("1");
- jstList.add(jst);
- }
- if (jstList.size() > 0) {
- kwaiJSTMapper.replaceKwaixiaodianJst(jstList);
- }
- }
- } else {
- log.error("获取JST普通商品失败,原因:\n{}", info);
- }
- }
- if (hasNext) {
- addSku(params, secret, date, pageIndex + 1);
- }
- }
- private void addCombineSku(Map<String, Object> params, String secret, String date, int pageIndex) throws InterruptedException {
- boolean hasNext = false;
- JSONObject biz = new JSONObject();
- biz.put("page_index", pageIndex);
- biz.put("page_size", 50);
- biz.put("modified_begin", date + " 00:00:00");
- biz.put("modified_end", date + " 23:59:59");
- params.put("biz", biz);
- String sign = JSTSignUtils.getSign(secret, params);
- params.put("sign", sign);
- Thread.sleep(500l);
- String request = HttpUtil.httpPostFormRequest(KuaiShouConstants.JST_URL + "/open/combine/sku/query", params, null);
- JSONObject info = JSONObject.parseObject(request);
- if (!Check.isNull(info)) {
- Integer code = info.getInteger("code");
- if (code == 0) {
- JSONObject data = info.getJSONObject("data");
- if (!Check.isNull(data)) {
- if (data.getBoolean("has_next")) {
- hasNext = true;
- }
- List<KwaixiaodianJst> jstList = new ArrayList<>();
- JSONArray datas = data.getJSONArray("datas");
- for (int i = 0; i < datas.size(); i++) {
- JSONObject skuInfo = datas.getJSONObject(i);
- KwaixiaodianJst jst = JSONObject.parseObject(skuInfo.toJSONString(), KwaixiaodianJst.class);
- jst.setDataType("2");
- jstList.add(jst);
- if (!Check.isNull(skuInfo.getJSONArray("items"))) {
- addCombineSkuItems(jst.getSkuId(), skuInfo.getJSONArray("items"));
- }
- }
- if (jstList.size() > 0) {
- kwaiJSTMapper.replaceKwaixiaodianJst(jstList);
- }
- }
- } else {
- log.error("获取JST组合装商品失败,原因:\n{}", info);
- }
- }
- if (hasNext) {
- addCombineSku(params, secret, date, pageIndex + 1);
- }
- }
- private void addCombineSkuItems(String skuId, JSONArray items) {
- List<KwaixiaodianJstItems> itemList = new ArrayList<>();
- for (int i = 0; i < items.size(); i++) {
- JSONObject it = items.getJSONObject(i);
- KwaixiaodianJstItems item = JSONObject.parseObject(it.toJSONString(), KwaixiaodianJstItems.class);
- item.setSkuId(skuId);
- itemList.add(item);
- }
- if (itemList.size() > 0) {
- kwaiJSTMapper.replaceKwaixiaodianJstItems(itemList);
- }
- }
- @Override
- public void queryAndInsertPurchase(String date) {
- try {
- List<JSONObject> list = kwaiJSTMapper.getList();
- if (Check.isNull(list)) {
- throw new Exception("token信息为空");
- }
- for (int i = 0; i < list.size(); i++) {
- JSONObject obj = list.get(i);
- Map<String, Object> params = new HashMap<>();
- params.put("app_key", obj.getString("appKey"));
- params.put("access_token", obj.getString("accessToken"));
- params.put("charset", "utf-8");
- params.put("version", "2");
- params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
- //采购单查询
- addPurchase(params, obj.getString("appSecret"), date, 1);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- log.info("{} 完毕", date);
- }
- @Override
- public JSONObject costDetail(String skuNick, Integer dataType, Long date, Integer count) {
- JSONObject returnJSon = new JSONObject();
- try {
- List<Long> sukNumList = kwaiJSTMapper.getSukNumList(skuNick);
- if (Check.isNull(sukNumList)) {
- returnJSon.put("code", 200);
- returnJSon.put("message", "根据此sku未查询到sukNumList数据");
- return returnJSon;
- }
- List<JSONObject> orderList = new ArrayList<>();
- for (int i = 0; i < sukNumList.size(); i++) {
- Long skuNumId = sukNumList.get(i);
- List<JSONObject> list = kwaiJSTMapper.getOrderListBySkuNumId(skuNumId);
- if (!Check.isNull(list)) {
- orderList.addAll(list);
- }
- }
- if (Check.isNull(orderList)) {
- returnJSon.put("code", 200);
- returnJSon.put("message", "根据此sku未查询到数据");
- return returnJSon;
- }
- List<KwaixiaodianCostDetail> adds = new ArrayList<>();
- // 单品sku
- if (dataType == 1) {
- for (int i = 0; i < orderList.size(); i++) {
- JSONObject jsonObject = orderList.get(i);
- Long companyId = jsonObject.getLong("companyId");
- Long oid = jsonObject.getLong("oid");
- Long itemId = jsonObject.getLong("itemId");
- Long shopId = jsonObject.getLong("shopId");
- Integer num = jsonObject.getInteger("num");
- Long statDate = jsonObject.getLong("statDate");
- Long orderTemp = jsonObject.getLong("orderTemp");
- Long sendTime = jsonObject.getLong("sendTime");
- for (int j = 0; j < num; j++) {
- KwaixiaodianCostDetail addJson = new KwaixiaodianCostDetail();
- addJson.setCompanyId(companyId);
- addJson.setOid(oid);
- addJson.setItemId(itemId);
- addJson.setShopId(shopId);
- addJson.setSkuId(skuNick);
- addJson.setStatDate(statDate);
- addJson.setOrderTemp(orderTemp);
- addJson.setDataType(dataType);
- addJson.setDataLongDate(date);
- addJson.setSendTime(sendTime);
- adds.add(addJson);
- }
- }
- } else if (dataType == 2) { // 组合sku
- List<JSONObject> skuInfo = kwaiJSTMapper.getSkuInfo(skuNick);
- if (Check.isNull(skuInfo)) {
- returnJSon.put("code", 200);
- returnJSon.put("message", "根据此组合sku未查询到sku明细");
- return returnJSon;
- }
- for (int i = 0; i < orderList.size(); i++) { // 循环订单
- JSONObject jsonObject = orderList.get(i);
- Long companyId = jsonObject.getLong("companyId");
- Long oid = jsonObject.getLong("oid");
- Long itemId = jsonObject.getLong("itemId");
- Long shopId = jsonObject.getLong("shopId");
- Integer num = jsonObject.getInteger("num");
- Long statDate = jsonObject.getLong("statDate");
- Long orderTemp = jsonObject.getLong("orderTemp");
- Long sendTime = jsonObject.getLong("sendTime");
- for (int j = 0; j < num; j++) { // 循环订单数量
- for (int k = 0; k < skuInfo.size(); k++) { // 循环此组合sku下的单品sku
- JSONObject skuDetailJson = skuInfo.get(k);
- String combinationSkuId = skuDetailJson.getString("combinationSkuId");
- String skuId = skuDetailJson.getString("skuId");
- Integer qty = skuDetailJson.getInteger("qty");
- for (int l = 0; l < qty; l++) { // 循环单品sku数量
- KwaixiaodianCostDetail addJson = new KwaixiaodianCostDetail();
- addJson.setCompanyId(companyId);
- addJson.setOid(oid);
- addJson.setItemId(itemId);
- addJson.setShopId(shopId);
- addJson.setSkuId(skuId);
- addJson.setCombinationSkuId(combinationSkuId);
- addJson.setStatDate(statDate);
- addJson.setOrderTemp(orderTemp);
- addJson.setDataType(dataType);
- addJson.setDataLongDate(date);
- addJson.setSendTime(sendTime);
- adds.add(addJson);
- }
- }
- }
- }
- }
- if (!Check.isNull(adds)) {
- List<List<KwaixiaodianCostDetail>> partition = Lists.partition(adds, 2000);
- for (int i = 0; i < partition.size(); i++) {
- List<KwaixiaodianCostDetail> kwaixiaodianCostDetails = partition.get(i);
- if (!Check.isNull(kwaixiaodianCostDetails)) {
- kwaiJSTMapper.addDpSkuCost(kwaixiaodianCostDetails);
- }
- }
- }
- log.info("{},suk数据处理完成", skuNick);
- } catch (Exception e) {
- e.printStackTrace();
- this.send("快手小店成本数据清洗失败,请处理,序号:1,失败次数:" + count + "sukId:" + skuNick + " ," + e.getMessage());
- if (count <= 3) {
- costDetail(skuNick,dataType, date,count + 1);
- }
- }
- return returnJSon;
- }
- @Override
- public List<JSONObject> getSkuDataList(Long date) {
- return kwaiJSTMapper.getSkuDataList(date);
- }
- @Override
- public JSONObject cleanCost(String skuNick, Integer count) {
- JSONObject returnJson = new JSONObject();
- try {
- kwaiJSTMapper.deleteCostDetailEnd(skuNick);
- List<JSONObject> ranges = kwaiJSTMapper.getItemsRange(skuNick);
- if (Check.isNull(ranges)) { // 如果此sku未查询到采购记录,那么出单成本为0
- returnJson.put("code", 200);
- returnJson.put("message", "未查询到采购记录");
- return returnJson;
- }
- List<JSONObject> costDetail = kwaiJSTMapper.getCostDetail(skuNick); // 根据此skuId未查询到数据
- if (Check.isNull(costDetail)) {
- returnJson.put("code", 200);
- returnJson.put("message", "未查询到订单记录");
- return returnJson;
- }
- Integer dataSize = costDetail.size() - 1;
- Integer sortNum = 0;
- Boolean isBreak = false;
- List<KwaixiaodianCostDetailEnd> costDetailEnds = new ArrayList<>();
- for (int i = 0; i < ranges.size(); i++) {
- JSONObject rangeJson = ranges.get(i);
- Integer startQty = rangeJson.getInteger("startQty");
- Integer endQty = rangeJson.getInteger("endQty");
- BigDecimal price = rangeJson.getBigDecimal("price");
- Long poiId = rangeJson.getLong("poiId");
- Long poId = rangeJson.getLong("poId");
- List<JSONObject> details = new ArrayList<>();
- if (dataSize >= endQty) { // 如果数据量大于采购
- details = costDetail.subList(startQty, endQty + 1);
- } else { // 如果数据量小于采购,获取到数据的最后一条
- details = costDetail.subList(startQty, dataSize + 1);
- isBreak = true;
- }
- for (int j = 0; j < details.size(); j++) {
- sortNum++;
- KwaixiaodianCostDetailEnd addCostDetailEnd = new KwaixiaodianCostDetailEnd();
- JSONObject info = details.get(j);
- Long oid = info.getLong("oid");
- Integer dataType = info.getInteger("dataType");
- Long companyId = info.getLong("companyId");
- Long shopId = info.getLong("shopId");
- Long itemId = info.getLong("itemId");
- String skuId = info.getString("skuId");
- String combinationSkuId = info.getString("combinationSkuId");
- Long statDate = info.getLong("statDate");
- Long orderTemp = info.getLong("orderTemp");
- Long sendTime = info.getLong("sendTime");
- addCostDetailEnd.setCompanyId(companyId);
- addCostDetailEnd.setOid(oid);
- addCostDetailEnd.setItemId(itemId);
- addCostDetailEnd.setShopId(shopId);
- addCostDetailEnd.setSkuId(skuId);
- addCostDetailEnd.setCombinationSkuId(combinationSkuId);
- addCostDetailEnd.setStatDate(statDate);
- addCostDetailEnd.setOrderTemp(orderTemp);
- addCostDetailEnd.setDataType(dataType);
- addCostDetailEnd.setSortNum(sortNum);
- addCostDetailEnd.setCostPrice(price);
- addCostDetailEnd.setPoId(poId);
- addCostDetailEnd.setPoiId(poiId);
- addCostDetailEnd.setSendTime(sendTime);
- costDetailEnds.add(addCostDetailEnd);
- }
- if (isBreak) {
- break;
- }
- }
- if (!Check.isNull(costDetailEnds)) {
- List<List<KwaixiaodianCostDetailEnd>> partition = Lists.partition(costDetailEnds, 2000);
- for (int i = 0; i < partition.size(); i++) {
- List<KwaixiaodianCostDetailEnd> kwaixiaodianCostDetailEnds = partition.get(i);
- if (!Check.isNull(kwaixiaodianCostDetailEnds)) {
- kwaiJSTMapper.addsCostDetailEnds(kwaixiaodianCostDetailEnds);
- }
- }
- }
- log.info("{},suk数据处理最终清洗完成", skuNick);
- } catch (Exception e) {
- e.printStackTrace();
- this.send("快手小店成本数据清洗失败,请处理,序号:2,失败次数:" + count + "sukId:" + skuNick + " ," + e.getMessage());
- if (count <= 3) {
- cleanCost(skuNick, count + 1);
- }
- }
- return null;
- }
- @Override
- public List<JSONObject> getCleanDataList(Long date) {
- return kwaiJSTMapper.getCleanDataList(date);
- }
- private void addPurchase(Map<String, Object> params, String secret, String date, int pageIndex) throws InterruptedException {
- boolean hasNext = false;
- JSONObject biz = new JSONObject();
- biz.put("page_index", pageIndex);
- biz.put("page_size", 50);
- biz.put("modified_begin", date + " 00:00:00");
- biz.put("modified_end", date + " 23:59:59");
- params.put("biz", biz);
- String sign = JSTSignUtils.getSign(secret, params);
- params.put("sign", sign);
- Thread.sleep(500l);
- String request = HttpUtil.httpPostFormRequest(KuaiShouConstants.JST_URL + "/open/purchase/query", params, null);
- JSONObject info = JSONObject.parseObject(request);
- if (!Check.isNull(info)) {
- Integer code = info.getInteger("code");
- if (code == 0) {
- JSONObject data = info.getJSONObject("data");
- if (!Check.isNull(data)) {
- if (data.getBoolean("has_next")) {
- hasNext = true;
- }
- List<KwaixiaodianJstPurchase> jstList = new ArrayList<>();
- JSONArray datas = data.getJSONArray("datas");
- if (!Check.isNull(datas)) {
- for (int i = 0; i < datas.size(); i++) {
- JSONObject skuInfo = datas.getJSONObject(i);
- KwaixiaodianJstPurchase jst = JSONObject.parseObject(skuInfo.toJSONString(), KwaixiaodianJstPurchase.class);
- jstList.add(jst);
- if (!Check.isNull(skuInfo.getJSONArray("items"))) {
- addPurchaseItems(skuInfo.getString("po_date"), skuInfo.getJSONArray("items"));
- }
- }
- }
- if (jstList.size() > 0) {
- kwaiJSTMapper.replacekwaixiaodianJstPurchase(jstList);
- }
- }
- } else {
- log.error("查询JST采购单失败,原因:\n{}", info);
- }
- }
- if (hasNext) {
- addPurchase(params, secret, date, pageIndex + 1);
- }
- }
- private void addPurchaseItems(String poDate, JSONArray items) {
- List<KwaixiaodianJstPurchaseItems> itemList = new ArrayList<>();
- for (int i = 0; i < items.size(); i++) {
- JSONObject it = items.getJSONObject(i);
- KwaixiaodianJstPurchaseItems item = JSONObject.parseObject(it.toJSONString(), KwaixiaodianJstPurchaseItems.class);
- item.setPoDate(poDate);
- itemList.add(item);
- }
- if (itemList.size() > 0) {
- kwaiJSTMapper.replaceKwaixiaodianJstPurchaseItems(itemList);
- }
- }
- @Autowired
- private CorpWexinUtils corpWexinUtils;
- private void send(String text) {
- JSONObject weChat = new JSONObject();
- weChat.put("wexin_id", "YuMeng");
- weChat.put("corp_id", "ww24b8a47826f5875f");
- corpWexinUtils.sendMessage(weChat, text);
- }
- }
|