rx
2023-12-07 a095fb55c6f742ee67cf1193a3e6ba8c29b1828a
提交 | 用户 | age
a811f2 1 package com.hx.phip.service.impl;
052f63 2
394051 3 import com.hx.exception.TipsException;
R 4 import com.hx.phiappt.model.coupon.CouponNumber;
a811f2 5 import com.hx.phiappt.model.CommonShopQuotaConfig;
ddfed3 6 import com.hx.phiappt.model.organize.OrganizeDepartmentQuotaConfig;
a811f2 7 import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed;
394051 8 import com.hx.phip.dao.mapper.CouponNumberMapper;
a811f2 9 import com.hx.phip.dao.mapper.CommonShopQuotaConfigMapper;
ddfed3 10 import com.hx.phip.dao.mapper.OrganizeDepartmentQuotaConfigMapper;
a811f2 11 import com.hx.phip.dao.mapper.UserMoneyUnclaimedMapper;
R 12 import com.hx.phip.service.CommonShopQuotaConfigService;
394051 13 import com.hx.util.DateUtil;
052f63 14 import com.hz.his.dto.coupon.CouponAddDto;
e30f22 15 import lombok.extern.slf4j.Slf4j;
052f63 16 import org.springframework.stereotype.Service;
R 17 import org.springframework.transaction.annotation.Transactional;
18
19 import javax.annotation.Resource;
394051 20 import java.math.BigDecimal;
ddfed3 21 import java.util.*;
052f63 22
R 23 @Transactional
24 @Service
e30f22 25 @Slf4j
a811f2 26 public class CommonShopQuotaConfigServiceImpl implements CommonShopQuotaConfigService {
052f63 27
R 28     @Resource
a811f2 29     private CommonShopQuotaConfigMapper commonShopQuotaConfigMapper;
394051 30     @Resource
R 31     private CouponNumberMapper couponNumberMapper;
ddfed3 32     @Resource
R 33     private OrganizeDepartmentQuotaConfigMapper orgDeptQuotaConfigMapper;
a811f2 34     @Resource
R 35     private UserMoneyUnclaimedMapper userMoneyUnclaimedMapper;
052f63 36
394051 37     /**
R 38      * 获取门店-优惠券 限额配置
39      */
052f63 40     @Override
394051 41     public void checkQuotaApply(CouponAddDto couponAddDto) {
R 42
21c9ca 43         if(!couponAddDto.getType().equals(CommonShopQuotaConfig.TYPE_CCJ) && !couponAddDto.getType().equals(CommonShopQuotaConfig.TYPE_KS)){
a811f2 44             throw new TipsException("类型错误!");
R 45         }
46
394051 47         String shopId = couponAddDto.getShopId();
a811f2 48         //CommonShopQuotaConfig 定义的类型
R 49         String typeStr = couponAddDto.getType();
394051 50         //所申请优惠券总数
R 51         Integer applyNum = couponAddDto.getApplyNum();
52         //总金额
53         BigDecimal applyAmount = couponAddDto.getApplyAmount();
54
55         //查询指定类型的额度配置 为空 拿取通用额度的数据
a811f2 56         CommonShopQuotaConfig quotaConfig = commonShopQuotaConfigMapper.getConfigInfo(shopId, typeStr);
394051 57         if (quotaConfig == null) {
a811f2 58             quotaConfig = commonShopQuotaConfigMapper.getConfigInfo(shopId, CommonShopQuotaConfig.TYPE_COMMON);
a57c1a 59             if (quotaConfig == null) {
394051 60                 throw new TipsException("门店没有配置限额请配置!");
a57c1a 61             }
394051 62         }
R 63
64         if (quotaConfig.getQuatoTotalPrice() != null || quotaConfig.getQuatoTotalNum() != null || quotaConfig.getQuatoNum() != null || quotaConfig.getQuatoPrice() != null) {
65             //查询总的使用额度 查询月的额度
a811f2 66             CouponNumber useQuotaTotal = couponNumberMapper.couponUseQuotaInfo(shopId, typeStr, null, null);
394051 67             //查询月的使用额度
a811f2 68             CouponNumber useQuotaMonth = couponNumberMapper.couponUseQuotaInfo(shopId, typeStr, DateUtil.getMonthStart(new Date()), DateUtil.getMonthEnd(new Date()));
394051 69             //检验判断
R 70             if (useQuotaTotal != null) {
71                 //总额度不为空时 判断 总额度 是否大于已使用的额度
72                 if (quotaConfig.getQuatoTotalPrice() != null
e30f22 73                         && quotaConfig.getQuatoTotalPrice().compareTo(useQuotaTotal.getUseAmount()) <= 0) {
394051 74                     throw new TipsException("申请总额度已使用完");
R 75                 }
76                 //总数量不为空时 判断 总数量 是否大于已使用的数量
77                 if (quotaConfig.getQuatoTotalNum() != null
e30f22 78                         && quotaConfig.getQuatoTotalNum() <= useQuotaTotal.getUseNum()) {
394051 79                     throw new TipsException("申请总额度数量已使用完");
R 80                 }
81             }
82
83             if (useQuotaMonth != null) {
84                 //月额度不为空时 判断 总额度 是否大于已使用的额度
85                 if (quotaConfig.getQuatoPrice() != null
e30f22 86                         && quotaConfig.getQuatoPrice().compareTo(useQuotaMonth.getUseAmount()) <= 0) {
394051 87                     throw new TipsException("申请月额度已使用完");
R 88                 }
89                 //月数量不为空时 判断 总数量 是否大于已使用的数量
90                 if (quotaConfig.getQuatoNum() != null
e30f22 91                         && quotaConfig.getQuatoNum() <= useQuotaMonth.getUseNum()) {
394051 92                     throw new TipsException("申请月额度数量已使用完");
R 93                 }
94             }
95
fc99d4 96             BigDecimal surplusTotalPrice;
R 97             if(quotaConfig.getQuatoTotalPrice()!= null){
98                 surplusTotalPrice = quotaConfig.getQuatoTotalPrice().subtract(useQuotaTotal.getUseAmount());
99                 if (applyAmount.compareTo(surplusTotalPrice) == 1) {
100                     throw new TipsException("总申请额度已超出,无法申请");
101                 }
394051 102             }
R 103
fc99d4 104             BigDecimal surplusMonthPrice;
R 105             if(quotaConfig.getQuatoPrice()!= null){
106                 surplusMonthPrice = quotaConfig.getQuatoPrice().subtract(useQuotaTotal.getUseAmount());
107                 if (applyAmount.compareTo(surplusMonthPrice) == 1) {
108                     throw new TipsException("月申请额度已超出,无法申请");
109                 }
394051 110             }
R 111
fc99d4 112             Integer surplusTotalNum;
R 113             if(quotaConfig.getQuatoTotalNum()!= null){
114                 surplusTotalNum = quotaConfig.getQuatoTotalNum() - useQuotaMonth.getUseNum();
115                 if (applyNum > surplusTotalNum) {
116                     throw new TipsException("总申请数量额度已超出,无法申请");
117                 }
394051 118             }
R 119
fc99d4 120             Integer surplusMonthNum;
R 121             if(quotaConfig.getQuatoNum()!= null){
122                 surplusMonthNum = quotaConfig.getQuatoNum() - useQuotaMonth.getUseNum();
123                 if (applyNum > surplusMonthNum) {
124                     throw new TipsException("月申请数量额度已超出,无法申请");
125                 }
394051 126             }
R 127
128         }
129
052f63 130     }
ddfed3 131
R 132
133     @Override
134     public void checkDeptQuotaApply(CouponAddDto couponAddDto) {
135
21c9ca 136         if (!couponAddDto.getType().equals(CommonShopQuotaConfig.TYPE_DB)) {
ddfed3 137             throw new TipsException("类型错误!");
R 138         }
139
140         //优惠券成本部门名称 数量 金额
141         String asDeptName;
142         Integer applyNum;
143         BigDecimal applyAmount;
144         OrganizeDepartmentQuotaConfig quotaConfig;
177ef0 145         for (Object obj :  couponAddDto.getCostDeptListArr()) {
R 146             Map<String, ArrayList<Object>> stringMap = (Map<String, ArrayList<Object>>) obj;
ddfed3 147             for (String key : stringMap.keySet()) {
R 148                 //成本部门名称
149                 asDeptName = key;
177ef0 150                 ArrayList<Object> bodyPartData = stringMap.get(key);
R 151                 applyNum = Integer.valueOf((String) bodyPartData.get(0));
152                 applyAmount = new BigDecimal((String) bodyPartData.get(1));
ddfed3 153
R 154                 quotaConfig = orgDeptQuotaConfigMapper.getConfigInfo(asDeptName, OrganizeDepartmentQuotaConfig.TYPE_SLAT);
177ef0 155                 if (quotaConfig == null) {
ddfed3 156                     throw new TipsException(asDeptName + "没有配置额度限制");
R 157                 }
158
159                 if (quotaConfig.getQuatoTotalPrice() != null || quotaConfig.getQuatoTotalNum() != null || quotaConfig.getQuatoNum() != null || quotaConfig.getQuatoPrice() != null) {
a811f2 160                     String sendReason = couponAddDto.getType();
ddfed3 161                     //查询总的使用额度 查询月的额度
R 162                     CouponNumber useQuotaTotal = couponNumberMapper.couponUseDeptQuotaInfo(asDeptName, sendReason, null, null);
163                     //查询月的使用额度
164                     CouponNumber useQuotaMonth = couponNumberMapper.couponUseQuotaInfo(asDeptName, sendReason, DateUtil.getMonthStart(new Date()), DateUtil.getMonthEnd(new Date()));
165                     //检验判断
166                     if (useQuotaTotal != null) {
167                         //总额度不为空时 判断 总额度 是否大于已使用的额度
168                         if (quotaConfig.getQuatoTotalPrice() != null
169                                 && quotaConfig.getQuatoTotalPrice().compareTo(useQuotaTotal.getUseAmount()) <= 0) {
170                             throw new TipsException(asDeptName + "总额度已使用完");
171                         }
172                         //总数量不为空时 判断 总数量 是否大于已使用的数量
173                         if (quotaConfig.getQuatoTotalNum() != null
174                                 && quotaConfig.getQuatoTotalNum() <= useQuotaTotal.getUseNum()) {
175                             throw new TipsException(asDeptName + "总额度数量已使用完");
176                         }
177                     }
178
179                     if (useQuotaMonth != null) {
180                         //月额度不为空时 判断 总额度 是否大于已使用的额度
181                         if (quotaConfig.getQuatoPrice() != null
182                                 && quotaConfig.getQuatoPrice().compareTo(useQuotaMonth.getUseAmount()) <= 0) {
183                             throw new TipsException(asDeptName + "月额度已使用完");
184                         }
185                         //月数量不为空时 判断 总数量 是否大于已使用的数量
186                         if (quotaConfig.getQuatoNum() != null
187                                 && quotaConfig.getQuatoNum() <= useQuotaMonth.getUseNum()) {
188                             throw new TipsException(asDeptName + "月额度数量已使用完");
189                         }
190                     }
191
192                     BigDecimal surplusTotalPrice;
193                     if (quotaConfig.getQuatoTotalPrice() != null) {
194                         surplusTotalPrice = quotaConfig.getQuatoTotalPrice().subtract(useQuotaTotal.getUseAmount());
195                         if (applyAmount.compareTo(surplusTotalPrice) == 1) {
196                             throw new TipsException(asDeptName + "总额度已超出,无法申请");
197                         }
198                     }
199
200                     BigDecimal surplusMonthPrice;
201                     if (quotaConfig.getQuatoPrice() != null) {
a095fb 202                         surplusMonthPrice = quotaConfig.getQuatoPrice().subtract(useQuotaMonth.getUseAmount());
ddfed3 203                         if (applyAmount.compareTo(surplusMonthPrice) == 1) {
R 204                             throw new TipsException(asDeptName + "月额度已超出,无法申请");
205                         }
206                     }
207
208                     Integer surplusTotalNum;
209                     if (quotaConfig.getQuatoTotalNum() != null) {
a095fb 210                         surplusTotalNum = quotaConfig.getQuatoTotalNum() - useQuotaTotal.getUseNum();
ddfed3 211                         if (applyNum > surplusTotalNum) {
R 212                             throw new TipsException(asDeptName + "总数量额度已超出,无法申请");
213                         }
214                     }
215
216                     Integer surplusMonthNum;
217                     if (quotaConfig.getQuatoNum() != null) {
218                         surplusMonthNum = quotaConfig.getQuatoNum() - useQuotaMonth.getUseNum();
219                         if (applyNum > surplusMonthNum) {
220                             throw new TipsException(asDeptName + "月申请数量额度已超出,无法申请");
221                         }
222                     }
223                 }
224             }
225         }
226     }
227
a811f2 228     @Override
R 229     public void checkZzjQuotaApply(CouponAddDto couponAddDto) {
230
231         if(!couponAddDto.getType().equals(CommonShopQuotaConfig.TYPE_ZZJ)){
232             throw new TipsException("类型错误!");
233         }
234
235         String shopId = couponAddDto.getShopId();
236         //总金额
237         BigDecimal applyAmount = couponAddDto.getApplyAmount();
238
239         //查询增值金额度配置
240         CommonShopQuotaConfig quotaConfig = commonShopQuotaConfigMapper.getConfigInfo(shopId, CommonShopQuotaConfig.TYPE_ZZJ);
241         if (quotaConfig.getQuatoTotalPrice() != null || quotaConfig.getQuatoPrice() != null) {
242             //查询总的使用额度 查询月的额度
243             UserMoneyUnclaimed useQuotaTotal = userMoneyUnclaimedMapper.moneyUseInfoByFundType(shopId, 1, null, null);
244             //查询月的使用额度
245             UserMoneyUnclaimed useQuotaMonth = userMoneyUnclaimedMapper.moneyUseInfoByFundType(shopId, 1, DateUtil.getMonthStart(new Date()), DateUtil.getMonthEnd(new Date()));
246             //检验判断
247             if (useQuotaTotal != null) {
248                 //总额度不为空时 判断 总额度 是否大于已使用的额度
249                 if (quotaConfig.getQuatoTotalPrice() != null
250                         && quotaConfig.getQuatoTotalPrice().compareTo(useQuotaTotal.getUseAmount()) <= 0) {
251                     throw new TipsException("申请总额度已使用完");
252                 }
253             }
254
255             if (useQuotaMonth != null) {
256                 //月额度不为空时 判断 总额度 是否大于已使用的额度
257                 if (quotaConfig.getQuatoPrice() != null
258                         && quotaConfig.getQuatoPrice().compareTo(useQuotaMonth.getUseAmount()) <= 0) {
259                     throw new TipsException("申请月额度已使用完");
260                 }
261             }
262
263             BigDecimal surplusTotalPrice;
264             if (quotaConfig.getQuatoTotalPrice() != null) {
265                 surplusTotalPrice = quotaConfig.getQuatoTotalPrice().subtract(useQuotaTotal.getUseAmount());
266                 if (applyAmount.compareTo(surplusTotalPrice) == 1) {
267                     throw new TipsException("总申请额度已超出,无法申请");
268                 }
269             }
270
271             BigDecimal surplusMonthPrice;
272             if (quotaConfig.getQuatoPrice() != null) {
273                 surplusMonthPrice = quotaConfig.getQuatoPrice().subtract(useQuotaTotal.getUseAmount());
274                 if (applyAmount.compareTo(surplusMonthPrice) == 1) {
275                     throw new TipsException("月申请额度已超出,无法申请");
276                 }
277             }
278         }
279     }
052f63 280 }