gongzuming
2024-11-12 24f7f046f8854d63839ad52d0cff34ea45a0f449
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.mzl.flower.service.point;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.point.ChangePointDTO;
import com.mzl.flower.dto.request.point.QueryCustomerPointDTO;
import com.mzl.flower.dto.request.point.QueryPointDetailDTO;
import com.mzl.flower.dto.response.point.CustomerPointDTO;
import com.mzl.flower.dto.response.point.CustomerPointDetailDTO;
import com.mzl.flower.entity.customer.Customer;
import com.mzl.flower.entity.point.CustomerPoint;
import com.mzl.flower.entity.point.CustomerPointDetail;
import com.mzl.flower.mapper.point.CustomerPointDetailMapper;
import com.mzl.flower.mapper.point.CustomerPointMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.service.payment.RedisLockService;
import com.mzl.flower.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
 
import com.mzl.flower.constant.Constants.*;
 
@Service
@Transactional
public class CustomerPointService extends BaseService {
 
    private final CustomerPointMapper customerPointMapper;
 
    private final CustomerPointDetailMapper customerPointDetailMapper;
 
    private final  RedisLockService redisLockService;
 
 
    public CustomerPointService(CustomerPointMapper customerPointMapper, CustomerPointDetailMapper customerPointDetailMapper, RedisLockService redisLockService) {
        this.customerPointMapper = customerPointMapper;
        this.customerPointDetailMapper = customerPointDetailMapper;
        this.redisLockService = redisLockService;
    }
 
    public Page<CustomerPointDTO> queryPage(QueryCustomerPointDTO dto, Page page) {
        List<CustomerPointDTO> list = customerPointMapper.queryPage(dto, page);
        page.setRecords(list);
        return page;
    }
 
    public  Page<CustomerPointDetailDTO> queryCustomerDetails(QueryPointDetailDTO dto, Page page) {
        if(StringUtils.isNotBlank(dto.getCreateTimeStartStr())){
            dto.setCreateTimeStart(DateUtils.dateTimeStringToLocalDateTime(dto.getCreateTimeStartStr()));
        }
        if(StringUtils.isNotBlank(dto.getCreateTimeEndStr())){
            dto.setCreateTimeEnd(DateUtils.dateTimeStringToLocalDateTime(dto.getCreateTimeEndStr()));
        }
        List<CustomerPointDetailDTO> list = customerPointMapper.queryCustomerDetails(dto, page);
        page.setRecords(list);
 
        return page;
    }
 
    public void giveawayPoint(ChangePointDTO dto) {
        CustomerPointDetail detail = new CustomerPointDetail();
        Customer customer = getCustomer(dto.getCustomerId());
        detail.setCustomerId(customer.getId());
        detail.setUserId(customer.getUserId());
        detail.setPoint(dto.getPoint());
        detail.setRemarks(dto.getRemarks());
        detail.setRecordDate(LocalDate.now());
        detail.setChangeType(POINT_CHANGE_TYPE.add.name());
        detail.setType(POINT_TYPE.giveaway.name());
        detail.create(SecurityUtils.getUserId());
        customerPointDetailMapper.insert(detail);
 
        //更新汇总表
        updateCustomerPoint(detail);
    }
 
    public void updateCustomerPoint(CustomerPointDetail detail) {
        boolean lock = redisLockService.getObjectLock("LOCK_KEY_CUSTOMER_POINT_", detail.getUserId());
        if(!lock){
            return;
        }
        try {
            CustomerPoint point = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>()
                    .eq(CustomerPoint::getCustomerId, detail.getCustomerId()));
            boolean isAdd = false;
            if(point == null){
                isAdd = true;
                point = new CustomerPoint();
                point.setCustomerId(detail.getCustomerId());
                point.setUserId(detail.getUserId());
                point.setTotalPoint(0);
                point.setUsedPoint(0);
                point.setExpiredPoint(0);
                point.setDeductionPoint(0);
                point.create(SecurityUtils.getUserId());
            }
 
            if(POINT_CHANGE_TYPE.add.name().equals(detail.getChangeType())){
                point.setTotalPoint(point.getTotalPoint() + detail.getPoint());
            }else if(POINT_CHANGE_TYPE.reduce.name().equals(detail.getChangeType())){
                Integer detailPoint = detail.getPoint();
                if(POINT_TYPE.deduction.name().equals(detail.getType())){ //积分扣减
                    Integer balancePoint = point.getTotalPoint() - point.getUsedPoint()-point.getExpiredPoint()-point.getDeductionPoint();//可用积分
                    balancePoint = balancePoint.intValue()>0?balancePoint.intValue():0;
                    if(balancePoint.intValue() >= detail.getPoint().intValue()){
                        point.setDeductionPoint(point.getDeductionPoint() + detail.getPoint());
                    }else{
                        //积分不足,直接清0
                        point.setDeductionPoint(point.getDeductionPoint() + balancePoint);
                        detail.setPoint(balancePoint);
                        detail.setRemarks(detail.getRemarks()+",当前积分不足"+detailPoint+",实际扣除积分"+balancePoint);
                    }
                }
            }
            if(isAdd){
                customerPointMapper.insert(point);
            }else{
                customerPointMapper.updateById(point);
            }
        }finally {
            redisLockService.releaseObjectLock("LOCK_KEY_CUSTOMER_POINT_", detail.getUserId());
        }
    }
 
    public void deductionPoint(ChangePointDTO dto) {
        CustomerPointDetail detail = new CustomerPointDetail();
        Customer customer = getCustomer(dto.getCustomerId());
        detail.setCustomerId(customer.getId());
        detail.setUserId(customer.getUserId());
        detail.setPoint(dto.getPoint());
        detail.setRemarks(dto.getRemarks());
        detail.setRecordDate(LocalDate.now());
        detail.setChangeType(POINT_CHANGE_TYPE.reduce.name());
        detail.setType(POINT_TYPE.deduction.name());
        detail.create(SecurityUtils.getUserId());
        //更新汇总表
        updateCustomerPoint(detail);
 
        customerPointDetailMapper.insert(detail);
    }
 
    public void consumptionPoint(BigDecimal orderAmount, String orderNo,String userId)  {
        CustomerPointDetail detail = new CustomerPointDetail();
        Customer customer = getCustomerByUserId(userId);
        detail.setCustomerId(customer.getId());
        detail.setUserId(customer.getUserId());
        BigDecimal point = orderAmount.setScale(0, BigDecimal.ROUND_HALF_UP);
        detail.setPoint(point.intValue());
        detail.setRemarks(orderNo);
        detail.setRecordDate(LocalDate.now());
        detail.setChangeType(POINT_CHANGE_TYPE.add.name());
        detail.setType(POINT_TYPE.consume.name());
        detail.create(SecurityUtils.getUserId());
        customerPointDetailMapper.insert(detail);
 
        //更新汇总表
        updateCustomerPoint(detail);
    }
 
    public void signIn() {
        if(signToday()){
            throw new ValidationException("今天已经签到过了");
        }
        int signNum = customerPointDetailMapper
                .selectCount(new LambdaQueryWrapper<CustomerPointDetail>()
                        .eq(CustomerPointDetail::getUserId, SecurityUtils.getUserId())
                        .ge(CustomerPointDetail::getRecordDate,LocalDate.now().minusDays(9))
                        .eq(CustomerPointDetail::getChangeType,POINT_CHANGE_TYPE.add.name())
                        .eq(CustomerPointDetail::getType,POINT_TYPE.sign.name())
                );
        int point = 1;
        if(signNum==9){
            point = 3;
        }
        CustomerPointDetail detail = new CustomerPointDetail();
        Customer customer = getCurrentCustomer();
        detail.setCustomerId(customer.getId());
        detail.setUserId(customer.getUserId());
        detail.setPoint(point);
        detail.setRemarks("签到");
        detail.setRecordDate(LocalDate.now());
        detail.setChangeType(POINT_CHANGE_TYPE.add.name());
        detail.setType(POINT_TYPE.sign.name());
        detail.create(SecurityUtils.getUserId());
        customerPointDetailMapper.insert(detail);
 
        //更新汇总表
        updateCustomerPoint(detail);
    }
 
    public List<CustomerPointDetailDTO> signList(LocalDate startDate, LocalDate endDate) {
        LambdaQueryWrapper<CustomerPointDetail> lambdaQueryWrapper = new LambdaQueryWrapper<CustomerPointDetail>()
                .eq(CustomerPointDetail::getUserId, SecurityUtils.getUserId())
                .eq(CustomerPointDetail::getChangeType, POINT_CHANGE_TYPE.add.name())
                .eq(CustomerPointDetail::getType, POINT_TYPE.sign.name());
        lambdaQueryWrapper.orderByDesc(CustomerPointDetail::getRecordDate);
        if(startDate!=null){
            lambdaQueryWrapper.ge(CustomerPointDetail::getRecordDate,startDate);
        }
        if (endDate != null) {
            lambdaQueryWrapper.le(CustomerPointDetail::getRecordDate, endDate);
        }
 
        List<CustomerPointDetail> details = customerPointDetailMapper.selectList(lambdaQueryWrapper);
        List<CustomerPointDetailDTO> dtos = new ArrayList<>();
        for (CustomerPointDetail detail : details) {
            CustomerPointDetailDTO dto = new CustomerPointDetailDTO();
            BeanUtils.copyProperties(detail, dto);
            dtos.add(dto);
        }
        return dtos;
    }
 
    public Boolean signToday() {
        return customerPointDetailMapper.selectCount(new LambdaQueryWrapper<CustomerPointDetail>()
                        .eq(CustomerPointDetail::getUserId, SecurityUtils.getUserId())
                        .eq(CustomerPointDetail::getRecordDate,LocalDate.now())
                        .eq(CustomerPointDetail::getChangeType,POINT_CHANGE_TYPE.add.name())
                        .eq(CustomerPointDetail::getType,POINT_TYPE.sign.name()))>0;
    }
}