package cn.iocoder.yudao.module.employee.controller.admin.employeeinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.employee.controller.admin.employeeinfo.vo.EmployeeContractInfoPageReqVO;
import cn.iocoder.yudao.module.employee.controller.admin.employeeinfo.vo.EmployeeContractInfoRespVO;
import cn.iocoder.yudao.module.employee.controller.admin.employeeinfo.vo.EmployeeContractInfoSaveReqVO;
import cn.iocoder.yudao.module.employee.dal.dataobject.employeeinfo.EmployeeContractInfoDO;
import cn.iocoder.yudao.module.employee.service.employeeinfo.EmployeeContractInfoService;
import org.springframework.web.bind.annotation.*;
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 java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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

@Tag(name = "管理后台 - 员工合同信息")
@RestController
@RequestMapping("/presonnel/employee/contract")
@Validated
public class EmployeeContractInfoController {

    @Resource
    private EmployeeContractInfoService contractInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建员工合同信息")
    @PreAuthorize("@ss.hasPermission('employee:contract-info:create')")
    public CommonResult<Long> createContractInfo(@Valid @RequestBody EmployeeContractInfoSaveReqVO createReqVO) {
        return success(contractInfoService.createContractInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新员工合同信息")
    @PreAuthorize("@ss.hasPermission('employee:contract-info:update')")
    public CommonResult<Boolean> updateContractInfo(@Valid @RequestBody EmployeeContractInfoSaveReqVO updateReqVO) {
        contractInfoService.updateContractInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除员工合同信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('employee:contract-info:delete')")
    public CommonResult<Boolean> deleteContractInfo(@RequestParam("id") Long id) {
        contractInfoService.deleteContractInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得员工合同信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('employee:contract-info:query')")
    public CommonResult<EmployeeContractInfoRespVO> getContractInfo(@RequestParam("id") Long id) {
        EmployeeContractInfoDO contractInfo = contractInfoService.getContractInfo(id);
        return success(BeanUtils.toBean(contractInfo, EmployeeContractInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得员工合同信息分页")
    @PreAuthorize("@ss.hasPermission('employee:contract-info:query')")
    public CommonResult<PageResult<EmployeeContractInfoRespVO>> getContractInfoPage(@Valid EmployeeContractInfoPageReqVO pageReqVO) {
        PageResult<EmployeeContractInfoDO> pageResult = contractInfoService.getContractInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EmployeeContractInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出员工合同信息 Excel")
    @PreAuthorize("@ss.hasPermission('employee:contract-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContractInfoExcel(@Valid EmployeeContractInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EmployeeContractInfoDO> list = contractInfoService.getContractInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "员工合同信息.xls", "数据", EmployeeContractInfoRespVO.class,
                        BeanUtils.toBean(list, EmployeeContractInfoRespVO.class));
    }

}