类目同步

20230922-ljl-fixBug
wangchaoxu 1 year ago
parent 1cfb737c34
commit d8f90bf52e

@ -32,9 +32,9 @@ public interface CategoryService {
List<Long> recordShopCategorys(Integer shopId, List<DataItem> shopAllCategoryList); List<Long> recordShopCategorys(Integer shopId, List<DataItem> shopAllCategoryList);
void getAllLeafProductCatListFromTos(Integer shopId, boolean isFlushCache);
boolean buildProductCatePath(List<Long> recordCategoryIds); boolean buildProductCatePath(List<Long> recordCategoryIds);
Map<Long, Category> getAllCategoryMap(); Map<Long, Category> getAllCategoryMap();
List<Category> getAllLeafProductCatListFromTos(Integer shopId, boolean isFlushCache);
} }

@ -1,10 +1,14 @@
package com.ms.api.service.impl; package com.ms.api.service.impl;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.doudian.open.api.shop_getShopCategory.data.DataItem; import com.doudian.open.api.shop_getShopCategory.data.DataItem;
import com.ms.api.bo.CompareAndGetNeedProcessCidsBO; import com.ms.api.bo.CompareAndGetNeedProcessCidsBO;
import com.ms.api.common.Ret;
import com.ms.api.consts.RedisKeyConst; import com.ms.api.consts.RedisKeyConst;
import com.ms.api.paas.StorageService;
import com.ms.api.tool.CommonTool; import com.ms.api.tool.CommonTool;
import com.ms.api.tool.MoveTool;
import com.ms.api.util.DdRequestUtil; import com.ms.api.util.DdRequestUtil;
import com.ms.dal.entity.Category; import com.ms.dal.entity.Category;
import com.ms.api.service.CategoryService; import com.ms.api.service.CategoryService;
@ -33,6 +37,9 @@ public class CategoryServiceImpl implements CategoryService{
@Autowired @Autowired
private RedisTemplate<String, String> redisTemplate; private RedisTemplate<String, String> redisTemplate;
@Autowired
private StorageService storageService;
@Autowired @Autowired
private RsyncCategoryQueueMapper rsyncCategoryQueueMapper; private RsyncCategoryQueueMapper rsyncCategoryQueueMapper;
@ -234,11 +241,13 @@ public class CategoryServiceImpl implements CategoryService{
int successCnt = 0; int successCnt = 0;
Map<Long, Category> categoryMap = getAllCategoryMap(); Map<Long, Category> categoryMap = getAllCategoryMap();
log.info("start buildProductCatePath");
for (Map.Entry<Long, Category> entry : categoryMap.entrySet()) { for (Map.Entry<Long, Category> entry : categoryMap.entrySet()) {
Long categoryId = entry.getKey(); Long categoryId = entry.getKey();
Category categoryInfo = entry.getValue(); Category categoryInfo = entry.getValue();
// todo: 之前有用个本地文件控制同步停止,可有可无先忽略
if (categoryInfo.getLevel() <= 1) { if (categoryInfo.getLevel() <= 1) {
continue; continue;
} }
@ -247,16 +256,37 @@ public class CategoryServiceImpl implements CategoryService{
continue; continue;
} }
// todo: 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;
}
// todo: 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 successCnt > 0; return path;
} }
@Override @Override
@ -281,8 +311,53 @@ public class CategoryServiceImpl implements CategoryService{
} }
@Override @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;
} }
} }

@ -96,21 +96,9 @@ public class SyncShopCategoryQueueTaskService extends TaskBaseService {
log.info("end buildProductCatePath"); log.info("end buildProductCatePath");
} }
log.info("start flush ShopCategorys tos");
log.info("start flush ShopCategorys oss");
// oss
categoryService.getAllLeafProductCatListFromTos(shopId, true); categoryService.getAllLeafProductCatListFromTos(shopId, true);
log.info("end flush ShopCategorys tos");
// String ossPath = String.format("move_product_publish_to_pic_queue/%s/%s.txt", shopId, detailId);
// Ret ret = storageService.uploadContent(ossPath, JSON.toJSONString(data));
// if (CommonTool.isFailRet(ret)) {
// return ret;
// }
// String ossUrl = storageService.getPubOssUrlByOssPath(ossPath);
// log.info("uploadMoveDataToOss success ossUrl: " + ossUrl);
// return CommonTool.successResult("ossUrl", ossUrl);
log.info("end flush ShopCategorys oss");
return queue; return queue;
} }

@ -26,4 +26,6 @@ public interface CategoryMapper {
List<Category> selectByCids(List<Long> categoryIds); List<Category> selectByCids(List<Long> categoryIds);
List<Category> selectByPositionPage(Long startId, Integer limit); List<Category> selectByPositionPage(Long startId, Integer limit);
List<Category> selectLeafByPositionPage(Integer shopId, Long startId, Integer limit);
} }

@ -45,6 +45,15 @@
order by category_id asc order by category_id asc
limit #{limit} limit #{limit}
</select> </select>
<select id="selectLeafByPositionPage" resultMap="BaseResultMap">
select c.*
from category c
left join category_shop cs on cs.category_id = c.category_id and cs.shop_id = #{shopId}
where c.category_id > #{startId}
and c.has_children = 0
order by c.category_id asc
limit #{limit}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long"> <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from category delete from category

Loading…
Cancel
Save