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.search.MyCourseSearch;
import com.yonge.cooleshow.biz.dal.entity.CourseCalendarEntity;
import com.yonge.cooleshow.biz.dal.service.CourseScheduleService;
import com.yonge.cooleshow.biz.dal.service.HolidaysFestivalsService;
import com.yonge.cooleshow.biz.dal.vo.ArrangeCourseVo;
import com.yonge.cooleshow.biz.dal.vo.CourseAdjustVo;
import com.yonge.cooleshow.biz.dal.vo.CourseScheduleRecordVo;
import com.yonge.cooleshow.biz.dal.vo.CourseStudentVo;
import com.yonge.cooleshow.biz.dal.vo.MyCourseVo;
import com.yonge.cooleshow.biz.dal.vo.PianoRoomTimeVo;
import com.yonge.cooleshow.biz.dal.vo.StudentHomePage;
import com.yonge.cooleshow.biz.dal.vo.TeacherLiveCourseInfoVo;
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.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 老师课程表(CourseSchedule)表控制层
 *
 * @author hgw
 * @since 2022-03-18 15:29:11
 */
@Api(tags = "老师课程表")
@RestController
@RequestMapping("${app-config.url.website:}/courseSchedule")
public class WebCourseScheduleController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private CourseScheduleService courseScheduleService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private HolidaysFestivalsService holidaysFestivalsService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "classDate", dataType = "Integer", value = "年月"),
            @ApiImplicitParam(name = "status", dataType = "String", value = "课程状态 NOT_START未开始 ING进行中 COMPLETE已完成"),
            @ApiImplicitParam(name = "subjectId", dataType = "Long", value = "声部id"),
            @ApiImplicitParam(name = "page", dataType = "Integer", value = "页数"),
            @ApiImplicitParam(name = "rows", dataType = "Integer", value = "每页数量"),
    })
    @ApiOperation("老师端-首页-我的课程-直播课")
    @PostMapping("/queryTeacherLiveCourse")
    public HttpResponseResult<PageInfo<TeacherLiveCourseInfoVo>> queryTeacherLiveCourse(@RequestBody Map<String, Object> param) {
        return succeed(courseScheduleService.queryTeacherLiveCourse(param));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", dataType = "Integer", value = "年"),
            @ApiImplicitParam(name = "month", dataType = "Integer", value = "月"),
            @ApiImplicitParam(name = "singleCourseMinutes", dataType = "Integer", value = "单课时时长(不包含休息时间)"),
            @ApiImplicitParam(name = "freeCourseMinutes", dataType = "Integer", value = "单课时休息时长)"),
            @ApiImplicitParam(name = "teacherId", dataType = "Long", value = "老师id"),
    })
    @ApiOperation("直播课创建时的日历数据")
    @PostMapping("/createLiveCourseCalendar")
    public HttpResponseResult<List<CourseCalendarEntity>> createLiveCourseCalendar(@RequestBody Map<String, Object> param) {
        return succeed(courseScheduleService.createLiveCourseCalendar(param));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId", dataType = "Long", value = "老师id"),
            @ApiImplicitParam(name = "studentId", dataType = "Long", value = "学生id"),
            @ApiImplicitParam(name = "year", dataType = "Integer", value = "年"),
            @ApiImplicitParam(name = "month", dataType = "Integer", value = "月"),
            @ApiImplicitParam(name = "day", dataType = "Integer", value = "日"),
    })
    @ApiOperation("趣纠课日历-用于学生购买指定老师趣纠课")
    @PostMapping("/createPracticeCourseCalendar")
    public HttpResponseResult<List<CourseCalendarEntity>> generatePracticeCourseCalender(@RequestBody Map<String, Object> param) {
        return succeed(courseScheduleService.createPracticeCourseCalender(param));
    }

    @ApiOperation(value = "趣纠课查询",
            notes = "老师端-首页-我的课程-趣纠课\n" +
                    "search:{\"classMonth\":\"2022-03\",\"status\":\"COMPLETE\",\"subjectId\":1}\n" +
                    "老师端-首页-课后评价\n" +
                    "search:{\"classMonth\":\"2022-03\",\"replied\":0,\"studentName\":\"测试王\"}\n" +
                    "老师端-我的-我的主页\n" +
                    "search:{\"classMonth\":\"2022-03\"}\n" +
                    "老师端-课表\n" +
                    "search:{\"classDate\":\"2022-03-27\"}\n" +
                    "     search:{\"classMonth\":\"2022-03\"}\n" +
                    "老师端-课表-趣纠课\n" +
                    "     search:{\"classDate\":\"2022-03-27\"}")
    @PostMapping("/queryTeacherPracticeCourse")
    public HttpResponseResult<PageInfo<MyCourseVo>> queryTeacherPracticeCourse(@RequestBody MyCourseSearch search) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        search.setTeacherId(user.getId());
        IPage<MyCourseVo> pages = courseScheduleService.queryTeacherPracticeCourse(PageUtil.getPage(search), search);
        return succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation("老师端-课表-日历")
    @PostMapping("/queryCourseSchedule")
    public HttpResponseResult<Set<String>> queryCourseSchedule(@RequestBody MyCourseSearch search) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        search.setTeacherId(user.getId());
        return succeed(courseScheduleService.queryCourseSchedule(search));
    }

    @ApiOperation(value = "老师端-课表-日历-用户", notes = "search:{\"classDate\":\"2022-03-27\"}")
    @PostMapping("/queryCourseUser")
    public HttpResponseResult<Map<String, Object>> queryCourseUser(@RequestBody MyCourseSearch search) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        search.setTeacherId(user.getId());
        return succeed(courseScheduleService.queryCourseUser(search));
    }

    @ApiOperation(value = "老师端-课表-日历-调课")
    @PostMapping("/courseAdjust")
    public HttpResponseResult<Object> courseAdjust(@Validated @RequestBody CourseAdjustVo adjustVo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        courseScheduleService.courseAdjust(adjustVo, user.getId());
        return succeed();
    }

    @ApiOperation("老师-首页-最近课程")
    @GetMapping("/queryLiveAndVideo")
    public HttpResponseResult<StudentHomePage> queryLiveAndVideo() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(courseScheduleService.queryLiveAndVideo(null, user.getId(), null));
    }

    @ApiOperation("老师端-查询房间配置")
    @GetMapping("/selectRoomConfig")
    public HttpResponseResult<Object> selectRoomConfig() {
        return succeed(courseScheduleService.selectRoomConfig());
    }

    //测试未约满课老师
    @GetMapping("/test")
    public HttpResponseResult<Object> test(Integer amount) {
//        courseScheduleService.scheduleTask();
//        return succeed(courseScheduleService.getTeacherId(amount));
//        courseScheduleService.scheduleTask();
//        courseScheduleService.teacherSalaryTask();
        courseScheduleService.scheduleTask();
        return succeed();
    }

    @ApiOperation("查询琴房剩余时长、冻结时长、统计学员人数")
    @GetMapping("/selectRemainTime")
    public HttpResponseResult<PianoRoomTimeVo> selectRemainTime() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(courseScheduleService.selectRemainTime(user.getId()));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "subjectId", dataType = "Long", value = "学员声部id"),
            @ApiImplicitParam(name = "userName", dataType = "String", value = "学员姓名"),
            @ApiImplicitParam(name = "courseId", dataType = "Long", value = "课程id"),
    })
    @ApiOperation("我的学员&课内学员")
    @PostMapping("/selectStudent")
    public HttpResponseResult<PageInfo<CourseStudentVo>> selectStudent(@RequestBody Map<String, Object> param) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        param.put("teacherId", user.getId());
        return succeed(courseScheduleService.selectStudent(param));
    }

    @ApiOperation("排课")
    @PostMapping("/arrangeCourse")
    public HttpResponseResult<Object> arrangeCourse(@RequestBody ArrangeCourseVo arrangeCourseVo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        String message = courseScheduleService.arrangeCourse(arrangeCourseVo, user.getId());
        return succeed(message);
    }

    @ApiOperation("根据月份查询消耗时长")
    @GetMapping("/selectConsumeTime")
    public HttpResponseResult<Object> selectConsumeTime(String month) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(courseScheduleService.selectConsumeTime(month, user.getId()));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "subjectId", dataType = "Long", value = "声部id"),
            @ApiImplicitParam(name = "month", dataType = "String", value = "月份,yyyy-mm"),
            @ApiImplicitParam(name = "status", dataType = "String", value = "课程状态"),
    })
    @ApiOperation("琴房课程列表")
    @PostMapping("/selectCourseList")
    public HttpResponseResult<PageInfo<CourseScheduleRecordVo>> selectCourseList(@RequestBody Map<String, Object> param) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        param.put("teacherId", user.getId());
        return succeed(courseScheduleService.selectCourseList(param));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "subjectId", dataType = "Long", value = "声部id"),
            @ApiImplicitParam(name = "month", dataType = "String", value = "月份,yyyy-mm"),
            @ApiImplicitParam(name = "status", dataType = "String", value = "课程状态"),
    })
    @ApiOperation("消耗时长列表")
    @PostMapping("/selectConsumeTimeList")
    public HttpResponseResult<PageInfo<CourseScheduleRecordVo>> selectConsumeTimeList(@RequestBody Map<String, Object> param) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        param.put("teacherId", user.getId());
        return succeed(courseScheduleService.selectConsumeTimeList(param));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId", dataType = "Long", value = "课程id"),
    })
    @ApiOperation("删除课程")
    @PostMapping("/deleteCourse")
    public HttpResponseResult<Object> deleteCourse(@RequestBody Map<String, Object> param) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        param.put("teacherId", user.getId());
        courseScheduleService.deleteCourse(param);
        return succeed();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId", dataType = "Long", value = "课程id"),
            @ApiImplicitParam(name = "startTime", dataType = "String", value = "上课时间"),
            @ApiImplicitParam(name = "endTime", dataType = "String", value = "下课时间"),
    })
    @ApiOperation("调整上课时间")
    @PostMapping("/updateCourseTime")
    public HttpResponseResult<Object> updateCourseTime(@RequestBody Map<String, Object> param) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        param.put("teacherId", user.getId());
        courseScheduleService.updateCourseTime(param);
        return succeed();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentIds", dataType = "List", value = "学生id集合"),
            @ApiImplicitParam(name = "courseId", dataType = "Long", value = "课程id"),
    })
    @ApiOperation("调整上课学员")
    @PostMapping("/updateCourseStudent")
    public HttpResponseResult<Object> updateCourseStudent(@RequestBody Map<String, Object> param) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        param.put("teacherId", user.getId());
        courseScheduleService.updateCourseStudent(param);
        return succeed();
    }

    @ApiOperation("查询节假日")
    @GetMapping("/selectHoliday")
    public HttpResponseResult<Object> selectHoliday(Integer year) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(holidaysFestivalsService.queryByYear(year));
    }
}

