package com.ym.mec.web.controller;

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.entity.Teacher;
import com.ym.mec.biz.dal.page.*;
import com.ym.mec.biz.service.ClassGroupService;
import com.ym.mec.biz.service.TeacherService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.QueryInfo;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RequestMapping("teacher")
@Api(tags = "教师服务")
@RestController
public class TeacherController extends BaseController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private ClassGroupService classGroupService;

    @ApiOperation(value = "新增教师")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('teacher/add')")
    public HttpResponseResult add(Teacher teacher) throws Exception {
        return succeed(teacherService.add(teacher));
    }

    @ApiOperation(value = "分页查询教师列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('teacher/queryPage')")
    public Object queryPage(TeacherQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(queryInfo.getOrganId())) {
                queryInfo.setOrganId(employee.getOrganIdList());
            }else if(StringUtils.isEmpty(employee.getOrganIdList())){
                return failed("用户所在分部异常");
            }else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
                    return failed("非法请求");
                }
            }
        }
        return succeed(teacherService.queryPageDetail(queryInfo));
    }

    @ApiOperation(value = "修改教师信息")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('teacher/update')")
    public Object update(Teacher teacher) {
        teacherService.updateTea(teacher);
        return succeed();
    }

    @ApiOperation(value = "根据教师编号查询教师基本信息")
    @GetMapping("/get")
    @ApiParam(value = "教师编号", required = true)
    @PreAuthorize("@pcs.hasPermissions('teacher/get')")
    @ApiImplicitParams({@ApiImplicitParam(name = "teacherId", value = "教师编号", required = true, dataType = "Integer")})
    public Object get(Integer teacherId) {
        return succeed(teacherService.getDetail(teacherId));
    }

    @ApiOperation(value = "获取分部所有老师")
    @GetMapping("/findTeachers")
    @PreAuthorize("@pcs.hasPermissions('teacher/findTeachers','system')")
    public HttpResponseResult findTeachers(String organId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(organId)) {
                organId = employee.getOrganIdList();
            }else if(StringUtils.isEmpty(employee.getOrganIdList())){
                return failed("用户所在分部异常");
            }else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if(!list.containsAll(Arrays.asList(organId.split(",")))){
                    return failed("非法请求");
                }
            }
        }
        return succeed(teacherService.findTeachers(organId));
    }

    @ApiOperation(value = "根据科目获取部门下的教师")
    @GetMapping("/findOrganTeacherBySubject")
    @PreAuthorize("@pcs.hasPermissions('teacher/findOrganTeacherBySubject')")
    public HttpResponseResult findOrganTeacherBySubject(Integer subjecId) {
        return succeed(teacherService.findOrganTeacherBySubject(subjecId));
    }

    @ApiOperation(value = "获取分部教师")
    @GetMapping("/findTeacherByOrganId")
    @PreAuthorize("@pcs.hasPermissions('teacher/findTeacherByOrganId')")
    public HttpResponseResult findTeacherByOrganId(String organId,String subjectIds,String jobNature) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(organId)) {
                organId = employee.getOrganIdList();
            }else if(StringUtils.isEmpty(employee.getOrganIdList())){
                return failed("用户所在分部异常");
            }else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if(!list.containsAll(Arrays.asList(organId.split(",")))){
                    return failed("非法请求");
                }
            }
        }
        return succeed(teacherService.findTeacherByOrganId(organId,subjectIds,jobNature));
    }

    @ApiOperation(value = "分页获取教师名称编号列表")
    @GetMapping("/queryNameList")
    @PreAuthorize("@pcs.hasPermissions('teacher/queryNameList', 'system', 'education')")
    public HttpResponseResult queryPageName(TeacherNameQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(queryInfo.getOrganId())) {
                queryInfo.setOrganId(employee.getOrganIdList());
            }else if(StringUtils.isEmpty(employee.getOrganIdList())){
                return failed("用户所在分部异常");
            }else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
                    return failed("非法请求");
                }
            }
        }
        return succeed(teacherService.queryPageName(queryInfo));
    }

    @ApiOperation(value = "获取乐团所有老师")
    @GetMapping("/findMusicGroupTeachers")
    @PreAuthorize("@pcs.hasPermissions('teacher/findMusicGroupTeachers')")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public HttpResponseResult findMusicGroupTeachers(String musicGroupId){
        return succeed(teacherService.queryMusicGroupTeacher(musicGroupId));
    }

    @ApiOperation(value = "分页查询乐团管理--详情--老师列表")
    @GetMapping("/queryMusicGroupTeachers")
    @PreAuthorize("@pcs.hasPermissions('teacher/queryMusicGroupTeachers')")
    public HttpResponseResult queryMusicGroupTeachers(MusicGroupTeachersQueryInfo queryInfo){
        return succeed(teacherService.queryMusicGroupTeachers(queryInfo));
    }

    @ApiOperation(value = "分页查询乐团管理--详情--老师上课记录")
    @GetMapping("/queryTeacherAttendance")
    @PreAuthorize("@pcs.hasPermissions('teacher/queryTeacherAttendance')")
    public HttpResponseResult queryTeacherAttendance(MusicGroupTeacherAttendanceQueryInfo queryInfo){
        return succeed(teacherService.queryTeacherAttendance(queryInfo));
    }

    @ApiOperation(value = "获取老师乐团课程信息列表")
    @GetMapping("/getTeacherMusicClass")
    @ApiParam(value = "教师编号", required = true)
    @PreAuthorize("@pcs.hasPermissions('teacher/getTeacherMusicClass')")
    public Object getTeacherMusicClass(TeacherMusicClassQueryInfo queryInfo){
        return succeed(classGroupService.getTeacherMusicClass(queryInfo));
    }

    @ApiOperation(value = "获取老师VIP课程信息列表")
    @GetMapping("/getTeacherVipClass")
    @PreAuthorize("@pcs.hasPermissions('teacher/getTeacherVipClass')")
    public Object getTeacherVipClass(VipClassQueryInfo queryInfo){
        return succeed(classGroupService.getTeacherVipClass(queryInfo));
    }

    @ApiOperation(value = "获取教师上课结算列表")
    @GetMapping("/queryCloses")
    @PreAuthorize("@pcs.hasPermissions('teacher/queryCloses')")
    public Object queryCloses(TeacherCloseQueryInfo queryInfo){
        return succeed(teacherService.queryCloses(queryInfo));
    }

    @ApiOperation(value = "获取教师上课课酬未结算总额")
    @GetMapping("/sumTeacherNoPay")
    @PreAuthorize("@pcs.hasPermissions('teacher/sumTeacherNoPay')")
    public Object findTeacherNoPay(Integer teacherId){
        return succeed(teacherService.findTeacherNoPay(teacherId));
    }


    @ApiOperation(value = "获取教师布置的作业")
    @GetMapping("/getCourseScheduleHomework")
    @PreAuthorize("@pcs.hasPermissions('teacher/getCourseScheduleHomework')")
    public Object getCourseScheduleHomework(Integer courseScheduleId){
        return succeed(teacherService.getCourseScheduleHomework(courseScheduleId));
    }

    @ApiOperation(value = "获取老师指导的学员列表")
    @GetMapping("/queryStudent")
    @PreAuthorize("@pcs.hasPermissions('teacher/queryStudent')")
    public Object queryStudent(QueryInfo queryInfo){
        return succeed(teacherService.queryStudent(queryInfo));
    }
}
