package com.ym.mec.web.controller.education;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.page.CourseScheduleQueryInfo;
import com.ym.mec.biz.service.CourseScheduleService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
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.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2020/1/8
 */

@RestController
@RequestMapping("educationCourseSchedule")
@Api(tags = "教务端课程计划服务")
public class EducationCourseScheduleController extends BaseController {

    @Autowired
    private CourseScheduleService scheduleService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;

    @ApiOperation(value = "根据月份获取乐团在该月有课的日期")
    @GetMapping("/getCourseScheduleDateByMonth")
    public Object getCourseScheduleDateByMonth(@ApiParam(value = "月份", required = true) @RequestParam Date month,String type) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (null == user) {
            throw new BizException("请登录");
        }
        Employee employee = employeeDao.get(user.getId());
        if(Objects.isNull(employee)){
            throw new BizException("员工信息不存在");
        }
        return succeed(scheduleService.getCourseScheduleDates(month,employee.getOrganIdList(),type));
    }

    @ApiOperation(value = "根据日期获取当日排课")
    @GetMapping("/getCourseSchedulesWithDate")
    public Object getCourseSchedulesWithDate(CourseScheduleQueryInfo queryInfo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (null == user) {
            throw new BizException("请登录");
        }
        Employee employee = employeeDao.get(user.getId());
        if(Objects.nonNull(employee)){
            queryInfo.setOrganIdList(employee.getOrganIdList());
        }
        return succeed(scheduleService.getCourseSchedulesWithDateByEdu(queryInfo));
    }

    @ApiOperation(value = "课时交换")
    @PostMapping(value = "/courseSwap",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object courseSwap(Long courseScheduleId1,Long courseScheduleId2){
        if(Objects.isNull(courseScheduleId1)||Objects.isNull(courseScheduleId2)){
            return failed("请指定课程!");
        }
        scheduleService.courseSwap(courseScheduleId1,courseScheduleId2);
        return succeed();
    }
}
