package cn.common.api;

import cn.common.common.anon.ApiLog;
import cn.common.common.entity.Tree;
import cn.common.common.redis.RedisRepository;
import cn.common.interaction.req.department.DepartmentAddReq;
import cn.common.interaction.req.department.DepartmentQueryReq;
import cn.common.interaction.req.department.DepartmentUpdateReq;
import cn.common.interaction.resp.base.ApiResponse;
import cn.common.interaction.resp.department.AuthDepartmentTreeResp;
import cn.common.interaction.resp.department.AuthDepartmentTreeWrapResp;
import cn.common.service.AuthDepartmentService;
import cn.common.service.AuthUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;

/**
 * @author Singer create by singer email:singer-coder@qq.com
 * @projectName health-platform-api
 * @Description: 部门前端控制器
 * @date 2019-08-23
 */
@RestController
@RequestMapping(value = "api/v1/authDepartment")
public class AuthDepartmentController extends BaseApiController {


    @Resource
    private AuthUserService authUserService;

    @Resource
    private RedisRepository redisRepository;

    @Resource
    private AuthDepartmentService authDepartmentService;


    /**
     * 新增部门
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2019-08-30
     * @param departmentAddReq 新增部门
     */
    @PostMapping(value = "/addAuthDepartment")
    @RequiresPermissions("authDepartment:add")
    @ApiLog(value = "新增部门")
    public ApiResponse addAuthDepartment(@RequestBody DepartmentAddReq departmentAddReq){
        authDepartmentService.addAuthDepartment(departmentAddReq);
        return apiResponse();
    }

    /**
     * 更新部门信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2019-08-30
     * @param departmentUpdateReq 部门更新req
     * @return
     */
    @PutMapping(value = "/updateDepartment")
    @RequiresPermissions("authDepartment:updateItem")
    @ApiLog(value = "更新部门信息")
    public ApiResponse updateDepartment(@RequestBody DepartmentUpdateReq departmentUpdateReq){
        authDepartmentService.updateDepartment(departmentUpdateReq);
        return apiResponse();
    }

    /**
     * 删除部门
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2019-09-05
     * @param departmentIdList 需要被删除的部门的主键ID
     */
    @DeleteMapping("/deleteDepartment/{departmentIdList}")
    @RequiresPermissions("updateDepartment:delete")
    @ApiLog(value = "删除部门")
    public ApiResponse deleteDepartment(@PathVariable(name= "departmentIdList") String departmentIdList){
        authDepartmentService.deleteDepartment(Arrays.asList(departmentIdList.split(",")));
        return apiResponse();
    }

    /**
     * 查询所有的部门的树型结构
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2019-08-23
     * @return Tree
     */
    @PostMapping(value = "/queryAllDepartmentTreeList")
    @RequiresPermissions("authDepartment:itemList")
    @ApiLog(value = "查询所有的部门的树型结构")
    public ApiResponse<AuthDepartmentTreeWrapResp> queryAllDepartmentTreeList(
            @RequestBody(required = false) @Valid DepartmentQueryReq queryReq){
        return apiResponse(authDepartmentService.queryAllDepartmentTreeList(queryReq));
    }


    /**
     * 查询所有的部门的树型结构
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2019-08-23
     * @return Tree
     */
    @GetMapping(value = "/queryAllDepartmentTree")
    @RequiresPermissions("authDepartment:itemTreeList")
    @ApiLog(value = "查询所有的部门的树型结构")
    public ApiResponse<Tree<AuthDepartmentTreeResp>> queryAllDepartmentTree(){
        return apiResponse(authDepartmentService.queryAllDepartmentTree());
    }
}
