package com.cm.common.plugin.oauth.controller.apis.department;

import com.alibaba.fastjson.JSONArray;
import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.AccessTokenException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.oauth.service.department.IDepartmentService;
import com.cm.common.result.ErrorResult;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DepartmentController
 * @Description: 组织部门
 * @Author: WangGeng
 * @Date: 2019-07-29 21:30
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "组织部门管理")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/department")
public class DepartmentController extends AbstractController {

    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation(value = "部门列表", notes = "部门列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "上级部门ID", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listdepartments")
    public JSONArray listDepartments() throws SearchException, AccessTokenException {
        Map<String, Object> params = requestParams();
        String parentId = "0";
        if (!StringUtils.isBlank(params.get("id") == null ? null : params.get("id").toString())) {
            parentId = params.get("id").toString();
        }
        params.put("parentId", parentId);
        return departmentService.listDepartments(params);
    }

    @ApiOperation(value = "部门列表（zTree）", notes = "部门列表（zTree）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "上级部门ID", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listztreedepartment")
    public JSONArray listZTreeDepartment() throws AccessTokenException, SearchException {
        Map<String, Object> params = requestParams();
        return departmentService.listZTreeDepartment(params);
    }

    @ApiOperation(value = "部门人员列表", notes = "部门人员列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "上级部门ID", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listztreedepartmentwithuser")
    public JSONArray listZTreeDepartmentWithUser(@RequestParam(required = false) String id) {
        String departmentParentId = "0";
        if (!StringUtils.isBlank(id)) {
            departmentParentId = id;
        }
        return departmentService.listZTreeDepartmentWithUser(departmentParentId);
    }

}
