package com.qs.mp.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qs.mp.admin.domain.Goods;
import com.qs.mp.admin.domain.PrizeRecovery;
import com.qs.mp.admin.domain.vo.PrizeRecoveryListVO;
import com.qs.mp.admin.mapper.PrizeRecoveryMapper;
import com.qs.mp.admin.service.IGoodsService;
import com.qs.mp.admin.service.IPrizeRecoveryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qs.mp.common.enums.CoinLogTypeEnum;
import com.qs.mp.common.enums.PrizeStorageInTypeEnum;
import com.qs.mp.common.enums.PrizeStorageStatusEnum;
import com.qs.mp.common.enums.TicketPrizeTypeEnum;
import com.qs.mp.common.exception.ServiceException;
import com.qs.mp.user.domain.UserPrizeStorage;
import com.qs.mp.user.domain.param.UserPrizeRecoveryCreateParam;
import com.qs.mp.user.domain.vo.PrizeRecoverySettleVO;
import com.qs.mp.user.service.IUserCoinService;
import com.qs.mp.user.service.IUserPrizeStorageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 奖品回收表 服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-05-23
 */
@Service
public class PrizeRecoveryServiceImpl extends ServiceImpl<PrizeRecoveryMapper, PrizeRecovery> implements IPrizeRecoveryService {

    @Autowired
    private IUserPrizeStorageService userPrizeStorageService;

    @Autowired
    private IUserCoinService userCoinService;

    @Override
    public int countByWrapper(QueryWrapper<PrizeRecovery> queryWrapper) {
        return this.baseMapper.countByWrapper(queryWrapper);
    }

    @Override
    public List<PrizeRecoveryListVO> listByWrapper(QueryWrapper<PrizeRecovery> queryWrapper) {
        return this.baseMapper.listByWrapper(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitRecovery(Long userId, PrizeRecoverySettleVO settleVO) {
        PrizeRecovery prizeRecovery = new PrizeRecovery();
        BeanUtils.copyProperties(settleVO, prizeRecovery);
        prizeRecovery.setUserId(userId);

        UserPrizeStorage userPrizeStorage = userPrizeStorageService.getById(settleVO.getStorageId());

        // 更新用户奖品状态
        boolean rtn = userPrizeStorageService.update(new LambdaUpdateWrapper<UserPrizeStorage>()
                .set(userPrizeStorage.getGoodsNum() - settleVO.getNum() == 0, UserPrizeStorage::getStatus, PrizeStorageStatusEnum.RECOVERY)
                .set(UserPrizeStorage::getGoodsNum, userPrizeStorage.getGoodsNum() - settleVO.getNum())
                .eq(UserPrizeStorage::getUserId, userId)
                .eq(UserPrizeStorage::getStorageId, settleVO.getStorageId())
                .eq(UserPrizeStorage::getGoodsNum, userPrizeStorage.getGoodsNum())
                .eq(UserPrizeStorage::getStatus, PrizeStorageStatusEnum.NOT_DISTRIBUTED));

        Assert.isTrue(rtn, "更新奖品状态失败。storageId:" + settleVO.getStorageId());

        // 保存回收记录
        this.save(prizeRecovery);

        // 发放盲豆
        userCoinService.produce(userId, settleVO.getReturnCoin(), prizeRecovery.getId().toString(), CoinLogTypeEnum.RECOVERY);


    }
}
