|
|
package com.yohobuy.platform.grass.service.impl;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yohobuy.platform.common.exception.PlatformException;
|
|
|
import com.yohobuy.platform.common.service.IBusinessImportService;
|
|
|
import com.yohobuy.platform.common.util.ListUtil;
|
|
|
import com.yohobuy.platform.dal.cms.IGrassFindGoodsDAO;
|
|
|
import com.yohobuy.platform.dal.cms.model.GrassFindGoods;
|
|
|
import com.yohobuy.platform.model.cms.bo.FindGoodsImportBO;
|
|
|
import com.yohobuy.platform.model.grass.request.GrassCacheReq;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.yohobuy.platform.grass.service.IGrassRefreshCacheService;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 发现好货批量导入
|
|
|
*/
|
|
|
@Service("grassFindGoodsService")
|
|
|
public class GrassFindGoodsServiceImpl implements IBusinessImportService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(GrassFindGoodsServiceImpl.class);
|
|
|
|
|
|
@Resource
|
|
|
private IGrassFindGoodsDAO grassFindGoodsDAO;
|
|
|
@Resource
|
|
|
private IGrassRefreshCacheService grassRefreshCacheService;
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public Class getDataClass() {
|
|
|
return FindGoodsImportBO.class;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public Object batchImport(List<Object> dataList) throws PlatformException, ExecutionException, InterruptedException {
|
|
|
if (CollectionUtils.isEmpty(dataList)) {
|
|
|
throw new PlatformException("导入数据为空", 400);
|
|
|
}
|
|
|
List<FindGoodsImportBO> importBOs = ListUtil.convertActualObjectList(dataList, FindGoodsImportBO.class);
|
|
|
//查询已存在的skn,做更新
|
|
|
List<Integer> sknList = new ArrayList<>();
|
|
|
int insertSize = 0, updateSize = 0;
|
|
|
Integer skn;
|
|
|
List<Integer> failSKNs = Lists.newArrayList();
|
|
|
for (FindGoodsImportBO importBO : importBOs) {
|
|
|
|
|
|
// 如果包含大 E 说明被转成 5.1120003E7 这种形式了
|
|
|
if (importBO.getProductSKN().contains("E")) {
|
|
|
skn = Double.valueOf(importBO.getProductSKN()).intValue();
|
|
|
} else {
|
|
|
skn = Integer.valueOf(importBO.getProductSKN());
|
|
|
}
|
|
|
|
|
|
if (String.valueOf(skn).length() > 11) {
|
|
|
failSKNs.add(skn);
|
|
|
continue;
|
|
|
}
|
|
|
importBO.setProductSKN(String.valueOf(skn));
|
|
|
sknList.add(skn);
|
|
|
}
|
|
|
//批量更新
|
|
|
List<GrassFindGoods> existList = grassFindGoodsDAO.selectBySknList(sknList);
|
|
|
Map<Integer,GrassFindGoods> existMap = existList.stream().collect(Collectors.toMap(GrassFindGoods::getProductSkn, obj -> obj));
|
|
|
if(CollectionUtils.isNotEmpty(existList)) {
|
|
|
List<Integer> existSkns = existList.stream().map(GrassFindGoods::getProductSkn).collect(Collectors.toList());
|
|
|
List<FindGoodsImportBO> updateList = importBOs.stream().filter(importBO -> existSkns.contains
|
|
|
(Integer.parseInt(importBO.getProductSKN()))).collect(Collectors.toList());
|
|
|
for(int i = 0;i < updateList.size();i++) {
|
|
|
FindGoodsImportBO importBO = updateList.get(i);
|
|
|
try {
|
|
|
GrassFindGoods grassFindGoods = new GrassFindGoods();
|
|
|
grassFindGoods.setUpdateTime(System.currentTimeMillis());
|
|
|
grassFindGoods.setProductSkn(Integer.parseInt(importBO.getProductSKN()));
|
|
|
grassFindGoods.setProductType(importBO.getProductType());
|
|
|
grassFindGoods.setOrderBy(importBO.getOrderBy());
|
|
|
grassFindGoods.setArticleId(importBO.getArticleId());
|
|
|
grassFindGoods.setId(existMap.get(Integer.parseInt(importBO.getProductSKN())).getId());
|
|
|
grassFindGoods.setIntroduce(importBO.getIntroduce());
|
|
|
grassFindGoodsDAO.updateByPrimaryKeySelective(grassFindGoods);
|
|
|
updateSize += 1;
|
|
|
}catch (Exception e) {
|
|
|
failSKNs.add(Integer.parseInt(importBO.getProductSKN()));
|
|
|
logger.info("GrassFindGoodsServiceImpl update error , skn is {} , exception is {}" ,
|
|
|
importBO.getProductSKN(),e);
|
|
|
}
|
|
|
}
|
|
|
importBOs.removeAll(updateList);
|
|
|
}
|
|
|
//批量insert
|
|
|
if(CollectionUtils.isNotEmpty(importBOs)){
|
|
|
for(int i = 0;i < importBOs.size();i++) {
|
|
|
FindGoodsImportBO importBO = importBOs.get(i);
|
|
|
try {
|
|
|
GrassFindGoods grassFindGoods = new GrassFindGoods();
|
|
|
grassFindGoods.setUpdateTime(System.currentTimeMillis());
|
|
|
grassFindGoods.setCreateTime(System.currentTimeMillis());
|
|
|
grassFindGoods.setProductSkn(Integer.parseInt(importBO.getProductSKN()));
|
|
|
grassFindGoods.setProductType(importBO.getProductType());
|
|
|
grassFindGoods.setOrderBy(importBO.getOrderBy());
|
|
|
grassFindGoods.setArticleId(importBO.getArticleId());
|
|
|
grassFindGoods.setIntroduce(importBO.getIntroduce());
|
|
|
grassFindGoodsDAO.insert(grassFindGoods);
|
|
|
insertSize += 1;
|
|
|
}catch (Exception e) {
|
|
|
failSKNs.add(Integer.parseInt(importBO.getProductSKN()));
|
|
|
logger.info("GrassFindGoodsServiceImpl add error , skn is {},exception is {}" ,
|
|
|
importBO.getProductSKN(),e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder message = new StringBuilder();
|
|
|
|
|
|
message.append("本次导入,成功 ").append(insertSize).append(" 个");
|
|
|
|
|
|
if (updateSize > 0) {
|
|
|
message.append(",更新 ").append(updateSize).append(" 个");
|
|
|
}
|
|
|
|
|
|
if (!failSKNs.isEmpty()) {
|
|
|
message.append(",失败 ").append(failSKNs.size()).append(" 个").append(",失败 SKN ").append(failSKNs.toString());
|
|
|
}
|
|
|
|
|
|
boolean hasFail = !failSKNs.isEmpty();
|
|
|
|
|
|
if (hasFail) {
|
|
|
throw new PlatformException(message.toString(), 400);
|
|
|
}
|
|
|
logger.info(message.toString());
|
|
|
//全部更新成功 清除发现好物缓存
|
|
|
GrassCacheReq cacheReq = new GrassCacheReq();
|
|
|
cacheReq.setClearCode(1008);
|
|
|
logger.info("clear grassFindGoodsCache");
|
|
|
grassRefreshCacheService.refreshGrassCache(cacheReq);
|
|
|
return message;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|