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


import com.starry.admin.common.domain.TreeSelect;
import com.starry.admin.modules.system.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.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-07-12
 */
@RestController
@RequestMapping("/ucenter/dept")
public class SysDeptController {

    @Resource
    private SysDeptService deptService;

    @ApiOperation("获取部门列表")
    @PreAuthorize("@customSs.hasPermission('system:dept:list')")
    @GetMapping("/list")
    public R list(SysDeptEntity dept) {
        List<SysDeptEntity> depts = deptService.selectDeptList(dept);
        return R.ok(depts);
    }

    @ApiOperation("添加部门")
    @PreAuthorize("@customSs.hasPermission('system:dept:create')")
    @PostMapping("/create")
    public R creat(@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("修改部门")
    @PreAuthorize("@customSs.hasPermission('system:dept:edit')")
    @PostMapping("/update/{id}")
    public R update(@PathVariable Long id, @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("删除部门")
    @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("获取部门下拉树列表")
    @GetMapping("/treeselect")
    public R treeselect(SysDeptEntity dept) {
        List<SysDeptEntity> list = deptService.selectDeptList(dept);
        List<TreeSelect> treeList = deptService.buildDeptTreeSelect(list);
        return R.ok(treeList);
    }

    @ApiOperation("根据id获取详细信息")
    @GetMapping(value = "/{deptId}")
    public R getInfo(@PathVariable Long deptId) {
        return R.ok(deptService.getById(deptId));
    }

}

