package com.miaxis.app.controller.gzpt;


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.exception.CustomException;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.newgzpt.domain.GzptUserInfo;
import com.miaxis.newgzpt.dto.GzptUserInfoDTO;
import com.miaxis.newgzpt.dto.GzptVideoVipDTO;
import com.miaxis.newgzpt.service.IGzptSchActivationService;
import com.miaxis.newgzpt.service.IGzptSchPayConfigService;
import com.miaxis.newgzpt.service.IGzptSchPayLogService;
import com.miaxis.newgzpt.service.IGzptUserInfoService;
import com.miaxis.newgzpt.vo.GzptExamInfoVO;
import com.miaxis.newgzpt.vo.GzptSchActivationVO;
import com.miaxis.userInfo.service.IUserVipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学员基本信息表 前端控制器
 * </p>
 *
 * @since 2021-03-09
 */
@RestController
@Api(tags = "【app-学员基本信息】")
@RequestMapping(Constants.OPEN_PREFIX + "/gzpt/userInfo")
public class GzptUserInfoController extends BaseController {

    @Autowired
    private IGzptUserInfoService userInfoService;

    @Autowired
    private IGzptSchPayConfigService schPayConfigService;

    @Autowired
    private IGzptSchPayLogService schPayLogService;

    @Autowired
    private IGzptSchActivationService schActivationService;

    @Autowired
    private IUserVipService userVipService;

    /**
     * 获取用户id获取详细信息
     */
    @GetMapping
    @ApiOperation("获取学员详细信息")
    public Response<GzptUserInfo> getInfo() {
        UserInfo userInfo = SecurityUtils.getLoginUser().getStudent();
        return Response.success(userInfoService.getInfoById(userInfo.getGzptUserId()));
    }


    /**
     * 获取用户id获取详细信息
     */
    @GetMapping("/activa")
    @ApiOperation("获取学员合作与激活状态")
    public Response<GzptSchActivationVO> getSchActivation(GzptVideoVipDTO gzptVideoVipDTO) throws Exception {
        int isShare = 0;
        int isActiva = 0;
        GzptUserInfo gzptUserInfo = userInfoService.getInfoById(gzptVideoVipDTO.getUserId());

        if(gzptUserInfo == null){
            throw new CustomException("查不到用户数据");
        }
        //获取合作驾校配置表
        isShare = schPayConfigService.isSchPayConfigByUser(gzptUserInfo);
        //如果是合作驾校，查询激活状态
        if(isShare==1) {
            isActiva = schActivationService.isSchActivation(gzptUserInfo.getId());
        }
        GzptSchActivationVO vo = new GzptSchActivationVO();
        vo.setIsActive(isActiva);  //是否激活
        vo.setIsShare(isShare);     //是否合作
        vo.setUserId(gzptVideoVipDTO.getUserId());
        return Response.success(vo);

    }


    /**
     * 根据学员stuOutId获取学员考试信息
     */
    @GetMapping("/getUserScoreByOutId")
    @ApiOperation("根据学员stuOutId获取学员考试信息")
    public Response<List<GzptExamInfoVO>> getUserScoreByOutId(Long stuOutId)   {
        List<GzptExamInfoVO> scoreList = userInfoService.getUserScoreByOutId(stuOutId);
        return Response.success(scoreList);

    }




    /**
     * （测试专用）清除学员激活状态与会员信息接口
     */
    @GetMapping("/clearUserVip")
    @ApiOperation("（测试专用）清除学员激活状态与会员信息接口")
    public Response clearUserVip(GzptVideoVipDTO gzptVideoVipDTO) throws Exception {
        GzptUserInfo gzptUserInfo = userInfoService.getInfoById(gzptVideoVipDTO.getUserId());
        //清除会员信息
        int i = userVipService.removeByUserId(gzptVideoVipDTO.getUserId());
        //清除激活信息
        schActivationService.delSchActivationByUserId(gzptUserInfo.getId());
        return Response.success();

    }

    /**
     * 绑定学员账号
     */
    @PutMapping(value = "/bind")
    @ApiOperation("绑定学员账号")
    public Response getUserWxlogin(@RequestBody GzptUserInfoDTO gzptUserInfoDTO) {
        GzptUserInfo gzptUserInfo = userInfoService.getUserWxlogin(gzptUserInfoDTO);
        if (gzptUserInfo == null) {
            throw new CustomException("该学员账户不存在或者密码错误");
        }
        //绑定用户与学员关系
        UserInfo student = SecurityUtils.getLoginUser().getStudent();
        student.setGzptUserId(gzptUserInfo.getId());
        return Response.success();
    }




    /**
     * 获取用户vip信息

    @GetMapping
    @ApiOperation("获取用户vip信息")
    public Response<GzptVideoVipVO> getVideoVip(GzptVideoVipDTO gzptVideoVipDTO) throws Exception {
        GzptVideoVip gzptVideoVip  = gzptVideoVipService.getGzptVideoVipByUserId(gzptVideoVipDTO);
        GzptVideoVipVO vo = new GzptVideoVipVO();
        if(gzptVideoVip!=null) {
            BeanUtils.copyProperties(gzptVideoVip, vo);
            String sign = getSign(vo.getUserId(), vo.getSubject2(), vo.getSubject3());
            vo.setSign(sign);
            return Response.success(vo);
        } else {
            return Response.success(vo);
        }
    }
     */


}

