| | |
| | | package cc.mrbird.febs.mall.service.impl; |
| | | |
| | | import cc.mrbird.febs.common.entity.FebsResponse; |
| | | import cc.mrbird.febs.common.enumerates.DataDictionaryEnum; |
| | | import cc.mrbird.febs.common.enumerates.FlowTypeEnum; |
| | | import cc.mrbird.febs.common.enumerates.GreenScoreEnum; |
| | | import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum; |
| | | import cc.mrbird.febs.common.exception.FebsException; |
| | | import cc.mrbird.febs.common.utils.AppContants; |
| | | import cc.mrbird.febs.common.utils.LoginUserUtil; |
| | | import cc.mrbird.febs.common.utils.MallUtils; |
| | | import cc.mrbird.febs.mall.conversion.MallOrderInfoConversion; |
| | | import cc.mrbird.febs.mall.dto.ApiMallScoreSignRecordDto; |
| | | import cc.mrbird.febs.mall.dto.ScoreSettingDto; |
| | | import cc.mrbird.febs.mall.dto.*; |
| | | import cc.mrbird.febs.mall.entity.*; |
| | | import cc.mrbird.febs.mall.mapper.DataDictionaryCustomMapper; |
| | | import cc.mrbird.febs.mall.mapper.MallMemberWalletMapper; |
| | | import cc.mrbird.febs.mall.mapper.MallScoreSignRecordMapper; |
| | | import cc.mrbird.febs.mall.mapper.*; |
| | | import cc.mrbird.febs.mall.service.IApiMallMemberService; |
| | | import cc.mrbird.febs.mall.service.IApiMallMemberWalletService; |
| | | import cc.mrbird.febs.mall.service.IMallMoneyFlowService; |
| | | import cc.mrbird.febs.mall.service.IScoreService; |
| | | import cc.mrbird.febs.mall.vo.ApiMallScoreSignRecordVo; |
| | | import cc.mrbird.febs.mall.vo.ScoreSignVo; |
| | | import cc.mrbird.febs.mall.vo.*; |
| | | import cn.hutool.core.date.DateField; |
| | | import cn.hutool.core.date.DateTime; |
| | | import cn.hutool.core.date.DateUnit; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | |
| | | private final MallScoreSignRecordMapper mallScoreSignRecordMapper; |
| | | private final IMallMoneyFlowService mallMoneyFlowService; |
| | | private final IApiMallMemberWalletService walletService; |
| | | private final MallMemberMapper mallMemberMapper; |
| | | private final MallScoreVoucherMapper mallScoreVoucherMapper; |
| | | private final IApiMallMemberWalletService memberWalletService; |
| | | private final MallMemberBankMapper mallMemberBankMapper; |
| | | private final MallMemberWithdrawMapper mallMemberWithdrawMapper; |
| | | private final IApiMallMemberService mallMemberService; |
| | | private final MallScoreRecordMapper mallScoreRecordMapper; |
| | | |
| | | @Override |
| | | public ScoreSignVo scoreSign() { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public ApiScoreVoucherInfoVo memberScoreVoucher() { |
| | | MallMember member = LoginUserUtil.getLoginUser(); |
| | | ApiScoreVoucherInfoVo apiScoreVoucherInfoVo = new ApiScoreVoucherInfoVo(); |
| | | |
| | | MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(member.getId()); |
| | | apiScoreVoucherInfoVo.setVoucherCnt(mallMemberWallet.getVoucherCnt().compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : mallMemberWallet.getVoucherCnt()); |
| | | apiScoreVoucherInfoVo.setVoucherAmount(mallMemberWallet.getVoucherAmount().compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : mallMemberWallet.getVoucherAmount()); |
| | | apiScoreVoucherInfoVo.setVoucherFireCnt(mallMemberWallet.getVoucherFireCnt().compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : mallMemberWallet.getVoucherFireCnt()); |
| | | DataDictionaryCustom scorePriceDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | GreenScoreEnum.SCORE_PRICE.getType(), |
| | | GreenScoreEnum.SCORE_PRICE.getCode()); |
| | | apiScoreVoucherInfoVo.setScorePrice(new BigDecimal(scorePriceDic.getValue()).setScale(2,BigDecimal.ROUND_DOWN)); |
| | | DataDictionaryCustom scorePoolCntDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | GreenScoreEnum.SCORE_POOL_CNT.getType(), |
| | | GreenScoreEnum.SCORE_POOL_CNT.getCode()); |
| | | apiScoreVoucherInfoVo.setScorePoolCnt(new BigDecimal(scorePoolCntDic.getValue()).setScale(2,BigDecimal.ROUND_DOWN)); |
| | | |
| | | BigDecimal voucherCntSurplus = mallScoreRecordMapper.selectSurplusCntByMemberId(member.getId()); |
| | | apiScoreVoucherInfoVo.setVoucherCntSurplus(voucherCntSurplus); |
| | | return apiScoreVoucherInfoVo; |
| | | } |
| | | |
| | | @Override |
| | | public List<ApiScoreVoucherRecordVo> findMallScoreVoucherList(ApiScoreVoucherRecordDto apiScoreVoucherRecordDto) { |
| | | IPage<ApiScoreVoucherRecordVo> page = new Page<>(apiScoreVoucherRecordDto.getPageNum(), apiScoreVoucherRecordDto.getPageSize()); |
| | | IPage<ApiScoreVoucherRecordVo> apiScoreVoucherRecordVoIPage = mallScoreVoucherMapper.findMallScoreVoucherListInPage(page, apiScoreVoucherRecordDto); |
| | | return apiScoreVoucherRecordVoIPage.getRecords(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public FebsResponse voucherBusinessBuy(ApiVoucherBusinessDto apiVoucherBusinessDto) { |
| | | Long memberId = LoginUserUtil.getLoginUser().getId(); |
| | | BigDecimal voucherCnt = apiVoucherBusinessDto.getVoucherCnt(); |
| | | if(voucherCnt.compareTo(BigDecimal.ZERO) <= 0){ |
| | | throw new FebsException("请输入合理的数量"); |
| | | } |
| | | DataDictionaryCustom scorePoolCntDic = dataDictionaryCustomMapper.selectScorePoolCntForUpdate( |
| | | GreenScoreEnum.SCORE_POOL_CNT.getType(), |
| | | GreenScoreEnum.SCORE_POOL_CNT.getCode()); |
| | | BigDecimal scorePoolCnt = new BigDecimal(scorePoolCntDic.getValue() == null ? "0" : scorePoolCntDic.getValue()); |
| | | if(scorePoolCnt.compareTo(voucherCnt) < 0){ |
| | | throw new FebsException("可购买总量不足"); |
| | | } |
| | | String tradePassword = apiVoucherBusinessDto.getTradePassword(); |
| | | if (StrUtil.isBlank(tradePassword)) { |
| | | throw new FebsException("支付密码错误"); |
| | | } |
| | | |
| | | MallMember mallMember = mallMemberMapper.selectById(memberId); |
| | | if (StrUtil.isBlank(mallMember.getTradePassword())) { |
| | | throw new FebsException("未设置支付密码"); |
| | | } |
| | | |
| | | if (!SecureUtil.md5(tradePassword).equals(mallMember.getTradePassword())) { |
| | | throw new FebsException("支付密码错误"); |
| | | } |
| | | |
| | | DataDictionaryCustom scorePriceDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | GreenScoreEnum.SCORE_PRICE.getType(), |
| | | GreenScoreEnum.SCORE_PRICE.getCode()); |
| | | BigDecimal scorePrice = new BigDecimal(scorePriceDic.getValue()).setScale(2, BigDecimal.ROUND_DOWN); |
| | | BigDecimal voucherAmount = voucherCnt.multiply(scorePrice); |
| | | |
| | | MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId); |
| | | BigDecimal balance = mallMemberWallet.getBalance(); |
| | | if(voucherAmount.compareTo(balance) > 0){ |
| | | throw new FebsException("余额不足"); |
| | | } |
| | | //减少余额 |
| | | memberWalletService.reduceBalance(voucherAmount,memberId); |
| | | //减少绿色积分池 |
| | | scorePoolCnt = scorePoolCnt.subtract(voucherCnt); |
| | | scorePoolCntDic.setValue(scorePoolCnt.toString()); |
| | | dataDictionaryCustomMapper.updateById(scorePoolCntDic); |
| | | //增加凭证数量 |
| | | // mallMemberWalletMapper.addVorCherCntByMemberId(voucherCnt,memberId); |
| | | //增加凭证燃烧值数量 |
| | | mallMemberWalletMapper.addVorCherFireCntByMemberId(voucherCnt,memberId); |
| | | //增加购买记录 |
| | | String voucherNo = MallUtils.getOrderNum("VB"); |
| | | MallScoreVoucher mallScoreVoucher = new MallScoreVoucher(); |
| | | mallScoreVoucher.setVoucherNo(voucherNo); |
| | | mallScoreVoucher.setMemberId(memberId); |
| | | mallScoreVoucher.setVoucherCnt(voucherCnt); |
| | | mallScoreVoucher.setPrice(scorePrice); |
| | | mallScoreVoucher.setVoucherAmount(voucherAmount); |
| | | mallScoreVoucher.setType("B"); |
| | | mallScoreVoucherMapper.insert(mallScoreVoucher); |
| | | |
| | | //产生一条流水记录 |
| | | mallMoneyFlowService.addMoneyFlow( |
| | | mallMemberWallet.getMemberId(), |
| | | voucherCnt, |
| | | MoneyFlowTypeEnum.VOUCHER_BUY.getValue(), |
| | | voucherNo, |
| | | FlowTypeEnum.VOUCHER_CNT.getValue()); |
| | | return new FebsResponse().success().message("操作成功"); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public FebsResponse voucherBusinessSale(ApiVoucherBusinessDto apiVoucherBusinessDto) { |
| | | Long memberId = LoginUserUtil.getLoginUser().getId(); |
| | | BigDecimal voucherCnt = apiVoucherBusinessDto.getVoucherCnt(); |
| | | if(voucherCnt.compareTo(BigDecimal.ZERO) <= 0){ |
| | | throw new FebsException("请输入合理的数量"); |
| | | } |
| | | String tradePassword = apiVoucherBusinessDto.getTradePassword(); |
| | | if (StrUtil.isBlank(tradePassword)) { |
| | | throw new FebsException("支付密码错误"); |
| | | } |
| | | |
| | | MallMember mallMember = mallMemberMapper.selectById(memberId); |
| | | if (StrUtil.isBlank(mallMember.getTradePassword())) { |
| | | throw new FebsException("未设置支付密码"); |
| | | } |
| | | |
| | | if (!SecureUtil.md5(tradePassword).equals(mallMember.getTradePassword())) { |
| | | throw new FebsException("支付密码错误"); |
| | | } |
| | | |
| | | MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId); |
| | | BigDecimal voucherCntMember = mallMemberWallet.getVoucherCnt(); |
| | | if(voucherCntMember.compareTo(voucherCnt) < 0){ |
| | | throw new FebsException("数量不足"); |
| | | } |
| | | |
| | | DataDictionaryCustom scorePriceDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | GreenScoreEnum.SCORE_PRICE.getType(), |
| | | GreenScoreEnum.SCORE_PRICE.getCode()); |
| | | BigDecimal scorePrice = new BigDecimal(scorePriceDic.getValue()).setScale(2, BigDecimal.ROUND_DOWN); |
| | | BigDecimal voucherAmount = voucherCnt.multiply(scorePrice); |
| | | //减少凭证积分 |
| | | //增加凭证现金 |
| | | mallMemberWalletMapper.addVorCherAmountAndCntById(voucherAmount,voucherCnt,mallMemberWallet.getId()); |
| | | //增加积分池 |
| | | DataDictionaryCustom scorePoolCntDic = dataDictionaryCustomMapper.selectScorePoolCntForUpdate( |
| | | GreenScoreEnum.SCORE_POOL_CNT.getType(), |
| | | GreenScoreEnum.SCORE_POOL_CNT.getCode()); |
| | | BigDecimal scorePoolCnt = new BigDecimal(scorePoolCntDic.getValue() == null ? "0" : scorePoolCntDic.getValue()); |
| | | scorePoolCnt = scorePoolCnt.add(voucherCnt).setScale(2,BigDecimal.ROUND_DOWN); |
| | | scorePoolCntDic.setValue(scorePoolCnt.toString()); |
| | | dataDictionaryCustomMapper.updateById(scorePoolCntDic); |
| | | |
| | | //增加购买记录 |
| | | String voucherNo = MallUtils.getOrderNum("VS"); |
| | | MallScoreVoucher mallScoreVoucher = new MallScoreVoucher(); |
| | | mallScoreVoucher.setVoucherNo(voucherNo); |
| | | mallScoreVoucher.setMemberId(memberId); |
| | | mallScoreVoucher.setVoucherCnt(voucherCnt); |
| | | mallScoreVoucher.setPrice(scorePrice); |
| | | mallScoreVoucher.setVoucherAmount(voucherAmount); |
| | | mallScoreVoucher.setType("S"); |
| | | mallScoreVoucherMapper.insert(mallScoreVoucher); |
| | | |
| | | //产生一条流水记录 |
| | | mallMoneyFlowService.addMoneyFlow( |
| | | mallMemberWallet.getMemberId(), |
| | | voucherAmount, |
| | | MoneyFlowTypeEnum.VOUCHER_SALE.getValue(), |
| | | voucherNo, |
| | | FlowTypeEnum.BALANCE.getValue()); |
| | | return new FebsResponse().success().message("操作成功"); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void withdrawal(WithdrawalDto withdrawalDto) { |
| | | |
| | | Long memberId = LoginUserUtil.getLoginUser().getId(); |
| | | MallMember mallMember = mallMemberMapper.selectById(memberId); |
| | | if (StrUtil.isBlank(mallMember.getTradePassword())) { |
| | | throw new FebsException("未设置支付密码"); |
| | | } |
| | | |
| | | if (!mallMember.getTradePassword().equals(SecureUtil.md5(withdrawalDto.getTradePwd()))) { |
| | | throw new FebsException("支付密码错误"); |
| | | } |
| | | |
| | | CashOutSettingVo cashOutSettingVo = new CashOutSettingVo(); |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.CASHOUT_SETTING.getType(), DataDictionaryEnum.CASHOUT_SETTING.getCode()); |
| | | if (dic != null) { |
| | | cashOutSettingVo = JSONObject.parseObject(dic.getValue(), CashOutSettingVo.class); |
| | | } |
| | | |
| | | if (withdrawalDto.getAmount().compareTo(cashOutSettingVo.getMinCashOut()) < 0) { |
| | | throw new FebsException("最小提现金额为"+cashOutSettingVo.getMinCashOut().setScale(2,BigDecimal.ROUND_DOWN)); |
| | | } |
| | | |
| | | MallMemberBank mallMemberBank = mallMemberBankMapper.selectById(withdrawalDto.getBankId()); |
| | | if(ObjectUtil.isEmpty(mallMemberBank)){ |
| | | throw new FebsException("未找到银行卡信息"); |
| | | } |
| | | |
| | | MallMemberWallet wallet = mallMemberWalletMapper.selectWalletByMemberId(memberId); |
| | | // 可提现 |
| | | BigDecimal canMoney = wallet.getVoucherAmount(); |
| | | |
| | | if(withdrawalDto.getAmount().compareTo(canMoney) > 0) { |
| | | throw new FebsException("金额不足"); |
| | | } |
| | | |
| | | walletService.reduce(withdrawalDto.getAmount(), memberId, "voucherAmount"); |
| | | String orderNo = MallUtils.getOrderNum("W"); |
| | | |
| | | MallMemberWithdraw withdraw = new MallMemberWithdraw(); |
| | | withdraw.setWithdrawNo(orderNo); |
| | | withdraw.setMemberId(memberId); |
| | | withdraw.setAmount(withdrawalDto.getAmount()); |
| | | withdraw.setStatus(1); |
| | | withdraw.setAmountFee(BigDecimal.ZERO); |
| | | withdraw.setRemark(AppContants.MEMBER_WITHDRAW_VOUCHER_AMOUNT); |
| | | withdraw.setWtihdrawTypeId(mallMemberBank.getId()); |
| | | mallMemberWithdrawMapper.insert(withdraw); |
| | | |
| | | mallMemberService.addMoneyFlow(memberId, withdrawalDto.getAmount().negate(), MoneyFlowTypeEnum.WITHDRAWAL.getValue(), orderNo, null, null, null, 1, FlowTypeEnum.VOUCHER_AMOUNT.getValue()); |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse findMallScoreVoucherListInPage(ApiScoreVoucherRecordDto apiScoreVoucherRecordDto) { |
| | | IPage<ApiScoreVoucherRecordVo> page = new Page<>(apiScoreVoucherRecordDto.getPageNum(), apiScoreVoucherRecordDto.getPageSize()); |
| | | Long id = LoginUserUtil.getLoginUser().getId(); |
| | | apiScoreVoucherRecordDto.setMemberId(id); |
| | | IPage<ApiScoreVoucherRecordVo> pages = mallScoreVoucherMapper.findMallScoreVoucherListInPage(page, apiScoreVoucherRecordDto); |
| | | return new FebsResponse().success().data(pages); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void sign() { |
| | | MallMember member = LoginUserUtil.getLoginUser(); |
| | |
| | | throw new FebsException("今日已签到"); |
| | | } |
| | | /** |
| | | * 获取签到次数 |
| | | * 判断签到次数是否大于规定的时长signScoreDays,超出则获得的积分数为signScoreCnt |
| | | * 用户注册时间开始计算 |
| | | * 判断签到时间是否大于规定的signScoreDays,超出则获得的积分数为signScoreCnt |
| | | */ |
| | | Integer count = mallScoreSignRecordMapper.selectCountByMemberId(member.getId()); |
| | | // Integer count = mallScoreSignRecordMapper.selectCountByMemberId(member.getId()); |
| | | DataDictionaryCustom signScoreDaysDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | DataDictionaryEnum.SIGN_SCORE_DAYS.getType(), |
| | | DataDictionaryEnum.SIGN_SCORE_DAYS.getCode()); |
| | | Integer signScoreDays = Integer.parseInt(signScoreDaysDic.getValue()); |
| | | if(count >= signScoreDays){ |
| | | |
| | | MallMember mallMember = mallMemberMapper.selectById(member.getId()); |
| | | Date createdTime = mallMember.getCreatedTime(); |
| | | |
| | | DateTime dateTime = DateUtil.offsetDay(createdTime, signScoreDays); |
| | | /** |
| | | * 签到时间不在系统设置的第一个阶段,则获取签到设置-签到积分后续获得积分数量 |
| | | */ |
| | | if(DateUtil.compare(dateTime, new Date()) < 0){ |
| | | DataDictionaryCustom signScoreCntDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | DataDictionaryEnum.SIGN_SCORE_CNT.getType(), |
| | | DataDictionaryEnum.SIGN_SCORE_CNT.getCode()); |