Authored by liangyi.chen@yoho.cn

发现好物批量导入

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;
}
}
... ...
... ... @@ -136,6 +136,7 @@
<entry key="priceUpdateImport" value-ref="priceUpdateImportServiceImpl"></entry>
<entry key="vipReturnCoinBatch" value-ref="vipReturnCoinServiceImpl"></entry>
<entry key="changeSize" value-ref="changeSizeImportServiceImpl"></entry>-->
<entry key="grassFindGoods" value-ref="grassFindGoodsService"/>
</util:map>
<!-- 批量操作服务定义 -->
<util:map id="batchExportBusiness" key-type="java.lang.String"
... ...
... ... @@ -362,6 +362,7 @@ datasources:
- com.yohobuy.platform.dal.grass.IGrassRewardsConfigDAO
- com.yohobuy.platform.dal.grass.ITopicRewardsRelateDAO
- com.yohobuy.platform.dal.grass.IGrassArticleExtraDao
- com.yohobuy.platform.dal.cms.IGrassFindGoodsDAO
yhb_promotion:
servers:
... ...
... ... @@ -360,6 +360,7 @@ datasources:
- com.yohobuy.platform.dal.grass.IGrassRewardsConfigDAO
- com.yohobuy.platform.dal.grass.ITopicRewardsRelateDAO
- com.yohobuy.platform.dal.grass.IGrassArticleExtraDao
- com.yohobuy.platform.dal.cms.IGrassFindGoodsDAO
yhb_promotion:
servers:
... ...
... ... @@ -71,6 +71,7 @@
<url-pattern>/common/shopCouponLimit.xlsx</url-pattern>
<url-pattern>/common/vipCoinReturn.xlsx</url-pattern>
<url-pattern>/common/productShowSize.xlsx</url-pattern>
<url-pattern>/common/findGoodsTemplate.xlsx</url-pattern>
</servlet-mapping>
... ...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Yoho!Buy运营平台</title>
<script src="/pfcms/js/include.js"></script>
<script src="/pfcms/js/ajaxfileupload.js"></script>
<script src="/pfcms/html/grass/findgoods/index.js"></script>
<style>
.btn-long {
width: 120px;
height: 37px;
line-height: 37px;
font-size: 15px;
color: white;
border-radius: 5px;
display: inline-block;
cursor: pointer;
text-align: center;
}
.btn-long:hover {
opacity: 0.9;
}
</style>
</head>
<body class="easyui-layout" >
<div region="north" style="height: 200px;">
<script>
document.write(addHead('种草社区管理', '发现好货批量导入'));
</script>
<a id="import" style="margin-left: 30px"></a>
</div>
</body>
</html>
\ No newline at end of file
... ...
$(function(){
$("#import").linkbutton({
text : "批量导入",
iconCls : "icon-save"
}).click(function() {
importProduct();
});
function importProduct(){
var div = $("<div>").appendTo($(window.document.body));
window.$(div).myDialog({
width : "800px",
height : "60%",
title : "批量设置",
href : contextPath+"/html/grass/findgoods/upload.html",
modal : true,
collapsible : true,
cache : false,
buttons : [{
text : "上传",
id : "saveUserBtn",
iconCls : "icon-save",
handler : function() {
$("#uploadForm").form("submit", {
url : contextPath + "/batch/import",
onSubmit : function() {
var file=$("#uploadForm #file").filebox("getValue");
if ( file== "") {
$.messager.alert("错误", "请选择上传文件", "error");
return false;
}
if(file.indexOf("xlsx")<0&&file.indexOf("XLSX")<0){
$.messager.alert("错误", "请选择上传xlsx文件", "error");
return false;
}
},
success : function(data) {
data = JSON.parse(data);
if (data.code==200) {
if(data.data&&data.data.failFileReason&&data.data.failFileReason.length>0){
var text="";
$.each(data.data.failFileReason,function(){
text+=this+"<br>"
});
$.messager.alert("提示", text);
}else{
$.messager.alert("提示", "上传成功");
$("#uploadForm #file").filebox("clear");
load(1);
window.$(div).dialog("close");
}
}
else if(data.code==500){
$.messager.alert("提示",data.message?data.message:"上传内容不合法");
}else {
$.messager.alert("提示", "上传失败");
}
}
});
}
}, {
text : "关闭",
iconCls : "icon-cancel",
handler : function() {
window.$(div).dialog("close");
}
}]
});
}
});
... ...
<!DOCTYPE html>
<form id="uploadForm" method="post" name="uploadForm" enctype="multipart/form-data" style="margin-top: 30px">
<input type="hidden" name="type"value="grassFindGoods"/>
<table border="0" align="center" style="line-height: 30px;width:95%">
<tr>
<td align="right" style="width:200px">上传EXCEL<span class="requriedInput">*</span></td>
<td><input name="file" id="file" class="easyui-filebox" data-options="buttonText: '请选择文件'" accept=".xlsx" style="width:600px" /></td>
</tr>
<tr>
<td align="right" valign="top">说明:</td>
<td>
1、上传文件必须是.xlsx文件<br>
2、Excel表头为:文章Id、SKN、产品类型、排序<br>
3、第一行为表头内容,第二行开始为正式内容<br>
4、每个文件控制在1000行以内<br>
<!--<a href="/pfcms/common/ok.jsp" download="ok.jsp">下载样例</a>-->
5、<a href="/pfcms/common/findGoodsTemplate.xlsx" download="findGoodsTemplate.xlsx">下载样例</a>
</td>
</tr>
</table>
</form>
... ...