diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/basic/TtMerchantPoiController.java b/mallinkAdmin/src/main/java/com/iformall/controller/basic/TtMerchantPoiController.java index a36b9334c..adc8c0ef1 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/basic/TtMerchantPoiController.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/basic/TtMerchantPoiController.java @@ -2,26 +2,38 @@ package com.iformall.controller.basic; import com.github.pagehelper.PageInfo; import com.iformall.annotation.SystemControllerLog; +import com.iformall.annotation.TenantIgnore; import com.iformall.common.ErrorCode; +import com.iformall.common.Result; import com.iformall.common.ResultData; import com.iformall.controller.base.BaseController; +import com.iformall.controller.mem.AsyncTask; import com.iformall.domain.po.*; import com.iformall.domain.po.base.BaseEntity; import com.iformall.domain.po.base.TenantEntity; import com.iformall.enums.*; +import com.iformall.exception.MallinkException; import com.iformall.service.*; +import com.iformall.utils.Constant; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** @@ -32,6 +44,9 @@ import java.util.stream.Collectors; public class TtMerchantPoiController extends BaseController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); + @Autowired + private String fmUploadDir; + @Autowired private TtMerchantPoiService ttMerchantPoiService; @@ -41,6 +56,12 @@ public class TtMerchantPoiController extends BaseController { @Autowired private WxCouponService wxCouponService; + @Autowired + StringRedisTemplate stringRedisTemplate; + + @Autowired + private AsyncTask asyncTask; + @ApiOperation("分页列表接口") @GetMapping("list") @ApiImplicitParams({ @@ -213,4 +234,88 @@ public class TtMerchantPoiController extends BaseController { // return ttMerchantPoiService.spuStockSync(getTenantInfo(),couponChannelId); // } + @TenantIgnore + @PostMapping(value = "/importPoi", consumes = "multipart/*") + @SystemControllerLog(description = "poi-导入数据") + public ResultData importPoi(@RequestParam("file") MultipartFile mFile) { + logger.debug("[" + getIpAddr() + "] TtMerchantPoiController::importPoi"); + if (mFile.isEmpty()) { + throw new MallinkException(Result.ERROR, "上传文件不能为空"); + } + //得到当前用户ID + final MallUserInfo user = getUser(); + String userId = "" + user.getId(); + String importKey = Constant.importMemPrev + userId; + + //查询当前用户得到的值是否为空,为空继续,不为空,返回模板正在导入 + Boolean allCount = stringRedisTemplate.opsForHash().hasKey(importKey, "allCount"); + if (allCount) { + return new ResultData(Result.SUCCESS, "模板正在导入"); + } + + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allCount", 0 + ""); + stringRedisTemplate.expire(importKey,30, TimeUnit.MINUTES); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allSuccessCount", 0 + ""); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "processCount", ""); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "failCount", ""); + + String fpath = fmUploadDir; + File targetFile = new File(fpath); + if (!targetFile.exists()) { + targetFile.mkdirs(); + } + String fileName = "poi" + Math.round(Math.random() * 100000000000L); + int dot = mFile.getOriginalFilename().lastIndexOf('.'); + fileName = fileName + mFile.getOriginalFilename().substring(dot, mFile.getOriginalFilename().length()); + + File lFile = new File(fpath + File.separator + fileName); + + FileOutputStream fos = null; + BufferedInputStream fs = null; + try { + fos = new FileOutputStream(lFile); + fs = (BufferedInputStream) mFile.getInputStream(); + byte[] buffer = new byte[1024]; + int len = 0; + while ((len = fs.read(buffer)) != -1) { + fos.write(buffer, 0, len); + } + fos.close(); + fs.close(); + } catch (Exception e) { + stringRedisTemplate.expire(importKey,3,TimeUnit.SECONDS); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allCount", "1"); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "failCount", "1"); + logger.error(e.getMessage()); + return new ResultData(ErrorCode.MEM_IMPORT_ERR.getCode(), "模板上传失败"); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + stringRedisTemplate.expire(importKey,3,TimeUnit.SECONDS); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allCount", "1"); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "failCount", "1"); + logger.error(e.getMessage()); + return new ResultData(ErrorCode.MEM_IMPORT_ERR.getCode(), "模板上传失败"); + } + } + if (fs != null) { + try { + fs.close(); + } catch (IOException e) { + stringRedisTemplate.expire(importKey,3,TimeUnit.SECONDS); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allCount", "1"); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "failCount", "1"); + logger.error(e.getMessage()); + return new ResultData(ErrorCode.MEM_IMPORT_ERR.getCode(), "模板上传失败"); + } + } + } + + asyncTask.importExcelPoiData(lFile, user, importKey); + + return new ResultData(Result.SUCCESS, "模板正在导入"); + } + } diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/mem/AsyncTask.java b/mallinkAdmin/src/main/java/com/iformall/controller/mem/AsyncTask.java index 782b0ed6b..e2af3637d 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/mem/AsyncTask.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/mem/AsyncTask.java @@ -8,6 +8,8 @@ import cn.afterturn.easypoi.handler.inter.IExcelDataHandler; import com.iformall.domain.po.MallUserInfo; import com.iformall.domain.po.WxTags; import com.iformall.domain.vo.CUserBaseInfoT; +import com.iformall.domain.vo.MerchantPoiT; +import com.iformall.service.TtMerchantPoiService; import com.iformall.service.WxCUserBasicInfoService; import com.iformall.service.WxTagsService; import org.apache.shiro.session.UnknownSessionException; @@ -29,6 +31,9 @@ public class AsyncTask { @Autowired private WxCUserBasicInfoService wxCUserBasicInfoService; + @Autowired + private TtMerchantPoiService ttMerchantPoiService; + @Autowired private WxTagsService wxTagsService; @@ -47,6 +52,18 @@ public class AsyncTask { } + private class PoiExcelHandler extends ExcelDataHandlerDefaultImpl { + @Override + public Object importHandler(MerchantPoiT obj, String name, Object value) { + if (value == null) { + value = ""; + } + System.out.println(name + " + " + value.toString()); + return super.importHandler(obj, name, value); + } + + } + private void set_redis_value(String importKey, String allCount, String allSuccessCount, String processCount, String failCount, boolean fail) { stringRedisTemplate.opsForHash().put(importKey, "allCount", allCount); stringRedisTemplate.opsForHash().put(importKey, "allSuccessCount", allSuccessCount); @@ -118,4 +135,64 @@ public class AsyncTask { } } } + + @Async + public void importExcelPoiData(File file, MallUserInfo user, String importKey) { + ImportParams params = new ImportParams(); + // 需要验证 + params.setImportFields(new String[]{"服务商POI_ID", "POI名称", "省份", "城市", "地址", "经度", "纬度", "高德ID(非必填)", + "已匹配POI_ID", "已匹配POI名称", "已匹配POI省份", "已匹配POI城市", "已匹配POI地址", "未匹配原因", "其他信息"}); + IExcelDataHandler handler = new AsyncTask.PoiExcelHandler(); + handler.setNeedHandlerFields(new String[]{"服务商POI_ID","已匹配POI_ID"}); + params.setNeedVerify(true); + + ExcelImportResult datalist = null; + + try { + datalist = ExcelImportUtil.importExcelMore(file, MerchantPoiT.class, params); + } catch (Exception e) { + set_redis_value(importKey, "1", "0", "0", "1", true); + logger.error(e.getMessage()); + // 删除缓存文件 + file.delete(); + return; + } + // 删除缓存文件 + file.delete(); + + if(datalist == null) { + logger.error("导入模板失败: 模板数据解析失败"); + set_redis_value(importKey, "1", "0", "0", "1", true); + return; + } + + List successList = datalist.getList(); + List failList = datalist.getFailList(); + + logger.info("验证通过的数量: " + successList.size()); + logger.info("验证未通过的数量: " + failList.size()); + + int total = successList.size() + failList.size(); + int all_success = successList.size(); + int all_fail = failList.size(); + + //添加到redis里 + set_redis_value(importKey, "" + total, "" + all_success, "0", "" + all_fail, total == all_fail); + + if(successList.size() > 0) { + try { + successList.parallelStream().forEach(poiBase -> { + try { + ttMerchantPoiService.importOneMem(user,importKey, poiBase); + } catch (UnknownSessionException ue) { + logger.error("session :"+ue.getMessage()); + } + }); + } catch (Exception e) { + set_redis_value(importKey, "1", "0", "0", "1", true); + e.printStackTrace(); + logger.error("导入模板失败:"+e.getMessage()); + } + } + } } diff --git a/mallinkCallback/src/main/java/com/iformall/controller/callback/TtWebController.java b/mallinkCallback/src/main/java/com/iformall/controller/callback/TtWebController.java index 97a29fabc..d8130b221 100644 --- a/mallinkCallback/src/main/java/com/iformall/controller/callback/TtWebController.java +++ b/mallinkCallback/src/main/java/com/iformall/controller/callback/TtWebController.java @@ -69,20 +69,20 @@ public class TtWebController extends BaseController { return resultMap; } - String header = request.getHeader("X-Douyin-Signature"); - String sha1gen = SHA1.gen(appInfo.getSecret(), JSON.toJSONString(parameterMap)); - //发邮件 - String[] receivers = fmExceptionEmails.split(","); - StringBuilder sb = new StringBuilder(); - sb.append(DateUtils.date2String(new Date())); - sb.append("\n"); - sb.append("抖音开放平台通知{}"+JSON.toJSONString(parameterMap)); - sb.append("\n"); - sb.append("sha1gen{}"+sha1gen); - sb.append("\n"); - sb.append("header{}"+header); - //发送邮件 - mailService.sendSimpleMail(receivers, "抖音开放平台通知", sb.toString()); +// String header = request.getHeader("X-Douyin-Signature"); +// String sha1gen = SHA1.gen(appInfo.getSecret(), JSON.toJSONString(parameterMap)); +// //发邮件 +// String[] receivers = fmExceptionEmails.split(","); +// StringBuilder sb = new StringBuilder(); +// sb.append(DateUtils.date2String(new Date())); +// sb.append("\n"); +// sb.append("抖音开放平台通知{}"+JSON.toJSONString(parameterMap)); +// sb.append("\n"); +// sb.append("sha1gen{}"+sha1gen); +// sb.append("\n"); +// sb.append("header{}"+header); +// //发送邮件 +// mailService.sendSimpleMail(receivers, "抖音开放平台通知", sb.toString()); if("life_goods_audit".equals(event)){ diff --git a/mallinkService/src/main/java/com/iformall/domain/vo/MerchantPoiT.java b/mallinkService/src/main/java/com/iformall/domain/vo/MerchantPoiT.java new file mode 100644 index 000000000..9a262d161 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/domain/vo/MerchantPoiT.java @@ -0,0 +1,70 @@ +package com.iformall.domain.vo; + +import cn.afterturn.easypoi.excel.annotation.Excel; +import com.baomidou.mybatisplus.annotation.TableField; +import lombok.Data; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import java.io.Serializable; +import java.math.BigDecimal; + +@Data +public class MerchantPoiT implements Serializable { + + @NotNull + @Excel(name="服务商POI_ID",width = 20,orderNum = "1") + @io.swagger.annotations.ApiModelProperty(value="门店ID",name="merchantId") + private String merchantId; + @Excel(name="POI名称",width = 20,orderNum = "2") + @io.swagger.annotations.ApiModelProperty(value="POI名称",name="merchantName") + private String merchantName; + @Excel(name = "省份", width = 20, orderNum = "3") + @io.swagger.annotations.ApiModelProperty(value="省份",name="merchantProvince") + private String merchantProvince; + @Excel(name="城市",width = 20,orderNum = "4") + @io.swagger.annotations.ApiModelProperty(value="城市",name="merchantCity") + private String merchantCity; + @Excel(name="地址",width = 20,orderNum = "5") + @io.swagger.annotations.ApiModelProperty(value="地址",name="merchantAddr") + private String merchantAddr; + @Excel(name="经度",width = 20,orderNum = "6") + @io.swagger.annotations.ApiModelProperty(value="经度",name="longitude") + private BigDecimal longitude; + @Excel(name = "纬度", width = 20, orderNum = "7") + @io.swagger.annotations.ApiModelProperty(value="纬度",name="latitude") + private BigDecimal latitude; + @Excel(name = "高德ID(非必填)", width = 20, orderNum = "8") + @io.swagger.annotations.ApiModelProperty(value="高德ID(非必填)",name="amapId") + private String amapId; + + @NotNull + @Excel(name = "已匹配POI_ID", width = 20, orderNum = "9") + @io.swagger.annotations.ApiModelProperty(value="已匹配POI_ID",name="poiId") + private String poiId; + + @Excel(name = "已匹配POI名称", width = 20, orderNum = "10") + @io.swagger.annotations.ApiModelProperty(value="已匹配POI名称",name="poiName") + private String poiName; + + @Excel(name = "已匹配POI省份", width = 20, orderNum = "11") + @io.swagger.annotations.ApiModelProperty(value="已匹配POI省份",name="province") + private String province; + + @Excel(name = "已匹配POI城市", width = 20, orderNum = "12") + @io.swagger.annotations.ApiModelProperty(value="已匹配POI城市",name="city") + private String city; + + @Excel(name = "已匹配POI地址", width = 20, orderNum = "13") + @io.swagger.annotations.ApiModelProperty(value="已匹配POI地址",name="address") + private String address; + + @Excel(name = "未匹配原因", width = 20, orderNum = "14") + @io.swagger.annotations.ApiModelProperty(value="未匹配原因",name="mismatchStatusDesc") + private String mismatchStatusDesc; + + @Excel(name = "其他信息", width = 20, orderNum = "15") + @io.swagger.annotations.ApiModelProperty(value="其他信息",name="extra") + private String extra; + +} diff --git a/mallinkService/src/main/java/com/iformall/service/TtMerchantPoiService.java b/mallinkService/src/main/java/com/iformall/service/TtMerchantPoiService.java index b6887ae9c..d0b2c8343 100644 --- a/mallinkService/src/main/java/com/iformall/service/TtMerchantPoiService.java +++ b/mallinkService/src/main/java/com/iformall/service/TtMerchantPoiService.java @@ -2,8 +2,10 @@ package com.iformall.service; import com.github.pagehelper.PageInfo; import com.iformall.common.ResultData; +import com.iformall.domain.po.MallUserInfo; import com.iformall.domain.po.TtMerchantPoi; import com.iformall.domain.po.base.TenantEntity; +import com.iformall.domain.vo.MerchantPoiT; import com.iformall.douyin.web.api.TtWebService; import java.util.List; @@ -53,4 +55,6 @@ public interface TtMerchantPoiService { // ResultData spuStockSync(TenantEntity tenantInfo, Long couponChannelId); + void importOneMem(MallUserInfo user, String importKey, MerchantPoiT poiBase); + } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/TtMerchantPoiServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/TtMerchantPoiServiceImpl.java index cf2bae65a..8d3d85e4b 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/TtMerchantPoiServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/TtMerchantPoiServiceImpl.java @@ -6,11 +6,11 @@ import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import com.iformall.common.ErrorCode; import com.iformall.common.ResultData; import com.iformall.domain.po.*; import com.iformall.domain.po.base.TenantEntity; +import com.iformall.domain.vo.MerchantPoiT; import com.iformall.douyin.web.api.TtWebService; import com.iformall.douyin.web.bean.*; import com.iformall.enums.*; @@ -20,16 +20,15 @@ import com.iformall.service.*; import com.iformall.utils.Constant; import com.iformall.utils.MaUtil; import me.chanjar.weixin.common.error.WxErrorException; -import me.chanjar.weixin.common.util.json.GsonHelper; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; -import java.text.SimpleDateFormat; import java.util.*; -import java.util.stream.Collectors; +import java.util.concurrent.TimeUnit; /** * @author gongbiao @@ -69,6 +68,9 @@ public class TtMerchantPoiServiceImpl implements TtMerchantPoiService { @Autowired MaUtil maUtil; + @Autowired + StringRedisTemplate stringRedisTemplate; + @Override public PageInfo listAsPage(TtMerchantPoi record, Integer pageIndex, Integer pageSize) { return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> ttMerchantPoiMapper.findList(record)); @@ -505,6 +507,60 @@ public class TtMerchantPoiServiceImpl implements TtMerchantPoiService { return ttWebService; } + @Override + public void importOneMem(MallUserInfo user, String importKey, MerchantPoiT poiBase) { + if (StringUtils.isBlank(poiBase.getMerchantId())) { + stringRedisTemplate.opsForHash().increment(importKey, "processCount", 1); + logger.error("服务商POI_ID为空", poiBase.toString()); + return; + } + if (StringUtils.isBlank(poiBase.getPoiId())) { + stringRedisTemplate.opsForHash().increment(importKey, "processCount", 1); + logger.error("已匹配POI_ID为空", poiBase.toString()); + return; + } + + try { + Date now = new Date(); + TtMerchantPoi merchantPoi = ttMerchantPoiMapper.selectById(Long.parseLong(poiBase.getMerchantId())); + if(merchantPoi == null){ + merchantPoi = new TtMerchantPoi(); + } + merchantPoi.updateTenantInfo(user); + merchantPoi.setSupplierExtId(poiBase.getMerchantId()); + merchantPoi.setMerchantName(poiBase.getMerchantName()); + merchantPoi.setMerchantProvince(poiBase.getMerchantProvince()); + merchantPoi.setMerchantCity(poiBase.getMerchantCity()); + merchantPoi.setMerchantAddr(poiBase.getMerchantAddr()); + merchantPoi.setLongitude(poiBase.getLongitude()); + merchantPoi.setLatitude(poiBase.getLatitude()); + merchantPoi.setAmapId(poiBase.getAmapId()); + merchantPoi.setExtra(poiBase.getExtra()); + merchantPoi.setPoiId(poiBase.getPoiId()); + merchantPoi.setPoiName(poiBase.getPoiName()); + merchantPoi.setProvince(poiBase.getProvince()); + merchantPoi.setCity(poiBase.getCity()); + merchantPoi.setAddress(poiBase.getAddress()); + merchantPoi.setMatchStatus(EnumSupplierMathStatus.match_success.getCode()); + merchantPoi.setUpdateDate(now); + if(merchantPoi.getId() == null){ + merchantPoi.setId(Long.parseLong(poiBase.getMerchantId())); + ttMerchantPoiMapper.insert(merchantPoi); + }else{ + merchantPoi.setCreateDate(now); + ttMerchantPoiMapper.updateById(merchantPoi); + } + } catch (Exception e) { + stringRedisTemplate.opsForHash().increment(importKey, "processCount", 1); + logger.error(e.getMessage()); + return; + } + + stringRedisTemplate.opsForHash().increment(importKey,"processCount",1); + stringRedisTemplate.expire(importKey,10, TimeUnit.SECONDS); + + } + // @Override // public ResultData findPoi(TenantEntity tenantInfo, Long couponChannelId) { // WxCouponChannel couponChannel = wxCouponChannelMapper.selectById(couponChannelId, tenantInfo.getTenantId()); diff --git a/mallinkService/src/main/java/com/iformall/utils/Constant.java b/mallinkService/src/main/java/com/iformall/utils/Constant.java index c1ad6fbe3..4afd874e7 100644 --- a/mallinkService/src/main/java/com/iformall/utils/Constant.java +++ b/mallinkService/src/main/java/com/iformall/utils/Constant.java @@ -85,6 +85,9 @@ public class Constant { public static final String importMemPrev = "importmem:"; public static final String importInvestCustomerPrev = "importinvestcustomer:"; + // 导入POI + public static final String importPoiPrev = "importpoi:"; + //商品门店 public static final String coupon_merchants_key = "coupon:merchants:";