package com.yida.data.school.classSchedule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.classschedule.EduClassSchedule;
import com.yida.data.common.core.entity.classschedule.EduClassScheduleTimeClass;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.core.utils.FileUtil;
import com.yida.data.school.classSchedule.service.EduClassScheduleService;
import com.yida.data.school.classSchedule.service.EduClassScheduleTimeClassService;
import com.yida.data.school.dto.classSchedule.ClassSchedulePageDTO;
import com.yida.data.school.dto.classSchedule.DownloadTemplateDTO;
import com.yida.data.school.dto.classSchedule.UpdateClassTimeDTO;
import com.yida.data.school.vo.classSchedule.ClassScheduleInfoVO;
import com.yida.data.school.vo.classSchedule.ClassSchedulePageInfoVO;
import com.yida.data.school.vo.classSchedule.ClassScheduleTimeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 课程表管理 Controller
 *
 * @author wjm
 * @date 2021-08-13 16:53:48
 */
@Api(tags = "课程表")
@Slf4j
@Validated
@RestController
@RequestMapping("/classSchedule/classSchedule")
@RequiredArgsConstructor
public class EduClassScheduleController {

    private final EduClassScheduleService eduClassScheduleService;

    private final EduClassScheduleTimeClassService eduClassScheduleTimeClassService;


    @ApiOperation("课程表分页查询")
    @GetMapping("/listClassSchedulePage")
    public ResultBean<IPage<ClassSchedulePageInfoVO>> listClassSchedulePage(ClassSchedulePageDTO dto) {
        if (dto.getSchoolId() == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (currentUser.getDeptType() == 0) {
                dto.setSchoolId(currentUser.getDeptId());
            }
        }
        return ResultBean.buildSuccess(eduClassScheduleService.listClassSchedulePage(dto));
    }

    @ApiOperation("保存课程表")
    @PostMapping("/saveClassSchedule")
    public ResultBean saveClassSchedule(@RequestBody EduClassSchedule classSchedule) {
        eduClassScheduleService.saveClassSchedule(classSchedule);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("停用或启用课程表")
    @GetMapping("/updateClassScheduleStatus")
    public ResultBean updateClassScheduleStatus(@ApiParam("课程表 id") Long classScheduleId) {
        eduClassScheduleService.updateClassScheduleStatus(classScheduleId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("获取课程表时间安排")
    @GetMapping("/listClassScheduleTime")
    public ResultBean<ClassScheduleTimeVO> listClassScheduleTime(@ApiParam("课程表id") Long classScheduleId) {
        return ResultBean.buildSuccess(eduClassScheduleService.listClassScheduleTime(classScheduleId));
    }

    @ApiOperation("更新课程表时间")
    @PostMapping("/updateClassTime")
    public ResultBean updateClassTime(@RequestBody UpdateClassTimeDTO dto) {
        eduClassScheduleService.saveClassTime(dto.getClassScheduleId(), dto.getTimeList());
        return ResultBean.buildSuccess();
    }

    @ApiOperation("获取课程表详情")
    @GetMapping("/getClassScheduleInfo")
    public ResultBean<List<ClassScheduleInfoVO>> getClassScheduleInfo(@ApiParam("课程表id") Long classScheduleId,
        @ApiParam("班级id") Long classId) {
        return ResultBean
            .buildSuccess(eduClassScheduleTimeClassService.listClassScheduleInfo(classScheduleId, classId));
    }

    @ApiOperation("修改某一堂课程(课程与教师为空则进行清空操作)")
    @PostMapping("/updateOneClassTime")
    public ResultBean updateOneClassTime(@RequestBody EduClassScheduleTimeClass timeClass) {
        eduClassScheduleTimeClassService.updateClassTime(timeClass);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除课程表")
    @PostMapping("/delClassScheduleBatch")
    public ResultBean delClassScheduleBatch(@RequestBody List<Long> ids) {
        eduClassScheduleService.delClassScheduleBatch(ids);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("根据班级id与年级id查询课程表")
    @GetMapping("/getClassScheduleByGradeAndClass")
    public ResultBean<ClassScheduleInfoVO> getClassScheduleByGradeAndClass(@ApiParam("年级id") Long gradeId,
        @ApiParam("班级id") Long classId,
        @ApiParam("学期id") Long semsterId) {
        return ResultBean
            .buildSuccess(eduClassScheduleTimeClassService.getClassScheduleInfo(gradeId, classId, semsterId));
    }

    @ApiOperation("下载课程表导入模板")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(@RequestBody DownloadTemplateDTO dto,
        HttpServletResponse response) {
        eduClassScheduleService.downloadTemplate(dto, response);
    }

    @ApiOperation("批量导入")
    @PostMapping("/importTime")
    public ResultBean importTime(@RequestParam("file") MultipartFile file,
        @RequestParam("scheduleId") Long scheduleId,
        @ApiParam("0-总课表，1-按班级导入课表") @RequestParam(value = "type", required = false) Integer type) {
        eduClassScheduleService.importTime(FileUtil.uploadMultipartFile(file), scheduleId, type);
        return ResultBean.buildSuccess();
    }
}
