package com.yonge.cooleshow.student.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.Subject;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.MyFollow;
import com.yonge.cooleshow.biz.dal.vo.StudentHomeVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.page.QueryInfo;
import com.yonge.toolset.base.util.StringUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import com.yonge.cooleshow.biz.dal.entity.Student;
import com.yonge.cooleshow.biz.dal.service.StudentService;

import java.util.List;

@RestController
@RequestMapping("/student")
@Api(value = "学生表", tags = "学生表")
public class StudentController extends BaseController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "查询学员")
    @GetMapping("/queryUser")
    public HttpResponseResult<Student> queryUser() throws Exception {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        Student student = studentService.getById(user.getId());
        return succeed(student);
    }

    @ApiOperation(value = "查询学员基本信息")
    @GetMapping("/queryUserInfo")
    public HttpResponseResult<StudentHomeVo> queryUserInfo() throws Exception {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(studentService.queryUserInfo(user));
    }

    @ApiOperation(value = "设置声部")
    @GetMapping("/setSubject")
    public HttpResponseResult setSubject(@ApiParam(value = "声部主键集合", required = true) @RequestParam("subjectIds") String subjectIds) {
        if (StringUtil.isEmpty(subjectIds)) {
            return failed("参数不能为空");
        }
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return status(studentService.setSubject(user.getId(), subjectIds) > 0);
    }

    @ApiOperation(value = "获取声部搜索下拉框")
    @PostMapping("/querySubjectItem")
    public HttpResponseResult<List<Subject>> querySubjectItem() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(studentService.querySubjectItem(user.getId()));
    }

    @ApiOperation(value = "我的关注")
    @PostMapping(value = "/queryMyFollow")
    public HttpResponseResult<PageInfo<MyFollow>> queryMyFollow(@RequestBody QueryInfo query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        IPage<MyFollow> pages = studentService.queryMyFollow(PageUtil.getPage(query), sysUser.getId());
        return succeed(PageUtil.pageInfo(pages));
    }

}