Browse Source

fix 加入企业微信后并购买相应平台订单后发放抽奖机会

zoujiajian 3 năm trước cách đây
mục cha
commit
0e0b7828a6

+ 5 - 0
netflix-common/src/main/java/com/cyksj/common/constant/Constant.java

@@ -40,4 +40,9 @@ public interface Constant {
 	 * 特定奖池平台配置key
 	 */
 	String LOTTERY_SPECIFIC_GOODS = "lottery_specific_goods";
+
+	/**
+	 * 特定订单有效时间
+	 */
+	String LOTTERY_SPECIFIC_ORDER_EFFECTIVE_TIME = "lottery_specific_order_effective_time";
 }

+ 3 - 0
netflix-dao/src/main/java/com/cyksj/mapper/market/draw/LuckyDrawSpecificChanceMapper.java

@@ -15,6 +15,9 @@ public interface LuckyDrawSpecificChanceMapper extends BaseMapper<LuckyDrawSpeci
 	@Update("update lucky_draw_specific_chance set num = num - 1 where user_id = #{userId} and num = #{num} and num > 0")
 	Integer subChanceNum(@Param("userId") Long userId, @Param("num") Integer num);
 
+	@Update("update lucky_draw_specific_chance set num = num - 1 where user_id = #{userId} and num > 0")
+	Integer subChanceNumNoRepeat(@Param("userId") Long userId);
+
 	@Update("update lucky_draw_specific_chance set num = num + 1 where user_id = #{userId} and num >= 0")
 	void addChanceNum(Long userId);
 }

+ 4 - 0
netflix-dao/src/main/java/com/cyksj/model/entity/LuckyDrawSpecificChanceRecord.java

@@ -3,6 +3,8 @@ package com.cyksj.model.entity;
 import lombok.Getter;
 import lombok.Setter;
 
+import java.util.Date;
+
 /*
  *项目名: netflix
  *文件名: LuckyDrawSpecificChanceRecord
@@ -19,4 +21,6 @@ public class LuckyDrawSpecificChanceRecord extends BaseEntity{
 	private Long goodsId;
 
 	private Long skuId;
+
+	private Date refundTime;
 }

+ 0 - 2
netflix-service/src/main/java/com/cyksj/service/corp/CorpService.java

@@ -28,6 +28,4 @@ public interface CorpService {
 	String getAgentJsTicket(String corpId, String agentId) throws Exception;
 
 	YHLXUserDetailView getUserDetailView(String externalUserid, String corpId) throws Exception;
-
-	Boolean isJoinCorpWx(Long userId, Boolean isJoin);
 }

+ 48 - 3
netflix-service/src/main/java/com/cyksj/service/corp/impl/CorpEventActionServiceImpl.java

@@ -1,8 +1,13 @@
 package com.cyksj.service.corp.impl;
 
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.cyksj.common.constant.Constant;
+import com.cyksj.common.util.Jsons;
 import com.cyksj.config.corp.WeChatCorpFactory;
+import com.cyksj.mapper.OrderDonMapper;
 import com.cyksj.mapper.UserMapper;
 import com.cyksj.mapper.corp.*;
 import com.cyksj.model.entity.*;
@@ -12,10 +17,13 @@ import com.cyksj.model.response.CorpExternalContactInfo;
 import com.cyksj.model.response.CorpUserExternalTagGroupList;
 import com.cyksj.model.response.ExternalContact;
 import com.cyksj.model.response.FollowedUser;
-import com.cyksj.service.corp.CorpService;
 import com.cyksj.service.corp.WxCorpOps;
 import com.cyksj.service.corp.msg.CorpEventActionService;
+import com.cyksj.service.market.MarketFrontService;
+import com.cyksj.service.relation.GroupRelationFrontService;
+import com.cyksj.service.sys.SysConfigService;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -31,6 +39,7 @@ import java.util.stream.Stream;
  */
 @Service
 @RequiredArgsConstructor
+@Slf4j
 public class CorpEventActionServiceImpl implements CorpEventActionService {
 
 	private final WxCorpOps wxCorpOps;
@@ -49,7 +58,13 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
 
 	private final CorpUserAuthMapper corpUserAuthMapper;
 
-	private final CorpService corpService;
+	private final GroupRelationFrontService groupRelationFrontService;
+
+	private final SysConfigService sysConfigService;
+
+	private final OrderDonMapper orderDonMapper;
+
+	private final MarketFrontService marketFrontService;
 
 	/**
 	 * 客户添加成员
@@ -82,7 +97,9 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
 			if (user != null) {
 				corpUser.setUserId(user.getId());
 				//加入企业微信后发放优惠券
-				corpService.isJoinCorpWx(user.getId(), true);
+				groupRelationFrontService.isJoinCorpWx(user.getId(), true, false);
+				//发放抽奖机会
+				sendSpecificChanceNum(user.getId());
 			}
 			corpUserMapper.insert(corpUser);
 		} else {
@@ -113,6 +130,34 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
 		return null;
 	}
 
+	private void sendSpecificChanceNum(Long userId) throws Exception {
+		SysConfig lottery_specific_goods = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.LOTTERY_SPECIFIC_GOODS).last("limit 1"));
+		if (lottery_specific_goods == null) {
+			return;
+		}
+		String sysValue = lottery_specific_goods.getSysValue();
+		if (sysValue == null) {
+			return;
+		}
+		List<Long> goodsIdList = Jsons.parseList(sysValue, Long.class);
+		SysConfig timeSys = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.LOTTERY_SPECIFIC_ORDER_EFFECTIVE_TIME).last("limit 1"));
+		Integer effectiveTime = timeSys == null ? 1 : Integer.parseInt(Optional.ofNullable(timeSys.getSysValue()).orElse("1"));
+		List<OrderDon> orderDons = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class)
+				.eq(OrderDon::getUserId, userId)
+				.notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
+				.ge(OrderDon::getCreatedTime, DateUtil.offsetDay(DateTime.now(), -effectiveTime))
+				.select(OrderDon::getId, OrderDon::getGoodsId, OrderDon::getSkuId));
+		orderDons.forEach(order -> {
+			if (goodsIdList.isEmpty() || goodsIdList.contains(order.getGoodsId())) {
+				try {
+					marketFrontService.addSpecificChanceNumNoWxCorp(order.getUserId(), order.getId(), order.getGoodsId(), order.getSkuId());
+				} catch (Exception e) {
+					log.error("加入企业微信发放特定抽奖机会错误,orderId:{},e:{}", order.getId(), e);
+				}
+			}
+		});
+	}
+
 	/**
 	 * 修改外部联系人的备注、手机号或标签
 	 */

+ 0 - 29
netflix-service/src/main/java/com/cyksj/service/corp/impl/CorpServiceImpl.java

@@ -354,33 +354,4 @@ public class CorpServiceImpl implements CorpService {
 		}
 		return view;
 	}
-
-	@Override
-	public Boolean isJoinCorpWx(Long userId, Boolean isJoin) {
-		if (!isJoin) {
-			User user = userMapper.selectById(userId);
-			if (StrUtil.isEmpty(user.getUnionid())) {
-				return true;
-			}
-			CorpUser corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class)
-					.ne(CorpUser::getUnionid, "")
-					.eq(CorpUser::getUnionid, user.getUnionid())
-					.last("limit 1")
-					.select(CorpUser::getId)
-			);
-			if (corpUser == null) {
-				return false;
-			}
-		}
-		//用户是否买了奈飞车票
-		Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
-				.field(RenewalView::getUserId, userId)
-				.field(RenewalView::getGoodsId, 1)
-				.field(RenewalView::getStatus, List.of("validity", "overdue")).op(Operator.InList)
-				.build());
-		if (number.intValue() > 0) {
-			couponCommonService.sendCouponToUser(userId, 34l, 0l, 0l, 0l, CouponUser.Channel.manual);
-		}
-		return true;
-	}
 }

+ 2 - 3
netflix-service/src/main/java/com/cyksj/service/mange/coupon/CouponCommonService.java

@@ -3,7 +3,6 @@ package com.cyksj.service.mange.coupon;
 import com.cyksj.model.entity.Coupon;
 import com.cyksj.model.entity.CouponDistributePopularize;
 import com.cyksj.model.entity.CouponUser;
-import com.cyksj.model.request.CorpCouponSendReq;
 import com.cyksj.model.request.CouponActiveReq;
 import com.cyksj.model.request.CouponDistributeReq;
 
@@ -59,10 +58,10 @@ public interface CouponCommonService {
 	/**
 	 * 发放优惠券
 	 */
-	void corpSendCoupon(CorpCouponSendReq corpCouponSendReq) throws Exception;
+	void corpSendCoupon(String unionid, Long couponId) throws Exception;
 
 	/**
 	 * 发放优惠券至用户
 	 */
 	Boolean sendCouponToUser(Long userId, Long couponId, Long couponActiveId, Long recommendId, Long popularizeId, CouponUser.Channel channel);
-}
+}

+ 2 - 43
netflix-service/src/main/java/com/cyksj/service/mange/coupon/impl/CouponCommonServiceImpl.java

@@ -1,23 +1,16 @@
 package com.cyksj.service.mange.coupon.impl;
 
 import cn.hutool.core.util.StrUtil;
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.exception.BusinessRuntimeException;
 import com.cyksj.common.util.Jsons;
-import com.cyksj.config.corp.WeChatCorpFactory;
-import com.cyksj.config.corp.YHLXWxCorpConfig;
 import com.cyksj.mapper.GoodsDonMapper;
 import com.cyksj.mapper.GoodsDonSkuMapper;
 import com.cyksj.mapper.UserMapper;
-import com.cyksj.mapper.corp.CorpUserAuthMapper;
-import com.cyksj.mapper.corp.CorpUserMapper;
 import com.cyksj.mapper.manage.coupon.*;
 import com.cyksj.model.entity.*;
-import com.cyksj.model.request.CorpCouponSendReq;
 import com.cyksj.model.request.CouponActiveReq;
 import com.cyksj.model.request.CouponDistributeReq;
-import com.cyksj.service.corp.msg.CorpEventActionService;
 import com.cyksj.service.coupon.CouponFontService;
 import com.cyksj.service.mange.coupon.CouponCommonService;
 import lombok.RequiredArgsConstructor;
@@ -52,12 +45,6 @@ public class CouponCommonServiceImpl implements CouponCommonService {
 
 	private final CouponSkuMapper couponSkuMapper;
 
-	private final CorpUserMapper corpUserMapper;
-
-	private final CorpEventActionService corpEventActionService;
-
-	private final YHLXWxCorpConfig yhlXWxCorpConfig;
-
 	private final UserMapper userMapper;
 
 	private final CouponUserMapper couponUserMapper;
@@ -66,8 +53,6 @@ public class CouponCommonServiceImpl implements CouponCommonService {
 
 	private final CouponFontService couponFontService;
 
-	private final CorpUserAuthMapper corpUserAuthMapper;
-
 	@Override
 	@Transactional(rollbackFor = Throwable.class)
 	public void saveCoupon(Coupon coupon) throws Exception {
@@ -164,43 +149,17 @@ public class CouponCommonServiceImpl implements CouponCommonService {
 	}
 
 	@Override
-	public void corpSendCoupon(CorpCouponSendReq corpCouponSendReq) throws Exception {
-		WxCorpApp wxCorpApp = WeChatCorpFactory.getCorpConfigStrategy(corpCouponSendReq.getCorpId());
-		String externalUserId = corpCouponSendReq.getExternalUserId();
-		LambdaQueryWrapper<CorpUserAuth> corpUserAuthWrapper = Wrappers.lambdaQuery(CorpUserAuth.class)
-				.eq(CorpUserAuth::getExternalUserid, externalUserId)
-				.eq(CorpUserAuth::getWxCorpId, wxCorpApp.getId()).last("limit 1");
-		CorpUserAuth corpUserAuth = corpUserAuthMapper.selectOne(corpUserAuthWrapper);
-		CorpUser corpUser = null;
-		if (corpUserAuth == null) {
-			//尝试添加客户
-			corpEventActionService.addExternalContact(wxCorpApp.getCorpId(), null, externalUserId, null, null);
-			corpUserAuth = corpUserAuthMapper.selectOne(corpUserAuthWrapper);
-			if (corpUserAuth != null) {
-				corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class).eq(CorpUser::getId, corpUserAuth.getCorpUserId()).last("limit 1"));
-			}
-		} else {
-			corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class).eq(CorpUser::getId, corpUserAuth.getCorpUserId()).last("limit 1"));
-		}
-		if (corpUser == null) {
-			log.info("该客户{}不存在", externalUserId);
-			throw BusinessRuntimeException.getInstance("该企业微信用户不存在..");
-		}
-		String unionid = corpUser.getUnionid();
-		if (StrUtil.isEmpty(unionid)) {
-			throw BusinessRuntimeException.getInstance("该用户不是微信用户");
-		}
+	public void corpSendCoupon(String unionid, Long couponId) throws Exception {
 		User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class).eq(User::getUnionid, unionid).select(User::getId).last("limit 1"));
 		if (user == null) {
 			throw BusinessRuntimeException.getInstance("该用户未在银河录像注册");
 		}
-		Long couponId = corpCouponSendReq.getCouponId();
 		Coupon coupon = couponMapper.selectById(couponId);
 		if (coupon == null || !coupon.getDeleted()) {
 			throw BusinessRuntimeException.getInstance("该优惠券已被删除");
 		}
 		//发放优惠券给用户
-		this.sendCouponToUser(user.getId(), couponId, null, null,null, CouponUser.Channel.wxCorp);
+		this.sendCouponToUser(user.getId(), couponId, null, null, null, CouponUser.Channel.wxCorp);
 	}
 
 	@Override

+ 7 - 0
netflix-service/src/main/java/com/cyksj/service/market/MarketFrontService.java

@@ -9,6 +9,7 @@ import com.cyksj.model.request.TaskFollowReq;
 import com.cyksj.model.views.LuckyDrawView;
 import com.cyksj.model.views.TaskFrontView;
 
+import java.util.Date;
 import java.util.List;
 
 public interface MarketFrontService {
@@ -36,4 +37,10 @@ public interface MarketFrontService {
 	Integer getSpecificChanceNum(Long userId);
 
 	List<GroupsRelationView> getSpecificTickets(Long userId) throws Exception;
+
+	void addSpecificChanceNum(Long userId, Long orderId, Long goodsId, Long skuId) throws Exception;
+
+	void addSpecificChanceNumNoWxCorp(Long userId, Long orderId, Long goodsId, Long skuId) throws Exception;
+
+	void subSpecificChanceNum(Long userId, Long orderId, Long goodsId, Date refundTime) throws Exception;
 }

+ 86 - 0
netflix-service/src/main/java/com/cyksj/service/market/impl/MarketFrontServiceImpl.java

@@ -18,6 +18,7 @@ import com.cyksj.mapper.manage.coupon.CouponUserMapper;
 import com.cyksj.mapper.manage.distribute.UserDistributeSharedMapper;
 import com.cyksj.mapper.market.draw.LuckyDrawRecordMapper;
 import com.cyksj.mapper.market.draw.LuckyDrawSpecificChanceMapper;
+import com.cyksj.mapper.market.draw.LuckyDrawSpecificChanceRecordMapper;
 import com.cyksj.mapper.market.sign.SignConfigMapper;
 import com.cyksj.mapper.market.sign.SignUserMapper;
 import com.cyksj.mapper.market.sign.UserPointsRecordMapper;
@@ -34,6 +35,7 @@ import com.cyksj.redis.RedisService;
 import com.cyksj.service.exchange.ExchangeCodeService;
 import com.cyksj.service.mange.coupon.CouponCommonService;
 import com.cyksj.service.market.MarketFrontService;
+import com.cyksj.service.relation.GroupRelationFrontService;
 import com.cyksj.service.sys.SysConfigService;
 import com.ejlchina.searcher.BeanSearcher;
 import com.ejlchina.searcher.param.Operator;
@@ -99,6 +101,10 @@ public class MarketFrontServiceImpl implements MarketFrontService {
 
 	private final LuckyDrawSpecificChanceMapper luckyDrawSpecificChanceMapper;
 
+	private final LuckyDrawSpecificChanceRecordMapper luckyDrawSpecificChanceRecordMapper;
+
+	private final GroupRelationFrontService groupRelationFrontService;
+
 	@Override
 	public SignUser signByUserId(Long userId) {
 		//第一次签到
@@ -685,4 +691,84 @@ public class MarketFrontServiceImpl implements MarketFrontService {
 		relation.setExpiryTime(DateUtil.offsetDay(expiryTime, days));
 		groupsRelationMapper.updateById(relation);
 	}
+
+	@Override
+	public void addSpecificChanceNum(Long userId, Long orderId, Long goodsId, Long skuId) throws Exception {
+		if (!checkSpecificGoods(goodsId)) {
+			return;
+		}
+		//加入企微之后 发放抽奖机会
+		if (groupRelationFrontService.isJoinCorpWx(userId, false, true)) {
+			addSpecificChanceNumNoWxCorp(userId, orderId, goodsId, skuId);
+		}
+	}
+
+	@Override
+	public void addSpecificChanceNumNoWxCorp(Long userId, Long orderId, Long goodsId, Long skuId) {
+		if (saveChanceRecord(userId, orderId, goodsId, skuId)) {
+			LuckyDrawSpecificChance chance = luckyDrawSpecificChanceMapper.selectOne(Wrappers.lambdaQuery(LuckyDrawSpecificChance.class)
+					.eq(LuckyDrawSpecificChance::getUserId, userId).last("limit 1").select(LuckyDrawSpecificChance::getNum));
+			if (chance == null) {
+				try {
+					LuckyDrawSpecificChance entity = new LuckyDrawSpecificChance();
+					entity.setUserId(userId);
+					entity.setNum(1);
+					luckyDrawSpecificChanceMapper.insert(entity);
+				} catch (DuplicateKeyException e) {
+					luckyDrawSpecificChanceMapper.addChanceNum(userId);
+				}
+			} else {
+				luckyDrawSpecificChanceMapper.addChanceNum(userId);
+			}
+		}
+	}
+
+	@Override
+	public void subSpecificChanceNum(Long userId, Long orderId, Long goodsId, Date refundTime) throws Exception {
+		if (!checkSpecificGoods(goodsId)) {
+			return;
+		}
+		LuckyDrawSpecificChance chance = luckyDrawSpecificChanceMapper.selectOne(Wrappers.lambdaQuery(LuckyDrawSpecificChance.class)
+				.eq(LuckyDrawSpecificChance::getUserId, userId).last("limit 1").select(LuckyDrawSpecificChance::getNum));
+		if (chance == null) {
+			return;
+		}
+		LuckyDrawSpecificChanceRecord record = luckyDrawSpecificChanceRecordMapper.selectOne(Wrappers.lambdaQuery(LuckyDrawSpecificChanceRecord.class)
+				.eq(LuckyDrawSpecificChanceRecord::getOrderId, orderId)
+				.eq(LuckyDrawSpecificChanceRecord::getUserId, userId)
+				.last("limit 1"));
+		record.setRefundTime(refundTime);
+		luckyDrawSpecificChanceRecordMapper.updateById(record);
+		luckyDrawSpecificChanceMapper.subChanceNumNoRepeat(userId);
+	}
+
+	public Boolean checkSpecificGoods(Long goodsId) throws Exception {
+		SysConfig lottery_specific_goods = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.LOTTERY_SPECIFIC_GOODS).last("limit 1"));
+		if (lottery_specific_goods == null) {
+			return false;
+		}
+		String sysValue = lottery_specific_goods.getSysValue();
+		if (sysValue == null) {
+			return false;
+		}
+		List<Long> goodsIdList = Jsons.parseList(sysValue, Long.class);
+		if (goodsIdList.isEmpty() || goodsIdList.contains(goodsId)) {
+			return true;
+		}
+		return false;
+	}
+
+	public Boolean saveChanceRecord(Long userId, Long orderId, Long goodsId, Long skuId) {
+		try {
+			LuckyDrawSpecificChanceRecord record = new LuckyDrawSpecificChanceRecord();
+			record.setUserId(userId);
+			record.setOrderId(orderId);
+			record.setGoodsId(goodsId);
+			record.setSkuId(skuId);
+			luckyDrawSpecificChanceRecordMapper.insert(record);
+			return true;
+		} catch (DuplicateKeyException e) {
+			return false;
+		}
+	}
 }

+ 25 - 55
netflix-service/src/main/java/com/cyksj/service/order/impl/OrderDonServiceImpl.java

@@ -39,8 +39,6 @@ import com.cyksj.mapper.manage.coupon.CouponMapper;
 import com.cyksj.mapper.manage.coupon.CouponSkuMapper;
 import com.cyksj.mapper.manage.coupon.CouponUserMapper;
 import com.cyksj.mapper.manage.distribute.*;
-import com.cyksj.mapper.market.draw.LuckyDrawSpecificChanceMapper;
-import com.cyksj.mapper.market.draw.LuckyDrawSpecificChanceRecordMapper;
 import com.cyksj.mapper.market.task.TaskTypeMapper;
 import com.cyksj.model.dto.AliPayParams;
 import com.cyksj.model.dto.CouponPopularizeDto;
@@ -58,9 +56,9 @@ import com.cyksj.redis.RedisService;
 import com.cyksj.service.coupon.CouponFontService;
 import com.cyksj.service.exchange.ExchangeCodeService;
 import com.cyksj.service.mange.stock.GoodsDonStockService;
+import com.cyksj.service.market.MarketFrontService;
 import com.cyksj.service.market.task.TaskService;
 import com.cyksj.service.order.OrderDonService;
-import com.cyksj.service.sys.SysConfigService;
 import com.cyksj.service.template.TemplateCommonService;
 import com.cyksj.service.wechat.WeChatService;
 import com.cyksj.task.JobManager;
@@ -170,11 +168,7 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 
 	private final TemplateCommonService templateCommonService;
 
-	private final SysConfigService sysConfigService;
-
-	private final LuckyDrawSpecificChanceMapper luckyDrawSpecificChanceMapper;
-
-	private final LuckyDrawSpecificChanceRecordMapper luckyDrawSpecificChanceRecordMapper;
+	private final MarketFrontService marketFrontService;
 
 	private final List<String> noChekGoodsTemp = List.of("Apple One", "Youtube", "Spotify");
 
@@ -489,9 +483,9 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		//配置了特定奖池,增加抽奖机会
 		TASK_POOL.execute(() -> {
 			try {
-				addSpecificChanceNum(order.getUserId(), order.getId(), order.getGoodsId(), order.getSkuId());
+				marketFrontService.addSpecificChanceNum(order.getUserId(), order.getId(), order.getGoodsId(), order.getSkuId());
 			} catch (Exception e) {
-				log.error("wx增加抽奖机会error:{}", e);
+				log.error("wx增加抽奖机会错误,orderId:{},error:{}", order.getId(), e);
 			}
 		});
 	}
@@ -559,7 +553,8 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 	@Override
 	@Transactional(rollbackFor = Throwable.class)
 	public OrderDon renewal(OrderPayRequest payRequest) throws Exception {
-		User user = userMapper.selectById(payRequest.getUserId());
+		Long userId = payRequest.getUserId();
+		User user = userMapper.selectById(userId);
 		CouponUser couponUser = null;
 		if (StrUtil.isNotBlank(payRequest.getCouponExCode()) || payRequest.getCouponId() != null) {
 			checkCouponStatus(payRequest.getSkuId(), payRequest.getCouponExCode(), payRequest.getCouponId(), payRequest.getUserId(), true);
@@ -577,9 +572,10 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
         if (relation == null) {
             throw new BusinessRuntimeException("");
         }
-        if (!relation.getUserId().equals(user.getId())) {
-            throw new BusinessRuntimeException("");
-        }
+
+		if (!relation.getUserId().equals(user.getId())) {
+			throw new BusinessRuntimeException("");
+		}
         GroupsTrips groupsTrips = groupsMapper.selectById(relation.getGroupsId());
 
 		GoodsDon goodsDon = goodsDonMapper.selectById(sku.getGoodsId());
@@ -758,9 +754,9 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		//配置了特定奖池,增加抽奖机会
 		TASK_POOL.execute(() -> {
 			try {
-				addSpecificChanceNum(orderDon.getUserId(), orderDon.getId(), orderDon.getGoodsId(), orderDon.getSkuId());
+				marketFrontService.addSpecificChanceNum(orderDon.getUserId(), orderDon.getId(), orderDon.getGoodsId(), orderDon.getSkuId());
 			} catch (Exception e) {
-				log.error("zfb增加抽奖机会error:{}", e);
+				log.error("zfb增加抽奖机会错误,orderId:{},error:{}", orderDon.getId(), e);
 			}
 		});
 	}
@@ -889,6 +885,11 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 			THREAD_POOL_TASK_EXECUTOR.execute(()->{
 				refundRecord(orderDon, goodsDon, sku, outNo);
 				goodsDonStockService.upStockRelate(null, orderDon.getId(),"refund");
+				try {
+					marketFrontService.subSpecificChanceNum(orderDon.getUserId(), orderDon.getId(), orderDon.getGoodsId(), orderDon.getUpdateTime());
+				} catch (Exception e) {
+					log.error("zfb减少抽奖机会错误,orderId:{},error:{}", orderDon.getId(), e);
+				}
 			});
 			//虚物订单退款,清出车队,车票变为已过期
 			clearUnrealGoodsDetail(orderDon, goodsDon, sku);
@@ -1080,6 +1081,14 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 			}
 		}
 		log.info("微信退款: 退款单号[{}] 退款成功.", outRefundNo);
+
+		TASK_POOL.execute(()->{
+			try {
+				marketFrontService.subSpecificChanceNum(orderDon.getUserId(), orderDon.getId(), orderDon.getGoodsId(), orderDon.getUpdateTime());
+			} catch (Exception e) {
+				log.error("wx减少抽奖机会错误,orderId:{},error:{}", orderDon.getId(), e);
+			}
+		});
 	}
 
 	public void setRelation(Long relationId, Long userId, Long goodsId, Boolean isLoginPopularize) {
@@ -1522,43 +1531,4 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 			}
 		}
 	}
-
-	private void addSpecificChanceNum(Long userId, Long orderId, Long goodsId, Long skuId) throws Exception {
-		SysConfig lottery_specific_goods = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.LOTTERY_SPECIFIC_GOODS).last("limit 1"));
-		if (lottery_specific_goods == null) {
-			return;
-		}
-		String sysValue = lottery_specific_goods.getSysValue();
-		if (sysValue == null) {
-			return;
-		}
-		List<Long> goodsIdList = Jsons.parseList(sysValue, Long.class);
-		if (!goodsIdList.contains(goodsId)) {
-			return;
-		}
-		LuckyDrawSpecificChance chance = luckyDrawSpecificChanceMapper.selectOne(Wrappers.lambdaQuery(LuckyDrawSpecificChance.class)
-				.eq(LuckyDrawSpecificChance::getUserId, userId).last("limit 1").select(LuckyDrawSpecificChance::getNum));
-		if (chance == null) {
-			try {
-				LuckyDrawSpecificChance entity = new LuckyDrawSpecificChance();
-				entity.setUserId(userId);
-				entity.setNum(1);
-				luckyDrawSpecificChanceMapper.insert(entity);
-			} catch (DuplicateKeyException e) {
-
-			}
-		} else {
-			luckyDrawSpecificChanceMapper.addChanceNum(userId);
-		}
-		saveChanceRecord(userId, orderId, goodsId, skuId);
-	}
-
-	public void saveChanceRecord(Long userId, Long orderId, Long goodsId, Long skuId) {
-		LuckyDrawSpecificChanceRecord record = new LuckyDrawSpecificChanceRecord();
-		record.setUserId(userId);
-		record.setOrderId(orderId);
-		record.setGoodsId(goodsId);
-		record.setSkuId(skuId);
-		luckyDrawSpecificChanceRecordMapper.insert(record);
-	}
 }

+ 2 - 0
netflix-service/src/main/java/com/cyksj/service/relation/GroupRelationFrontService.java

@@ -12,4 +12,6 @@ import java.util.List;
  */
 public interface GroupRelationFrontService {
 	List<RenewalView> getMyTicket(long userId, Boolean isLoginPopularize);
+
+	Boolean isJoinCorpWx(Long userId, Boolean isJoin, Boolean noCoupon);
 }

+ 36 - 4
netflix-service/src/main/java/com/cyksj/service/relation/impl/GroupRelationFrontServiceImpl.java

@@ -5,13 +5,12 @@ import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.mapper.UserMapper;
+import com.cyksj.mapper.corp.CorpUserMapper;
 import com.cyksj.mapper.market.task.UserBindPhoneMapper;
-import com.cyksj.model.entity.GoodsDonSku;
-import com.cyksj.model.entity.GoodsDonSpec;
-import com.cyksj.model.entity.User;
-import com.cyksj.model.entity.UserBindPhone;
+import com.cyksj.model.entity.*;
 import com.cyksj.model.views.GoodsDonViews;
 import com.cyksj.model.views.RenewalView;
+import com.cyksj.service.mange.coupon.CouponCommonService;
 import com.cyksj.service.relation.GroupRelationFrontService;
 import com.ejlchina.searcher.BeanSearcher;
 import com.ejlchina.searcher.param.Operator;
@@ -39,6 +38,10 @@ public class GroupRelationFrontServiceImpl implements GroupRelationFrontService
 
 	private final BeanSearcher beanSearcher;
 
+	private final CorpUserMapper corpUserMapper;
+
+	private final CouponCommonService couponCommonService;
+
 	@Override
 	public List<RenewalView> getMyTicket(long userId, Boolean isLoginPopularize) {
 		User u = userMapper.selectById(userId);
@@ -92,4 +95,33 @@ public class GroupRelationFrontServiceImpl implements GroupRelationFrontService
 		}).collect(Collectors.toList());
 		return collect;
 	}
+
+	@Override
+	public Boolean isJoinCorpWx(Long userId, Boolean isJoin, Boolean noCoupon) {
+		if (!isJoin) {
+			User user = userMapper.selectById(userId);
+			if (StrUtil.isEmpty(user.getUnionid())) {
+				return true;
+			}
+			CorpUser corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class)
+					.ne(CorpUser::getUnionid, "")
+					.eq(CorpUser::getUnionid, user.getUnionid())
+					.last("limit 1")
+					.select(CorpUser::getId)
+			);
+			if (corpUser == null) {
+				return false;
+			}
+		}
+		//用户是否买了奈飞车票
+		Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
+				.field(RenewalView::getUserId, userId)
+				.field(RenewalView::getGoodsId, 1)
+				.field(RenewalView::getStatus, List.of("validity", "overdue")).op(Operator.InList)
+				.build());
+		if (number.intValue() > 0 && !noCoupon) {
+			couponCommonService.sendCouponToUser(userId, 34l, 0l, 0l, 0l, CouponUser.Channel.manual);
+		}
+		return true;
+	}
 }

+ 1 - 1
netflix-web/src/main/java/com/cyksj/web/controller/group/GroupRelationController.java

@@ -87,7 +87,7 @@ public class GroupRelationController {
     @GetMapping("/check/joinCorpWx")
     public Result<Boolean> isJoinCorpWx() {
         long userId = StpUserUtil.getLoginIdAsLong();
-        Boolean flag = corpService.isJoinCorpWx(userId, false);
+        Boolean flag = groupRelationFrontService.isJoinCorpWx(userId, false, false);
         return GatewayResponse.SUCCESS.newBuilder().toResult(flag);
     }
 }

+ 45 - 1
netflix-web/src/main/java/com/cyksj/web/controller/manage/coupon/CouponController.java

@@ -1,9 +1,14 @@
 package com.cyksj.web.controller.manage.coupon;
 
+import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.exception.BusinessRuntimeException;
+import com.cyksj.config.corp.WeChatCorpFactory;
 import com.cyksj.dto.Result;
 import com.cyksj.enums.GatewayResponse;
+import com.cyksj.mapper.corp.CorpUserAuthMapper;
+import com.cyksj.mapper.corp.CorpUserMapper;
 import com.cyksj.mapper.manage.coupon.*;
 import com.cyksj.model.entity.*;
 import com.cyksj.model.request.CorpCouponSendReq;
@@ -13,11 +18,13 @@ import com.cyksj.model.views.CouponActiveView;
 import com.cyksj.model.views.CouponDistributePopularizeView;
 import com.cyksj.model.views.CouponRecommendView;
 import com.cyksj.model.views.CouponView;
+import com.cyksj.service.corp.msg.CorpEventActionService;
 import com.cyksj.service.mange.coupon.CouponCommonService;
 import com.ejlchina.searcher.BeanSearcher;
 import com.ejlchina.searcher.SearchResult;
 import com.ejlchina.searcher.util.MapUtils;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
@@ -38,6 +45,7 @@ import java.util.stream.Collectors;
 @RestController
 @RequestMapping("/manage/coupon")
 @RequiredArgsConstructor
+@Slf4j
 public class CouponController {
 	private final CouponCommonService couponCommonService;
 
@@ -55,6 +63,12 @@ public class CouponController {
 
 	private final CouponSkuMapper couponSkuMapper;
 
+	private final CorpUserMapper corpUserMapper;
+
+	private final CorpEventActionService corpEventActionService;
+
+	private final CorpUserAuthMapper corpUserAuthMapper;
+
 	/**
 	 * 新增优惠券
 	 */
@@ -287,8 +301,38 @@ public class CouponController {
 	 */
 	@PostMapping("/corp/send")
 	public Result<String> corpSendCoupon(@RequestBody @Validated CorpCouponSendReq corpCouponSendReq) throws Exception {
-		couponCommonService.corpSendCoupon(corpCouponSendReq);
+		String corpId = corpCouponSendReq.getCorpId();
+		String externalUserId = corpCouponSendReq.getExternalUserId();
+		String unionid = getUnionId(corpId, externalUserId);
+		Long couponId = corpCouponSendReq.getCouponId();
+		if (StrUtil.isEmpty(unionid)) {
+			throw BusinessRuntimeException.getInstance("该用户不是微信用户");
+		}
+		couponCommonService.corpSendCoupon(unionid, couponId);
 		return GatewayResponse.SUCCESS.newBuilder().toResult("发放优惠券成功");
 	}
 
+	public String getUnionId(String corpId, String externalUserId) throws Exception {
+		WxCorpApp wxCorpApp = WeChatCorpFactory.getCorpConfigStrategy(corpId);
+		LambdaQueryWrapper<CorpUserAuth> corpUserAuthWrapper = Wrappers.lambdaQuery(CorpUserAuth.class)
+				.eq(CorpUserAuth::getExternalUserid, externalUserId)
+				.eq(CorpUserAuth::getWxCorpId, wxCorpApp.getId()).last("limit 1");
+		CorpUserAuth corpUserAuth = corpUserAuthMapper.selectOne(corpUserAuthWrapper);
+		CorpUser corpUser = null;
+		if (corpUserAuth == null) {
+			//尝试添加客户
+			corpEventActionService.addExternalContact(wxCorpApp.getCorpId(), null, externalUserId, null, null);
+			corpUserAuth = corpUserAuthMapper.selectOne(corpUserAuthWrapper);
+			if (corpUserAuth != null) {
+				corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class).eq(CorpUser::getId, corpUserAuth.getCorpUserId()).last("limit 1"));
+			}
+		} else {
+			corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class).eq(CorpUser::getId, corpUserAuth.getCorpUserId()).last("limit 1"));
+		}
+		if (corpUser == null) {
+			log.info("该客户{}不存在", externalUserId);
+			throw BusinessRuntimeException.getInstance("该企业微信用户不存在..");
+		}
+		return corpUser.getUnionid();
+	}
 }