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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qs.mp.admin.domain.CdKey;
import com.qs.mp.admin.domain.CdKeyExchange;
import com.qs.mp.admin.domain.CdKeyGroup;
import com.qs.mp.admin.domain.vo.CdKeyExchangeVO;
import com.qs.mp.admin.domain.vo.CdKeyGroupListVO;
import com.qs.mp.admin.domain.vo.CdKeyVO;
import com.qs.mp.admin.mapper.CdKeyMapper;
import com.qs.mp.admin.service.ICdKeyExchangeService;
import com.qs.mp.admin.service.ICdKeyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qs.mp.common.enums.CdKeyGroupGoodsTypeEnum;
import com.qs.mp.common.enums.PrizeStorageInTypeEnum;
import com.qs.mp.core.domain.LoginUser;
import com.qs.mp.user.service.IUserPrizeStorageService;
import com.qs.mp.utils.SecurityUtils;
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.util.Date;
import java.util.List;

/**
 * @auther quanshu
 * @create 2023-05-16 17:20:42
 * @describe 兑换码服务实现类
 */
@Service
public class CdKeyServiceImpl extends ServiceImpl<CdKeyMapper, CdKey> implements ICdKeyService {

    @Autowired
    private IUserPrizeStorageService userPrizeStorageService;

    @Autowired
    private ICdKeyExchangeService cdKeyExchangeService;

    @Override
    public List<CdKeyGroupListVO> listCdKeyVO(QueryWrapper<CdKeyGroup> queryWrapper) {
        return getBaseMapper().listCdKeyVO(queryWrapper);
    }

    @Override
    public List<CdKeyExchangeVO> listCdKeyExchangeVO(QueryWrapper<CdKey> queryWrapper) {
        return getBaseMapper().listCdKeyExchangeVO(queryWrapper);
    }


    @Override
    public List<CdKeyVO> listExportVO(QueryWrapper<CdKey> queryWrapper) {
        return getBaseMapper().listExportVO(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exchangeCdKey(Long keyId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 更新兑换码状态
        boolean rst = update(new UpdateWrapper<CdKey>().set("status", "1").eq("key_id", keyId));
        Assert.isTrue(rst, "更新兑换码状态失败");

        // 插入兑换记录
        CdKey cdKey = getById(keyId);
        CdKeyExchange exchange = new CdKeyExchange();
        exchange.setGroupId(cdKey.getGroupId());
        exchange.setKeyId(String.valueOf(cdKey.getKeyId()));
        exchange.setUserId(loginUser.getUserId());
        exchange.setExchangeTime(new Date());
        cdKeyExchangeService.save(exchange);

        // 分发对应商品
        if (CdKeyGroupGoodsTypeEnum.GOODS == cdKey.getType()) {
            userPrizeStorageService.takeInStorage(loginUser.getUserId(), cdKey.getGoodsName(), cdKey.getPicUrl(), cdKey.getRefId(), PrizeStorageInTypeEnum.CD_KEY_EXCHANGE, String.valueOf(exchange.getId()));
        }else if (CdKeyGroupGoodsTypeEnum.COUPON == cdKey.getType()) {

        }


    }

}
