package com.jeelowcode.service.system.controller;

import com.jeelowcode.tool.framework.common.enums.CommonStatusEnum;
import com.jeelowcode.tool.framework.common.pojo.CommonResult;
import com.jeelowcode.tool.framework.common.pojo.PageParam;
import com.jeelowcode.tool.framework.common.pojo.PageResult;
import com.jeelowcode.tool.framework.common.util.object.BeanUtils;
import com.jeelowcode.tool.framework.excel.core.util.ExcelUtils;
import com.jeelowcode.tool.framework.operatelog.core.annotations.OperateLog;
import com.jeelowcode.service.system.controller.vo.dept.rank.RankPageReqVO;
import com.jeelowcode.service.system.controller.vo.dept.rank.RankRespVO;
import com.jeelowcode.service.system.controller.vo.dept.rank.RankSaveReqVO;
import com.jeelowcode.service.system.controller.vo.dept.rank.RankSimpleRespVO;
import com.jeelowcode.service.system.entity.RankDO;
import com.jeelowcode.service.system.service.IRankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.jeelowcode.tool.framework.common.pojo.CommonResult.success;
import static com.jeelowcode.tool.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 职级")
@RestController
@RequestMapping("/system/rank")
@Validated
public class RankController {

    @Resource
    private IRankService rankService;

    @PostMapping("/create")
    @Operation(tags = "职级管理",summary = "创建职级")
    @PreAuthorize("@ss.hasPermission('system:rank:create')")
    public CommonResult<Long> create(@Valid @RequestBody RankSaveReqVO createReqVO) {
        Long id = rankService.createRank(createReqVO);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(tags = "职级管理",summary = "修改职级")
    @PreAuthorize("@ss.hasPermission('system:rank:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody RankSaveReqVO updateReqVO) {
        rankService.updateRank(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(tags = "职级管理",summary = "删除职级")
    @PreAuthorize("@ss.hasPermission('system:rank:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        rankService.deleteRank(id);
        return success(true);
    }

    @GetMapping(value = "/get")
    @Operation(tags = "职级管理",summary = "获得职级信息")
    @Parameter(name = "id", description = "职级编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:rank:query')")
    public CommonResult<RankRespVO> get(@RequestParam("id") Long id) {
        RankDO Rank = rankService.getRank(id);
        return success(BeanUtils.toBean(Rank, RankRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "simple-list"})
    @Operation(tags = "职级管理",summary = "获取职级全列表", description = "只包含被开启的职级，主要用于前端的下拉选项")
    public CommonResult<List<RankSimpleRespVO>> getSimpleList() {
        // 获得职级列表，只要开启状态的
        List<RankDO> list = rankService.getRankList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(RankDO::getSort));
        return success(BeanUtils.toBean(list, RankSimpleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(tags = "职级管理",summary = "获得职级分页列表")
    @PreAuthorize("@ss.hasPermission('system:rank:query')")
    public CommonResult<PageResult<RankRespVO>> getPage(@Validated RankPageReqVO pageReqVO) {
        PageResult<RankDO> pageResult = rankService.getRankPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RankRespVO.class));
    }

    @GetMapping("/export")
    @Operation(tags = "职级管理",summary = "职级管理")
    @PreAuthorize("@ss.hasPermission('system:rank:export')")
    @OperateLog(type = EXPORT)
    public void export(HttpServletResponse response, @Validated RankPageReqVO reqVO) throws IOException {
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RankDO> list = rankService.getRankPage(reqVO).getList();
        // 输出
        ExcelUtils.write(response, "职级数据.xls", "职级列表", RankRespVO.class,
                BeanUtils.toBean(list, RankRespVO.class));
    }

}
