package com.yonge.cooleshow.teacher.controller;

import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.service.*;
import com.yonge.cooleshow.biz.dal.vo.PianoRoomTimeVo;
import com.yonge.cooleshow.biz.dal.vo.TeacherHomeStatisticalVo;
import com.yonge.cooleshow.biz.dal.wrapper.TeacherIndexWrapper;
import com.yonge.cooleshow.biz.dal.wrapper.teacher.TeacherHomeWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Description 老师课后作业相关接口
 *
 * @author liujunchi
 * @date 2022-04-13
 */
@Api(tags = "老师APP首页接口")
@RestController
@RequestMapping("${app-config.url.teacher:}/home")
public class TeacherHomeController extends BaseController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private CourseScheduleService courseScheduleService;
    @Resource
    private MusicSheetService musicSheetService;
    @Resource
    private UserAccountRecordService userAccountRecordService;
    @Resource
    private SysMusicCompareRecordService sysMusicCompareRecordService;
    @Resource
    private HomeService homeService;

    @ApiOperation(value = "首页统计数据")
    @GetMapping(value="/count")
    public HttpResponseResult<TeacherHomeStatisticalVo> countTeacherHome() {
        Long userId = sysUserService.getUserId();

        // 我的课程
        Integer courseSechedule  = courseScheduleService.getWeekNotStart(userId);

        // 课后作业
        Integer courseHomework = courseScheduleService.getHomeworkNotDecorate(userId);

        // 课后评价
        Integer courseScheduleReplied = courseScheduleService.getNotRepliedCourseSchedule(userId);

        // 我的乐谱
        Integer musicSheet = musicSheetService.getTeacherMusicSheetCount(userId);

        // 我收到的评价
        Integer studentReplied = courseScheduleService.getWeekStudentRepliedCourseSchedule(userId);

        // 我的收入
        BigDecimal decimal = userAccountRecordService.getMonthDecimal(userId);


        // 琴房剩余时长
        PianoRoomTimeVo pianoRoomTimeVo = courseScheduleService.selectRemainTime(userId);

        TeacherHomeStatisticalVo teacherHomeStatisticalVo = new TeacherHomeStatisticalVo();
        teacherHomeStatisticalVo.setCourseHomework(courseHomework);
        teacherHomeStatisticalVo.setCourseScheduleReplied(courseScheduleReplied);
        teacherHomeStatisticalVo.setCourseSchedule(courseSechedule);
        teacherHomeStatisticalVo.setDecimal(decimal);
        teacherHomeStatisticalVo.setMusicSheet(musicSheet);
        teacherHomeStatisticalVo.setStudentReplied(studentReplied);
        teacherHomeStatisticalVo.setPianoTime(pianoRoomTimeVo.getRemainTime());
        return succeed(teacherHomeStatisticalVo);
    }

    @ApiOperation("首页练习数据统计")
    @PostMapping(value = "/practice")
    public HttpResponseResult<TeacherIndexWrapper.TeacherPracticeHome> practice(@RequestBody TeacherIndexWrapper.SummarySearch summarySearch) {
        summarySearch.setTeacherId(sysUserService.getUserId());
        return succeed(homeService.getTeacherPracticeHome(summarySearch));
    }

    @ApiOperation("首页练习数据学员列表")
    @PostMapping(value = "/studentPractice")
    public HttpResponseResult<List<TeacherIndexWrapper.StudentPracticeSummaryDto>> studentPractice(@RequestBody TeacherIndexWrapper.StudentSearch studentSearch) {
        studentSearch.setTeacherId(sysUserService.getUserId());
        return succeed(homeService.getTeacherHomeStudent(studentSearch));
    }

    @ApiOperation(value = "课程统计")
    @PostMapping("/totalCourse")
    public HttpResponseResult<List<TeacherHomeWrapper.TeacherTotalCourse>> totalCourse(@RequestBody TeacherHomeWrapper.TeacherTotalCourseQuery query) {
        SysUser sysUser = sysUserService.getUser();
        if (sysUser == null  || sysUser.getId() == null) {

            return failed("用户信息获取失败");
        }
        query.setTeacherId(sysUser.getId());

        return succeed(homeService.teacherTotalCourse(query));
    }


    @ApiOperation(value = "我的乐谱统计")
    @GetMapping("/musicSheetTotal")
    public HttpResponseResult<TeacherHomeWrapper.MusicSheetTotal> musicSheetTotal() {
        SysUser sysUser = sysUserService.getUser();
        if (sysUser == null  || sysUser.getId() == null) {

            return failed("用户信息获取失败");
        }

        return succeed(homeService.musicSheetTotal(sysUser.getId()));
    }

    @ApiOperation(value = "我的乐谱分页")
    @PostMapping("/musicSheetPage")
    public HttpResponseResult<List<TeacherHomeWrapper.MusicSheetTotal>> musicSheetPage(@RequestBody TeacherHomeWrapper.MusicSheetQuery query) {
        SysUser sysUser = sysUserService.getUser();
        if (sysUser == null  || sysUser.getId() == null) {

            return failed("用户信息获取失败");
        }
        query.setTeacherId(sysUser.getId());

        return succeed(homeService.musicSheetPage(query));
    }



    @ApiOperation(value = "课程曝光/购买")
    @PostMapping("/courseExposure")
    public HttpResponseResult<Map<String, List<TeacherIndexWrapper.CourseExposureTotal>>> courseExposure(@RequestBody TeacherIndexWrapper.CourseExposureSearch query) {
        //获取购买的统计数据
        query.setTeacherId(sysUserService.getUserId());
        Map<String, List<TeacherIndexWrapper.CourseExposureTotal>> stringListMap = homeService.courseBuyTotal(query);
        //获取曝光的统计数据
        stringListMap.putAll(homeService.courseExposureTotal(query));
        return succeed(stringListMap);
    }

}
