package com.yida.data.user.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.user.EduStaff;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.common.core.utils.WxPublicUtil;
import com.yida.data.common.core.utils.WxUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.user.dto.ListStaffDTO;
import com.yida.data.user.service.EduStaffService;
import com.yida.data.user.service.EduTeacherDeptService;
import com.yida.data.user.service.QywxSyncService;
import com.yida.data.user.vo.TeacherClassVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "教师管理（不鉴权）")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/in/staff")
public class InStaffController {

    private final EduStaffService eduStaffService;
    private final EduTeacherDeptService eduTeacherDeptService;
    private final QywxSyncService qywxSyncService;

    private final CommonService commonService;
    private final WxUtil wxUtil;
    private final WxPublicUtil wxPublicUtil;

    @ApiOperation("处理企业微信更改教师信息")
    @PostMapping("/dealWxTeacherChange")
    public ResultBean<EduStaff> dealWxTeacherChange(Long schoolId, Long campusId, Long sectionId, Long gradeId,
        Long classId, @RequestBody List<Map> teacherList, Long deptWxId) {
        eduStaffService.dealWxTeacherChange(schoolId, campusId, sectionId, gradeId, classId, teacherList, deptWxId);
        return ResultBean.buildSuccess();
    }


    @ApiOperation("根据微信id或手机号查询教师id")
    @GetMapping("/getStaffByWxOrMobileOrOpenId")
    public ResultBean<EduStaff> getStaffByWxOrMobileOrOpenId(
        @RequestParam(required = false) String wxId,
        @RequestParam(required = false) String mobile,
        @RequestParam(required = false) String wxOpenId,
        Long schoolId) {
        LambdaQueryWrapper<EduStaff> condition = Wrappers.<EduStaff>lambdaQuery().eq(EduStaff::getSchoolId, schoolId);
        if (StrUtil.isNotBlank(wxId)) {
            condition.eq(EduStaff::getWxId, wxId);
        }
        if (StrUtil.isNotBlank(mobile)) {
            condition.eq(EduStaff::getMobile, mobile);
        }
        if (StrUtil.isNotBlank(wxOpenId)) {
            condition.eq(EduStaff::getWxPublicOpenId, wxOpenId);
        }
        return ResultBean.buildSuccess(eduStaffService.getOne(condition));
    }

    @ApiOperation("根据学校和微信更新或新增教师")
    @PostMapping("/saveStaffBySchoolAndWx")
    public ResultBean saveTeacherBySchoolAndWx(@RequestBody EduStaff staff) {
        eduStaffService.saveStaffBySchoolAndWx(staff);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("处理企业微信删除职工事件")
    @GetMapping("/dealWxUserDelete")
    ResultBean dealWxUserDelete(@RequestParam("wxId") String wxId,
        @RequestParam("schoolId") Long schoolId) {
        eduStaffService.dealWxUserDelete(wxId, schoolId);
        return ResultBean.buildSuccess();
    }

    @GetMapping("/getStaff")
    public ResultBean<EduStaff> getStaff(Long id) {
        return ResultBean.buildSuccess(eduStaffService.getStaffById(id));
    }

    @ApiOperation("保存教师易达app用户id")
    @GetMapping("/saveYidaAppUserId")
    ResultBean saveYidaAppUserId(@RequestParam("staffId") Long staffId,
        @RequestParam("yidaAppUserId") String yidaAppUserId) {
        EduStaff staff = new EduStaff();
        staff.setId(staffId);
        staff.setYidaAppUserId(yidaAppUserId);
        eduStaffService.updateById(staff);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("同步企业微信职工信息")
    @GetMapping("/syncQywxStaff")
    public ResultBean syncQywxStaff() {
//        eduStaffService.syncQywxStaff();
        qywxSyncService.syncQywxStaff(null);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("同步指定学校企业微信职工数据")
    @GetMapping("/syncSchoolQywxStaff")
    public ResultBean syncSchoolQywxStaff(@RequestParam Long schoolId) {
//        eduStudentService.syncQywxStudent();
        qywxSyncService.syncQywxStaff(schoolId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("根据大华code查询教职工")
    @GetMapping("/getStaffByDahua")
    ResultBean<EduStaff> getStaffByDahuaNoPermission(@RequestParam("dahuaCode") String dahuaCode) {
        return ResultBean.buildSuccess(eduStaffService.getOne(Wrappers.<EduStaff>lambdaQuery()
            .eq(EduStaff::getDahuaCode, dahuaCode)));
    }

    @ApiOperation("查询职工列表")
    @PostMapping("/listStaff")
    ResultBean<List<EduStaff>> listStaff(@RequestBody ListStaffDTO dto) {
        return ResultBean.buildSuccess(eduStaffService.listStaff(dto));
    }

    @ApiOperation("绑定职工公众号信息")
    @GetMapping("/bindWxOpenId")
    public ResultBean bindWxOpenId(@ApiParam("code") String code,
        @ApiParam("id") Long staffId,
        @ApiParam("学校id") Long schoolId) {
        Dept school = commonService.getDept(schoolId);
        EduStaff staff = eduStaffService.getById(staffId);
        String openId = staff.getWxPublicOpenId();
        Asserts.isNull(openId, "该职工已绑定，请联系管理人员");
        if (StrUtil.isBlank(openId)) {
            openId = wxUtil.getOpenId(school.getWxPublicAppId(), school.getWxPublicSecret(), code);
            staff.setWxPublicOpenId(openId);
            eduStaffService.updateById(staff);
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("根据家校部门id和类型查询对应教师信息")
    @GetMapping("/listTeacherByDeptId")
    public ResultBean<List<EduStaff>> listTeacherByDeptId(@RequestParam("deptId") Long deptId,
        @RequestParam("type") String type) {
        return ResultBean.buildSuccess(eduTeacherDeptService.listTeacherDept(deptId, type));
    }

    @ApiOperation("保存职工openId")
    @GetMapping("/saveWxPublicOpenId")
    ResultBean saveWxPublicOpenId(@RequestParam("staffId") Long staffId,
        @RequestParam("openId") String openId) {
        EduStaff staff = new EduStaff();
        staff.setId(staffId);
        staff.setWxPublicOpenId(openId);
        eduStaffService.updateById(staff);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("根据员工id获取企业微信密文用户id")
    @GetMapping("/getWxIdByStaffId")
    public ResultBean<String> getWxIdByStaffId(@RequestParam Long staffId) {
        eduStaffService.getWxIdByStaffId(staffId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("根据StaffID查询教师家校沟通部门信息")
    @GetMapping("/listDeptByTeacher")
    public ResultBean<List<TeacherClassVO>> listDeptByTeacher(@RequestParam Long teacherId, Integer type) {
        List<TeacherClassVO> teacherClassVOS = eduStaffService.listDeptByTeacher(teacherId, type);
        return ResultBean.buildSuccess(teacherClassVOS);
    }

    @ApiOperation("查询职工角色")
    @GetMapping("/listStaffRolesByStaffId")
    public ResultBean<List<Integer>> listStaffRolesByStaffId(@RequestParam Long staffId) {
        return ResultBean.buildSuccess(eduStaffService.listStaffRolesByStaffId(staffId));
    }

    @ApiOperation("查询职工管理班级")
    @GetMapping("/listClassIdByStaffId")
    public ResultBean<List<Long>> listClassIdByStaffId() {
        return ResultBean.buildSuccess(eduStaffService.listClassIdByStaffId());
    }
}
