package com.ym.mec.teacher.controller;

import com.ym.mec.biz.service.*;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.QueryInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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 com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.SubjectDao;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.page.TeacherCloseQueryInfo;
import com.ym.mec.biz.dal.page.TeacherMusicClassQueryInfo;
import com.ym.mec.biz.dal.page.VipClassQueryInfo;
import com.ym.mec.biz.dal.page.queryMusicGroupStudentQueryInfo;
import com.ym.mec.common.controller.BaseController;

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

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ImGroupNoticeService imGroupNoticeService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private ClassGroupService classGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
	@Autowired
	private SubjectDao subjectDao;
	@Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "修改教师个人中心信息")
    @PostMapping("/update")
    public Object update(Teacher teacher) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        teacher.setUpdateTime(new Date());
        teacher.setId(sysUser.getId());
        teacherService.updateTea(teacher);
        return succeed();
    }

    @ApiOperation(value = "根据教师编号查询教师基本信息")
    @GetMapping("/queryUserInfo")
    public Object queryUserInfo() {
    	SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		Teacher teacher = teacherService.get(user.getId());
		//专业技能
		if(StringUtils.isNotEmpty(teacher.getSubjectId())){
			teacher.setSubjectName(subjectDao.findBySubIds(teacher.getSubjectId()));
		}
        return succeed(teacher);
    }

    @ApiOperation(value = "根据教师编号查询教师基本信息")
    @GetMapping("/get")
    @ApiParam(value = "教师编号", required = true)
    public Object get(Integer id) {
        return succeed(teacherService.getDetail(id));
    }

    @ApiOperation(value = "根据教师编号查询教师课程班列表")
    @GetMapping("/getClass")
    @ApiParam(value = "教师编号", required = true)
    public Object getClassGroup(Integer id) {
        return succeed(classGroupService.findClassGroup4Teacher(id));
    }

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

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

    @ApiOperation(value = "获取教师所有聊天群组")
    @GetMapping("/queryTeacherGroups")
    public Object queryTeacherGroups(String search){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        return succeed(teacherService.queryTeacherGroups(sysUser.getId(),search));
    }

    @ApiOperation(value = "获取当前教师通讯录列表")
    @GetMapping("/queryGroupStudents")
    public Object queryGroupStudents(String search){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        return succeed(teacherService.queryGroupStudents(sysUser.getId(),search));
    }

    @ApiOperation(value = "获取当前教师通讯录学员列表")
    @GetMapping("/queryGroupStudentList")
    public Object queryGroupStudentList(String search){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        return succeed(teacherService.queryGroupStudentList(sysUser.getId(),search));
    }

    @ApiOperation(value = "分页查询群公告列表")
    @GetMapping(value = "/queryNoticePage")
    public Object queryNoticePage(QueryInfo queryInfo){
        return succeed(imGroupNoticeService.queryPage(queryInfo));
    }

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

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

    @ApiOperation(value = "获取乐团学院学员报名列表")
    @GetMapping("/queryStudentApply")
    public Object queryMusicGroupStudentApply(queryMusicGroupStudentQueryInfo queryInfo) {
        return succeed(teacherService.queryMusicGroupStudentApply(queryInfo));
    }

    @ApiOperation(value = "通过乐团编号查询乐团科目规划")
    @GetMapping("/querySubByMusicGroupId")
    @ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public Object findSubByMusicGroupId(String musicGroupId){
        return succeed(subjectService.findSubByMusicGroupId(musicGroupId));
    }

	@ApiOperation(value = "实名认证")
    @PostMapping("/realNameAuthentication")
	@ApiImplicitParams({ @ApiImplicitParam(name = "realName", value = "姓名", required = true, dataType = "String"),
			@ApiImplicitParam(name = "idcardNo", value = "身份证号码", required = true, dataType = "String"),
			@ApiImplicitParam(name = "idcardFrontImg", value = "身份证正面", required = true, dataType = "String"),
			@ApiImplicitParam(name = "idcardBackImg", value = "身份证背面", required = true, dataType = "String"),
			@ApiImplicitParam(name = "idcardHandImg", value = "手持身份证照", required = true, dataType = "String") })
	public Object realNameAuthentication(String realName, String idcardNo, String idcardFrontImg, String idcardBackImg, String idcardHandImg) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		teacherService.realNameAuthentication(user.getId(), realName, idcardNo, idcardFrontImg, idcardBackImg, idcardHandImg);
		return succeed();
	}

    @ApiOperation(value = "获取教务人员")
    @GetMapping("/findEducationUsers")
    public HttpResponseResult findEducationUsers(Integer userId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        String organIds = new String();
        if (!sysUser.getIsSuperAdmin()) {
            Teacher teacher = teacherService.get(sysUser.getId());
            if (StringUtils.isEmpty(organIds)) {
                organIds = teacher.getTeacherOrganId() + "";
            }
            if(StringUtils.isNotBlank(teacher.getFlowOrganRange())){
                organIds = organIds + "," + teacher.getFlowOrganRange();
            }
        }
        return succeed(employeeService.findByRole("4,5",organIds));
    }
}
