package com.yida.data.user.controller;

import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.FebsResponse;
import com.yida.data.common.core.entity.QueryRequest;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.entity.user.EduCollectData;
import com.yida.data.common.core.entity.user.EduStudent;
import com.yida.data.common.core.entity.user.EduUserDept;
import com.yida.data.common.core.entity.user.EduUserDeptStandardGrade;
import com.yida.data.common.core.entity.user.enums.SectionTypeEnum;
import com.yida.data.common.core.enums.IncludeTypeEnum;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.system.vo.ClassInfoVO;
import com.yida.data.user.dto.*;
import com.yida.data.user.dto.h5.ListUserDeptAttendanceDTO;
import com.yida.data.user.service.EduUserDeptService;
import com.yida.data.user.vo.AllDeptInfoVO;
import com.yida.data.user.vo.ClassAttendanceDataVO;
import com.yida.data.user.vo.CollectDataVO;
import com.yida.data.user.vo.TeacherDeptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 家校部门Controller
 *
 * @author ZYJ
 * @date 2021-11-11 09:25:44
 */
@Slf4j
@RestController
@Api(tags = "家校部门后台API")
@RequiredArgsConstructor
@RequestMapping("eduUserDept")
public class EduUserDeptController {

    private final EduUserDeptService eduUserDeptService;

    /**
     * 查询父级家校部门
     *
     * @param deptId 家校部门id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.common.core.entity.user.EduUserDept>
     * @author ZYJ
     * @date 2021/11/11 10:04
     */
    @ApiOperation("查询父级家校部门")
    @GetMapping("/getParent")
    public ResultBean<EduUserDept> getParent(@ApiParam("家校部门id") @RequestParam Long deptId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.getById(this.eduUserDeptService.getById(deptId).getParentId()));
    }

    /**
     * 查询所有子节点
     *
     * @param deptId 家校部门id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.common.core.entity.user.EduUserDept>
     * @author ZYJ
     * @date 2021/11/11 10:09
     */
    @ApiOperation("查询所有子节点")
    @GetMapping("/findListByParent")
    public ResultBean<List<EduUserDept>> findListByParent(@ApiParam("家校部门id") @RequestParam Long deptId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.findListByParent(Collections.singletonList(deptId)));
    }

    /**
     * 查询直接子节点
     *
     * @param deptId 家校部门父节点id，不能为空
     * @return com.yida.data.common.core.common.ResultBean<java.util.List < com.yida.data.common.core.entity.user.EduUserDept>>
     * @author ZYJ
     * @date 2021/11/11 10:21
     */
    @ApiOperation("查询直接子节点")
    @GetMapping("/findChildByParent")
    public ResultBean<List<EduUserDept>> findChildByParent(@ApiParam("家校部门父节点id，不能为空") @RequestParam Long deptId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.findChildByParent(deptId));
    }

    /**
     * 向下查询指定家校部门类型的家校部门数据(可查询非直接子家校部门)
     *
     * @param parentId 父节点id   schoolId  为 负数
     * @param type     子节点类型, 1表示班级，2表示年级，3表示学段，4表示校区, 默认所有直接子节点
     * @return com.yida.data.common.core.common.ResultBean<java.util.List < com.yida.data.common.core.entity.user.EduUserDept>>
     * @author ZYJ
     * @date 2021/11/11 10:27
     */
    @ApiOperation("向下查询指定家校部门类型的家校部门数据(可查询非直接子家校部门)")
    @GetMapping("/findChildByParentAndType")
    public ResultBean<List<EduUserDept>> findChildByParentAndType(@ApiParam("父节点id，不能为空") @RequestParam Long parentId,
        @ApiParam("子节点类型, 1表示班级，2表示年级，3表示学段，4表示校区, 默认所有直接子节点") Integer type) {
        return ResultBean
            .buildSuccess(this.eduUserDeptService.findChildByParentAndType(Collections.singletonList(parentId), type));
    }


    @ApiOperation("根据用户ID查询可见班级采集数据统计")
    @GetMapping("/findClassChildByParent")
    public ResultBean<List<CollectDataVO>> findVisibleClassByUserId(@ApiParam("用户主键ID") @RequestParam Long staffId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.findVisibleClassByUserId(staffId));
    }

    @ApiOperation("根据用户ID查询可见班级考勤统计数据")
    @PostMapping("/findClassAttendanceByUserId")
    public ResultBean<List<ClassAttendanceDataVO>> findClassAttendanceByUserId(@RequestBody ListUserDeptAttendanceDTO dto) {
        return ResultBean.buildSuccess(this.eduUserDeptService
            .findClassAttendanceByUserId(dto.getStaffId(), dto.getDate()));
    }

    @ApiOperation("查询班级详细宿舍考勤数据")
    @PostMapping("/findClassAttendanceDetailByDeptId")
    public ResultBean<List<Object>> findClassAttendanceDetailByDeptId(@RequestBody ListUserDeptAttendanceDTO dto) {
        return ResultBean.buildSuccess(this.eduUserDeptService
            .findClassAttendanceDetailByDeptId(dto.getDeptId(), dto.getType(), dto.getDate()));
    }


    /**
     * 根据家校部门id查询所属学校
     *
     * @param deptId 根据家校部门id查询所属学校
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.common.core.entity.user.EduUserDept>
     * @author ZYJ
     * @date 2021/11/11 10:48
     */
    @ApiOperation("根据家校部门id查询所属学校")
    @GetMapping("findSchoolByDept")
    public ResultBean<Dept> findSchoolByDept(@ApiParam("父节点id，不能为空") @RequestParam Long deptId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.findSchoolByDept(deptId));
    }

    /**
     * 查询家校部门
     *
     * @param request     分页请求
     * @param dept        查询条件
     * @param includeType 是否包含班级 {@link IncludeTypeEnum}
     * @return com.yida.data.common.core.entity.FebsResponse
     * @author ZYJ
     * @date 2021/11/11 10:57
     */
    @ApiOperation("查询家校部门")
    @GetMapping("/userDeptList")
    public FebsResponse userDeptList(QueryRequest request, EduUserDept dept,
        @ApiParam("是否包含班级. 0:包含, 1:不包含.不传默认查询全部") String includeType) {
        Map<String, Object> deptList = this.eduUserDeptService.findDeptList(request, dept, includeType);
        return new FebsResponse().data(deptList);
    }

    /**
     * 根据id查询家校部门
     *
     * @param deptId 家校部门id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.common.core.entity.user.EduUserDept>
     * @author ZYJ
     * @date 2021/11/11 11:31
     */
    @ApiOperation("根据id查询家校部门")
    @GetMapping("/getInfoById")
    public ResultBean<EduUserDept> getInfoById(@ApiParam("家校部门id") @RequestParam Long deptId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.getById(deptId));
    }

    /**
     * 家校部门树
     *
     * @param deptId 根节点Id
     * @return com.yida.data.common.core.common.ResultBean<java.util.List>
     * @author ZYJ
     * @date 2021/11/11 13:38
     */
    @ApiOperation("家校部门树(若当前登陆角色是学校负责人则不用传递参数)")
    @GetMapping("/getUserDeptTree")
    public ResultBean<List> getUserDeptTree(@ApiParam("根节点Id") Long deptId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.getUserDeptTree(deptId));
    }

    /**
     * 根据各层名称查询班级信息
     *
     * @param schoolId    学校id
     * @param campusName  学期名称
     * @param sectionName 学段名称
     * @param gradeName   年级名称
     * @param className   班级名称
     * @return com.yida.data.common.core.common.ResultBean<java.util.Map < java.lang.String,
        * com.yida.data.common.core.entity.user.EduUserDept>>
     * @author ZYJ
     * @date 2021/11/11 13:50
     */
    @ApiOperation("根据各层名称查询班级信息")
    @GetMapping("/findClassInfoByName")
    public ResultBean<Map<String, EduUserDept>> findClassInfoByName(
        Long schoolId, String campusName, String sectionName,
        String gradeName, String className) {
        return ResultBean.buildSuccess(this.eduUserDeptService.findClassInfoByName(schoolId, campusName, sectionName,
            gradeName, className));
    }

    /**
     * 查询班级年级等信息
     *
     * @param classId 班级id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.system.vo.ClassInfoVO>
     * @author ZYJ
     * @date 2021/12/3 9:43
     */
    @ApiOperation("查询班级信息")
    @GetMapping("/getClassInfo")
    public ResultBean<ClassInfoVO> getClassInfo(Long classId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.getClassInfo(classId));
    }

    /**
     * 新增学区信息(包含学段、年级和班级)
     *
     * @param saveCampusDTO 保存校区数据请求类
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/11/17 11:10
     */
    @ApiOperation(value = "新增学区信息(包含学段、年级和班级)", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "addCampus", consumes = MediaType.APPLICATION_JSON_VALUE)
    @OperationLog(module = ModuleName.USER_DEPT, methods = "新增学区信息(包含学段、年级和班级)", type = OperationLogTypeEnum.INSERT)
    public ResultBean<String> addCampus(@RequestBody SaveCampusDTO saveCampusDTO) {
        this.eduUserDeptService.addCampus(saveCampusDTO);
        return ResultBean.buildSuccess();
    }

    /**
     * 新增学段信息(包含年级和班级)
     *
     * @param sectionDTO 保存学段数据请求类
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/11/18 14:00
     */
    @ApiOperation(value = "新增学段信息(包含年级和班级)", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "addSection", consumes = MediaType.APPLICATION_JSON_VALUE)
    @OperationLog(module = ModuleName.USER_DEPT, methods = "新增学段信息(包含年级和班级)", type = OperationLogTypeEnum.INSERT)
    public ResultBean<String> addSection(@RequestBody SectionDTO sectionDTO) {
        this.eduUserDeptService.addSection(sectionDTO, true, false, null, false);
        return ResultBean.buildSuccess();
    }

    /**
     * 新增年级信息(包含班级)
     *
     * @param saveGradeDTO 保存年级数据请求类
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/11/18 16:49
     */
    @ApiOperation(value = "新增年级信息(包含班级)", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "addGrade", consumes = MediaType.APPLICATION_JSON_VALUE)
    @OperationLog(module = ModuleName.USER_DEPT, methods = "新增年级信息(包含班级)", type = OperationLogTypeEnum.INSERT)
    public ResultBean<String> addGrade(@RequestBody SaveGradeDTO saveGradeDTO) {
        this.eduUserDeptService.addGrade(saveGradeDTO);
        return ResultBean.buildSuccess();
    }

    /**
     * 页面添加家校部门
     *
     * @param addUserDeptDTO 添加家校部门请求类
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/11/18 16:51
     */
    @ApiOperation(value = "页面添加家校部门", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "addUserDept", consumes = MediaType.APPLICATION_JSON_VALUE)
    @OperationLog(module = ModuleName.USER_DEPT, methods = "添加家校部门", type = OperationLogTypeEnum.INSERT)
    public ResultBean<String> addUserDept(@RequestBody AddUserDeptDTO addUserDeptDTO) {
        this.eduUserDeptService.addUserDept(addUserDeptDTO);
        return ResultBean.buildSuccess();
    }

    /**
     * 编辑家校部门
     *
     * @param editUserDeptDTO 编辑家校部门请求类
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/11/17 17:27
     */
    @ApiOperation(value = "编辑家校部门", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "editUserDept", consumes = MediaType.APPLICATION_JSON_VALUE)
    @OperationLog(module = ModuleName.USER_DEPT, methods = "编辑家校部门", type = OperationLogTypeEnum.UPDATE)
    public ResultBean<String> editUserDept(@RequestBody EditUserDeptDTO editUserDeptDTO) {
        this.eduUserDeptService.editUserDept(editUserDeptDTO);
        return ResultBean.buildSuccess();
    }

    /**
     * 批量删除家校部门
     *
     * @param deptIds  家校部门id字符串
     * @param schoolId 学校id
     * @author ZYJ
     * @date 2021/11/18 16:54
     */
    @ApiOperation(value = "批量删除家校部门")
    @GetMapping("/deleteUserDeptBatch")
    @OperationLog(module = ModuleName.USER_DEPT, methods = "批量删除家校部门", type = OperationLogTypeEnum.DELETE)
    public ResultBean<String> deleteUserDeptBatch(
        @ApiParam(value = "家校部门id字符串", required = true) @RequestParam String deptIds,
        @ApiParam(value = "学校id", required = true) @RequestParam Long schoolId) {
        this.eduUserDeptService.deleteUserDeptBatch(deptIds, schoolId);
        return ResultBean.buildSuccess();
    }

    /**
     * 根据学段类型查询对应的标准年级
     *
     * @param sectionType 学段类型 {@link SectionTypeEnum}
     * @return com.yida.data.common.core.common.ResultBean<java.util.List < com.yida.data.common.core.entity.user.EduUserDeptStandardGrade>>
     * @author ZYJ
     * @date 2021/11/18 17:04
     */
    @ApiOperation(value = "根据学段类型查询对应的标准年级")
    @GetMapping("/listStandardGradeBySectionType")
    public ResultBean<List<EduUserDeptStandardGrade>> listStandardGradeBySectionType(
        @ApiParam(value = "学段类型", required = true) @RequestParam String sectionType) {
        return ResultBean.buildSuccess(this.eduUserDeptService.listStandardGradeBySectionType(sectionType));
    }


    @ApiOperation("向上查询父级部门")
    @GetMapping("/findParentUp")
    public ResultBean<AllDeptInfoVO> findParentUp(@ApiParam("部门id") Long deptId,
        @ApiParam("最上级部门类型,1表示班级，2表示年级，3表示学段，4表示校区") Integer type) {
        return ResultBean.buildSuccess(eduUserDeptService.findParentUp(deptId, type));
    }


    @ApiOperation("查询职工管理班级,包含班级学生人数")
    @GetMapping("/listClassByStaffId")
    public ResultBean<List<TeacherDeptVO>> listClassByStaffId(@RequestParam Long staffId) {
        return ResultBean.buildSuccess(eduUserDeptService.listClassByStaffId(staffId));
    }

}
