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

import static com.tashow.cloud.common.pojo.CommonResult.success;
import static com.tashow.cloud.web.apilog.core.enums.OperateTypeEnum.EXPORT;

import com.tashow.cloud.common.enums.CommonStatusEnum;
import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageParam;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import com.tashow.cloud.excel.excel.core.util.ExcelUtils;
import com.tashow.cloud.system.controller.admin.dept.vo.post.PostPageReqVO;
import com.tashow.cloud.system.controller.admin.dept.vo.post.PostRespVO;
import com.tashow.cloud.system.controller.admin.dept.vo.post.PostSaveReqVO;
import com.tashow.cloud.system.controller.admin.dept.vo.post.PostSimpleRespVO;
import com.tashow.cloud.system.dal.dataobject.dept.PostDO;
import com.tashow.cloud.system.service.dept.PostService;
import com.tashow.cloud.web.apilog.core.annotation.ApiAccessLog;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
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/post")
@Validated
public class PostController {

  @Resource private PostService postService;

  /** 创建岗位 */
  @PostMapping("/create")
  @PreAuthorize("@ss.hasPermission('system:post:create')")
  public CommonResult<Long> createPost(@Valid @RequestBody PostSaveReqVO createReqVO) {
    Long postId = postService.createPost(createReqVO);
    return success(postId);
  }

  /** 修改岗位 */
  @PutMapping("/update")
  @PreAuthorize("@ss.hasPermission('system:post:update')")
  public CommonResult<Boolean> updatePost(@Valid @RequestBody PostSaveReqVO updateReqVO) {
    postService.updatePost(updateReqVO);
    return success(true);
  }

  /** 删除岗位 */
  @DeleteMapping("/delete")
  @PreAuthorize("@ss.hasPermission('system:post:delete')")
  public CommonResult<Boolean> deletePost(@RequestParam("id") Long id) {
    postService.deletePost(id);
    return success(true);
  }

  /** 获得岗位信息 */
  @GetMapping(value = "/get")
  @PreAuthorize("@ss.hasPermission('system:post:query')")
  public CommonResult<PostRespVO> getPost(@RequestParam("id") Long id) {
    PostDO post = postService.getPost(id);
    return success(BeanUtils.toBean(post, PostRespVO.class));
  }

  /** 获取岗位全列表", description = "只包含被开启的岗位，主要用于前端的下拉选项 */
  @GetMapping(value = {"/list-all-simple", "simple-list"})
  public CommonResult<List<PostSimpleRespVO>> getSimplePostList() {
    // 获得岗位列表，只要开启状态的
    List<PostDO> list =
        postService.getPostList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
    // 排序后，返回给前端
    list.sort(Comparator.comparing(PostDO::getSort));
    return success(BeanUtils.toBean(list, PostSimpleRespVO.class));
  }

  /** 获得岗位分页列表 */
  @GetMapping("/page")
  @PreAuthorize("@ss.hasPermission('system:post:query')")
  public CommonResult<PageResult<PostRespVO>> getPostPage(@Validated PostPageReqVO pageReqVO) {
    PageResult<PostDO> pageResult = postService.getPostPage(pageReqVO);
    return success(BeanUtils.toBean(pageResult, PostRespVO.class));
  }

  /** 岗位管理 */
  @GetMapping("/export")
  @PreAuthorize("@ss.hasPermission('system:post:export')")
  @ApiAccessLog(operateType = EXPORT)
  public void export(HttpServletResponse response, @Validated PostPageReqVO reqVO)
      throws IOException {
    reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
    List<PostDO> list = postService.getPostPage(reqVO).getList();
    // 输出
    ExcelUtils.write(
        response, "岗位数据.xls", "岗位列表", PostRespVO.class, BeanUtils.toBean(list, PostRespVO.class));
  }
}
