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.service.IGzptUserInfoService;
import com.miaxis.user.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

    @Autowired
    private IGzptUserInfoService gzptUserInfoService;

    @Autowired
    private IUserInfoService userInfoService;


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


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

