package com.tashow.cloud.system.controller.admin.dept;

import static com.tashow.cloud.common.pojo.CommonResult.success;

import com.tashow.cloud.common.enums.CommonStatusEnum;
import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import com.tashow.cloud.system.controller.admin.dept.vo.dept.DeptListReqVO;
import com.tashow.cloud.system.controller.admin.dept.vo.dept.DeptRespVO;
import com.tashow.cloud.system.controller.admin.dept.vo.dept.DeptSaveReqVO;
import com.tashow.cloud.system.controller.admin.dept.vo.dept.DeptSimpleRespVO;
import com.tashow.cloud.system.dal.dataobject.dept.DeptDO;
import com.tashow.cloud.system.service.dept.DeptService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/** 管理后台 - 部门 */
@RestController
@RequestMapping("/system/dept")
@Validated
public class DeptController {

  @Resource private DeptService deptService;

  /** 创建部门 */
  @PostMapping("create")
  @PreAuthorize("@ss.hasPermission('system:dept:create')")
  public CommonResult<Long> createDept(@Valid @RequestBody DeptSaveReqVO createReqVO) {
    Long deptId = deptService.createDept(createReqVO);
    return success(deptId);
  }

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

  /** 删除部门 */
  @DeleteMapping("delete")
  @PreAuthorize("@ss.hasPermission('system:dept:delete')")
  public CommonResult<Boolean> deleteDept(@RequestParam("id") Long id) {
    deptService.deleteDept(id);
    return success(true);
  }

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

  /** 获取部门精简信息列表", description = "只包含被开启的部门，主要用于前端的下拉选项 */
  @GetMapping(value = {"/list-all-simple", "/simple-list"})
  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")
  @PreAuthorize("@ss.hasPermission('system:dept:query')")
  public CommonResult<DeptRespVO> getDept(@RequestParam("id") Long id) {
    DeptDO dept = deptService.getDept(id);
    return success(BeanUtils.toBean(dept, DeptRespVO.class));
  }
}
