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("月申请额度已超出,无法申请");
|
}
|
}
|
}
|
}
|
}
|