From c826259fd5b5f02042dccf3d390b4fa4d38b1b47 Mon Sep 17 00:00:00 2001 From: xmzhao71 Date: Thu, 14 Sep 2023 16:29:35 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8F=91=E5=8A=B5=E5=95=86=E6=88=B7=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=8A=B5=E8=AE=A2=E5=8D=95=E6=9E=84?= =?UTF-8?q?=E9=80=A0=E6=95=B0=E6=8D=AE=E9=80=BB=E8=BE=91=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/iformall/exception/BizException.java | 5 + .../impl/WxCouponOrderServiceImpl.java | 161 ++++++++++++++++-- .../com/iformall/utils/service/MallUtils.java | 35 ++++ 3 files changed, 187 insertions(+), 14 deletions(-) create mode 100644 mallinkService/src/main/java/com/iformall/utils/service/MallUtils.java diff --git a/mallinkService/src/main/java/com/iformall/exception/BizException.java b/mallinkService/src/main/java/com/iformall/exception/BizException.java index 358ecc37f..522dbbb9c 100644 --- a/mallinkService/src/main/java/com/iformall/exception/BizException.java +++ b/mallinkService/src/main/java/com/iformall/exception/BizException.java @@ -12,6 +12,11 @@ public class BizException extends RuntimeException { private int code; private String msg; + public BizException(String msg) { + super(msg); + this.msg = msg; + } + public BizException(int code, String msg) { super(msg); this.code = code; diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java index 8137fbeba..6fbf14706 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java @@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.google.common.collect.Lists; +import com.google.gson.JsonArray; import com.iformall.common.*; import com.iformall.domain.dto.WxSharingOrderDto; import com.iformall.domain.po.*; @@ -18,6 +19,7 @@ import com.iformall.domain.po.msg.FmInsideCouponVerifyMsg; import com.iformall.domain.po.msg.MpAppMsg; import com.iformall.domain.vo.*; import com.iformall.enums.*; +import com.iformall.exception.BizException; import com.iformall.exception.MallinkException; import com.iformall.mapper.*; import com.iformall.mq.MqBaseProducer; @@ -33,7 +35,9 @@ import com.iformall.service.pay.service.share.PayShareAdapterService; import com.iformall.utils.Constant; import com.iformall.utils.DateUtils; import com.iformall.utils.RedisLock; +import com.iformall.utils.service.MallUtils; import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -203,6 +207,9 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { @Autowired WxMallMapper wxMallMapper; + @Autowired + private WxCouponSendMapper wxCouponSendMapper; + private final SimpleDateFormat mydateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override @@ -959,10 +966,6 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { } private List handleQueryResult(boolean cUserEntry,List list,TenantEntity couponOrderTenantEntity) { - // 获取该租户下所有的商户 - List merchants = wxMerchantMapper.selectList(new LambdaQueryWrapper().select(WxMerchant::getId, WxMerchant::getName).eq(WxMerchant::getTenantId, couponOrderTenantEntity.getFinalTenantId())); - Map merchantMap = merchants.stream().collect(Collectors.toMap(WxMerchant::getId, WxMerchant::getName)); - if (null != list && list.size() > 0) { //循环list,获取 cUserId @@ -971,6 +974,7 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { Map> mallMerchantIdList = new HashMap>(); List couponIdList = new ArrayList(); + // mallTenantIdList:当前广场的兄弟广场以及当前广场的子广场 List mallTenantIdList = new ArrayList(); mallTenantIdList.add(couponOrderTenantEntity.getTenantId()); //查询子广场 @@ -1059,12 +1063,9 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { for (int i = 0 ; i < mallTenantIdList.size(); i++) { String mallTenantId = mallTenantIdList.get(i); - List merchantIds = mallMerchantIdList.get(mallTenantId); + List merchantIds = Optional.ofNullable(mallMerchantIdList.get(mallTenantId)).orElse(new ArrayList<>()); List merchantProductVos = wxCouponMerchantMapper.findMerchantProduct(mallTenantId, couponIdList); if (null != merchantProductVos) { - if (null == merchantIds) { - merchantIds = new ArrayList(); - } for (int j = 0 ; j < merchantProductVos.size(); j ++) { WxMerchantProductVo mp = merchantProductVos.get(j); if (mp.getMc() > 1) { @@ -1076,8 +1077,24 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { } } } - mallMerchantIdList.put(mallTenantId, merchantIds); } + // 可从wx_coupon_send表中取发劵商户 + List couponSends = wxCouponSendMapper.selectList(new LambdaQueryWrapper() + .select(WxCouponSend::getConditions) + .eq(WxCouponSend::getTenantId, mallTenantId) + .in(!CollectionUtils.isEmpty(couponIdList), WxCouponSend::getCouponId, couponIdList)); + List sendMerchantIds = couponSends.stream().filter(x -> StringUtils.isNotBlank(x.getConditions())).map(x -> { + JSONObject jsonObject = new JSONObject(); + try { + jsonObject = JSONObject.parseObject(x.getConditions()); + } catch (Exception e) { + logger.error(e.getMessage()); + } + return jsonObject.getLong("id"); + }).collect(Collectors.toList()); + Set merchantIdsSet = new HashSet<>(merchantIds); + merchantIdsSet.addAll(sendMerchantIds); + mallMerchantIdList.put(mallTenantId, new ArrayList<>(merchantIdsSet)); } } @@ -1130,8 +1147,7 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { co.setUpdateDate(null); } if (null != co.getSendMerchantId()) { -// co.setSendMerchantName(merchantNameMap.get(co.getSendMerchantId())); - co.setSendMerchantName(merchantMap.get(co.getSendMerchantId())); + co.setSendMerchantName(merchantNameMap.get(co.getSendMerchantId())); } WxCoupon coupon = couponMap.get(co.getCouponId()); if (null != coupon) { @@ -1196,9 +1212,126 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { return new ArrayList(); } - - - private List getMerchantIdsQueryParam(WxCouponOrderBVo wxCouponOrder) { + + private List buildQueryResult(boolean cUserEntry,List list,TenantEntity couponOrderTenantEntity) { + // 必有字段:couponId,cuserId 非必有字段:buserId,bmerchantId + // 根据集合查询c端用户 + List cUserIds = list.stream().map(WxCouponOrder::getCUserId).distinct().collect(Collectors.toList()); + List cUsers = wxCUserBasicInfoMapper.selectList(new LambdaQueryWrapper() + .eq(WxCUserBasicInfo::getFinalTenantId, couponOrderTenantEntity.getFinalTenantId()) + .in(!CollectionUtils.isEmpty(cUserIds), WxCUserBasicInfo::getId, cUserIds)); + Map cUserMap = cUsers.stream().collect(Collectors.toMap(WxCUserBasicInfo::getId, Function.identity())); + // 根据集合查询优惠券 + List couponIds = list.stream().map(WxCouponOrder::getCouponId).distinct().collect(Collectors.toList()); + List wxCoupons = wxCouponMapper.selectList(new LambdaQueryWrapper() + .eq(WxCoupon::getTenantId, couponOrderTenantEntity.getTenantId()) + .in(!CollectionUtils.isEmpty(couponIds), WxCoupon::getId, couponIds)); + Map couponMap = wxCoupons.stream().collect(Collectors.toMap(WxCoupon::getId, Function.identity())); + // 此处由于集团版劵的所属商户可能是兄弟广场或是子广场,可用以下方式得到商户 + // 根据卷商户映射,得到集合中每个租户的商户id(劵商户映射不包括商户发劵) 可从wx_coupon_merchant表中取商户 + List malls = wxMallMapper.selectList(new LambdaQueryWrapper() + .select(WxMall::getTenantId, WxMall::getParentTenantId)); + List tenantIds = MallUtils.findTenantIds(couponOrderTenantEntity, malls); + List couponMerchants = wxCouponMerchantMapper.selectList(new LambdaQueryWrapper() + .in(!CollectionUtils.isEmpty(tenantIds), WxCouponMerchant::getTenantId, tenantIds) + .in(!CollectionUtils.isEmpty(couponIds), WxCouponMerchant::getProductId, couponIds)); + Map> tenantMerchantMap = couponMerchants.stream().collect(Collectors.groupingBy(WxCouponMerchant::getTenantId)); + Map> couponMerchantMap = couponMerchants.stream().collect(Collectors.groupingBy(WxCouponMerchant::getProductId)); + + Map merchantNameMap = new HashMap<>(); + Map bUserMap = new HashMap<>(); + // 根据集合统计b端用户id + List bUserIds = list.stream().filter(x -> x.getBUserId() != null).map(WxCouponOrder::getBUserId).distinct().collect(Collectors.toList()); + for (Map.Entry> entry : tenantMerchantMap.entrySet()) { + String tenantId = entry.getKey(); + Set merchantIds = entry.getValue().stream().map(WxCouponMerchant::getMerchantId).collect(Collectors.toSet()); + List productIds = entry.getValue().stream().map(WxCouponMerchant::getProductId).distinct().collect(Collectors.toList()); + + // 可从wx_coupon_send表中取发劵商户 + List couponSends = wxCouponSendMapper.selectList(new LambdaQueryWrapper() + .select(WxCouponSend::getConditions) + .eq(WxCouponSend::getTenantId, tenantId) + .in(!CollectionUtils.isEmpty(productIds), WxCouponSend::getCouponId, productIds)); + Set sendMerchantIds = couponSends.stream().filter(x -> StringUtils.isNotBlank(x.getConditions())).map(x -> { + JSONObject jsonObject = new JSONObject(); + try { + jsonObject = JSONObject.parseObject(x.getConditions()); + } catch (Exception e) { + logger.error(e.getMessage()); + } + return jsonObject.getLong("id"); + }).collect(Collectors.toSet()); + merchantIds.addAll(sendMerchantIds); + + // 商户数据 + List merchants = wxMerchantMapper.selectList(new LambdaQueryWrapper() + .select(WxMerchant::getId, WxMerchant::getName) + .eq(WxMerchant::getTenantId, tenantId) + .in(!CollectionUtils.isEmpty(merchantIds), WxMerchant::getId, merchantIds)); + merchants.stream().map(x -> merchantNameMap.put(x.getId(), x.getName())); + + // b端用户数据 可从wx_merchant_b_user表中查用户 + List bUsers = wxMerchantBUserMapper.selectList(new LambdaQueryWrapper() + .eq(WxMerchantBUser::getTenantId, tenantId) + .in(!CollectionUtils.isEmpty(bUserIds), WxMerchantBUser::getId, bUserIds)); + bUsers.stream().map(x -> bUserMap.put(x.getId(), x)); + } + return list.stream().map(x -> { + WxCouponOrderBVo co = new WxCouponOrderBVo(); + try { + BeanUtils.copyProperties(co, x); + if (co.getCouponOrderStatus().equals(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode())) { + co.setUpdateDate(null); + } + if (null != co.getSendMerchantId()) { + co.setSendMerchantName(merchantNameMap.get(co.getSendMerchantId())); + } + WxCoupon coupon = couponMap.get(co.getCouponId()); + if (null != coupon) { + co.setTitle(coupon.getTitle()); + co.setSalePrice(coupon.getSalePrice()); + co.setUsePrice(coupon.getUsePrice()); + co.setPrice(coupon.getPrice()); + co.setAutoRefund(coupon.getAutoRefund()); + co.setBusiness(coupon.getBusiness()); + co.setSubsidyType(coupon.getSubsidyType()); + co.setSourceType(coupon.getSourceType()); + } + co.setSubsidyNum(co.getSubsidy()); + List couponMerchantNum = couponMerchantMap.get(co.getCouponId()); + if (couponMerchantNum.size() > 1) { + co.setMerchantName("多商户通用"); + co.setMerchantId(0L); + }else { + Long merchantId = couponMerchantNum.get(0).getMerchantId(); + co.setMerchantName(merchantNameMap.get(merchantId)); + co.setMerchantId(merchantId); + } + if(co.getBMerchantId() != null){ + co.setbMerchantName(merchantNameMap.get(co.getBMerchantId())); + co.setVerifyMerchantName(merchantNameMap.get(co.getBMerchantId())); + } + WxCUserBasicInfo basicInfo = cUserMap.get(co.getCUserId()); + if (!cUserEntry) { + co.setcuserPhone(basicInfo.getPhone()); + co.setUserNickName(basicInfo.getNickName()); + }else { + co.setcuserPhone("保密"); + co.setUserNickName("保密"); + } + if(null != co.getBUserId()){ + WxMerchantBUser bUser = bUserMap.get(co.getBUserId()); + co.setbUserPhone(bUser.getPhone()); + co.setbuserName(bUser.getName()); + } + } catch (IllegalAccessException | InvocationTargetException e) { + logger.error(e.getMessage()); + } + return co; + }).collect(Collectors.toList()); + } + + private List getMerchantIdsQueryParam(WxCouponOrderBVo wxCouponOrder) { List retMerchantIdList = new ArrayList(); boolean isConditiondQquery = false; diff --git a/mallinkService/src/main/java/com/iformall/utils/service/MallUtils.java b/mallinkService/src/main/java/com/iformall/utils/service/MallUtils.java new file mode 100644 index 000000000..5857b7e6d --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/utils/service/MallUtils.java @@ -0,0 +1,35 @@ +package com.iformall.utils.service; + +import com.iformall.domain.po.WxMall; +import com.iformall.domain.po.base.TenantEntity; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class MallUtils { + + public static List findTenantIds(TenantEntity tenantEntity, List malls) { + List tenantIds = new ArrayList<>(); + for (WxMall mall : malls) { + if (tenantEntity.getParentTenantId() != null && tenantEntity.getParentTenantId().equals(mall.getParentTenantId())) { + tenantIds.add(mall.getTenantId()); + findTenantIds(mall, malls); + } + } + Optional.ofNullable(tenantEntity.getParentTenantId()).ifPresent(x -> tenantIds.add(tenantEntity.getParentTenantId())); + return tenantIds; + } + + public static List findChildren(TenantEntity tenantEntity, List malls) { + List tenantIds = new ArrayList<>(); + for (WxMall mall : malls) { + if (tenantEntity.getTenantId().equals(mall.getParentTenantId())) { + tenantIds.add(mall.getTenantId()); + findChildren(mall, malls); + } + } + tenantIds.add(tenantEntity.getTenantId()); + return tenantIds; + } +}