package com.zhongshu.card.server.core.controller.org;

import com.github.microservice.auth.client.constant.AuthConstant;
import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.type.AuthType;
import com.zhongshu.card.client.model.base.IDParam;
import com.zhongshu.card.client.model.base.OidModel;
import com.zhongshu.card.client.model.org.DepartmentParam;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.service.org.DepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("department")
@Tag(name = "部门管理--通用")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.Project, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.School, remark = "")
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到机构部门树-通用", description = "得到机构部门树")
    @RequestMapping(value = "getDepartmentManagerTree", method = {RequestMethod.POST})
    public ResultContent getDepartmentManagerTree(@RequestBody OidModel param) {
        return this.departmentService.getDepartmentTree(param.getOid());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "添加-编辑部门-通用", description = "添加-编辑部门")
    @RequestMapping(value = "addDepartment", method = {RequestMethod.POST})
    public ResultContent addDepartment(@RequestBody @Valid DepartmentParam param) {
        return this.departmentService.addDepartment(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到机构部门树-通用", description = "得到机构部门树")
    @RequestMapping(value = "getDepartmentTree", method = {RequestMethod.POST})
    public ResultContent getDepartmentTree(@RequestBody OidModel param) {
        return this.departmentService.getDepartmentTree(param.getOid());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除部门-通用", description = "删除部门")
    @RequestMapping(value = "deleteDepartment", method = {RequestMethod.POST})
    public ResultContent deleteDepartment(@RequestBody IDParam param) {
        return this.departmentService.deleteDepartment(param.getId());
    }

    @Operation(summary = "得到机构部门树-未登录", description = "得到机构部门树未登录")
    @RequestMapping(value = "getOrgDepartmentTree", method = {RequestMethod.POST})
    public ResultContent getOrgDepartmentTree(@RequestBody OidModel param) {
        Assert.hasText(param.getOid(), "oid不能为空");
        return this.departmentService.getDepartmentTree(param.getOid());
    }

}
