|
@@ -1,18 +1,27 @@
|
|
package com.ruixuan.isc.service.impl;
|
|
package com.ruixuan.isc.service.impl;
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.ruixuan.common.utils.Check;
|
|
import com.ruixuan.common.utils.Check;
|
|
import com.ruixuan.common.utils.http.HttpUtil;
|
|
import com.ruixuan.common.utils.http.HttpUtil;
|
|
|
|
+import com.ruixuan.isc.constants.KuaiShouConstants;
|
|
|
|
+import com.ruixuan.isc.entity.KwaixiaodianJst;
|
|
|
|
+import com.ruixuan.isc.entity.KwaixiaodianJstItems;
|
|
|
|
+import com.ruixuan.isc.entity.KwaixiaodianJstPurchase;
|
|
|
|
+import com.ruixuan.isc.entity.KwaixiaodianJstPurchaseItems;
|
|
import com.ruixuan.isc.mapper.KwaiJSTMapper;
|
|
import com.ruixuan.isc.mapper.KwaiJSTMapper;
|
|
import com.ruixuan.isc.service.IKwaiJSTService;
|
|
import com.ruixuan.isc.service.IKwaiJSTService;
|
|
import com.ruixuan.isc.utils.JSTSignUtils;
|
|
import com.ruixuan.isc.utils.JSTSignUtils;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
+@Slf4j
|
|
@Service
|
|
@Service
|
|
public class KwaiJSTServiceImpl implements IKwaiJSTService {
|
|
public class KwaiJSTServiceImpl implements IKwaiJSTService {
|
|
@Autowired
|
|
@Autowired
|
|
@@ -21,7 +30,6 @@ public class KwaiJSTServiceImpl implements IKwaiJSTService {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public JSONObject refreshToken() {
|
|
public JSONObject refreshToken() {
|
|
-
|
|
|
|
JSONObject returnJson = new JSONObject();
|
|
JSONObject returnJson = new JSONObject();
|
|
try {
|
|
try {
|
|
List<JSONObject> list = kwaiJSTMapper.getList();
|
|
List<JSONObject> list = kwaiJSTMapper.getList();
|
|
@@ -41,7 +49,7 @@ public class KwaiJSTServiceImpl implements IKwaiJSTService {
|
|
requestMap.put("charset", "utf-8");
|
|
requestMap.put("charset", "utf-8");
|
|
long timestamp = System.currentTimeMillis() / 1000;
|
|
long timestamp = System.currentTimeMillis() / 1000;
|
|
requestMap.put("timestamp", String.valueOf(timestamp));
|
|
requestMap.put("timestamp", String.valueOf(timestamp));
|
|
- // requestMap.put("code", JSTSignUtils.generateSoleName());
|
|
|
|
|
|
+ // requestMap.put("code", JSTSignUtils.generateSoleName());
|
|
|
|
|
|
requestMap.put("refresh_token", refreshToken);
|
|
requestMap.put("refresh_token", refreshToken);
|
|
requestMap.put("scope", "all");
|
|
requestMap.put("scope", "all");
|
|
@@ -50,14 +58,14 @@ public class KwaiJSTServiceImpl implements IKwaiJSTService {
|
|
|
|
|
|
String s = HttpUtil.httpPostFormRequest(accessTokenUrl, requestMap, null);
|
|
String s = HttpUtil.httpPostFormRequest(accessTokenUrl, requestMap, null);
|
|
JSONObject info = JSONObject.parseObject(s);
|
|
JSONObject info = JSONObject.parseObject(s);
|
|
- if(!Check.isNull(info)){
|
|
|
|
|
|
+ if (!Check.isNull(info)) {
|
|
Integer code = info.getInteger("code");
|
|
Integer code = info.getInteger("code");
|
|
- if(code == 0){
|
|
|
|
|
|
+ if (code == 0) {
|
|
JSONObject data = info.getJSONObject("data");
|
|
JSONObject data = info.getJSONObject("data");
|
|
- if(!Check.isNull(data)){
|
|
|
|
|
|
+ if (!Check.isNull(data)) {
|
|
String accessToken1 = data.getString("access_token");
|
|
String accessToken1 = data.getString("access_token");
|
|
String refreshToken1 = data.getString("refresh_token");
|
|
String refreshToken1 = data.getString("refresh_token");
|
|
- kwaiJSTMapper.updateToken(id,accessToken1,refreshToken1);
|
|
|
|
|
|
+ kwaiJSTMapper.updateToken(id, accessToken1, refreshToken1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -73,4 +81,214 @@ public class KwaiJSTServiceImpl implements IKwaiJSTService {
|
|
}
|
|
}
|
|
return returnJson;
|
|
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);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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.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(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);
|
|
|
|
+ itemList.add(item);
|
|
|
|
+ }
|
|
|
|
+ if (itemList.size() > 0) {
|
|
|
|
+ kwaiJSTMapper.replaceKwaixiaodianJstPurchaseItems(itemList);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|