package com.yonge.cooleshow.website.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.dto.TeacherDto;
import com.yonge.cooleshow.biz.dal.entity.Subject;
import com.yonge.cooleshow.biz.dal.service.StudentStarService;
import com.yonge.cooleshow.biz.dal.service.SubjectService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.service.TeacherService;
import com.yonge.cooleshow.biz.dal.vo.MyFens;
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.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.page.QueryInfo;
import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.mybatis.support.PageUtil;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("${app-config.url.website:}/teacher")
@Api(value = "教师表", tags = "教师表")
public class TeacherController extends BaseController {
    @Resource
    private TeacherService teacherService;
    @Resource
    private StudentStarService studentStarService;
    @Resource
    private SubjectService subjectService;
    @Resource
    private SysUserService sysUserService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "我的-查询教师基本信息")
    @GetMapping("/queryUserInfo")
    public HttpResponseResult<TeacherHomeVo> queryUserInfo() {
        return teacherService.queryUserInfo(sysUserService.getUserId());
    }

    @ApiOperation(value = "设置系统默认客服")
    @GetMapping("/updateUserCustomerService")
    public HttpResponseResult<String> updateUserCustomerService() {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        return succeed(teacherService.updateUserCustomerService(user.getId()));
    }

    @ApiOperation(value = "开通直播")
    @GetMapping("/openLive")
    public HttpResponseResult<Boolean> openLive() {
        return teacherService.openLive(sysUserService.getUserId());
    }

    @ApiOperation(value = "获取老师可授课声部列表")
    @PostMapping("/querySubject")
    public HttpResponseResult<List<Subject>> querySubject() {
        List<Subject> subjects = teacherService.querySubject(sysUserService.getUserId());
        subjectService.convertSubject(subjects);
        return succeed(subjects);
    }

    @ApiOperation(value = "设置声部")
    @GetMapping("/setSubject")
    public HttpResponseResult setSubject(@ApiParam(value = "声部主键集合", required = true) @RequestParam("subjectIds") String subjectIds) {
        if (StringUtil.isEmpty(subjectIds)) {
            return failed("参数不能为空");
        }
        return status(teacherService.setSubject(sysUserService.getUserId(), subjectIds) > 0);
    }

    @ApiOperation(value = "获取声部搜索下拉框")
    @PostMapping("/querySubjectItem")
    public HttpResponseResult<List<Subject>> querySubjectItem(
            @ApiParam(value = "类型 PRACTICE 趣纠课 LIVE 直播课 VIDEO 视频课 PIANO_ROOM 琴房 MUSIC 曲目 ") String type) {
        List<Subject> subjects = teacherService.querySubjectItem(sysUserService.getUserId(), type);
        subjectService.convertSubject(subjects);
        return succeed(subjects);
    }

    @ApiOperation(value = "我的-个人风采")
    @GetMapping("/queryTeacherStyle")
    public HttpResponseResult<TeacherVo> queryTeacherStyle() {
        return HttpResponseResult.succeed(teacherService.detail(sysUserService.getUserId()));
    }

    @ApiOperation(value = "我的-个人风采保存")
    @PostMapping("/saveTeacherStyle")
    public HttpResponseResult<TeacherVo> saveTeacherStyle(@RequestBody TeacherDto teacherDto) {
        if (StringUtil.isEmpty(teacherDto.getSubjectId())) {
            return failed("参数异常");
        }
        teacherDto.setUserId(sysUserService.getUserId());
        return teacherService.saveTeacherStyle(teacherDto);
    }

    @ApiOperation(value = "我的粉丝")
    @PostMapping(value = "/queryMyFans")
    public HttpResponseResult<PageInfo<MyFens>> queryMyFans(@RequestBody QueryInfo query) {
        IPage<MyFens> pages = teacherService.queryMyFans(PageUtil.getPage(query),sysUserService.getUserId());
        return succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation(value = "关注/取消关注")
    @GetMapping("/starOrUnStar")
    public HttpResponseResult<Boolean> starOrUnStar(@ApiParam(value = "老师ID", required = true) @RequestParam("userId") Long userId,
                                                    @ApiParam(value = "状态 0 取消关注 1 关注", required = true) @RequestParam("starStatus") Integer starStatus) {
        if (null == userId) {
            return failed("缺少老师ID");
        }
        YesOrNoEnum starStatusEnum = YesOrNoEnum.valueOf(starStatus);
        if (null == starStatusEnum) {
            return failed("缺少关注状态");
        }
        return studentStarService.starOrUnStar(sysUserService.getUserId(), userId, starStatusEnum);
    }

}