gongzuming
2024-09-19 a768dc3daa04d35fedfbe75c0a59b9b2545b85c4
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
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.MemberDTO;
import com.mzl.flower.dto.request.menber.MemberQueryDTO;
import com.mzl.flower.dto.response.member.MemberVO;
import com.mzl.flower.entity.menber.Member;
import com.mzl.flower.mapper.customer.CustomerMapper;
import com.mzl.flower.mapper.member.MemberMapper;
import com.mzl.flower.service.menber.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.util.List;
 
/**
 * @author fanghaowei
 * @version version2.0
 * @className MemberServiceImpl
 * @date 2024/8/26
 * @description 会员管理功能逻辑层
 */
@Service
@Transactional
@RequiredArgsConstructor
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
 
    private final MemberMapper memberMapper;
 
    private final CustomerMapper customerMapper;
 
    @Override
    public void saveMember(MemberDTO memberDTO) {
        //是否已经存在endpoint已经有最大值了,有的话需要删除或者修改
        if (memberDTO.getEndPoint() == null) {
//            Member pointByIntegerMaxValue = memberMapper.getEndPointByIntegerMaxValue(Integer.MAX_VALUE);
//            if (!ObjectUtils.isEmpty(pointByIntegerMaxValue)) {
//                throw new ValidationException("系统中已经有一条最大值或者空值记录,请删除或者修改再保存");
//            } else {
                memberDTO.setEndPoint(Integer.MAX_VALUE);
//            }
        }
        if (StringUtils.isEmpty(memberDTO.getName())) {
            throw new ValidationException("会员等级名称不能为空");
        }
 
        if (StringUtils.isEmpty(memberDTO.getDiscountType())) {
            throw new ValidationException("会员折扣类型不能为空");
        }
        if (Constants.DISCOUNT_TYPE.amount.name().equals(memberDTO.getDiscountType()) && StringUtils.isEmpty(memberDTO.getDiscountAmount())) {
            throw new ValidationException("会员折扣固定金额不能为空");
        }
        if (Constants.DISCOUNT_TYPE.ratio.name().equals(memberDTO.getDiscountType()) && StringUtils.isEmpty(memberDTO.getDiscountRatio())) {
            throw new ValidationException("会员折扣百分比不能为空");
        }
 
        if (memberDTO.getStartPoint() > memberDTO.getEndPoint()) {
            throw new ValidationException("成长点开始不能大于结束点");
        }
        if (!StringUtils.isEmpty(memberDTO.getDiscountRatio())) {
            int discountRatio1 = memberDTO.getDiscountRatio().compareTo(BigDecimal.ZERO);
            if (discountRatio1 == -1) {
                throw new ValidationException("会员折扣百分比不能小于0");
            }
            int discountRatio2 = memberDTO.getDiscountRatio().compareTo(new BigDecimal(100));
            if (discountRatio2 == 1) {
                throw new ValidationException("会员折扣百分比不能大于100");
            }
        }
        //区间交集判断
        //查询所有等级
        List<Member> allMember = memberMapper.getAllMember();
        allMember.forEach(a -> {
            if (a.getStartPoint() < memberDTO.getEndPoint() && a.getEndPoint() > memberDTO.getStartPoint()) {
                throw new ValidationException("存在交集,不允许保存");
            }
        });
        if (!StringUtils.isEmpty(memberDTO.getDiscountAmount())) {
            int discountAmount = memberDTO.getDiscountAmount().compareTo(BigDecimal.ZERO);
            if (discountAmount == -1) {
                throw new ValidationException("会员折扣固定金额不能小于0");
            }
        }
 
        //保存时判断是否有重复的名称
        Member memberByName = memberMapper.getMemberByName(memberDTO.getName());
        if (!ObjectUtils.isEmpty(memberByName)) {
            throw new ValidationException("会员等级名称重复");
        }
        Member member = new Member();
        BeanUtils.copyProperties(memberDTO, member);
        member.create(SecurityUtils.getUserId());
        memberMapper.insert(member);
        //更新会员等级
        customerMapper.updateMemberLevelByPoint(member.getId(), member.getStartPoint(), member.getEndPoint());
 
    }
 
    @Override
    public void updateMember(MemberDTO memberDTO) {
        Member memberInfo = memberMapper.selectById(memberDTO.getId());
        if (memberInfo == null) {
            throw new ValidationException("会员等级信息不存在");
        }
        if (StringUtils.isEmpty(memberDTO.getDiscountType())) {
            throw new ValidationException("会员折扣类型不能为空");
        }
        if (Constants.DISCOUNT_TYPE.amount.name().equals(memberDTO.getDiscountType()) && StringUtils.isEmpty(memberDTO.getDiscountAmount())) {
            throw new ValidationException("会员折扣固定金额不能为空");
        }
        if (Constants.DISCOUNT_TYPE.ratio.name().equals(memberDTO.getDiscountType()) && StringUtils.isEmpty(memberDTO.getDiscountRatio())) {
            throw new ValidationException("会员折扣百分比不能为空");
        }
        if (!StringUtils.isEmpty(memberDTO.getDiscountRatio())) {
            int discountRatio1 = memberDTO.getDiscountRatio().compareTo(BigDecimal.ZERO);
            if (discountRatio1 == -1) {
                throw new ValidationException("会员折扣百分比不能小于0");
            }
            int discountRatio2 = memberDTO.getDiscountRatio().compareTo(new BigDecimal(100));
            if (discountRatio2 == 1) {
                throw new ValidationException("会员折扣百分比不能大于100");
            }
        }
        if (!StringUtils.isEmpty(memberDTO.getDiscountAmount())) {
            int discountAmount = memberDTO.getDiscountAmount().compareTo(BigDecimal.ZERO);
            if (discountAmount == -1) {
                throw new ValidationException("会员折扣固定金额不能小于0");
            }
        }
        Member memberTemp = memberMapper.getMemberByName(memberDTO.getName());
        //判断如果按照会员等级查询到得名称和当前得Id不一致,不能修改。
        if (!ObjectUtils.isEmpty(memberTemp)) {
            if (memberTemp.getId() != memberDTO.getId()) {
                throw new ValidationException("已存在会员等级名称,无法修改");
            }
        }
        //是否已经存在endpoint已经有最大值了,有的话需要删除或者修改
//        if (memberDTO.getEndPoint() == null && memberInfo.getEndPoint() != Integer.MAX_VALUE) {
//            Member pointByIntegerMaxValue = memberMapper.getEndPointByIntegerMaxValue(Integer.MAX_VALUE);
//            if (!ObjectUtils.isEmpty(pointByIntegerMaxValue)) {
//                throw new ValidationException("系统中已经有一条最大值或者空值记录,请删除或者修改再保存");
//            } else {
//                memberDTO.setEndPoint(Integer.MAX_VALUE);
//            }
//        } else if (memberDTO.getEndPoint() == null && memberInfo.getEndPoint() == Integer.MAX_VALUE) {
//            memberDTO.setEndPoint(Integer.MAX_VALUE);
//        }
 
        if (memberDTO.getEndPoint() == null) {
            if (memberInfo.getEndPoint() != Integer.MAX_VALUE) {
                Member pointByIntegerMaxValue = memberMapper.getEndPointByIntegerMaxValue(Integer.MAX_VALUE);
                if (!ObjectUtils.isEmpty(pointByIntegerMaxValue)) {
                    throw new ValidationException("系统中已经有一条最大值或者空值记录,请删除或者修改再保存");
                } else {
                    memberDTO.setEndPoint(Integer.MAX_VALUE);
                }
            } else {
                memberDTO.setEndPoint(Integer.MAX_VALUE);
            }
        }
 
        if (memberDTO.getStartPoint() > memberDTO.getEndPoint()) {
            throw new ValidationException("成长点开始不能大于结束");
        }
        //区间交集判断
        //查询所有等级
        List<Member> allMember = memberMapper.getOtherAllMember(memberDTO.getId());
        allMember.forEach(a -> {
            if (a.getStartPoint() < memberDTO.getEndPoint() && a.getEndPoint() > memberDTO.getStartPoint()) {
                throw new ValidationException("存在交集,不允许修改");
            }
        });
 
        //更新会员等级
        if (memberInfo.getStartPoint() != memberDTO.getStartPoint() || memberInfo.getEndPoint() != memberDTO.getEndPoint()) {
            customerMapper.updateMemberLevelByPoint(memberInfo.getId(), memberInfo.getStartPoint(), memberInfo.getEndPoint());
        }
 
        BeanUtils.copyProperties(memberDTO,memberInfo);
        memberInfo.update(SecurityUtils.getUserId());
        memberMapper.updateById(memberInfo);
 
    }
 
    @Override
    public void deleteMember(String id) {
        if(id.equals(Constants.DEFAULT_MEMBER_ID)){
            throw new ValidationException("默认普通会员只能编辑,不能删除");
        }
        Member member = memberMapper.selectById(id);
        if (member == null) {
            throw new ValidationException("会员等级信息不存在");
        }
        Integer levelId = customerMapper.getByLevelId(id);
        if (levelId > 0) {
            throw new ValidationException("当前会员等级用户已使用,无法删除");
        }
        memberMapper.deleteById(id);
    }
 
    @Override
    public Page<MemberVO> queryPage(MemberQueryDTO memberQueryDTO, Page page) {
        List<MemberVO> list = memberMapper.queryPage(memberQueryDTO, page);
        page.setRecords(list);
        return page;
    }
}