Authored by liangyi.chen@yoho.cn

发现好物批量导入

  1 +package com.yohobuy.platform.grass.service.impl;
  2 +
  3 +import com.google.common.collect.Lists;
  4 +import com.yohobuy.platform.common.exception.PlatformException;
  5 +import com.yohobuy.platform.common.service.IBusinessImportService;
  6 +import com.yohobuy.platform.common.util.ListUtil;
  7 +import com.yohobuy.platform.dal.cms.IGrassFindGoodsDAO;
  8 +import com.yohobuy.platform.dal.cms.model.GrassFindGoods;
  9 +import com.yohobuy.platform.model.cms.bo.FindGoodsImportBO;
  10 +import com.yohobuy.platform.model.grass.request.GrassCacheReq;
  11 +import org.apache.commons.collections.CollectionUtils;
  12 +import org.slf4j.Logger;
  13 +import org.slf4j.LoggerFactory;
  14 +import org.springframework.stereotype.Service;
  15 +import com.yohobuy.platform.grass.service.IGrassRefreshCacheService;
  16 +
  17 +import javax.annotation.Resource;
  18 +import java.util.ArrayList;
  19 +import java.util.List;
  20 +import java.util.Map;
  21 +import java.util.concurrent.ExecutionException;
  22 +import java.util.stream.Collectors;
  23 +
  24 +/**
  25 + * 发现好货批量导入
  26 + */
  27 +@Service("grassFindGoodsService")
  28 +public class GrassFindGoodsServiceImpl implements IBusinessImportService {
  29 +
  30 + private static final Logger logger = LoggerFactory.getLogger(GrassFindGoodsServiceImpl.class);
  31 +
  32 + @Resource
  33 + private IGrassFindGoodsDAO grassFindGoodsDAO;
  34 + @Resource
  35 + private IGrassRefreshCacheService grassRefreshCacheService;
  36 +
  37 +
  38 +
  39 + @Override
  40 + public Class getDataClass() {
  41 + return FindGoodsImportBO.class;
  42 + }
  43 +
  44 +
  45 + @Override
  46 + public Object batchImport(List<Object> dataList) throws PlatformException, ExecutionException, InterruptedException {
  47 + if (CollectionUtils.isEmpty(dataList)) {
  48 + throw new PlatformException("导入数据为空", 400);
  49 + }
  50 + List<FindGoodsImportBO> importBOs = ListUtil.convertActualObjectList(dataList, FindGoodsImportBO.class);
  51 + //查询已存在的skn,做更新
  52 + List<Integer> sknList = new ArrayList<>();
  53 + int insertSize = 0, updateSize = 0;
  54 + Integer skn;
  55 + List<Integer> failSKNs = Lists.newArrayList();
  56 + for (FindGoodsImportBO importBO : importBOs) {
  57 +
  58 + // 如果包含大 E 说明被转成 5.1120003E7 这种形式了
  59 + if (importBO.getProductSKN().contains("E")) {
  60 + skn = Double.valueOf(importBO.getProductSKN()).intValue();
  61 + } else {
  62 + skn = Integer.valueOf(importBO.getProductSKN());
  63 + }
  64 +
  65 + if (String.valueOf(skn).length() > 11) {
  66 + failSKNs.add(skn);
  67 + continue;
  68 + }
  69 + importBO.setProductSKN(String.valueOf(skn));
  70 + sknList.add(skn);
  71 + }
  72 + //批量更新
  73 + List<GrassFindGoods> existList = grassFindGoodsDAO.selectBySknList(sknList);
  74 + Map<Integer,GrassFindGoods> existMap = existList.stream().collect(Collectors.toMap(GrassFindGoods::getProductSkn, obj -> obj));
  75 + if(CollectionUtils.isNotEmpty(existList)) {
  76 + List<Integer> existSkns = existList.stream().map(GrassFindGoods::getProductSkn).collect(Collectors.toList());
  77 + List<FindGoodsImportBO> updateList = importBOs.stream().filter(importBO -> existSkns.contains
  78 + (Integer.parseInt(importBO.getProductSKN()))).collect(Collectors.toList());
  79 + for(int i = 0;i < updateList.size();i++) {
  80 + FindGoodsImportBO importBO = updateList.get(i);
  81 + try {
  82 + GrassFindGoods grassFindGoods = new GrassFindGoods();
  83 + grassFindGoods.setUpdateTime(System.currentTimeMillis());
  84 + grassFindGoods.setProductSkn(Integer.parseInt(importBO.getProductSKN()));
  85 + grassFindGoods.setProductType(importBO.getProductType());
  86 + grassFindGoods.setOrderBy(importBO.getOrderBy());
  87 + grassFindGoods.setArticleId(importBO.getArticleId());
  88 + grassFindGoods.setId(existMap.get(Integer.parseInt(importBO.getProductSKN())).getId());
  89 + grassFindGoods.setIntroduce(importBO.getIntroduce());
  90 + grassFindGoodsDAO.updateByPrimaryKeySelective(grassFindGoods);
  91 + updateSize += 1;
  92 + }catch (Exception e) {
  93 + failSKNs.add(Integer.parseInt(importBO.getProductSKN()));
  94 + logger.info("GrassFindGoodsServiceImpl update error , skn is {} , exception is {}" ,
  95 + importBO.getProductSKN(),e);
  96 + }
  97 + }
  98 + importBOs.removeAll(updateList);
  99 + }
  100 + //批量insert
  101 + if(CollectionUtils.isNotEmpty(importBOs)){
  102 + for(int i = 0;i < importBOs.size();i++) {
  103 + FindGoodsImportBO importBO = importBOs.get(i);
  104 + try {
  105 + GrassFindGoods grassFindGoods = new GrassFindGoods();
  106 + grassFindGoods.setUpdateTime(System.currentTimeMillis());
  107 + grassFindGoods.setCreateTime(System.currentTimeMillis());
  108 + grassFindGoods.setProductSkn(Integer.parseInt(importBO.getProductSKN()));
  109 + grassFindGoods.setProductType(importBO.getProductType());
  110 + grassFindGoods.setOrderBy(importBO.getOrderBy());
  111 + grassFindGoods.setArticleId(importBO.getArticleId());
  112 + grassFindGoods.setIntroduce(importBO.getIntroduce());
  113 + grassFindGoodsDAO.insert(grassFindGoods);
  114 + insertSize += 1;
  115 + }catch (Exception e) {
  116 + failSKNs.add(Integer.parseInt(importBO.getProductSKN()));
  117 + logger.info("GrassFindGoodsServiceImpl add error , skn is {},exception is {}" ,
  118 + importBO.getProductSKN(),e);
  119 + }
  120 + }
  121 + }
  122 +
  123 +
  124 +
  125 + StringBuilder message = new StringBuilder();
  126 +
  127 + message.append("本次导入,成功 ").append(insertSize).append(" 个");
  128 +
  129 + if (updateSize > 0) {
  130 + message.append(",更新 ").append(updateSize).append(" 个");
  131 + }
  132 +
  133 + if (!failSKNs.isEmpty()) {
  134 + message.append(",失败 ").append(failSKNs.size()).append(" 个").append(",失败 SKN ").append(failSKNs.toString());
  135 + }
  136 +
  137 + boolean hasFail = !failSKNs.isEmpty();
  138 +
  139 + if (hasFail) {
  140 + throw new PlatformException(message.toString(), 400);
  141 + }
  142 + logger.info(message.toString());
  143 + //全部更新成功 清除发现好物缓存
  144 + GrassCacheReq cacheReq = new GrassCacheReq();
  145 + cacheReq.setClearCode(1008);
  146 + logger.info("clear grassFindGoodsCache");
  147 + grassRefreshCacheService.refreshGrassCache(cacheReq);
  148 + return message;
  149 + }
  150 +
  151 +}
@@ -136,6 +136,7 @@ @@ -136,6 +136,7 @@
136 <entry key="priceUpdateImport" value-ref="priceUpdateImportServiceImpl"></entry> 136 <entry key="priceUpdateImport" value-ref="priceUpdateImportServiceImpl"></entry>
137 <entry key="vipReturnCoinBatch" value-ref="vipReturnCoinServiceImpl"></entry> 137 <entry key="vipReturnCoinBatch" value-ref="vipReturnCoinServiceImpl"></entry>
138 <entry key="changeSize" value-ref="changeSizeImportServiceImpl"></entry>--> 138 <entry key="changeSize" value-ref="changeSizeImportServiceImpl"></entry>-->
  139 + <entry key="grassFindGoods" value-ref="grassFindGoodsService"/>
139 </util:map> 140 </util:map>
140 <!-- 批量操作服务定义 --> 141 <!-- 批量操作服务定义 -->
141 <util:map id="batchExportBusiness" key-type="java.lang.String" 142 <util:map id="batchExportBusiness" key-type="java.lang.String"
@@ -362,6 +362,7 @@ datasources: @@ -362,6 +362,7 @@ datasources:
362 - com.yohobuy.platform.dal.grass.IGrassRewardsConfigDAO 362 - com.yohobuy.platform.dal.grass.IGrassRewardsConfigDAO
363 - com.yohobuy.platform.dal.grass.ITopicRewardsRelateDAO 363 - com.yohobuy.platform.dal.grass.ITopicRewardsRelateDAO
364 - com.yohobuy.platform.dal.grass.IGrassArticleExtraDao 364 - com.yohobuy.platform.dal.grass.IGrassArticleExtraDao
  365 + - com.yohobuy.platform.dal.cms.IGrassFindGoodsDAO
365 366
366 yhb_promotion: 367 yhb_promotion:
367 servers: 368 servers:
@@ -360,6 +360,7 @@ datasources: @@ -360,6 +360,7 @@ datasources:
360 - com.yohobuy.platform.dal.grass.IGrassRewardsConfigDAO 360 - com.yohobuy.platform.dal.grass.IGrassRewardsConfigDAO
361 - com.yohobuy.platform.dal.grass.ITopicRewardsRelateDAO 361 - com.yohobuy.platform.dal.grass.ITopicRewardsRelateDAO
362 - com.yohobuy.platform.dal.grass.IGrassArticleExtraDao 362 - com.yohobuy.platform.dal.grass.IGrassArticleExtraDao
  363 + - com.yohobuy.platform.dal.cms.IGrassFindGoodsDAO
363 364
364 yhb_promotion: 365 yhb_promotion:
365 servers: 366 servers:
@@ -71,6 +71,7 @@ @@ -71,6 +71,7 @@
71 <url-pattern>/common/shopCouponLimit.xlsx</url-pattern> 71 <url-pattern>/common/shopCouponLimit.xlsx</url-pattern>
72 <url-pattern>/common/vipCoinReturn.xlsx</url-pattern> 72 <url-pattern>/common/vipCoinReturn.xlsx</url-pattern>
73 <url-pattern>/common/productShowSize.xlsx</url-pattern> 73 <url-pattern>/common/productShowSize.xlsx</url-pattern>
  74 + <url-pattern>/common/findGoodsTemplate.xlsx</url-pattern>
74 </servlet-mapping> 75 </servlet-mapping>
75 76
76 77
  1 +<!DOCTYPE html>
  2 +<html>
  3 +<head>
  4 + <meta charset="UTF-8" />
  5 + <title>Yoho!Buy运营平台</title>
  6 + <script src="/pfcms/js/include.js"></script>
  7 + <script src="/pfcms/js/ajaxfileupload.js"></script>
  8 + <script src="/pfcms/html/grass/findgoods/index.js"></script>
  9 + <style>
  10 + .btn-long {
  11 + width: 120px;
  12 + height: 37px;
  13 + line-height: 37px;
  14 + font-size: 15px;
  15 + color: white;
  16 + border-radius: 5px;
  17 + display: inline-block;
  18 + cursor: pointer;
  19 + text-align: center;
  20 + }
  21 + .btn-long:hover {
  22 + opacity: 0.9;
  23 + }
  24 +
  25 + </style>
  26 +</head>
  27 +<body class="easyui-layout" >
  28 +<div region="north" style="height: 200px;">
  29 + <script>
  30 + document.write(addHead('种草社区管理', '发现好货批量导入'));
  31 + </script>
  32 +
  33 + <a id="import" style="margin-left: 30px"></a>
  34 +</div>
  35 +
  36 +</body>
  37 +</html>
  1 +$(function(){
  2 +
  3 +
  4 + $("#import").linkbutton({
  5 + text : "批量导入",
  6 + iconCls : "icon-save"
  7 + }).click(function() {
  8 + importProduct();
  9 + });
  10 +
  11 + function importProduct(){
  12 + var div = $("<div>").appendTo($(window.document.body));
  13 + window.$(div).myDialog({
  14 + width : "800px",
  15 + height : "60%",
  16 + title : "批量设置",
  17 + href : contextPath+"/html/grass/findgoods/upload.html",
  18 + modal : true,
  19 + collapsible : true,
  20 + cache : false,
  21 + buttons : [{
  22 + text : "上传",
  23 + id : "saveUserBtn",
  24 + iconCls : "icon-save",
  25 + handler : function() {
  26 + $("#uploadForm").form("submit", {
  27 + url : contextPath + "/batch/import",
  28 + onSubmit : function() {
  29 + var file=$("#uploadForm #file").filebox("getValue");
  30 + if ( file== "") {
  31 + $.messager.alert("错误", "请选择上传文件", "error");
  32 + return false;
  33 + }
  34 + if(file.indexOf("xlsx")<0&&file.indexOf("XLSX")<0){
  35 + $.messager.alert("错误", "请选择上传xlsx文件", "error");
  36 + return false;
  37 + }
  38 + },
  39 + success : function(data) {
  40 + data = JSON.parse(data);
  41 + if (data.code==200) {
  42 + if(data.data&&data.data.failFileReason&&data.data.failFileReason.length>0){
  43 + var text="";
  44 + $.each(data.data.failFileReason,function(){
  45 + text+=this+"<br>"
  46 + });
  47 + $.messager.alert("提示", text);
  48 +
  49 + }else{
  50 + $.messager.alert("提示", "上传成功");
  51 + $("#uploadForm #file").filebox("clear");
  52 + load(1);
  53 + window.$(div).dialog("close");
  54 + }
  55 +
  56 + }
  57 + else if(data.code==500){
  58 + $.messager.alert("提示",data.message?data.message:"上传内容不合法");
  59 + }else {
  60 + $.messager.alert("提示", "上传失败");
  61 + }
  62 +
  63 + }
  64 + });
  65 + }
  66 + }, {
  67 + text : "关闭",
  68 + iconCls : "icon-cancel",
  69 + handler : function() {
  70 + window.$(div).dialog("close");
  71 +
  72 + }
  73 + }]
  74 + });
  75 + }
  76 +});
  77 +
  1 +<!DOCTYPE html>
  2 +<form id="uploadForm" method="post" name="uploadForm" enctype="multipart/form-data" style="margin-top: 30px">
  3 + <input type="hidden" name="type"value="grassFindGoods"/>
  4 + <table border="0" align="center" style="line-height: 30px;width:95%">
  5 + <tr>
  6 + <td align="right" style="width:200px">上传EXCEL<span class="requriedInput">*</span></td>
  7 + <td><input name="file" id="file" class="easyui-filebox" data-options="buttonText: '请选择文件'" accept=".xlsx" style="width:600px" /></td>
  8 + </tr>
  9 + <tr>
  10 + <td align="right" valign="top">说明:</td>
  11 + <td>
  12 + 1、上传文件必须是.xlsx文件<br>
  13 + 2、Excel表头为:文章Id、SKN、产品类型、排序<br>
  14 + 3、第一行为表头内容,第二行开始为正式内容<br>
  15 + 4、每个文件控制在1000行以内<br>
  16 + <!--<a href="/pfcms/common/ok.jsp" download="ok.jsp">下载样例</a>-->
  17 + 5、<a href="/pfcms/common/findGoodsTemplate.xlsx" download="findGoodsTemplate.xlsx">下载样例</a>
  18 + </td>
  19 + </tr>
  20 + </table>
  21 +</form>