| | |
| | | package com.matrix.system.hive.service.imp; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.matrix.core.exception.GlobleException; |
| | | import com.matrix.core.pojo.PaginationVO; |
| | | import com.matrix.core.pojo.VerificationResult; |
| | | import com.matrix.core.tools.DateUtil; |
| | | import com.matrix.core.tools.LogUtil; |
| | | import com.matrix.system.common.dao.SysUsersDao; |
| | | import com.matrix.system.common.validate.GroupValidateStatusCheckUtil; |
| | | import com.matrix.system.common.validate.group.Group; |
| | | import com.matrix.system.constance.Dictionary; |
| | | import com.matrix.system.hive.bean.MoneyCardUse; |
| | | import com.matrix.system.hive.bean.MoneyCardUseFreeze; |
| | | import com.matrix.system.hive.bean.SysOrderItem; |
| | | import com.matrix.system.hive.bean.SysVipInfo; |
| | | import com.matrix.system.hive.bean.*; |
| | | import com.matrix.system.hive.dao.*; |
| | | import com.matrix.system.hive.dto.MoneyCardOperationDto; |
| | | import com.matrix.system.hive.plugin.util.MoneyUtil; |
| | | import com.matrix.system.hive.service.MoneyCardUseService; |
| | | import com.matrix.system.hive.service.ShoppingGoodsService; |
| | | import com.matrix.system.hive.service.SysOrderService; |
| | | import com.matrix.system.hive.validation.MoneyCardUseGroup; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | |
| | | /** |
| | | * @date 2016-09-17 10:17 |
| | | */ |
| | | @Service("moneyCardUseService") |
| | | public class MoneyCardUseServiceImpl extends ServiceImpl<MoneyCardUseDao,MoneyCardUse> implements MoneyCardUseService { |
| | | public class MoneyCardUseServiceImpl extends ServiceImpl<MoneyCardUseDao, MoneyCardUse> implements MoneyCardUseService { |
| | | |
| | | |
| | | @Autowired |
| | |
| | | private SysOrderItemDao orderItemDao; |
| | | |
| | | @Autowired |
| | | SysOrderService sysOrderService; |
| | | |
| | | @Autowired |
| | | private SysVipLevelDao viplevelDao; |
| | | |
| | | @Autowired |
| | | ShoppingGoodsDao shoppingGoodsDao; |
| | | |
| | | @Autowired |
| | | MoneyCardAssembleDao moneyCardAssembleDao; |
| | | @Autowired |
| | | MoneyCardUseFlowDao moneyCardUseFlowDao; |
| | | |
| | | @Autowired |
| | | SysUsersDao sysUsersDao; |
| | | |
| | | @Autowired |
| | | private ShoppingGoodsService shoppingGoodsService; |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void addVipMoneyCard(List<MoneyCardOperationDto> moneyCardOperationDtos) { |
| | | moneyCardOperationDtos.forEach(dto -> { |
| | | |
| | | //参数校验 |
| | | GroupValidateStatusCheckUtil.check(dto,Group.ADD.class); |
| | | |
| | | ShoppingGoods shoppingGoods = shoppingGoodsDao.selectById(dto.getGoodsId()); |
| | | MoneyCardUse moneyCardUse = new MoneyCardUse(); |
| | | BeanUtil.copyProperties(dto, moneyCardUse); |
| | | moneyCardUse.setGiftMoney(shoppingGoods.getReferencePice()); |
| | | moneyCardUse.setRealMoney(shoppingGoods.getSealPice()); |
| | | moneyCardUse.setGoodsId(shoppingGoods.getId()); |
| | | moneyCardUse.setStatus(Dictionary.MONEYCARD_STATUS_YX); |
| | | moneyCardUse.setIsVipCar(Dictionary.FLAG_NO_N); |
| | | moneyCardUse.setCardName(shoppingGoods.getName()); |
| | | |
| | | //余次处理 |
| | | if (shoppingGoods.getCarUseCount() == null || shoppingGoods.getCarUseCount() == 0) { |
| | | moneyCardUse.setUseTotal(999999999); |
| | | moneyCardUse.setLastCount(999999999); |
| | | } else { |
| | | moneyCardUse.setUseTotal(shoppingGoods.getCarUseCount()); |
| | | moneyCardUse.setLastCount(shoppingGoods.getCarUseCount()); |
| | | } |
| | | //失效时间处理 |
| | | Date invalidTime = shoppingGoodsService.calInvalidTime(shoppingGoods, 1, null); |
| | | moneyCardUse.setFailTime(invalidTime); |
| | | moneyCardUseDao.insert(moneyCardUse); |
| | | |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public void deleteByOrderItemId(Long orderItemId) { |
| | | |
| | | List<MoneyCardUse> moneyCardUseList = getMoneyCardUseListByOrderItemId(orderItemId); |
| | | moneyCardUseList.forEach(moneyCardUse -> { |
| | | MoneyCardUseFlow moneyCardUseFlow=new MoneyCardUseFlow(); |
| | | moneyCardUseFlow.setCarUseId(moneyCardUse.getId()); |
| | | int i = moneyCardUseFlowDao.selectTotalRecord(moneyCardUseFlow); |
| | | if(i>0){ |
| | | throw new GlobleException(moneyCardUse.getCardName()+"储值卡已经被启用无法删除"); |
| | | }else { |
| | | LogUtil.info("根据订单明细id{},删除储值卡:{}", orderItemId,moneyCardUse.getCardName()); |
| | | moneyCardUseDao.deleteById(moneyCardUse.getId()); |
| | | } |
| | | }); |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void moneyCardTk(List<MoneyCardOperationDto> moneyCardOperationDtos) { |
| | | |
| | | moneyCardOperationDtos.forEach(moneyCardOperationDto -> { |
| | | |
| | | GroupValidateStatusCheckUtil.check(moneyCardOperationDto, MoneyCardUseGroup.moneyCardUseTk.class); |
| | | |
| | | List<MoneyCardUse> moneyCardUseList = getMoneyCardUseListByOrderItemId(moneyCardOperationDto.getOrderItemId()); |
| | | for (MoneyCardUse moneyCardUse : moneyCardUseList) { |
| | | //本金扣款 |
| | | if (moneyCardUse.getRealMoney() + moneyCardOperationDto.getRealMoney() >= 0) { |
| | | double surplus = MoneyUtil.add(moneyCardUse.getRealMoney(), moneyCardOperationDto.getRealMoney()); |
| | | moneyCardUse.setRealMoney(surplus); |
| | | } else { |
| | | throw new GlobleException(moneyCardUse.getCardName() + "余额不足"); |
| | | } |
| | | //余额为0时充值卡变为无效 |
| | | if (moneyCardUse.getRealMoney().equals(0D)) { |
| | | moneyCardUse.setIsOver(Dictionary.FLAG_YES_Y); |
| | | moneyCardUse.setStatus(Dictionary.TAOCAN_STATUS_TK); |
| | | } |
| | | moneyCardUse.setGiftMoney(0D); |
| | | moneyCardUseDao.update(moneyCardUse); |
| | | saveMoneyCardUseFlow(moneyCardOperationDto,moneyCardUse); |
| | | } |
| | | |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 根据订单明细id查询会员卡 |
| | | * @param orderItemId |
| | | * @return |
| | | */ |
| | | private List<MoneyCardUse> getMoneyCardUseListByOrderItemId(Long orderItemId) { |
| | | LambdaQueryWrapper<MoneyCardUse> lambdaQueryChainWrapper = new LambdaQueryWrapper(); |
| | | lambdaQueryChainWrapper.eq(MoneyCardUse::getOrderItemId, orderItemId); |
| | | List<MoneyCardUse> moneyCardUseList = list(lambdaQueryChainWrapper); |
| | | return moneyCardUseList; |
| | | } |
| | | |
| | | /** |
| | | * 保存储值卡流水 |
| | | * @param moneyCardOperationDto |
| | | * @param moneyCardUse |
| | | */ |
| | | private void saveMoneyCardUseFlow(MoneyCardOperationDto moneyCardOperationDto,MoneyCardUse moneyCardUse){ |
| | | MoneyCardUseFlow moneyCardUseFlow=new MoneyCardUseFlow(); |
| | | moneyCardUseFlow.setCarUseId(moneyCardUse.getId()); |
| | | moneyCardUseFlow.setOrderNo(moneyCardOperationDto.getOrderNo()); |
| | | moneyCardUseFlow.setVipId(moneyCardUse.getVipId()); |
| | | moneyCardUseFlow.setTimes(moneyCardUse.getLastCount()); |
| | | moneyCardUseFlow.setType(moneyCardOperationDto.getType()); |
| | | moneyCardUseFlow.setCreateTime(new Date()); |
| | | moneyCardUseFlow.setOperationId(moneyCardOperationDto.getUpdateUser()); |
| | | moneyCardUseFlow.setTotal(moneyCardUse.getRealMoney()); |
| | | moneyCardUseFlow.setGiftMoney(moneyCardUse.getGiftMoney()); |
| | | moneyCardUseFlow.setTotal(moneyCardOperationDto.getRealMoney()); |
| | | moneyCardUseFlow.setBalance(moneyCardUse.getGiftMoney() + moneyCardUse.getRealMoney()); |
| | | moneyCardUseFlow.setStaffName(sysUsersDao.selectById(moneyCardOperationDto.getUpdateUser()).getSuName()); |
| | | |
| | | |
| | | |
| | | moneyCardUseFlowDao.insert(moneyCardUseFlow); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void changeMoneyCard(List<MoneyCardOperationDto> moneyCardOperationDtoList) { |
| | | moneyCardOperationDtoList.forEach(moneyCardOperationDto -> { |
| | | |
| | | |
| | | if (MoneyCardUseFlow.USE_TYPE_CONSUMER.equals(moneyCardOperationDto.getType())) { |
| | | //储值卡支付订单 扣减储值卡余额 |
| | | GroupValidateStatusCheckUtil.check(moneyCardOperationDto,MoneyCardUseGroup.moneyCardUseOrderChange.class); |
| | | moneyCardPayOrder(moneyCardOperationDto); |
| | | } else if (MoneyCardUseFlow.USE_TYPE_ORDRE_CANCEL.equals(moneyCardOperationDto.getType())) { |
| | | //回退储值卡余额 |
| | | GroupValidateStatusCheckUtil.check(moneyCardOperationDto,MoneyCardUseGroup.moneyCardUseOrderChange.class); |
| | | returnMoneyCard(moneyCardOperationDto); |
| | | } else if (MoneyCardUseFlow.USE_TYPE_CZ.equals(moneyCardOperationDto.getType())) { |
| | | //回退储值卡余额 |
| | | GroupValidateStatusCheckUtil.check(moneyCardOperationDto,MoneyCardUseGroup.moneyCardUseCz.class); |
| | | moneyCardCz(moneyCardOperationDto); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 会员卡充值 |
| | | * @param moneyCardOperationDto |
| | | */ |
| | | private void moneyCardCz(MoneyCardOperationDto moneyCardOperationDto) { |
| | | |
| | | LogUtil.info("会员卡充值 {}", JSON.toJSONString(moneyCardOperationDto)); |
| | | MoneyCardUse moneyCardUse = moneyCardUseDao.selectById(moneyCardOperationDto.getCarUseId()); |
| | | |
| | | |
| | | if (Objects.nonNull(moneyCardOperationDto.getGiftMoney())) { |
| | | //jyytodo 余额余次的修改和新增在数据库层面做加减 |
| | | moneyCardUse.setGiftMoney(moneyCardUse.getGiftMoney() + moneyCardOperationDto.getGiftMoney()); |
| | | } |
| | | if (Objects.nonNull(moneyCardOperationDto.getRealMoney())) { |
| | | moneyCardUse.setRealMoney(moneyCardUse.getRealMoney() + moneyCardOperationDto.getRealMoney()); |
| | | } |
| | | moneyCardUseDao.update(moneyCardUse); |
| | | //设置卡项使用流水 |
| | | saveMoneyCardUseFlow(moneyCardOperationDto,moneyCardUse); |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 回退储值卡余额 |
| | | * |
| | | * @param moneyCardOperationDto |
| | | */ |
| | | private void returnMoneyCard(MoneyCardOperationDto moneyCardOperationDto) { |
| | | LogUtil.info("回退储值卡余额 {}", JSON.toJSONString(moneyCardOperationDto)); |
| | | MoneyCardUse moneyCardUse = moneyCardUseDao.selectById(moneyCardOperationDto.getCarUseId()); |
| | | MoneyCardUseFlow moneyCardUseFlow = new MoneyCardUseFlow(); |
| | | |
| | | if (Objects.nonNull(moneyCardOperationDto.getGiftMoney())) { |
| | | //jyytodo 余额余次的修改和新增在数据库层面做加减 |
| | | moneyCardUse.setGiftMoney(moneyCardUse.getGiftMoney() + moneyCardOperationDto.getGiftMoney()); |
| | | } |
| | | if (Objects.nonNull(moneyCardOperationDto.getRealMoney())) { |
| | | moneyCardUse.setRealMoney(moneyCardUse.getRealMoney() + moneyCardOperationDto.getRealMoney()); |
| | | } |
| | | moneyCardUse.setIsOver(Dictionary.FLAG_YES_Y); |
| | | moneyCardUse.setStatus(Dictionary.MONEYCARD_STATUS_YX); |
| | | moneyCardUseDao.update(moneyCardUse); |
| | | |
| | | |
| | | //设置卡项使用流水 |
| | | saveMoneyCardUseFlow(moneyCardOperationDto,moneyCardUse); |
| | | } |
| | | |
| | | /** |
| | | * 储值卡支付订单 扣减储值卡余额 |
| | | * |
| | | * @param moneyCardOperationDto |
| | | */ |
| | | private void moneyCardPayOrder(MoneyCardOperationDto moneyCardOperationDto) { |
| | | // 判断商品是否在充值卡的限制购买 |
| | | VerificationResult verificationResult = checkIsBangding(moneyCardOperationDto.getCarUseId(), moneyCardOperationDto.getGoodsIds()); |
| | | if (!verificationResult.isJudgeResult()) { |
| | | throw new GlobleException(verificationResult.getMsg()); |
| | | } |
| | | |
| | | LogUtil.info("扣减储值卡余额 {}", JSON.toJSONString(moneyCardOperationDto)); |
| | | MoneyCardUse moneyCardUse = moneyCardUseDao.selectById(moneyCardOperationDto.getCarUseId()); |
| | | |
| | | // 用户默认储值卡不进行欠款检查 20220512 wzy |
| | | if (Dictionary.FLAG_NO_N.equals(moneyCardUse.getIsVipCar())) { |
| | | //欠款检查 |
| | | checkOrderStatu(moneyCardUse.getOrderItemId()); |
| | | } |
| | | |
| | | //赠送金额扣款 |
| | | if (moneyCardUse.getGiftMoney() + moneyCardOperationDto.getGiftMoney() >= 0) { |
| | | double surplus = MoneyUtil.add(moneyCardUse.getGiftMoney(), moneyCardOperationDto.getGiftMoney()); |
| | | moneyCardUse.setGiftMoney(surplus); |
| | | } else { |
| | | throw new GlobleException(moneyCardUse.getCardName() + "余额不足"); |
| | | } |
| | | //本金扣款 |
| | | if (moneyCardUse.getRealMoney() + moneyCardOperationDto.getRealMoney() >= 0) { |
| | | double surplus = MoneyUtil.add(moneyCardUse.getRealMoney(), moneyCardOperationDto.getRealMoney()); |
| | | moneyCardUse.setRealMoney(surplus); |
| | | } else { |
| | | throw new GlobleException(moneyCardUse.getCardName() + "余额不足"); |
| | | } |
| | | |
| | | |
| | | if (moneyCardUse.getIsVipCar().equals(Dictionary.FLAG_NO_N)) { |
| | | //余额为0时充值卡变为无效 |
| | | if (moneyCardUse.getRealMoney().equals(0D) && moneyCardUse.getGiftMoney().equals(0D)) { |
| | | moneyCardUse.setIsOver(Dictionary.FLAG_YES_Y); |
| | | moneyCardUse.setStatus(Dictionary.MONEYCARD_STATUS_WX); |
| | | } |
| | | } |
| | | |
| | | ShoppingGoods shoppingGoods = shoppingGoodsDao.selectById(moneyCardUse.getGoodsId()); |
| | | if (shoppingGoods != null) { |
| | | Date invalidTime = shoppingGoodsService.calInvalidTime(shoppingGoods, 2, moneyCardUse.getFailTime()); |
| | | moneyCardUse.setFailTime(invalidTime); |
| | | } |
| | | |
| | | // 储值卡不减少使用次数 20220512 wzy |
| | | if (Dictionary.FLAG_NO_N.equals(moneyCardUse.getIsVipCar())) { |
| | | moneyCardUse.setLastCount(moneyCardUse.getLastCount() - moneyCardOperationDto.getCount()); |
| | | } |
| | | |
| | | // 更新充值卡信息 |
| | | moneyCardUseDao.update(moneyCardUse); |
| | | //设置卡项使用流水 |
| | | saveMoneyCardUseFlow(moneyCardOperationDto,moneyCardUse); |
| | | } |
| | | |
| | | /** |
| | | * 检查商品是否在储值卡的消费范围内 |
| | | * |
| | | * @param moneyCardUseId 储值卡id |
| | | * @param goodsIds 商品id集合 |
| | | * @return |
| | | */ |
| | | private VerificationResult checkIsBangding(Long moneyCardUseId, List<Long> goodsIds) { |
| | | // 如果是一卡通则肯定在绑定范围内 |
| | | MoneyCardUse moneyCardUse = moneyCardUseDao.selectById(moneyCardUseId); |
| | | //默认储值卡可以购买所有产品 |
| | | if (moneyCardUse.getIsVipCar().equals(Dictionary.FLAG_YES_Y)) { |
| | | return VerificationResult.success(); |
| | | } else { |
| | | // 不是默认储值卡判断卡是否可应用于所有产品 |
| | | ShoppingGoods cardGoods = shoppingGoodsDao.selectById(moneyCardUse.getGoodsId()); |
| | | if (cardGoods != null) { |
| | | if (Dictionary.FLAG_YES.equals(cardGoods.getCarIsAll())) { |
| | | return VerificationResult.success(); |
| | | } |
| | | } |
| | | // 比较分类 |
| | | MoneyCardAssemble moneyCardAssemble = new MoneyCardAssemble(); |
| | | moneyCardAssemble.setCardId(moneyCardUse.getGoodsId()); |
| | | List<MoneyCardAssemble> cardAssembleList = moneyCardAssembleDao.selectByModel(moneyCardAssemble); |
| | | List<ShoppingGoods> yhGoods = shoppingGoodsDao.selectByIds(goodsIds); |
| | | |
| | | StringBuilder msgBuilder = new StringBuilder(); |
| | | |
| | | for (ShoppingGoods goods : yhGoods) { |
| | | boolean isMatch = false; |
| | | for (MoneyCardAssemble cardAssemble : cardAssembleList) { |
| | | // 比较类型 |
| | | if (cardAssemble.getType().equals(Dictionary.CZK_ASSEMBLE_FL)) { |
| | | |
| | | if (goods.getCateId().equals(cardAssemble.getCateId())) { |
| | | isMatch = true; |
| | | break; |
| | | } |
| | | } else { |
| | | // 比较绑定 |
| | | if (goods.getId().equals(cardAssemble.getGoodsId())) { |
| | | isMatch = true; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | if (!isMatch) { |
| | | msgBuilder.append(goods.getName() + "不在" + moneyCardUse.getCardName() + "优惠中 "); |
| | | } |
| | | } |
| | | if (msgBuilder.length() > 0) { |
| | | return VerificationResult.fail(msgBuilder.toString()); |
| | | } else { |
| | | return VerificationResult.success(); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public int add(MoneyCardUse moneyCardUse) { |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void transfer(MoneyCardUse moneyCardUse, Long vipId2, Double money) { |
| | | Long oldId = moneyCardUse.getId(); |
| | | |
| | | moneyCardUse = moneyCardUseDao.selectById(moneyCardUse.getId()); |
| | | if (moneyCardUse.getVipId().equals(vipId2)) { |
| | | throw new GlobleException("转让人不能是该充值卡拥有者!"); |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | //设置为有效 |
| | | @Override |
| | | public void active(MoneyCardUse proj) { |
| | |
| | | /** |
| | | * 检测订单条目是否已付款完毕 |
| | | */ |
| | | public void checkOrderStatu(Long id) { |
| | | SysOrderItem item = orderItemDao.selectById(id); |
| | | if (!Dictionary.ORDER_STATU_YFK.equals(item.getStatus())) { |
| | | throw new GlobleException("该商品还存在欠款!"); |
| | | public void checkOrderStatu(Long orderItemId) { |
| | | SysOrder order = sysOrderService.findById(orderItemDao.selectById(orderItemId).getOrderId()); |
| | | if (!Dictionary.ORDER_STATU_YFK.equals(order.getStatu())) { |
| | | throw new GlobleException("储值卡购买订单还存在欠款,在补交欠款后才能使用!"); |
| | | } |
| | | } |
| | | |