package com.xiaopeng.operation.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.xiaopeng.api.dto.operation.XpActivityDto; import com.xiaopeng.api.feignClient.order.XpOrderClient; import com.xiaopeng.api.model.operation.*; import com.xiaopeng.api.query.operation.XpActivityPageListQuery; import com.xiaopeng.common.core.utils.bean.BeanUtils; import com.xiaopeng.common.core.web.page.PageInfo; import com.xiaopeng.operation.mapper.XpActicityMapper; import com.xiaopeng.operation.service.*; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; /** *

* 活动管理 服务实现类 *

* * @author mitao * @since 2026-02-03 */ @Service public class XpActicityServiceImpl extends ServiceImpl implements IXpActicityService { @Resource private IXpActicityStrategyService strategyService; @Resource private IXpActicityUserService userService; @Resource private IXpActivityNoticeService activityNotice; @Resource private XpOrderClient xpOrderClient; @Resource private IXpActicityCouponService xpActicityCouponService; @Override public PageInfo pageList(XpActivityPageListQuery query) { PageInfo pageInfo = new PageInfo<>(query.getPageNum(),query.getPageSize()); List list = this.baseMapper.pageList(pageInfo,query); pageInfo.setRecords(list); return pageInfo; } @Override public List findListToIds(String acticityName){ LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.select(XpActivity::getId); query.eq(XpActivity::getDeleted, 0); query.like(XpActivity::getActicityName, acticityName); return listObjs(query, Object::toString); } @Override public XpActivity activiting(Long uid) { //获取当前用户可参与的活动 List actitctiyIds = new ArrayList<>(); //全部用户 List list = userService.list(new QueryWrapper().eq("is_deleted", 0).eq("user_type", 1)); actitctiyIds.addAll(list.stream().map(XpActicityUser::getActitcityId).collect(Collectors.toList())); //判断用户类型 2=老用户,3=新用户,4=流失用户 Integer userType = xpOrderClient.determineUserType(uid).getData(); list = userService.list(new QueryWrapper().eq("is_deleted", 0).eq("user_type", userType)); actitctiyIds.addAll(list.stream().map(XpActicityUser::getActitcityId).collect(Collectors.toList())); //指定用户 list = userService.list(new QueryWrapper().eq("is_deleted", 0).eq("user_type", 5) .last(" and JSON_CONTAINS(user_list, '" + uid + "', '$')")); actitctiyIds.addAll(list.stream().map(XpActicityUser::getActitcityId).collect(Collectors.toList())); XpActivity one = this.getOne(new QueryWrapper().eq("status", 2).eq("is_deleted", 0) .eq("mini_program_notification", 1).in(!actitctiyIds.isEmpty(), "id", actitctiyIds) .last(" and now() between start_time and end_time order by float_window_priority desc limit 1")); return one; } @Override public void save(XpActivityDto dto) { XpActivity activity = new XpActivity(); BeanUtils.copyBeanProp(activity, dto); activity.setActicityType(dto.getActivityType()); activity.setAggregateLimit(dto.getEnableTotalLimit() ? 1 : 0); activity.setLimitation(dto.getTotalLimit()); activity.setCoverImage(dto.getImg()); activity.setRemarks(dto.getRichText()); save(activity); List strategy = dto.getStrategy(); List activityUser = dto.getActivityUser(); for (XpActicityStrategy xpActicityStrategy : strategy) { xpActicityStrategy.setActitcityId(activity.getId().intValue()); xpActicityStrategy.setDeleted(false); strategyService.save(xpActicityStrategy); List couponIds = xpActicityStrategy.getCouponIds(); for (Long couponId : couponIds) { XpActicityCoupon xpActicityCoupon = new XpActicityCoupon(); xpActicityCoupon.setActicityStrategyId(xpActicityStrategy.getId().longValue()); xpActicityCoupon.setCouponId(couponId); xpActicityCoupon.setActivityId(activity.getId()); xpActicityCouponService.save(xpActicityCoupon); } } Map strategyMap = strategy.stream().collect(Collectors.toMap( XpActicityStrategy::getName, Function.identity() )); for (XpActicityUser xpActicityUser : activityUser) { xpActicityUser.setActitcityId(activity.getId().intValue()); xpActicityUser.setActitcityStrategeId(strategyMap.get(xpActicityUser.getStrategyName()).getId()); userService.save(xpActicityUser); } } @Override public XpActivityDto detail(Long id) { XpActivity byId = getById(id); XpActivityDto ret = new XpActivityDto(); BeanUtils.copyBeanProp(ret, byId); ret.setActicityType(byId.getActicityType()); ret.setEnableTotalLimit(byId.getAggregateLimit() == 1); ret.setTotalLimit(byId.getLimitation()); ret.setImg(byId.getCoverImage()); ret.setRichText(byId.getRemarks()); ret.setStrategy(strategyService.listByActicityId(id)); ret.setActivityUser(userService.listByActicityId(id)); return ret; } /** * 获取悬浮弹窗活动数据 * @param uid * @return */ @Override public XpActivity getFloatActicity(Long uid) { //获取当前用户可参与的活动 List actitctiyIds = new ArrayList<>(); //全部用户 List list = userService.list(new QueryWrapper().eq("is_deleted", 0).eq("user_type", 1)); actitctiyIds.addAll(list.stream().map(XpActicityUser::getActitcityId).collect(Collectors.toList())); //判断用户类型 2=老用户,3=新用户,4=流失用户 Integer userType = xpOrderClient.determineUserType(uid).getData(); list = userService.list(new QueryWrapper().eq("is_deleted", 0).eq("user_type", userType)); actitctiyIds.addAll(list.stream().map(XpActicityUser::getActitcityId).collect(Collectors.toList())); //指定用户 list = userService.list(new QueryWrapper().eq("is_deleted", 0).eq("user_type", 5) .last(" and JSON_CONTAINS(user_list, '" + uid + "', '$')")); actitctiyIds.addAll(list.stream().map(XpActicityUser::getActitcityId).collect(Collectors.toList())); XpActivity one = this.getOne(new QueryWrapper().eq("status", 2).eq("is_deleted", 0) .eq("enable_float_window", 1).in(!actitctiyIds.isEmpty(), "id", actitctiyIds) .last(" and now() between start_time and end_time order by float_window_priority desc limit 1")); return one; } }