package com.example.xiaoshiweixinback.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.xiaoshiweixinback.business.common.base.Records; import com.example.xiaoshiweixinback.business.exception.BusinessException; import com.example.xiaoshiweixinback.business.exception.ExceptionEnum; import com.example.xiaoshiweixinback.business.exception.XiaoShiException; import com.example.xiaoshiweixinback.business.utils.CacheUtil; import com.example.xiaoshiweixinback.business.utils.FormatUtil; import com.example.xiaoshiweixinback.business.utils.LoginUtils; import com.example.xiaoshiweixinback.domain.AssoPersonVoucher; import com.example.xiaoshiweixinback.domain.Voucher; import com.example.xiaoshiweixinback.entity.assoPersonVoucher.PersonVoucherQueryDTO; import com.example.xiaoshiweixinback.entity.assoPersonVoucher.SendVoucherToPersonVO; import com.example.xiaoshiweixinback.entity.vo.PersonnelVO; import com.example.xiaoshiweixinback.entity.vouchar.VoucherQueryDTO; import com.example.xiaoshiweixinback.entity.vouchar.VoucherVO; import com.example.xiaoshiweixinback.mapper.AssoPersonVoucherMapper; import io.swagger.v3.oas.models.security.SecurityScheme; import lombok.RequiredArgsConstructor; import org.quartz.ListenerManager; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author admin * @description 针对表【asso_person_voucher(人员优惠券关联表)】的数据库操作Service实现 * @createDate 2024-06-26 11:35:27 */ @Service @RequiredArgsConstructor public class AssoPersonVoucherService extends ServiceImpl { private final VoucherService voucherService; private final CacheUtil cacheUtil; @Transactional(rollbackFor = Exception.class) public void sendVoucherToPerson(SendVoucherToPersonVO sendVoucherToPersonVO) { List personUuids = sendVoucherToPersonVO.getPersonUuids(); List inAddVoucherVOS = sendVoucherToPersonVO.getInAddVoucherVOS(); Integer getWay =sendVoucherToPersonVO.getGetWay(); //校验 if (sendVoucherToPersonVO == null || personUuids == null || personUuids.size() == 0 || sendVoucherToPersonVO.getInAddVoucherVOS() == null) { throw new BusinessException(ExceptionEnum.BUSINESS_ERROR, "请校验参数"); } List assoPersonVouchers = new ArrayList<>(); for (int i = 0; i < inAddVoucherVOS.size(); i++) { SendVoucherToPersonVO.InAddVoucherVO inAddVoucherVO = inAddVoucherVOS.get(i); Integer voucherId = inAddVoucherVO.getVoucherId(); Integer num = inAddVoucherVO.getNumber(); //根据voucherId查询优惠券 Voucher voucher = voucherService.getById(voucherId); if (voucher == null) { throw new BusinessException(ExceptionEnum.BUSINESS_ERROR, "优惠券不存在"); } for (int t = 0; t < num; t++) { for (String personUuid : personUuids) { AssoPersonVoucher assoPersonVoucher = this.loadAssoPersonVoucher(voucher, personUuid,getWay); assoPersonVouchers.add(assoPersonVoucher); } } } this.getBaseMapper().insertBatchSomeColumn(assoPersonVouchers); } private AssoPersonVoucher loadAssoPersonVoucher(Voucher voucher, String personUuid,Integer getWay) { Date date = new Date(); AssoPersonVoucher assoPersonVoucher = new AssoPersonVoucher(); assoPersonVoucher.setPersonUuid(personUuid); assoPersonVoucher.setVoucherName(voucher.getName()); assoPersonVoucher.setVoucherType(voucher.getType()); assoPersonVoucher.setAmount(voucher.getAmount()); assoPersonVoucher.setThreshold(voucher.getThreshold()); assoPersonVoucher.setAvailableTime(voucher.getAvailableTime()); assoPersonVoucher.setExpirationTime(voucher.getExpirationTime()); assoPersonVoucher.setUseScope(voucher.getUseScope()); assoPersonVoucher.setUseState(0); assoPersonVoucher.setGetWay(getWay); assoPersonVoucher.setCreateTime(date); return assoPersonVoucher; } /** * 查询个人优惠券 */ public Records queryPersonVoucher(PersonVoucherQueryDTO personVoucherQueryDTO) { Long size = personVoucherQueryDTO.getSize(); Long current = personVoucherQueryDTO.getCurrent(); String personUuid = personVoucherQueryDTO.getPersonUuid(); if (personUuid == null) { PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken()); personUuid = personnelVO.getUuid(); personVoucherQueryDTO.setPersonUuid(personUuid); } List assoPersonVouchers = this.getBaseMapper().queryPersonVoucher(personVoucherQueryDTO); List voucherVOS = this.loadVoucherVOs(assoPersonVouchers); Long total = this.getBaseMapper().queryPersonVoucherTotal(personVoucherQueryDTO); Records records = new Records(); records.setSize(size); records.setCurrent(current); records.setData(voucherVOS); records.setTotal(total); return records; } public List loadVoucherVOs(List assoPersonVouchers) { List voucherVOs = new ArrayList<>(); if (assoPersonVouchers == null || assoPersonVouchers.size() == 0) { return voucherVOs; } assoPersonVouchers.forEach(item -> { VoucherVO voucherVO = new VoucherVO(); BeanUtils.copyProperties(item, voucherVO); voucherVO.setType(item.getVoucherType()); voucherVO.setName(item.getVoucherName()); List useScopes = FormatUtil.StringToIntegerList(item.getUseScope(), ","); voucherVO.setUseScopes(useScopes); voucherVOs.add(voucherVO); }); return voucherVOs; } /** * 使用优惠券 * * @param assoIds * @param state 0未使用 1已使用 2冻结 */ @Transactional(rollbackFor = Exception.class) public void useVoucher(List assoIds, Integer state) { if (assoIds == null || assoIds.size() == 0) { return; } //校验 Integer useState = -1; if (state == 1) { useState = 2; } else if (state == 2) { useState = 0; } else { throw new BusinessException(ExceptionEnum.BUSINESS_ERROR, "非法修改优惠券状态"); } LambdaQueryWrapper assoPersonVoucherLambdaQueryWrapper = new LambdaQueryWrapper<>(); assoPersonVoucherLambdaQueryWrapper.in(AssoPersonVoucher::getId, assoIds) .eq(AssoPersonVoucher::getUseState, useState); List assoPersonVouchers = this.list(assoPersonVoucherLambdaQueryWrapper); if (assoPersonVouchers.size() < assoIds.size()) { throw new BusinessException(ExceptionEnum.BUSINESS_ERROR, "伪造的优惠券"); } //更新状态 UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.lambda().set(AssoPersonVoucher::getUseState, state) .in(AssoPersonVoucher::getId, assoIds); this.update(updateWrapper); } public Double verifyVouchers(List assoVoucherIds, PersonnelVO personnelVO) { Double price = 0d; if(assoVoucherIds==null){ return price; } String personId = personnelVO.getUuid(); LambdaQueryWrapper assoPersonVoucherLambdaQueryWrapper = new LambdaQueryWrapper<>(); assoPersonVoucherLambdaQueryWrapper.in(AssoPersonVoucher::getId, assoVoucherIds) .eq(AssoPersonVoucher::getUseState, 0) .eq(AssoPersonVoucher::getPersonUuid, personId); List assoPersonVouchers = this.list(assoPersonVoucherLambdaQueryWrapper); if (assoPersonVouchers.size() != assoVoucherIds.size()) { throw new BusinessException(ExceptionEnum.BUSINESS_ERROR, "伪造的优惠券"); } for (AssoPersonVoucher voucher : assoPersonVouchers) { price+=voucher.getAmount(); } return price; } }