xiangpei
2025-06-04 4e502853119c6d8e7ff686191e19bb0a19a4f875
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
package cn.lili.modules.lmk.service.impl;
 
import cn.lili.base.Result;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.lmk.domain.entity.Activity;
import cn.lili.modules.lmk.domain.entity.ActivityReport;
import cn.lili.modules.lmk.domain.form.ActivityReportForm;
import cn.lili.modules.lmk.domain.query.ActivityReportQuery;
import cn.lili.modules.lmk.domain.query.MyActivityQuery;
import cn.lili.modules.lmk.domain.vo.ActivityReportVO;
import cn.lili.modules.lmk.domain.vo.ActivityVO;
import cn.lili.modules.lmk.domain.vo.MyActivityVo;
import cn.lili.modules.lmk.mapper.ActivityMapper;
import cn.lili.modules.lmk.mapper.ActivityReportMapper;
import cn.lili.modules.lmk.service.MyActivityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xkzhangsan.time.utils.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
 
import java.util.Date;
import java.util.List;
 
@Service
@RequiredArgsConstructor
public class MyActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements MyActivityService {
    private final ActivityMapper activityMapper;
 
    private final ActivityReportMapper activityReportMapper;
 
 
    @Override
    public Result getMyActivityList(MyActivityQuery query) {
        String userId = UserContext.getCurrentUserId();
        List<MyActivityVo>  myActivityList = activityMapper.getMyActivityList(query,userId);
 
        return Result.ok().data(myActivityList);
    }
 
    @Override
    public Result activityCancel(String activityId) {
       //  TODO  判断是否在报名时间内 在的话可以取消报名, (若有报名费需要退款)
        //获得中间表信息
        ActivityReportVO vo = userReport(activityId);
 
        Activity activity = activityMapper.selectById(activityId);
 
        Date date = new Date();
        if(!date.before(activity.getReportStartTime()) && !date.after(activity.getReportEndTime())){
 
            boolean rowsAffected = new LambdaUpdateChainWrapper<ActivityReport>(activityReportMapper)
                    .eq(ActivityReport::getId, vo.getId())      // WHERE id = ?
                    .set(ActivityReport::getCancel, true)        // SET cancel = true
                    .update();                                   // 执行更新
 
 
            if (rowsAffected) {
                return Result.ok("活动取消成功");
            } else {
                // 可选:记录警告日志
                return Result.error("活动不存在或已取消");
            }
        }else {
            return Result.error("活动已不在报名时间内无法取消");
        }
    }
 
    @Override
    public Result activityReport(ActivityReportForm reportActivityForm) {
        //判断用户是否已经报名该活动
        ActivityReportVO reportVO = userReport(reportActivityForm.getActivityId());
        if (reportVO != null) {
            //判断是否已取消过该活动
            if (reportVO.getCancel()){
                //重新报名
 
                ActivityReport entity = ActivityReportForm.getEntityByForm(reportActivityForm, null);
                entity.setId(reportVO.getId());
                entity.setUserId(UserContext.getCurrentUserId());
                entity.setCancel(false);
                activityReportMapper.updateById(entity);
                return Result.ok("成功");
            }else {
                return Result.error("该活动已报名");
            }
 
 
        }
        ActivityReport entity = ActivityReportForm.getEntityByForm(reportActivityForm, null);
        entity.setUserId(UserContext.getCurrentUserId());
        activityReportMapper.insert(entity);
        return Result.ok("成功");
    }
 
    /**
     * 获得中间表信息通过activityId,userId
     * @param activityId 活动id
     * @return
     */
    @Override
    public ActivityReportVO userReport(String activityId){
 
        if (UserContext.getCurrentUserId() == null || UserContext.getCurrentUserId().isEmpty()){
            throw new RuntimeException("用户登录状态异常");
        }
        return activityReportMapper.getActivityReport(activityId,UserContext.getCurrentUserId());
    }
 
    @Override
    public Result detailByUsr(String activityId) {
        ActivityVO vo = baseMapper.getById(activityId);
        Assert.notNull(vo, "记录不存在");
 
        ActivityReportVO reportVO = userReport(activityId);
        //则当前用户未报名
        if (reportVO == null){
            vo.setIsReport(false);
        }else {
            //已取消则可以报名
            if (reportVO.getCancel()){
                vo.setIsReport(false);
            }else {
                vo.setIsReport(true);
            }
 
        }
        return Result.ok().data(vo);
    }
 
 
 
}