rx
2023-12-07 a095fb55c6f742ee67cf1193a3e6ba8c29b1828a
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.hx.phip.service.impl;
 
import com.hx.exception.TipsException;
import com.hx.phiappt.model.coupon.CouponNumber;
import com.hx.phiappt.model.CommonShopQuotaConfig;
import com.hx.phiappt.model.organize.OrganizeDepartmentQuotaConfig;
import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed;
import com.hx.phip.dao.mapper.CouponNumberMapper;
import com.hx.phip.dao.mapper.CommonShopQuotaConfigMapper;
import com.hx.phip.dao.mapper.OrganizeDepartmentQuotaConfigMapper;
import com.hx.phip.dao.mapper.UserMoneyUnclaimedMapper;
import com.hx.phip.service.CommonShopQuotaConfigService;
import com.hx.util.DateUtil;
import com.hz.his.dto.coupon.CouponAddDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
 
@Transactional
@Service
@Slf4j
public class CommonShopQuotaConfigServiceImpl implements CommonShopQuotaConfigService {
 
    @Resource
    private CommonShopQuotaConfigMapper commonShopQuotaConfigMapper;
    @Resource
    private CouponNumberMapper couponNumberMapper;
    @Resource
    private OrganizeDepartmentQuotaConfigMapper orgDeptQuotaConfigMapper;
    @Resource
    private UserMoneyUnclaimedMapper userMoneyUnclaimedMapper;
 
    /**
     * 获取门店-优惠券 限额配置
     */
    @Override
    public void checkQuotaApply(CouponAddDto couponAddDto) {
 
        if(!couponAddDto.getType().equals(CommonShopQuotaConfig.TYPE_CCJ) && !couponAddDto.getType().equals(CommonShopQuotaConfig.TYPE_KS)){
            throw new TipsException("类型错误!");
        }
 
        String shopId = couponAddDto.getShopId();
        //CommonShopQuotaConfig 定义的类型
        String typeStr = couponAddDto.getType();
        //所申请优惠券总数
        Integer applyNum = couponAddDto.getApplyNum();
        //总金额
        BigDecimal applyAmount = couponAddDto.getApplyAmount();
 
        //查询指定类型的额度配置 为空 拿取通用额度的数据
        CommonShopQuotaConfig quotaConfig = commonShopQuotaConfigMapper.getConfigInfo(shopId, typeStr);
        if (quotaConfig == null) {
            quotaConfig = commonShopQuotaConfigMapper.getConfigInfo(shopId, CommonShopQuotaConfig.TYPE_COMMON);
            if (quotaConfig == null) {
                throw new TipsException("门店没有配置限额请配置!");
            }
        }
 
        if (quotaConfig.getQuatoTotalPrice() != null || quotaConfig.getQuatoTotalNum() != null || quotaConfig.getQuatoNum() != null || quotaConfig.getQuatoPrice() != null) {
            //查询总的使用额度 查询月的额度
            CouponNumber useQuotaTotal = couponNumberMapper.couponUseQuotaInfo(shopId, typeStr, null, null);
            //查询月的使用额度
            CouponNumber useQuotaMonth = couponNumberMapper.couponUseQuotaInfo(shopId, typeStr, DateUtil.getMonthStart(new Date()), DateUtil.getMonthEnd(new Date()));
            //检验判断
            if (useQuotaTotal != null) {
                //总额度不为空时 判断 总额度 是否大于已使用的额度
                if (quotaConfig.getQuatoTotalPrice() != null
                        && quotaConfig.getQuatoTotalPrice().compareTo(useQuotaTotal.getUseAmount()) <= 0) {
                    throw new TipsException("申请总额度已使用完");
                }
                //总数量不为空时 判断 总数量 是否大于已使用的数量
                if (quotaConfig.getQuatoTotalNum() != null
                        && quotaConfig.getQuatoTotalNum() <= useQuotaTotal.getUseNum()) {
                    throw new TipsException("申请总额度数量已使用完");
                }
            }
 
            if (useQuotaMonth != null) {
                //月额度不为空时 判断 总额度 是否大于已使用的额度
                if (quotaConfig.getQuatoPrice() != null
                        && quotaConfig.getQuatoPrice().compareTo(useQuotaMonth.getUseAmount()) <= 0) {
                    throw new TipsException("申请月额度已使用完");
                }
                //月数量不为空时 判断 总数量 是否大于已使用的数量
                if (quotaConfig.getQuatoNum() != null
                        && quotaConfig.getQuatoNum() <= useQuotaMonth.getUseNum()) {
                    throw new TipsException("申请月额度数量已使用完");
                }
            }
 
            BigDecimal surplusTotalPrice;
            if(quotaConfig.getQuatoTotalPrice()!= null){
                surplusTotalPrice = quotaConfig.getQuatoTotalPrice().subtract(useQuotaTotal.getUseAmount());
                if (applyAmount.compareTo(surplusTotalPrice) == 1) {
                    throw new TipsException("总申请额度已超出,无法申请");
                }
            }
 
            BigDecimal surplusMonthPrice;
            if(quotaConfig.getQuatoPrice()!= null){
                surplusMonthPrice = quotaConfig.getQuatoPrice().subtract(useQuotaTotal.getUseAmount());
                if (applyAmount.compareTo(surplusMonthPrice) == 1) {
                    throw new TipsException("月申请额度已超出,无法申请");
                }
            }
 
            Integer surplusTotalNum;
            if(quotaConfig.getQuatoTotalNum()!= null){
                surplusTotalNum = quotaConfig.getQuatoTotalNum() - useQuotaMonth.getUseNum();
                if (applyNum > surplusTotalNum) {
                    throw new TipsException("总申请数量额度已超出,无法申请");
                }
            }
 
            Integer surplusMonthNum;
            if(quotaConfig.getQuatoNum()!= null){
                surplusMonthNum = quotaConfig.getQuatoNum() - useQuotaMonth.getUseNum();
                if (applyNum > surplusMonthNum) {
                    throw new TipsException("月申请数量额度已超出,无法申请");
                }
            }
 
        }
 
    }
 
 
    @Override
    public void checkDeptQuotaApply(CouponAddDto couponAddDto) {
 
        if (!couponAddDto.getType().equals(CommonShopQuotaConfig.TYPE_DB)) {
            throw new TipsException("类型错误!");
        }
 
        //优惠券成本部门名称 数量 金额
        String asDeptName;
        Integer applyNum;
        BigDecimal applyAmount;
        OrganizeDepartmentQuotaConfig quotaConfig;
        for (Object obj :  couponAddDto.getCostDeptListArr()) {
            Map<String, ArrayList<Object>> stringMap = (Map<String, ArrayList<Object>>) obj;
            for (String key : stringMap.keySet()) {
                //成本部门名称
                asDeptName = key;
                ArrayList<Object> bodyPartData = stringMap.get(key);
                applyNum = Integer.valueOf((String) bodyPartData.get(0));
                applyAmount = new BigDecimal((String) bodyPartData.get(1));
 
                quotaConfig = orgDeptQuotaConfigMapper.getConfigInfo(asDeptName, OrganizeDepartmentQuotaConfig.TYPE_SLAT);
                if (quotaConfig == null) {
                    throw new TipsException(asDeptName + "没有配置额度限制");
                }
 
                if (quotaConfig.getQuatoTotalPrice() != null || quotaConfig.getQuatoTotalNum() != null || quotaConfig.getQuatoNum() != null || quotaConfig.getQuatoPrice() != null) {
                    String sendReason = couponAddDto.getType();
                    //查询总的使用额度 查询月的额度
                    CouponNumber useQuotaTotal = couponNumberMapper.couponUseDeptQuotaInfo(asDeptName, sendReason, null, null);
                    //查询月的使用额度
                    CouponNumber useQuotaMonth = couponNumberMapper.couponUseQuotaInfo(asDeptName, sendReason, DateUtil.getMonthStart(new Date()), DateUtil.getMonthEnd(new Date()));
                    //检验判断
                    if (useQuotaTotal != null) {
                        //总额度不为空时 判断 总额度 是否大于已使用的额度
                        if (quotaConfig.getQuatoTotalPrice() != null
                                && quotaConfig.getQuatoTotalPrice().compareTo(useQuotaTotal.getUseAmount()) <= 0) {
                            throw new TipsException(asDeptName + "总额度已使用完");
                        }
                        //总数量不为空时 判断 总数量 是否大于已使用的数量
                        if (quotaConfig.getQuatoTotalNum() != null
                                && quotaConfig.getQuatoTotalNum() <= useQuotaTotal.getUseNum()) {
                            throw new TipsException(asDeptName + "总额度数量已使用完");
                        }
                    }
 
                    if (useQuotaMonth != null) {
                        //月额度不为空时 判断 总额度 是否大于已使用的额度
                        if (quotaConfig.getQuatoPrice() != null
                                && quotaConfig.getQuatoPrice().compareTo(useQuotaMonth.getUseAmount()) <= 0) {
                            throw new TipsException(asDeptName + "月额度已使用完");
                        }
                        //月数量不为空时 判断 总数量 是否大于已使用的数量
                        if (quotaConfig.getQuatoNum() != null
                                && quotaConfig.getQuatoNum() <= useQuotaMonth.getUseNum()) {
                            throw new TipsException(asDeptName + "月额度数量已使用完");
                        }
                    }
 
                    BigDecimal surplusTotalPrice;
                    if (quotaConfig.getQuatoTotalPrice() != null) {
                        surplusTotalPrice = quotaConfig.getQuatoTotalPrice().subtract(useQuotaTotal.getUseAmount());
                        if (applyAmount.compareTo(surplusTotalPrice) == 1) {
                            throw new TipsException(asDeptName + "总额度已超出,无法申请");
                        }
                    }
 
                    BigDecimal surplusMonthPrice;
                    if (quotaConfig.getQuatoPrice() != null) {
                        surplusMonthPrice = quotaConfig.getQuatoPrice().subtract(useQuotaMonth.getUseAmount());
                        if (applyAmount.compareTo(surplusMonthPrice) == 1) {
                            throw new TipsException(asDeptName + "月额度已超出,无法申请");
                        }
                    }
 
                    Integer surplusTotalNum;
                    if (quotaConfig.getQuatoTotalNum() != null) {
                        surplusTotalNum = quotaConfig.getQuatoTotalNum() - useQuotaTotal.getUseNum();
                        if (applyNum > surplusTotalNum) {
                            throw new TipsException(asDeptName + "总数量额度已超出,无法申请");
                        }
                    }
 
                    Integer surplusMonthNum;
                    if (quotaConfig.getQuatoNum() != null) {
                        surplusMonthNum = quotaConfig.getQuatoNum() - useQuotaMonth.getUseNum();
                        if (applyNum > surplusMonthNum) {
                            throw new TipsException(asDeptName + "月申请数量额度已超出,无法申请");
                        }
                    }
                }
            }
        }
    }
 
    @Override
    public void checkZzjQuotaApply(CouponAddDto couponAddDto) {
 
        if(!couponAddDto.getType().equals(CommonShopQuotaConfig.TYPE_ZZJ)){
            throw new TipsException("类型错误!");
        }
 
        String shopId = couponAddDto.getShopId();
        //总金额
        BigDecimal applyAmount = couponAddDto.getApplyAmount();
 
        //查询增值金额度配置
        CommonShopQuotaConfig quotaConfig = commonShopQuotaConfigMapper.getConfigInfo(shopId, CommonShopQuotaConfig.TYPE_ZZJ);
        if (quotaConfig.getQuatoTotalPrice() != null || quotaConfig.getQuatoPrice() != null) {
            //查询总的使用额度 查询月的额度
            UserMoneyUnclaimed useQuotaTotal = userMoneyUnclaimedMapper.moneyUseInfoByFundType(shopId, 1, null, null);
            //查询月的使用额度
            UserMoneyUnclaimed useQuotaMonth = userMoneyUnclaimedMapper.moneyUseInfoByFundType(shopId, 1, DateUtil.getMonthStart(new Date()), DateUtil.getMonthEnd(new Date()));
            //检验判断
            if (useQuotaTotal != null) {
                //总额度不为空时 判断 总额度 是否大于已使用的额度
                if (quotaConfig.getQuatoTotalPrice() != null
                        && quotaConfig.getQuatoTotalPrice().compareTo(useQuotaTotal.getUseAmount()) <= 0) {
                    throw new TipsException("申请总额度已使用完");
                }
            }
 
            if (useQuotaMonth != null) {
                //月额度不为空时 判断 总额度 是否大于已使用的额度
                if (quotaConfig.getQuatoPrice() != null
                        && quotaConfig.getQuatoPrice().compareTo(useQuotaMonth.getUseAmount()) <= 0) {
                    throw new TipsException("申请月额度已使用完");
                }
            }
 
            BigDecimal surplusTotalPrice;
            if (quotaConfig.getQuatoTotalPrice() != null) {
                surplusTotalPrice = quotaConfig.getQuatoTotalPrice().subtract(useQuotaTotal.getUseAmount());
                if (applyAmount.compareTo(surplusTotalPrice) == 1) {
                    throw new TipsException("总申请额度已超出,无法申请");
                }
            }
 
            BigDecimal surplusMonthPrice;
            if (quotaConfig.getQuatoPrice() != null) {
                surplusMonthPrice = quotaConfig.getQuatoPrice().subtract(useQuotaTotal.getUseAmount());
                if (applyAmount.compareTo(surplusMonthPrice) == 1) {
                    throw new TipsException("月申请额度已超出,无法申请");
                }
            }
        }
    }
}