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.position.PositionPageReqVO;
import com.jeelowcode.service.system.controller.vo.dept.position.PositionRespVO;
import com.jeelowcode.service.system.controller.vo.dept.position.PositionSaveReqVO;
import com.jeelowcode.service.system.controller.vo.dept.position.PositionSimpleRespVO;
import com.jeelowcode.service.system.entity.PositionDO;
import com.jeelowcode.service.system.service.IPositionService;
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/position")
@Validated
public class PositionController {

    @Resource
    private IPositionService positionService;

    @PostMapping("/create")
    @Operation(tags = "职位管理",summary = "创建职位")
    @PreAuthorize("@ss.hasPermission('system:position:create')")
    public CommonResult<Long> create(@Valid @RequestBody PositionSaveReqVO createReqVO) {
        Long id = positionService.createPosition(createReqVO);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(tags = "职位管理",summary = "修改职位")
    @PreAuthorize("@ss.hasPermission('system:position:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody PositionSaveReqVO updateReqVO) {
        positionService.updatePosition(updateReqVO);
        return success(true);
    }

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

    @GetMapping(value = "/get")
    @Operation(tags = "职位管理",summary = "获得职位信息")
    @Parameter(name = "id", description = "职位编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:position:query')")
    public CommonResult<PositionRespVO> get(@RequestParam("id") Long id) {
        PositionDO Position = positionService.getPosition(id);
        return success(BeanUtils.toBean(Position, PositionRespVO.class));
    }

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

    @GetMapping("/page")
    @Operation(tags = "职位管理",summary = "获得职位分页列表")
    @PreAuthorize("@ss.hasPermission('system:position:query')")
    public CommonResult<PageResult<PositionRespVO>> getPage(@Validated PositionPageReqVO pageReqVO) {
        PageResult<PositionDO> pageResult = positionService.getPositionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PositionRespVO.class));
    }

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

}
