cloudroam
2024-09-11 ec34e9cac976a734b2116f2651376490ca3f10a0
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
230
231
232
233
234
235
236
237
238
package com.mzl.flower.service.menber.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.menber.*;
import com.mzl.flower.dto.response.customer.CustomerDTO;
import com.mzl.flower.dto.response.member.MemberGrowthRecordVO;
import com.mzl.flower.dto.response.member.UserGrowthRecordVO;
import com.mzl.flower.entity.customer.Customer;
import com.mzl.flower.entity.menber.Member;
import com.mzl.flower.entity.menber.MemberGrowthRecord;
import com.mzl.flower.entity.payment.Order;
import com.mzl.flower.mapper.customer.CustomerMapper;
import com.mzl.flower.mapper.member.MemberGrowthRecordMapper;
import com.mzl.flower.mapper.member.MemberMapper;
import com.mzl.flower.service.menber.MemberGrowthRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @author fanghaowei
 * @version version2.0
 * @className MemberGrowthRecordServiceImpl
 * @date 2024/8/26
 * @description 会员记录功能逻辑层
 */
@Service
@Transactional
@RequiredArgsConstructor
public class MemberGrowthRecordServiceImpl extends ServiceImpl<MemberGrowthRecordMapper, MemberGrowthRecord> implements MemberGrowthRecordService {
 
    private final MemberGrowthRecordMapper memberGrowthRecordMapper;
 
    private final MemberMapper memberMapper;
 
    private final CustomerMapper customerMapper;
 
 
    @Override
    public void saveMemberGrowthRecord(MemberGrowthRecordDTO memberGrowthRecordDTO) {
        if (StringUtils.isEmpty(memberGrowthRecordDTO.getUserId())) {
            throw new ValidationException("用户ID不能为空");
        }
 
        MemberGrowthRecord memberGrowthRecord = new MemberGrowthRecord();
        BeanUtils.copyProperties(memberGrowthRecordDTO, memberGrowthRecord);
        memberGrowthRecord.create(SecurityUtils.getUserId());
        memberGrowthRecord.setRecordDate(new Date());
 
        CustomerDTO customerDTO = customerMapper.getCurrentCustomer(memberGrowthRecord.getUserId());
        if (ObjectUtils.isEmpty(customerDTO)) {
            throw new ValidationException("人员信息为空");
        }
 
        Customer customer = customerMapper.selectById(customerDTO.getId());
        if (ObjectUtils.isEmpty(customer)) {
            throw new ValidationException("人员信息为空");
        }
        Member memberBefore = memberMapper.selectById(customer.getLevelId());
        if(ObjectUtils.isEmpty(memberBefore)){
            throw new ValidationException("人员对应的会员等级不存在,请联系管理员");
        }
 
        //保存会员记录逻辑:
        if (Constants.GROWTH_SOURCE.consume.name().equals(memberGrowthRecordDTO.getSource())) {
            //消费:成长值=消费金额/消费金额(元)*已消费产生的成长值C
            BigDecimal totalAmount = memberGrowthRecordDTO.getTotalAmount();
            int consumptionAmount = memberBefore.getConsumptionAmount();
            int growthValue = memberBefore.getGrowthValue();
            BigDecimal actualGrowthValue = totalAmount.divide(new BigDecimal(consumptionAmount), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(growthValue));
            int growth = actualGrowthValue.setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); // 四舍五入取整
            memberGrowthRecord.setGrowth(growth);
            memberGrowthRecord.setType(Constants.GROWTH_TYPE.add.name());
        }
        memberGrowthRecordMapper.insert(memberGrowthRecord);
 
        //会员等级同步逻辑:
        Member memberAfter = getMemberByUserId(memberGrowthRecord.getUserId());
        customer.setLevelId(memberAfter.getId());
        customerMapper.updateById(customer);
    }
 
 
    /**
     * 查询当前人员的成长值 最少是0
     *
     * @param userId
     * @return
     */
    @Override
    public Member getMemberByUserId(String userId) {
        if (StringUtils.isEmpty(userId)) {
            throw new ValidationException("用户ID不能为空");
        }
        Integer sumGrowth = memberGrowthRecordMapper.getSumGrowthByUsertId(userId);
 
        //查询当前会员等级
        Member member = memberMapper.getMemberByGrowthValue(sumGrowth);
        if (ObjectUtils.isEmpty(member)) {
            throw new ValidationException("会员信息为空");
        }
        return member;
    }
 
    @Override
    public void updateMemberGrowthRecord(MemberGrowthRecordDTO memberGrowthRecordDTO) {
        MemberGrowthRecord memberGrowthRecord = memberGrowthRecordMapper.selectById(memberGrowthRecordDTO.getId());
        if (memberGrowthRecord == null) {
            throw new ValidationException("成长记录信息不存在");
        }
        BeanUtils.copyProperties(memberGrowthRecordDTO, memberGrowthRecord);
        memberGrowthRecord.update(SecurityUtils.getUserId());
        memberGrowthRecordMapper.updateById(memberGrowthRecord);
    }
 
    @Override
    public void deleteMemberGrowthRecord(String id) {
        MemberGrowthRecord memberGrowthRecord = memberGrowthRecordMapper.selectById(id);
        if (memberGrowthRecord == null) {
            throw new ValidationException("成长记录信息不存在");
        }
        memberGrowthRecordMapper.deleteById(id);
    }
 
    @Override
    public UserGrowthRecordDTO getInfoByUserId(String userId) {
        UserGrowthRecordDTO userGrowthRecordDTO = new UserGrowthRecordDTO();
        List<TargetMemberDTO> targetMemberDTOList = new ArrayList<>();
        //查询当前人员的成长值 最少是0
        Integer sumGrowth = memberGrowthRecordMapper.getSumGrowthByUsertId(userId);
        userGrowthRecordDTO.setCurrentGrowthValue(sumGrowth);
        //查询当前会员等级
        Member member = new Member();
        member = memberMapper.getMemberByGrowthValue(sumGrowth);
        if (ObjectUtils.isEmpty(member)) {
            member = memberMapper.selectById(Constants.DEFAULT_MEMBER_ID);
        }
        userGrowthRecordDTO.setCurrentMemberLevel(member.getName());
        userGrowthRecordDTO.setCurrentDiscountType(member.getDiscountType());
        switch (member.getDiscountType()) {
            case "ratio":
                userGrowthRecordDTO.setCurrentDiscountTypeStr(Constants.DISCOUNT_TYPE.ratio.getDesc());
                break;
            case "amount":
                userGrowthRecordDTO.setCurrentDiscountTypeStr(Constants.DISCOUNT_TYPE.amount.getDesc());
                break;
        }
        userGrowthRecordDTO.setCurrentDiscountRatio(StringUtils.isEmpty(member.getDiscountRatio()) ? BigDecimal.valueOf(100) : member.getDiscountRatio());
        userGrowthRecordDTO.setCurrentDiscountAmount(StringUtils.isEmpty(member.getDiscountAmount()) ? BigDecimal.ZERO  : member.getDiscountAmount());
        //查询比当前等级高的会员等级信息
        List<Member> memberList = memberMapper.getgtMembersByGrowthValue(sumGrowth);
        if (!CollectionUtils.isEmpty(memberList)) {
            memberList.forEach(m -> {
                TargetMemberDTO targetMemberDTO = new TargetMemberDTO();
                targetMemberDTO.setTargetMemberLevel(m.getName());
                targetMemberDTO.setTargetStartPoint(m.getStartPoint());
                targetMemberDTO.setTargetGap(m.getStartPoint() - sumGrowth);
                targetMemberDTO.setTargetDiscountAmount(StringUtils.isEmpty(m.getDiscountAmount()) ? BigDecimal.ZERO : m.getDiscountAmount());
                targetMemberDTO.setTargetDiscountType(m.getDiscountType());
                switch (m.getDiscountType()) {
                    case "ratio":
                        targetMemberDTO.setTargetDiscountTypeStr(Constants.DISCOUNT_TYPE.ratio.getDesc());
                        break;
                    case "amount":
                        targetMemberDTO.setTargetDiscountTypeStr(Constants.DISCOUNT_TYPE.amount.getDesc());
                        break;
                }
                targetMemberDTO.setTargetDiscountRatio(StringUtils.isEmpty(m.getDiscountRatio()) ? BigDecimal.valueOf(100) : m.getDiscountRatio());
                targetMemberDTOList.add(targetMemberDTO);
            });
        }
        userGrowthRecordDTO.setTargetMemberInfos(targetMemberDTOList);
 
        return userGrowthRecordDTO;
    }
 
    @Override
    public Page<MemberGrowthRecordVO> queryPage(MemberRecordQueryDTO memberRecordQueryDTO, Page page) {
        List<MemberGrowthRecordVO> list = memberGrowthRecordMapper.queryPage(memberRecordQueryDTO, page);
        page.setRecords(list);
        return page;
    }
 
    @Override
    public void growthValueDeduct(Order order) {
        //超过30天不到90天,每天处理当前会员等级的成长值
        CustomerDTO customerDTO = customerMapper.getCurrentCustomer(order.getCreateBy());
        Customer customer = customerMapper.selectById(customerDTO.getId());
        Member member = memberMapper.selectById(customer.getLevelId());
        if(ObjectUtils.isEmpty(member)){
            throw new ValidationException("用户会员等级未维护");
        }
        int deductGrowthValue = member.getDowngradeValue();
        Integer sumGrowthByUserId = memberGrowthRecordMapper.getSumGrowthByUsertId(order.getCreateBy());
 
        //当前成长值如果是等于0不需要走扣除逻辑
        if (sumGrowthByUserId != 0) {
        int waitDeductGrowthValue = 0;
 
        //判断当前用户的成长值是够扣除,如果够扣除直接减去成长值,如果不够扣除则全部减去
        if (sumGrowthByUserId - deductGrowthValue > 0) {
            waitDeductGrowthValue = deductGrowthValue;
        } else {
            waitDeductGrowthValue = sumGrowthByUserId;
        }
 
        //保存会员成长记录到记录表
        MemberGrowthRecordDTO memberGrowthRecordDTO = new MemberGrowthRecordDTO();
        memberGrowthRecordDTO.setUserId(order.getCreateBy());
        memberGrowthRecordDTO.setRecordDate(new Date());
        memberGrowthRecordDTO.setGrowth(-waitDeductGrowthValue);
        memberGrowthRecordDTO.setSource(Constants.GROWTH_SOURCE.downgrading.name());
        memberGrowthRecordDTO.setType(Constants.GROWTH_TYPE.reduce.name());
        memberGrowthRecordDTO.setRemarks("自动扣除");
        saveMemberGrowthRecord(memberGrowthRecordDTO);
        }
    }
 
    @Override
    public Page<UserGrowthRecordVO> queryUserPage(UserMemberRecordQueryDTO userMemberRecordQueryDTO, Page page) {
        List<UserGrowthRecordVO> list = memberGrowthRecordMapper.queryUserPage(userMemberRecordQueryDTO, page);
        page.setRecords(list);
        return page;
    }
}