package cn.iocoder.yudao.module.portal.controller.admin.jobcategory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.portal.controller.admin.jobcategory.vo.*;
import cn.iocoder.yudao.module.portal.dal.dataobject.jobcategory.JobCategoryDO;
import cn.iocoder.yudao.module.portal.service.jobcategory.JobCategoryService;

@Tag(name = "管理后台 - 职位类别")
@RestController
@RequestMapping("/portal/job-category")
@Validated
public class JobCategoryController {

    @Resource
    private JobCategoryService jobCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建职位类别")
    @PreAuthorize("@ss.hasPermission('portal:job-category:create')")
    public CommonResult<Long> createJobCategory(@Valid @RequestBody JobCategorySaveReqVO createReqVO) {
        return success(jobCategoryService.createJobCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职位类别")
    @PreAuthorize("@ss.hasPermission('portal:job-category:update')")
    public CommonResult<Boolean> updateJobCategory(@Valid @RequestBody JobCategorySaveReqVO updateReqVO) {
        jobCategoryService.updateJobCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除职位类别")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('portal:job-category:delete')")
    public CommonResult<Boolean> deleteJobCategory(@RequestParam("id") Long id) {
        jobCategoryService.deleteJobCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得职位类别")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('portal:job-category:query')")
    public CommonResult<JobCategoryRespVO> getJobCategory(@RequestParam("id") Long id) {
        JobCategoryDO jobCategory = jobCategoryService.getJobCategory(id);
        return success(BeanUtils.toBean(jobCategory, JobCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得职位类别分页")
    @PreAuthorize("@ss.hasPermission('portal:job-category:query')")
    public CommonResult<PageResult<JobCategoryRespVO>> getJobCategoryPage(@Valid JobCategoryPageReqVO pageReqVO) {
        PageResult<JobCategoryDO> pageResult = jobCategoryService.getJobCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JobCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出职位类别 Excel")
    @PreAuthorize("@ss.hasPermission('portal:job-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJobCategoryExcel(@Valid JobCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JobCategoryDO> list = jobCategoryService.getJobCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "职位类别.xls", "数据", JobCategoryRespVO.class,
                        BeanUtils.toBean(list, JobCategoryRespVO.class));
    }

}