package com.cico.nest.lcdp.boot.core.system.controller.admin.dept;

import com.cico.nest.lcdp.boot.core.system.controller.admin.dept.vo.dept.*;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.dept.DeptDO;
import com.cico.nest.lcdp.boot.core.system.service.dept.DeptService;
import com.cico.nest.lcdp.framework.common.enums.CommonStatusEnum;
import com.cico.nest.lcdp.framework.common.pojo.CommonResult;
import com.cico.nest.lcdp.framework.common.util.object.BeanUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.cico.nest.lcdp.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 部门")
@RestController
@RequestMapping("/system/dept/v2")
@Validated
public class DeptV2Controller {

    @Resource
    private DeptService deptService;

    @PostMapping("create")
    @Operation(summary = "创建部门")
    @PreAuthorize("@ss.hasPermission('system:dept:create')")
    public CommonResult<String> createDept(@Valid @RequestBody DeptSaveReqV2VO createReqVO) {
        String deptId = deptService.createDeptV2(createReqVO);
        return success(deptId);
    }

    @PutMapping("update")
    @Operation(summary = "更新部门")
    @PreAuthorize("@ss.hasPermission('system:dept:update')")
    public CommonResult<Boolean> updateDept(@Valid @RequestBody DeptSaveReqV2VO updateReqVO) {
        deptService.updateDeptV2(updateReqVO);
        return success(true);
    }

    @DeleteMapping("delete")
    @Operation(summary = "删除部门")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dept:delete')")
    public CommonResult<Boolean> deleteDept(@RequestParam("id") String id) {
        deptService.deleteDept(id);
        return success(true);
    }

    @PostMapping("deleteBatch")
    @Operation(summary = "批量删除部门")
    @PreAuthorize("@ss.hasPermission('system:dept:delete')")
    public CommonResult<Boolean> deleteBatchDept(@RequestBody List<String> ids) {
        deptService.deleteDepts(ids);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取部门列表")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public CommonResult<List<DeptRespVO>> getDeptList(DeptListReqVO reqVO) {
        List<DeptRespVO> list = deptService.getDeptListV2(reqVO);
        return success(list);
    }


    @GetMapping("/sublist")
    @Operation(summary = "获取关联子部门列表")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public CommonResult<List<DeptRespVO>> getSubDeptList(String deptId) {
        List<DeptRespVO> list = deptService.getSubDeptList(deptId);
        return success(list);
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取部门精简信息列表", description = "只包含被开启的部门，主要用于前端的下拉选项")
    public CommonResult<List<DeptSimpleRespVO>> getSimpleDeptList() {
        List<DeptDO> list = deptService.getDeptList(
                new DeptListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(BeanUtils.toBean(list, DeptSimpleRespVO.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得部门信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public CommonResult<DeptRespVO> getDept(@RequestParam("id") String id) {
        DeptDO dept = deptService.getDept(id);
        return success(BeanUtils.toBean(dept, DeptRespVO.class));
    }

}
