package com.srs.web.controller.comprehensive;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.srs.common.core.domain.entity.SysUser;
import com.srs.common.core.domain.model.LoginUser;
import com.srs.comprehensive.domain.Dto.CphExport;
import com.srs.comprehensive.domain.SrsGrade;
import com.srs.comprehensive.domain.SrsMajors;
import com.srs.comprehensive.domain.Vo.*;
import com.srs.comprehensive.service.ISrsCqScoreService;
import com.srs.routine.domain.vo.ClassStuDisciplinaryDataStatistics;
import com.srs.routine.domain.vo.ClassStuLeaveDataStatistics;
import com.srs.routine.service.IRtStuDisciplinaryApplicationService;
import com.srs.routine.service.IRtStuLeaveApplicationService;
import com.srs.system.service.ISysPostService;
import com.srs.system.service.ISysUserService;
import com.srs.web.controller.common.RoleBool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
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.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.srs.common.annotation.Log;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.enums.BusinessType;
import com.srs.comprehensive.domain.CphTeacher;
import com.srs.comprehensive.service.ICphTeacherService;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.core.page.TableDataInfo;

/**
 * 辅导员信息Controller
 *
 * @author srs
 * @date 2023-07-03
 */
@Api(value = "辅导员", tags = "辅导员")
@RestController
@RequestMapping("/system/teacher")
public class CphTeacherController extends BaseController {
    @Autowired
    private ICphTeacherService cphTeacherService;

    @Autowired
    private ISrsCqScoreService srsCqScoreService;

    @Autowired
    public ISysUserService _userService;

    @Autowired
    ISysPostService _postService;

    @ApiOperation("统计辅导员带过的学生")
    @GetMapping("/countOwnHisStu")
    public AjaxResult countOwnHisStu() {
        Long uid = getUserId();
        boolean isFdy = RoleBool.isFdy(uid, _postService);
        if (isFdy) {
            CphSearch param = new CphSearch();
            param.setTNo(getUsername());
            CountStu list = cphTeacherService.countOwnStu(param);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "您无权做这些");
        }
    }

    @ApiOperation("统计辅导员在校的学生")
    @GetMapping("/countOwnNowStu")
    public AjaxResult countOwnNowStu() {
        Long uid = getUserId();
        boolean isFdy = RoleBool.isFdy(uid, _postService);
        if (isFdy) {
            CphSearch param = new CphSearch();
            param.setTNo(getUsername());
            param.setStatus("01");
            CountStu list = cphTeacherService.countOwnStu(param);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "您无权做这些");
        }
    }

    @ApiOperation("统计辅导员在校的学生")
    @GetMapping("/countOwnLiveStu")
    public AjaxResult countOwnLiveStu() {
        Long uid = getUserId();
        boolean isFdy = RoleBool.isFdy(uid, _postService);
        if (isFdy) {
            CphSearch param = new CphSearch();
            param.setTNo(getUsername());
            CountStu list = cphTeacherService.countLiveStu(param);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "您无权做这些");
        }
    }

    @ApiOperation("统计辅导员的班级")
    @GetMapping("/countOwnHisClass")
    public AjaxResult countOwnHisClass() {
        Long uid = getUserId();
        boolean isFdy = RoleBool.isFdy(uid, _postService);
        if (isFdy) {
            CphSearch param = new CphSearch();
            param.setTNo(getUsername());
            List<CphClassRankScore> list = cphTeacherService.countOwnHisClass(param);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "您无权做这些");
        }
    }

    @ApiOperation("辅导员画像，按照班级统计每个班的学生人数、性别人数")
    @GetMapping("/countOwnHisAsClass")
    public AjaxResult countOwnHisAsClass() {
        Long uid = getUserId();
        boolean isFdy = RoleBool.isFdy(uid, _postService);
        if (isFdy) {
            CphSearch param = new CphSearch();
            param.setTNo(getUsername());
            List<Map> list = cphTeacherService.countOwnHisAsClass(param);
            LoginUser u = getLoginUser();
            return AjaxResult.success(list).put("fdy", u.getUser());
        } else {
            return AjaxResult.error(401, "您无权做这些");
        }
    }

    @ApiOperation("辅导员画像，按照班级统计每个班的学生人数、性别人数")
    @GetMapping("/countFdyHisAsClass/{tNo}")
    public AjaxResult countFdyHisAsClass(@PathVariable("tNo") String tNo) {
        Long uid = getUserId();
        boolean isFdy = RoleBool.isHigh(uid, _postService);
        if (isFdy) {
            CphSearch param = new CphSearch();
            param.setTNo(tNo);
            List<Map> list = cphTeacherService.countOwnHisAsClass(param);
            SysUser user = _userService.selectUserByUserName(tNo);
            return AjaxResult.success(list).put("fdy", user);
        } else {
            return AjaxResult.error(401, "您无权做这些");
        }
    }


    @GetMapping("/listOwnMajor")
    @ApiOperation("辅导员查询自己的专业")
    public AjaxResult listOwnMajor() {
        Long uid = getUserId();
        boolean isFdy = RoleBool.isFdy(uid, _postService);
        if (isFdy) {
            String tNo = getUsername();
            List<SrsMajors> list = cphTeacherService.listOwnMajor(tNo);
            return success(list);
        } else {
            return AjaxResult.error(401, "您无权做这些");
        }
    }

    @GetMapping("/listOwnGrade")
    @ApiOperation("辅导员查询自己的年级")
    public AjaxResult listOwnGrade() {
        Long uid = getUserId();
        boolean isFdy = RoleBool.isFdy(uid, _postService);
        if (isFdy) {
            String tNo = getUsername();
            List<SrsGrade> list = cphTeacherService.listOwnGrade(tNo);
            return success(list);
        } else {
            return AjaxResult.error(401, "您无权做这些");
        }
    }

    @GetMapping("/getOwnUnTodoCount")
    public AjaxResult getOwnUnTodoCount() {
        String tno = getUsername();
        List<Map<String, Object>> res = cphTeacherService.getOwnUnTodoCount(tno);
        return success(res);
    }

    @GetMapping("/getIamRecordByStuNo/{stuNo}")
    public TableDataInfo getIamRecordByStuNo(@PathVariable("stuNo") String stuNo) {
        startPage();
        List<CphRecord> list = cphTeacherService.getIamRecordByStuNo(stuNo);
        return getDataTable(list);
    }

    @GetMapping("/listAllSport")
    public AjaxResult listAllSport() {
        String tno = getUsername();
        List<CphTeacherLookScore> list = cphTeacherService.listSport(tno);
        return success(list);
    }

    @GetMapping("/listSport")
    public TableDataInfo listSport() {

        String tno = getUsername();
        startPage();
        List<CphTeacherLookScore> list = cphTeacherService.listSport(tno);
        return getDataTable(list);
    }


    @GetMapping("/listAllCourse")
    public AjaxResult listAllCourse() {
        String tno = getUsername();
        List<CphTeacherLookScore> list = cphTeacherService.listCourse(tno);
        return success(list);
    }

    @GetMapping("/listCourse")
    public TableDataInfo listCourse() {
        String tno = getUsername();
        startPage();
        List<CphTeacherLookScore> list = cphTeacherService.listCourse(tno);
        return getDataTable(list);
    }

    @GetMapping("/listAllCe")
    public AjaxResult listAllCe() {
        String tno = getUsername();
        List<CphTeacherLookScore> list = cphTeacherService.listCe(tno);
        return success(list);
    }

    @GetMapping("/listCe")
    public TableDataInfo listCe() {
        String tno = getUsername();
        startPage();
        List<CphTeacherLookScore> list = cphTeacherService.listCe(tno);
        return getDataTable(list);
    }

    @GetMapping("/listAllIam")
    public AjaxResult listAllIam() {
        String tno = getUsername();
        List<CphTeacherLookScore> list = cphTeacherService.listIam(tno);
        return success(list);
    }

    @GetMapping("/listIam")
    public TableDataInfo listIam() {
        String tno = getUsername();
        startPage();
        List<CphTeacherLookScore> list = cphTeacherService.listIam(tno);
        return getDataTable(list);
    }

    @GetMapping("/listAllClassTwo")
    public AjaxResult listAllClassTwo() {
        String tno = getUsername();
        List<CphTeacherLookScore> list = cphTeacherService.listClassTwo(tno);
        return success(list);
    }

    @GetMapping("/listClassTwo")
    public TableDataInfo listClassTwo() {
        String tno = getUsername();
        startPage();
        List<CphTeacherLookScore> list = cphTeacherService.listClassTwo(tno);
        return getDataTable(list);
    }

    @GetMapping("/getStuScore/{stuNo}")
    public AjaxResult getStuScore(@PathVariable("stuNo") String stuNo) {
        return success(srsCqScoreService.getOwn(stuNo));
    }

    @GetMapping("/listOwnAllStu")
    public AjaxResult listOwnAllStu() {
        String tNo = getUsername();
        List<CphTeacherOwnStu> list = cphTeacherService.listOwnStu(tNo);
        return success(list);
    }

    @GetMapping("/listOwnStu")
    public TableDataInfo listOwnStu() {
        startPage();
        String tNo = getUsername();
        List<CphTeacherOwnStu> list = cphTeacherService.listOwnStu(tNo);
        return getDataTable(list);
    }

    /**
     * 查询辅导员信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(CphTeacher cphTeacher) {
        startPage();
        List<CphTeacher> list = cphTeacherService.selectCphTeacherList(cphTeacher);
        return getDataTable(list);
    }

    /**
     * 查询辅导员 名字列表
     */
//    @PreAuthorize("@ss.hasPermi('system:teacher:list')")
    @GetMapping("/name")
    public AjaxResult listName() {
        return AjaxResult.success(cphTeacherService.selectCphTeacherNameList());
    }

    /**
     * 导出辅导员信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:teacher:export')")
    @Log(title = "辅导员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CphTeacher cphTeacher) {
        List<CphTeacher> list = cphTeacherService.selectCphTeacherList(cphTeacher);
        ExcelUtil<CphTeacher> util = new ExcelUtil<CphTeacher>(CphTeacher.class);
        util.exportExcel(response, list, "辅导员信息数据");
    }

    /**
     * 获取辅导员信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:teacher:query')")
    @GetMapping(value = "/{teacherId}")
    public AjaxResult getInfo(@PathVariable("teacherId") Long teacherId) {
        return success(cphTeacherService.selectCphTeacherByTeacherId(teacherId));
    }

    /**
     * 新增辅导员信息
     */
//    @PreAuthorize("@ss.hasPermi('system:teacher:add')")
    @Log(title = "辅导员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CphTeacher cphTeacher) {
        return toAjax(cphTeacherService.insertCphTeacher(cphTeacher));
    }

    /**
     * 修改辅导员信息
     */
//    @PreAuthorize("@ss.hasPermi('system:teacher:edit')")
    @Log(title = "辅导员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody CphTeacher cphTeacher) {
        return toAjax(cphTeacherService.updateCphTeacher(cphTeacher));
    }

    /**
     * 删除辅导员信息
     */
//    @PreAuthorize("@ss.hasPermi('system:teacher:remove')")
    @Log(title = "辅导员信息", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{teacherIds}")
    public AjaxResult remove(@PathVariable Long[] teacherIds) {
        return toAjax(cphTeacherService.deleteCphTeacherByTeacherIds(teacherIds));
    }

    @Autowired
    private IRtStuLeaveApplicationService rtStuLeaveApplicationService;

    @Autowired
    private IRtStuDisciplinaryApplicationService rtStuDisciplinaryApplicationService;

    @ApiOperation("辅导员带班驾驶舱接口-获取该辅导员所带班级的请假人数统计")
    @GetMapping("/getClassLeaveNumber")
    public AjaxResult<List<ClassStuLeaveDataStatistics>> getClassLeaveNumber() {
        return success(rtStuLeaveApplicationService.selectClassLeaveNumber(getUsername()));
    }


    @ApiOperation("辅导员带班驾驶舱接口-获取该辅导员所带班级的请假人数统计")
    @GetMapping("/getFdyClassLeaveNumber/{tNo}")
    public AjaxResult<List<ClassStuLeaveDataStatistics>> getFdyClassLeaveNumber(@PathVariable("tNo") String tNo) {
        Long uid = getUserId();
        boolean hasRole = RoleBool.isHigh(uid, _postService);
        if (hasRole) {
            return success(rtStuLeaveApplicationService.selectClassLeaveNumber(tNo));
        } else {
            return AjaxResult.error(401, "您无权做这些");
        }
    }


    @ApiOperation("辅导员带班驾驶舱接口-获取该辅导员所带班级处分人员")
    @GetMapping("/getClassDisciplinaryNumber")
    public AjaxResult<List<ClassStuDisciplinaryDataStatistics>> getClassDisciplinaryNumber() {
        return success(rtStuDisciplinaryApplicationService.getClassDisciplinaryNumber());
    }
    @ApiOperation("综合素质-思想政治素质-思想品德")
    @GetMapping("/exportsxzz")
    public AjaxResult exportsxzz(CphExport param) {
        List<Map<String, Object>> list = cphTeacherService.exportSXZZ(param);
        return success(list);
    }

    @ApiOperation("综合素质-思想政治素质-思想品德")
    @GetMapping("/exportzhpj")
    public AjaxResult exportzhpj(CphExport param) {
        List<Map<String, Object>> list = cphTeacherService.exportZHPJ(param);
        return success(list);
    }
}
