123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- 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<AssoPersonVoucherMapper, AssoPersonVoucher> {
- private final VoucherService voucherService;
- private final CacheUtil cacheUtil;
- @Transactional(rollbackFor = Exception.class)
- public void sendVoucherToPerson(SendVoucherToPersonVO sendVoucherToPersonVO) {
- List<String> personUuids = sendVoucherToPersonVO.getPersonUuids();
- List<SendVoucherToPersonVO.InAddVoucherVO> 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<AssoPersonVoucher> 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<AssoPersonVoucher> assoPersonVouchers = this.getBaseMapper().queryPersonVoucher(personVoucherQueryDTO);
- List<VoucherVO> 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<VoucherVO> loadVoucherVOs(List<AssoPersonVoucher> assoPersonVouchers) {
- List<VoucherVO> 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<Integer> 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<Integer> 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<AssoPersonVoucher> assoPersonVoucherLambdaQueryWrapper = new LambdaQueryWrapper<>();
- assoPersonVoucherLambdaQueryWrapper.in(AssoPersonVoucher::getId, assoIds)
- .eq(AssoPersonVoucher::getUseState, useState);
- List<AssoPersonVoucher> assoPersonVouchers = this.list(assoPersonVoucherLambdaQueryWrapper);
- if (assoPersonVouchers.size() < assoIds.size()) {
- throw new BusinessException(ExceptionEnum.BUSINESS_ERROR, "伪造的优惠券");
- }
- //更新状态
- UpdateWrapper<AssoPersonVoucher> updateWrapper = new UpdateWrapper<>();
- updateWrapper.lambda().set(AssoPersonVoucher::getUseState, state)
- .in(AssoPersonVoucher::getId, assoIds);
- this.update(updateWrapper);
- }
- public Double verifyVouchers(List<Integer> assoVoucherIds, PersonnelVO personnelVO) {
- Double price = 0d;
- if(assoVoucherIds==null){
- return price;
- }
- String personId = personnelVO.getUuid();
- LambdaQueryWrapper<AssoPersonVoucher> assoPersonVoucherLambdaQueryWrapper = new LambdaQueryWrapper<>();
- assoPersonVoucherLambdaQueryWrapper.in(AssoPersonVoucher::getId, assoVoucherIds)
- .eq(AssoPersonVoucher::getUseState, 0)
- .eq(AssoPersonVoucher::getPersonUuid, personId);
- List<AssoPersonVoucher> assoPersonVouchers = this.list(assoPersonVoucherLambdaQueryWrapper);
- if (assoPersonVouchers.size() != assoVoucherIds.size()) {
- throw new BusinessException(ExceptionEnum.BUSINESS_ERROR, "伪造的优惠券");
- }
- for (AssoPersonVoucher voucher : assoPersonVouchers) {
- price+=voucher.getAmount();
- }
- return price;
- }
- }
|