package com.ym.mec.teacher.controller;

import com.ym.mec.biz.dal.dao.StudentPaymentOrderDao;
import com.ym.mec.biz.dal.dao.SubjectDao;
import com.ym.mec.biz.dal.dto.*;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.page.*;
import com.ym.mec.biz.service.*;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
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.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.Date;
import java.util.List;
import java.util.stream.Collectors;

@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 SysUserService sysUserService;
	@Autowired
	private SubjectDao subjectDao;
	@Autowired
    private StudentPaymentOrderDao studentPaymentOrderDao;
	@Autowired
    private StudentRegistrationService studentRegistrationService;

    @ApiOperation(value = "修改教师个人中心信息")
    @PostMapping("/update")
    public Object update(Teacher teacher) {
        teacher.setUpdateTime(new Date());
        teacher.setId(sysUserService.getUserId());
        teacherService.updateTea1(teacher);
        return succeed();
    }

    @ApiOperation(value = "获取老师分部列表（包含流动分部）")
    @GetMapping("/queryOrganList")
    public HttpResponseResult<List<BaseMapDto<Integer,String>>> queryOrganList() {
        return succeed(teacherService.queryOrganList(sysUserService.getUserId()));
    }

    @ApiOperation(value = "根据教师编号查询教师基本信息")
    @GetMapping("/queryUserInfo")
    public Object queryUserInfo() {
		Teacher teacher = teacherService.get(sysUserService.getUserId());
		//专业技能
		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){
        return succeed(teacherService.queryTeacherGroups(sysUserService.getUserId(),search));
    }

    @ApiOperation(value = "获取当前教师通讯录列表")
    @GetMapping("/queryGroupStudents")
    public Object queryGroupStudents(String search){
        return succeed(teacherService.queryGroupStudents(sysUserService.getUserId(),search));
    }

    @ApiOperation(value = "获取当前教师通讯录学员列表")
    @GetMapping("/queryGroupStudentList")
    public Object queryGroupStudentList(String search){
        return succeed(teacherService.queryGroupStudentList(sysUserService.getUserId(),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) {
		teacherService.realNameAuthentication(sysUserService.getUserId(), realName, idcardNo, idcardFrontImg, idcardBackImg, idcardHandImg);
		return succeed();
	}


    @ApiOperation(value = "获取报名/预报名列表")
    @GetMapping("/getRegisterOrPreList")
    public HttpResponseResult<PageInfoReg<RegisterDto>> queryStudentApplyDetail(RegistrationOrPreQueryInfo queryInfo) {
        if (StringUtils.isNotBlank(queryInfo.getSearch())) {
            List<BasicUserDto> users = studentPaymentOrderDao.getUsers(queryInfo.getSearch());
            List<Integer> userIds = users.stream().map(BasicUserDto::getUserId).collect(Collectors.toList());
            if (userIds.size() > 0) {
                queryInfo.setUserIds(userIds);
            }
        }
        return succeed(studentRegistrationService.getRegisterOrPreList(queryInfo));
    }

    @ApiOperation(value = "查询教师关联的乐团预览信息")
    @GetMapping("/queryTeacherMusicStudentOverView")
    public HttpResponseResult<PageInfo<TeacherMusicStudentOverViewDto>> queryTeacherMusicStudentOverView(TeacherServeQueryInfo queryInfo){
        queryInfo.setTeacherId(sysUserService.getUserId());
        return succeed(teacherService.queryTeacherMusicStudentOverView(queryInfo));
    }
}
