package cn.iocoder.yudao.module.portal.controller.app.jobpost;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.portal.controller.admin.jobpost.vo.JobPostPageReqVO;
import cn.iocoder.yudao.module.portal.controller.admin.jobpost.vo.JobPostRespVO;
import cn.iocoder.yudao.module.portal.controller.admin.jobpost.vo.JobPostSaveReqVO;
import cn.iocoder.yudao.module.portal.dal.dataobject.jobpost.JobPostDO;
import cn.iocoder.yudao.module.portal.service.jobpost.JobPostService;
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.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.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 职位")
@RestController
@RequestMapping("/portal/job-post")
@Validated
public class AppJobPostController {

    @Resource
    private JobPostService jobPostService;

    @PostMapping("/create")
    @Operation(summary = "创建职位")
    public CommonResult<Long> createJobPost(@Valid @RequestBody JobPostSaveReqVO createReqVO) {
        return success(jobPostService.createJobPost(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职位")
    public CommonResult<Boolean> updateJobPost(@Valid @RequestBody JobPostSaveReqVO updateReqVO) {
        jobPostService.updateJobPost(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除职位")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteJobPost(@RequestParam("id") Long id) {
        jobPostService.deleteJobPost(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得职位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<JobPostRespVO> getJobPost(@RequestParam("id") Long id) {
        JobPostDO jobPost = jobPostService.getJobPost(id);
        return success(BeanUtils.toBean(jobPost, JobPostRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得职位分页")
    public CommonResult<PageResult<JobPostRespVO>> getJobPostPage(@Valid JobPostPageReqVO pageReqVO) {
        PageResult<JobPostDO> pageResult = jobPostService.getJobPostPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JobPostRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出职位 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJobPostExcel(@Valid JobPostPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JobPostDO> list = jobPostService.getJobPostPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "职位.xls", "数据", JobPostRespVO.class,
                        BeanUtils.toBean(list, JobPostRespVO.class));
    }

}