package com.yonge.cooleshow.teacher.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.TeacherDto;
import com.yonge.cooleshow.biz.dal.entity.Subject;
import com.yonge.cooleshow.biz.dal.entity.Teacher;
import com.yonge.cooleshow.biz.dal.service.TeacherService;
import com.yonge.cooleshow.biz.dal.vo.TeacherHomeVo;
import com.yonge.cooleshow.biz.dal.vo.TeacherVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher")
@Api(value = "教师表", tags = "教师表")
public class TeacherController extends BaseController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "我的-查询教师基本信息")
    @GetMapping("/queryUserInfo")
    public HttpResponseResult<TeacherHomeVo> queryUserInfo() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return teacherService.queryUserInfo(user.getId());
    }

    @ApiOperation(value = "开通直播")
    @GetMapping("/openLive")
    public HttpResponseResult<Boolean> openLive() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return teacherService.openLive(user.getId());
    }

    @ApiOperation(value = "获取老师可授课声部列表")
    @PostMapping("/querySubject")
    public HttpResponseResult<List<Subject>> querySubject() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(teacherService.querySubject(user.getId()));
    }

    @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(teacherService.setSubject(user.getId(), subjectIds) > 0);
    }

    @ApiOperation(value = "我的-个人风采")
    @GetMapping("/queryTeacherStyle")
    public HttpResponseResult<TeacherVo> queryTeacherStyle() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return HttpResponseResult.succeed(teacherService.detail(user.getId()));
    }

    @ApiOperation(value = "我的-个人风采保存")
    @PostMapping("/saveTeacherStyle")
    public HttpResponseResult<TeacherVo> saveTeacherStyle(@RequestBody TeacherDto teacherDto) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if (StringUtil.isEmpty(teacherDto.getSubjectId())) {
            return failed("参数异常");
        }
        teacherDto.setUserId(user.getId());
        return teacherService.saveTeacherStyle(teacherDto);
    }
}