xuhy
2026-04-29 4b358047c2d8b0a305c2ef9fbfe4fe7eaaea7a37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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;
 
/**
 * <p>
 * 活动管理 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2026-02-03
 */
@Service
public class XpActicityServiceImpl extends ServiceImpl<XpActicityMapper, XpActivity> 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<XpActivityDto> pageList(XpActivityPageListQuery query) {
        PageInfo<XpActivityDto> pageInfo = new PageInfo<>(query.getPageNum(),query.getPageSize());
        List<XpActivityDto> list = this.baseMapper.pageList(pageInfo,query);
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public List<String> findListToIds(String acticityName){
        LambdaQueryWrapper<XpActivity> 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<Integer> actitctiyIds = new ArrayList<>();
        //全部用户
        List<XpActicityUser> list = userService.list(new QueryWrapper<XpActicityUser>().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<XpActicityUser>().eq("is_deleted", 0).eq("user_type", userType));
        actitctiyIds.addAll(list.stream().map(XpActicityUser::getActitcityId).collect(Collectors.toList()));
        //指定用户
        list = userService.list(new QueryWrapper<XpActicityUser>().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<XpActivity>().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<XpActicityStrategy> strategy = dto.getStrategy();
        List<XpActicityUser> activityUser = dto.getActivityUser();
        for (XpActicityStrategy xpActicityStrategy : strategy) {
            xpActicityStrategy.setActitcityId(activity.getId().intValue());
            xpActicityStrategy.setDeleted(false);
            strategyService.save(xpActicityStrategy);
 
            List<Long> 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<String, XpActicityStrategy> 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<Integer> actitctiyIds = new ArrayList<>();
        //全部用户
        List<XpActicityUser> list = userService.list(new QueryWrapper<XpActicityUser>().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<XpActicityUser>().eq("is_deleted", 0).eq("user_type", userType));
        actitctiyIds.addAll(list.stream().map(XpActicityUser::getActitcityId).collect(Collectors.toList()));
        //指定用户
        list = userService.list(new QueryWrapper<XpActicityUser>().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<XpActivity>().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;
    }
}