|
|
|
@ -1,8 +1,14 @@
|
|
|
|
|
package com.ms.api.service.impl;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.doudian.open.api.shop_getShopCategory.data.DataItem;
|
|
|
|
|
import com.ms.api.bo.CompareAndGetNeedProcessCidsBO;
|
|
|
|
|
import com.ms.api.common.Ret;
|
|
|
|
|
import com.ms.api.consts.RedisKeyConst;
|
|
|
|
|
import com.ms.api.paas.StorageService;
|
|
|
|
|
import com.ms.api.tool.CommonTool;
|
|
|
|
|
import com.ms.api.tool.MoveTool;
|
|
|
|
|
import com.ms.api.util.DdRequestUtil;
|
|
|
|
|
import com.ms.dal.entity.Category;
|
|
|
|
|
import com.ms.api.service.CategoryService;
|
|
|
|
@ -17,10 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -34,6 +37,9 @@ public class CategoryServiceImpl implements CategoryService{
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private StorageService storageService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RsyncCategoryQueueMapper rsyncCategoryQueueMapper;
|
|
|
|
|
|
|
|
|
@ -122,7 +128,13 @@ public class CategoryServiceImpl implements CategoryService{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void recordShopCategorys(Integer shopId, List<DataItem> shopAllCategoryList) {
|
|
|
|
|
public List<Long> recordShopCategorys(Integer shopId, List<DataItem> shopAllCategoryList) {
|
|
|
|
|
recordCategoryShop(shopId, shopAllCategoryList);
|
|
|
|
|
List<Long> recordCategoryIds = recordCategory(shopAllCategoryList);
|
|
|
|
|
return recordCategoryIds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void recordCategoryShop(Integer shopId, List<DataItem> shopAllCategoryList) {
|
|
|
|
|
// Map<Long, DataItem> shopAllCategoryMap = CommonTool.convertListToMap(shopAllCategoryList, DataItem::getId);
|
|
|
|
|
List<Long> existCids = categoryShopMapper.selectCategoryIdListByShopId(shopId);
|
|
|
|
|
List<Long> realCids = shopAllCategoryList.stream().map(DataItem::getId).collect(Collectors.toList());
|
|
|
|
@ -143,9 +155,209 @@ public class CategoryServiceImpl implements CategoryService{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Long> recordCategory(List<DataItem> shopAllCategoryList) {
|
|
|
|
|
List<Long> recordCategoryIds = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
List<Long> realCids = shopAllCategoryList.stream().map(DataItem::getId).collect(Collectors.toList());
|
|
|
|
|
if (realCids.isEmpty()) {
|
|
|
|
|
return recordCategoryIds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<Long, DataItem> shopAllCategoryMap = CommonTool.convertListToMap(shopAllCategoryList, DataItem::getId);
|
|
|
|
|
|
|
|
|
|
CompareAndGetNeedProcessCidsBO compareAndGetNeedProcessCidsBO = compareAndGetNeedProcessCids(shopAllCategoryMap, realCids);
|
|
|
|
|
|
|
|
|
|
for (Long cid : compareAndGetNeedProcessCidsBO.getNeedInsertCategoryCids()) {
|
|
|
|
|
try {
|
|
|
|
|
DataItem category = shopAllCategoryMap.get(cid);
|
|
|
|
|
Category obj = new Category();
|
|
|
|
|
obj.setCategoryId(category.getId());
|
|
|
|
|
obj.setCategoryName(category.getName());
|
|
|
|
|
obj.setParentCategoryId(category.getParentId().toString());
|
|
|
|
|
obj.setLevel(category.getLevel().intValue());
|
|
|
|
|
obj.setHasChildren(ObjectUtil.isNotNull(category.getIsLeaf()) && category.getIsLeaf() ? 0 : 1);
|
|
|
|
|
obj.setGmtModified(new Date());
|
|
|
|
|
obj.setGmtCreate(new Date());
|
|
|
|
|
categoryMapper.insertSelective(obj);
|
|
|
|
|
recordCategoryIds.add(category.getId());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("recordCategory insert error: ", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Long cid : compareAndGetNeedProcessCidsBO.getNeedUpdateCategoryCids()) {
|
|
|
|
|
try {
|
|
|
|
|
DataItem category = shopAllCategoryMap.get(cid);
|
|
|
|
|
Category obj = new Category();
|
|
|
|
|
obj.setCategoryId(category.getId());
|
|
|
|
|
obj.setCategoryName(category.getName());
|
|
|
|
|
obj.setParentCategoryId(category.getParentId().toString());
|
|
|
|
|
obj.setLevel(category.getLevel().intValue());
|
|
|
|
|
obj.setHasChildren(ObjectUtil.isNotNull(category.getIsLeaf()) && category.getIsLeaf() ? 0 : 1);
|
|
|
|
|
obj.setGmtModified(new Date());
|
|
|
|
|
categoryMapper.updateByPrimaryKeySelective(obj);
|
|
|
|
|
recordCategoryIds.add(category.getId());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("recordCategory update error: ", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return recordCategoryIds;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CompareAndGetNeedProcessCidsBO compareAndGetNeedProcessCids(Map<Long, DataItem> shopAllCategoryMap, List<Long> realCids) {
|
|
|
|
|
final int chunkLen = 100;
|
|
|
|
|
List<Long> needInsertCategoryCids = new ArrayList<>();
|
|
|
|
|
List<Long> needUpdateCategoryCids = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < realCids.size(); i += chunkLen) {
|
|
|
|
|
List<Long> tempRealCids = realCids.subList(i, Math.min(i + chunkLen, realCids.size()));
|
|
|
|
|
List<Category> existCategoryInfos = categoryMapper.selectByCids(tempRealCids);
|
|
|
|
|
List<Long> existCids = existCategoryInfos.stream().map(Category::getCategoryId).collect(Collectors.toList());
|
|
|
|
|
tempRealCids.removeAll(existCids);
|
|
|
|
|
needInsertCategoryCids.addAll(tempRealCids);
|
|
|
|
|
|
|
|
|
|
for (Category existCategoryInfo : existCategoryInfos) {
|
|
|
|
|
DataItem category = shopAllCategoryMap.get(existCategoryInfo.getCategoryId());
|
|
|
|
|
Integer hasChildren = (ObjectUtil.isNotNull(category.getIsLeaf()) && category.getIsLeaf() ? 0 : 1);
|
|
|
|
|
if (category.getName().equals(existCategoryInfo.getCategoryName())
|
|
|
|
|
&& category.getParentId() == Long.valueOf(existCategoryInfo.getParentCategoryId())
|
|
|
|
|
&& category.getLevel() == existCategoryInfo.getLevel()
|
|
|
|
|
&& hasChildren == existCategoryInfo.getHasChildren()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
needUpdateCategoryCids.add(existCategoryInfo.getCategoryId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CompareAndGetNeedProcessCidsBO ret = new CompareAndGetNeedProcessCidsBO();
|
|
|
|
|
ret.setNeedInsertCategoryCids(needInsertCategoryCids);
|
|
|
|
|
ret.setNeedUpdateCategoryCids(needUpdateCategoryCids);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean buildProductCatePath(List<Long> categoryIds) {
|
|
|
|
|
int successCnt = 0;
|
|
|
|
|
|
|
|
|
|
Map<Long, Category> categoryMap = getAllCategoryMap();
|
|
|
|
|
log.info("start buildProductCatePath");
|
|
|
|
|
for (Map.Entry<Long, Category> entry : categoryMap.entrySet()) {
|
|
|
|
|
Long categoryId = entry.getKey();
|
|
|
|
|
Category categoryInfo = entry.getValue();
|
|
|
|
|
|
|
|
|
|
// todo: 之前有用个本地文件控制同步停止,可有可无先忽略
|
|
|
|
|
|
|
|
|
|
if (categoryInfo.getLevel() <= 1) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (categoryIds.size() > 0 && !categoryIds.contains(categoryId)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String realPath = findCategoryRealPath(categoryMap, categoryId, "");
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isEmpty(realPath) || realPath.equals(categoryInfo.getPath())) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Category updateData = new Category();
|
|
|
|
|
updateData.setCategoryId(categoryId);
|
|
|
|
|
updateData.setPath(realPath);
|
|
|
|
|
updateData.setGmtModified(new Date());
|
|
|
|
|
categoryMapper.updateByPrimaryKeySelective(updateData);
|
|
|
|
|
|
|
|
|
|
successCnt++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.info("end buildProductCatePath");
|
|
|
|
|
return successCnt > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String findCategoryRealPath(Map<Long, Category> categoryMap, Long categoryId, String path) {
|
|
|
|
|
if (!categoryMap.containsKey(categoryId)) {
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
Category categoryInfo = categoryMap.get(categoryId);
|
|
|
|
|
|
|
|
|
|
path = !StringUtils.isEmpty(path) ? (categoryInfo.getCategoryName() + ">" + path) : categoryInfo.getCategoryName();
|
|
|
|
|
if (!StringUtils.isEmpty(categoryInfo.getParentCategoryId()) && Long.valueOf(categoryInfo.getParentCategoryId()) != 0L) {
|
|
|
|
|
path = findCategoryRealPath(categoryMap, Long.valueOf(categoryInfo.getParentCategoryId()), path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Map<Long, Category> getAllCategoryMap() {
|
|
|
|
|
final int limit = 100;
|
|
|
|
|
long startCateId = 0L;
|
|
|
|
|
Map<Long, Category> retMap = new HashMap<>();
|
|
|
|
|
while (true) {
|
|
|
|
|
List<Category> rows = categoryMapper.selectByPositionPage(startCateId, limit);
|
|
|
|
|
if (rows.isEmpty()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
for (Category row : rows) {
|
|
|
|
|
retMap.put(row.getCategoryId(), row);
|
|
|
|
|
startCateId = row.getCategoryId();
|
|
|
|
|
}
|
|
|
|
|
if (rows.size() < limit) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return retMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void getAllLeafProductCatListFromTos(Integer shopId, boolean isFlushCache) {
|
|
|
|
|
public List<Category> getAllLeafProductCatListFromTos(Integer shopId, boolean isFlushCache) {
|
|
|
|
|
String tosPath = MoveTool.getAllLeafProductCatListOssPath(shopId);
|
|
|
|
|
|
|
|
|
|
List<Category> cateListAll = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Ret getTosContentRet = storageService.getContent(tosPath);
|
|
|
|
|
if (getTosContentRet.getResult().equals("success") && !isFlushCache) {
|
|
|
|
|
cateListAll = JSONObject.parseArray(getTosContentRet.getData().get("data").toString(), Category.class);
|
|
|
|
|
if (ObjectUtil.isNotEmpty(cateListAll)) {
|
|
|
|
|
return cateListAll;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("商品类目TOS读取操作过程异常:", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cateListAll = getAllLeafProductCatListFromDb(shopId);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
storageService.uploadContent(tosPath, JSONObject.toJSONString(cateListAll));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("商品类目TOS写入操作过程异常:", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo: tosPath文件 加入 tos自动清理队列,目前isFlushCache用的都是true,不清理也基本没影响,用到false的时候再说,有效期1天后自动删除,
|
|
|
|
|
|
|
|
|
|
return cateListAll;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Category> getAllLeafProductCatListFromDb(Integer shopId) {
|
|
|
|
|
List<Category> cateListAll = new ArrayList<>();
|
|
|
|
|
final int stepLen = 200;
|
|
|
|
|
Long maxCid = 0L;
|
|
|
|
|
while (true) {
|
|
|
|
|
List<Category> cateList = categoryMapper.selectLeafByPositionPage(shopId, maxCid, stepLen);
|
|
|
|
|
if (cateList.isEmpty()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
cateListAll.addAll(cateList);
|
|
|
|
|
if (cateList.size() < stepLen) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
maxCid = cateList.get(cateList.size() - 1).getCategoryId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cateListAll;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|