package com.judong.chuanyiserver.controller;

import com.judong.chuanyiserver.annotation.PermissionControl;
import com.judong.chuanyiserver.entity.Department;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.service.DepartmentService;
import com.judong.chuanyiserver.util.Blank;
import com.judong.chuanyiserver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 新增部门
     *
     * @param department
     * @return
     */
    @PostMapping("/addDepartment")
    @PermissionControl
    public Result addDepartment(Department department) {
        if (Blank.isEmpty(department)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return departmentService.addDepartment(department);
    }

    /**
     * 修改部门信息
     *
     * @param department
     * @return
     */
    @PostMapping("/updateDepartment")
    @PermissionControl
    public Result updateDepartment(Department department) {
        if (Blank.isEmpty(department)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return departmentService.updateDepartment(department);
    }

    /**
     * 分页查询所有的部门信息
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getDepartmentListByPage")
    @PermissionControl
    public Result getDepartmentListByPage(Integer page, Integer limit) {
        if (Blank.isEmpty(page, limit) || page < 1 || limit < 1) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return departmentService.getDepartmentListByPage(page, limit);
    }

    /**
     * 通过角色id查看部门信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getDepartmentById")
    @PermissionControl
    public Result getDepartmentById(Integer id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return departmentService.getDepartmentById(id);
    }

    /**
     * 根据父亲id查子节点（一级查询）
     */
    @GetMapping("/queryChildren")
    public Result queryDepartmentChildren(Integer parenId){
        if (Blank.isEmpty(parenId)){
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return departmentService.queryDepartmentByParentId(parenId);
    }


    /**
     * 根据父亲id查所有子节点
     */
    @GetMapping("/queryAllChildren")
    public Result queryDepartmentAllChildren(Integer parenId){
        if (Blank.isEmpty(parenId)){
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        List<Department> departments = departmentService.queryDepartmentAllChildren(parenId);
        return Result.ok(departments);
    }

    /**
     * 逻辑删除（批量删除)
     */
    @PostMapping("/isDeleteById")
    public Result isDeleteByid(@RequestBody List<Integer> ids){
        if (ids.size()<=0){
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return departmentService.isDeleteById(ids);
    }


}
