package cn.iocoder.yudao.module.system.controller.admin.position;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.position.vo.PositionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.position.vo.PositionRespVO;
import cn.iocoder.yudao.module.system.controller.admin.position.vo.PositionSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.position.PositionDO;
import cn.iocoder.yudao.module.system.service.position.PositionService;
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.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 岗位信息")
@RestController
@RequestMapping("/system/position")
@Validated
public class PositionController {

    @Resource
    private PositionService positionService;

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

    @PutMapping("/update")
    @Operation(summary = "更新岗位信息")
    @PreAuthorize("@ss.hasPermission('system:position:update')")
    public CommonResult<Boolean> updatePosition(@Valid @RequestBody PositionSaveReqVO updateReqVO) {
        positionService.updatePosition(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除岗位信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:position:delete')")
    public CommonResult<Boolean> deletePosition(@RequestParam("id") Long id) {
        positionService.deletePosition(id);
        return success(true);
    }

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

    @GetMapping("/get-list")
    @Operation(summary = "获得岗位信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:position:query')")
    @DataPermission(enable = false)
    public CommonResult<List<PositionRespVO>> getPositionList() {
        List<PositionDO> positions = positionService.getPositionList();
        return success(BeanUtils.toBean(positions, PositionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得岗位信息分页")
    @PreAuthorize("@ss.hasPermission('system:position:query')")
    public CommonResult<PageResult<PositionRespVO>> getPositionPage(@Valid PositionPageReqVO pageReqVO) {
        PageResult<PositionDO> pageResult = positionService.getPositionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PositionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出岗位信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:position:export')")
    @OperateLog(type = EXPORT)
    public void exportPositionExcel(@Valid PositionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PositionDO> list = positionService.getPositionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "岗位信息.xls", "数据", PositionRespVO.class,
                        BeanUtils.toBean(list, PositionRespVO.class));
    }

}