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.user.EduUserDept;
import com.yida.data.common.core.entity.user.enums.UserDeptTypeEnum;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.user.dto.DeleteUserDeptDTO;
import com.yida.data.user.service.DaHuaSyncService;
import com.yida.data.user.service.EduUserDeptService;
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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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

    private final EduUserDeptService eduUserDeptService;
    private final DaHuaSyncService daHuaSyncService;

    /**
     * 同步企业微信家校部门
     *
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/11/22 16:16
     */
    @ApiOperation("同步企业微信家校部门")
    @GetMapping("/syncUserDept")
    public ResultBean<String> syncUserDept() {
        log.info("同步企业微信家校部门数据");
        this.eduUserDeptService.syncUserDept(null);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("同步指定学校企业微信家校部门")
    @GetMapping("/syncSchoolUserDept")
    public ResultBean<String> syncSchoolUserDept(@RequestParam Long schoolId) {
        log.info("同步企业微信家校部门数据: schoolId: [{}]", schoolId);
        eduUserDeptService.syncUserDept(schoolId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("同步家校部门数据到大华")
    @GetMapping("/syncSchoolDaHuaDept")
    public ResultBean<String> syncSchoolDaHuaDept(@RequestParam Long schoolId) {
        daHuaSyncService.syncSchoolDaHuaDept(schoolId);
        return ResultBean.buildSuccess();
    }

    /**
     * 处理挂在学校下的家校部门
     *
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/11/22 16:19
     */
    @ApiOperation("处理挂在学校下的家校部门")
    @GetMapping("/dealNoParentUserDept")
    public ResultBean<String> dealNoParentUserDept() {
        log.info("处理挂在学校下的家校部门");
        this.eduUserDeptService.dealNoParentUserDept();
        return ResultBean.buildSuccess();
    }

    /**
     * 查询父级家校部门是否存在
     *
     * @param deptIdList 家校部门id集合
     * @param parentWxId 家校部门父级企业微信id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.common.core.entity.user.EduUserDept>
     * @author ZYJ
     * @date 2021/11/22 17:52
     */
    @ApiOperation("查询父级家校部门是否存在")
    @PostMapping("/getUserDeptByDeptAndWx")
    public ResultBean<EduUserDept> getUserDeptByDeptAndWx(@RequestBody List<Long> deptIdList, @RequestParam Long parentWxId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.getUserDeptByDeptAndWx(deptIdList, parentWxId));
    }

    /**
     * 创建家校部门
     *
     * @param userDept 创建的家校部门信息
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/11/22 18:00
     */
    @ApiOperation("创建家校部门")
    @PostMapping("/createUserDept")
    public ResultBean<String> createUserDept(@RequestBody EduUserDept userDept) {
        this.eduUserDeptService.createUserDept(userDept);
        return ResultBean.buildSuccess();
    }

    /**
     * 根据家校部门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/24 15:03
     */
    @ApiOperation("根据家校部门id查询家校部门信息")
    @GetMapping("/getByDeptId")
    public ResultBean<EduUserDept> getByDeptId(@RequestParam Long deptId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.getById(deptId));
    }

    /**
     * 根据家校部门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/30 10:06
     */
    @ApiOperation("根据家校部门id查询上级家校部门信息")
    @GetMapping("/getParentByDeptId")
    public ResultBean<EduUserDept> getParentByDeptId(@RequestParam Long deptId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.getParentByDeptId(deptId));
    }


    @ApiOperation("根据家校部门id查询全部上级家校部门信息")
    @GetMapping("/getParentListByDeptId")
    public ResultBean<List<EduUserDept>> getParentListByDeptId(@RequestParam Long deptId) {
        return ResultBean.buildSuccess(this.eduUserDeptService.getParentListByDeptId(deptId));
    }

    /**
     * 批量保存家校部门
     *
     * @param saveDeptList 批量保存的家校部门数据
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/11/30 10:19
     */
    @ApiOperation("批量保存家校部门")
    @PostMapping("/updateBatchUserDept")
    public ResultBean<String> updateBatchUserDept(@RequestBody List<EduUserDept> saveDeptList) {
        this.eduUserDeptService.updateBatchUserDept(saveDeptList);
        return ResultBean.buildSuccess();
    }

    /**
     * 递归删除家校部门
     *
     * @param deleteUserDeptDTO 企业微信回调删除本地家校部门请求类
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/11/30 10:44
     */
    @ApiOperation("递归删除家校部门")
    @PostMapping("/deleteUserDeptRecur")
    public ResultBean<String> deleteUserDeptRecur(@RequestBody DeleteUserDeptDTO deleteUserDeptDTO) {
        this.eduUserDeptService.deleteUserDeptRecur(deleteUserDeptDTO);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("向上获取部门id")
    @GetMapping("/getUpId")
    public ResultBean<List<Long>> getUpId(@RequestParam Long[] deptIds) {
        return ResultBean.buildSuccess(eduUserDeptService.getUpId(Arrays.asList(deptIds)));
    }

    @ApiOperation("向下获取部门id")
    @GetMapping("/getDownId")
    public ResultBean<List<Long>> getDownId(@RequestParam Long[] deptIds) {
        return ResultBean.buildSuccess(eduUserDeptService.getDownId(Arrays.asList(deptIds)));
    }

    @ApiOperation("向上向下获取部门id")
    @GetMapping("/getUpAndDownId")
    public ResultBean<List<Long>> getUpAndDownId(@RequestParam Long[] deptIds) {
        return ResultBean.buildSuccess(eduUserDeptService.getUpAndDownId(Arrays.asList(deptIds)));
    }

    /**
     * 向下查询指定家校部门类型的家校部门id(可查询非直接子家校部门) 未毕业家校部门
     *
     * @param parentId 父节点id
     * @param type     家校部门类型 {@link UserDeptTypeEnum}
     * @return com.yida.data.common.core.common.ResultBean<java.util.List < java.lang.Long>>
     * @author ZYJ
     * @date 2022/7/11 10:58
     */
    @ApiOperation("向下查询指定家校部门类型的家校部门数据(可查询非直接子家校部门)")
    @GetMapping("/findChildIdByParentAndType")
    public ResultBean<List<EduUserDept>> findChildIdByParentAndType(@ApiParam("父节点id，不能为空") @RequestParam Long parentId,
        @ApiParam("子节点类型. 默认所有直接子节点") Integer type) {
        List<EduUserDept> resultList = new ArrayList<>();
        return ResultBean.buildSuccess(
            eduUserDeptService.findChildIdByParentAndType(Collections.singletonList(parentId), type, resultList));
    }

    @ApiOperation("查询所有子节点")
    @GetMapping("/findListByParent")
    public ResultBean<List<EduUserDept>> findListByParent(@RequestParam Long deptId) {
        return ResultBean.buildSuccess(
            eduUserDeptService.findListByParent(Collections.singletonList(deptId)));
    }


    /**
     * 家校部门树
     *
     * @param deptIds 根节点Id集合
     * @return
     */
    @ApiOperation("家校部门树(根节点多个)")
    @PostMapping("/getUserDeptTree")
    @OperationLog(module = ModuleName.USER, methods = "家校部门树")
    public ResultBean<List> getUserDeptTree(@ApiParam("根节点Id") @RequestBody List<Long> deptIds) {
        return ResultBean.buildSuccess(this.eduUserDeptService.getUserDeptTree(deptIds));
    }


}
