Pu Zhibing
22 小时以前 d0d6f8f40e5d9762d940b47c566da1876361020e
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.SinataUtil;
import com.stylefeng.guns.modular.system.model.TCheckCarActivity;
import com.stylefeng.guns.modular.system.model.TCheckCarActivityRecord;
import com.stylefeng.guns.modular.system.dao.TCheckCarActivityMapper;
import com.stylefeng.guns.modular.system.service.ITCheckCarActivityService;
import com.stylefeng.guns.modular.system.service.ITCheckCarActivityRecordService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.util.*;
 
/**
 * <p>
 * 代检车活动表 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2025-08-02
 */
@Service
public class TCheckCarActivityServiceImpl extends ServiceImpl<TCheckCarActivityMapper, TCheckCarActivity> implements ITCheckCarActivityService {
    
    @Autowired
    private ITCheckCarActivityRecordService tCheckCarActivityRecordService;
    
    @Override
    public Page<TCheckCarActivity> getOrderList(String createTime, String branchOfficeName, String name, String activityAreaCode, Integer status) {
        ShiroUser user = ShiroKit.getUser();
        String beginTime = null;
        String endTime = null;
        if (SinataUtil.isNotEmpty(createTime)){
            String[] timeArray = createTime.split(" - ");
            beginTime = timeArray[0];
            endTime = timeArray[1];
        }
 
        String provinceCode = null;
        String cityCode = null;
        String areaCode = null;
        if(StringUtils.hasLength(activityAreaCode)){
            String[] split = activityAreaCode.split("-");
            provinceCode = split[0];
            cityCode = split[1];
            areaCode = split[2];
        }
        Page<TCheckCarActivity> page = new PageFactory<TCheckCarActivity>().defaultPage();
        List<TCheckCarActivity> list = baseMapper.getOrderList(page, beginTime, endTime, branchOfficeName, user.getRoleType(), user.getObjectId(), name, provinceCode, cityCode, areaCode, status);
        list.forEach(item -> {
            if (item.getAuditStatus().equals(1)) {
                //判断是否开始
                Date now = new Date();
                if (item.getStartTime() != null && item.getStartTime().after(now)) {
                    item.setActivityStatus(0); // 未开始
                } else if (item.getEndTime() != null && item.getEndTime().before(now)) {
                    item.setActivityStatus(2); // 已结束
                } else if (item.getPauseFlag() != null && item.getPauseFlag().equals(1) &&
                        item.getStartTime() != null && item.getStartTime().before(now) &&
                        item.getEndTime() != null && item.getEndTime().after(now)) {
                    item.setActivityStatus(3); // 已暂停
                } else if (item.getStartTime() != null && item.getStartTime().before(now) &&
                        item.getEndTime() != null && item.getEndTime().after(now)) {
                    item.setActivityStatus(1); // 进行中
                }else {
                    item.setActivityStatus(0); // 未开始
                }
            }
        });
        return page.setRecords(list);
    }
    
    @Override
    public Map<String, Object> getActivityStatistics(Integer activityId) {
        Map<String, Object> result = new HashMap<>();
        
        // 获取活动基本信息
        TCheckCarActivity activity = selectById(activityId);
        if (activity == null) {
            return result;
        }
        
        // 优惠券总计数量
        Integer totalCoupons = activity.getCouponNum();
        
        // 已领取优惠券数量
        Integer receivedCoupons = activity.getReceivedNum() != null ? activity.getReceivedNum() : 0;
        
        // 剩余优惠券数量
        Integer remainingCoupons = totalCoupons - receivedCoupons;
        
        // 优惠券总计金额
        BigDecimal couponAmount = activity.getCouponAmount();
        BigDecimal totalAmount = couponAmount.multiply(new BigDecimal(totalCoupons));
        
        // 查询已使用和未使用的统计
        List<TCheckCarActivityRecord> records = tCheckCarActivityRecordService.selectList(
            new EntityWrapper<TCheckCarActivityRecord>().eq("checkCarActivityId", activityId)
        );
        
        long usedCount = records.stream().filter(record -> record.getStatus() != null && record.getStatus() == 1).count();
        long unusedCount = records.stream().filter(record -> record.getStatus() != null && record.getStatus() == 0).count();
        
        // 已使用金额
        BigDecimal usedAmount = couponAmount.multiply(new BigDecimal(usedCount));
        
        // 未使用金额
        BigDecimal unusedAmount = couponAmount.multiply(new BigDecimal(unusedCount));
        
        result.put("totalCoupons", totalCoupons);
        result.put("receivedCoupons", receivedCoupons);
        result.put("remainingCoupons", remainingCoupons);
        result.put("totalAmount", totalAmount);
        result.put("usedAmount", usedAmount);
        result.put("unusedAmount", unusedAmount);
        
        return result;
    }
    
    @Override
    public Page<TCheckCarActivityRecord> getActivityRecords(Integer activityId, String userPhone) {
        Page<TCheckCarActivityRecord> page = new PageFactory<TCheckCarActivityRecord>().defaultPage();
        
        // 构建查询条件
        EntityWrapper<TCheckCarActivityRecord> wrapper = new EntityWrapper<>();
        wrapper.eq("checkCarActivityId", activityId);
        
        // 如果提供了手机号搜索条件,添加模糊查询
        if (StringUtils.hasText(userPhone)) {
            wrapper.like("userPhone", userPhone);
        }
        
        // 按领取时间倒序排列
        wrapper.orderBy("receiveTime", false);
        
        // 分页查询记录
        List<TCheckCarActivityRecord> records = tCheckCarActivityRecordService.selectPage(page, wrapper).getRecords();
        return page.setRecords(records);
    }
}