package com.starry.admin.modules.system.controller;

import com.starry.admin.common.domain.TreeSelect;
import com.starry.admin.modules.system.module.entity.SysDeptEntity;
import com.starry.admin.modules.system.service.SysDeptService;
import com.starry.common.constant.UserConstants;
import com.starry.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-07-12
 */
@Api(tags = "部门管理", description = "系统部门相关接口")
@RestController
@RequestMapping("/ucenter/dept")
public class SysDeptController {

    @Resource
    private SysDeptService deptService;

    @ApiOperation(value = "获取部门列表", notes = "根据条件查询部门列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = SysDeptEntity.class, responseContainer = "List")})
    @PreAuthorize("@customSs.hasPermission('system:dept:list')")
    @GetMapping("/list")
    public R list(@ApiParam(value = "查询条件") SysDeptEntity dept) {
        List<SysDeptEntity> depts = deptService.selectDeptList(dept);
        return R.ok(depts);
    }

    @ApiOperation(value = "添加部门", notes = "创建新的部门")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "添加失败，部门名称已存在")})
    @PreAuthorize("@customSs.hasPermission('system:dept:create')")
    @PostMapping("/create")
    public R creat(@ApiParam(value = "部门信息", required = true) @RequestBody SysDeptEntity dept) {
        if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
            return R.error("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        boolean success = deptService.create(dept);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    @ApiOperation(value = "修改部门", notes = "根据ID修改部门信息")
    @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "修改失败，部门名称已存在或上级部门不能是自己")})
    @PreAuthorize("@customSs.hasPermission('system:dept:edit')")
    @PostMapping("/update/{id}")
    public R update(@PathVariable Long id, @ApiParam(value = "部门信息", required = true) @RequestBody SysDeptEntity dept) {
        dept.setDeptId(id);
        if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
            return R.error("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        } else if (dept.getParentId().equals(id)) {
            return R.error("修改部门'" + dept.getDeptName() + "'失败，上级部门不能是自己");
        }
        boolean success = deptService.update(dept);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    @ApiOperation(value = "删除部门", notes = "根据ID删除部门")
    @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "删除失败，存在下级部门")})
    @PreAuthorize("@customSs.hasPermission('system:dept:delete')")
    @PostMapping(value = "/delete/{id}")
    public R delete(@PathVariable("id") Long deptId) {
        if (deptService.hasChildByDeptId(deptId)) {
            return R.error("存在下级部门,不允许删除");
        }
        boolean success = deptService.delete(Collections.singletonList(deptId));
        if (success) {
            return R.ok();
        }
        return R.error("删除失败");
    }

    @ApiOperation(value = "获取部门下拉树列表", notes = "获取部门的树形结构数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = TreeSelect.class, responseContainer = "List")})
    @GetMapping("/treeselect")
    public R treeselect(@ApiParam(value = "查询条件") SysDeptEntity dept) {
        List<SysDeptEntity> list = deptService.selectDeptList(dept);
        List<TreeSelect> treeList = deptService.buildDeptTreeSelect(list);
        return R.ok(treeList);
    }

    @ApiOperation(value = "获取部门详情", notes = "根据部门ID获取详细信息")
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = SysDeptEntity.class)})
    @GetMapping(value = "/{deptId}")
    public R getInfo(@PathVariable Long deptId) {
        return R.ok(deptService.getById(deptId));
    }

}
