package com.miaxis.app.controller.vip;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miaxis.common.annotation.Log;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.domain.entity.UserInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.common.exception.CustomException;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.common.utils.poi.ExcelUtil;
import com.miaxis.user.service.IUserInfoService;
import com.miaxis.vip.domain.VipCode;
import com.miaxis.vip.service.IVipCodeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 【激活码】Controller
 *
 * @author miaxis
 * @date 2021-08-18
 */
@RestController
@RequestMapping(Constants.STUDENT_PREFIX+"/vip/code")
@Api(tags={"【app-激活码】"})
public class AppVipCodeController extends BaseController{
    @Autowired
    private IVipCodeService vipCodeService;

    @Autowired
    private IUserInfoService userInfoService;
    @PutMapping
    @ApiOperation("用户激活码激活")
    public Response<Integer> edit(String vipCode){

        QueryWrapper<VipCode> queryWrapper = new QueryWrapper<VipCode>();
        queryWrapper.eq("vip_code", vipCode);
        VipCode vipCodeEntity = vipCodeService.getOne(queryWrapper);
        if (vipCodeEntity == null){
            throw new CustomException("该激活码不存在");
        }
        if (vipCodeEntity.getStatus() == 1){
            throw new CustomException("该激活码已被激活");
        }
        if (vipCodeEntity.getStatus() == 2){
            throw new CustomException("该激活码已作废");
        }

        UserInfo student = SecurityUtils.getLoginUser().getStudent();

        vipCodeEntity.setStatus(1);
        vipCodeEntity.setActivationUserId(student.getId());
        vipCodeEntity.setActivationTime(new Date());
        vipCodeService.updateById(vipCodeEntity);


        UserInfo userInfo = SecurityUtils.getLoginUser().getStudent();
        userInfo.setIsVip(1);
        userInfoService.updateById(userInfo);
        return Response.success();
    }



}
