package cn.iocoder.yudao.module.smartfactory.controller.admin.staffcertificateinformation;

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.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffcertificateinformation.vo.StaffCertificateInformationPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffcertificateinformation.vo.StaffCertificateInformationRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffcertificateinformation.vo.StaffCertificateInformationSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.staffcertificateinformation.StaffCertificateInformationDO;
import cn.iocoder.yudao.module.smartfactory.service.staffcertificateinformation.StaffCertificateInformationService;
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("/smartfactory/staff-certificate-information")
@Validated
public class StaffCertificateInformationController {

    @Resource
    private StaffCertificateInformationService staffCertificateInformationService;

    @PostMapping("/create")
    @Operation(summary = "创建叉车员工证件信息")
    @PreAuthorize("@ss.hasPermission('smartfactory:staff-certificate-information:create')")
    public CommonResult<Long> createStaffCertificateInformation(@Valid @RequestBody StaffCertificateInformationSaveReqVO createReqVO) {
        return success(staffCertificateInformationService.createStaffCertificateInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新叉车员工证件信息")
    @PreAuthorize("@ss.hasPermission('smartfactory:staff-certificate-information:update')")
    public CommonResult<Boolean> updateStaffCertificateInformation(@Valid @RequestBody StaffCertificateInformationSaveReqVO updateReqVO) {
        staffCertificateInformationService.updateStaffCertificateInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除叉车员工证件信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('smartfactory:staff-certificate-information:delete')")
    public CommonResult<Boolean> deleteStaffCertificateInformation(@RequestParam("id") Long id) {
        staffCertificateInformationService.deleteStaffCertificateInformation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得叉车员工证件信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:staff-certificate-information:query')")
    public CommonResult<StaffCertificateInformationRespVO> getStaffCertificateInformation(@RequestParam("id") Long id) {
        StaffCertificateInformationDO staffCertificateInformation = staffCertificateInformationService.getStaffCertificateInformation(id);
        return success(BeanUtils.toBean(staffCertificateInformation, StaffCertificateInformationRespVO.class));
    }

    @GetMapping("/getByStaffId")
    @Operation(summary = "通过工厂员工ID获得叉车员工证件信息")
    @Parameter(name = "staffId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:staff-certificate-information:query')")
    public CommonResult<StaffCertificateInformationRespVO> getByStaffId(@RequestParam("staffId") Long staffId) {
        StaffCertificateInformationDO staffCertificateInformation = staffCertificateInformationService.getByStaffId(staffId);
        return success(BeanUtils.toBean(staffCertificateInformation, StaffCertificateInformationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得叉车员工证件信息分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:staff-certificate-information:query')")
    public CommonResult<PageResult<StaffCertificateInformationRespVO>> getStaffCertificateInformationPage(@Valid StaffCertificateInformationPageReqVO pageReqVO) {
        PageResult<StaffCertificateInformationDO> pageResult = staffCertificateInformationService.getStaffCertificateInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StaffCertificateInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出叉车员工证件信息 Excel")
    @PreAuthorize("@ss.hasPermission('smartfactory:staff-certificate-information:export')")
    @OperateLog(type = EXPORT)
    public void exportStaffCertificateInformationExcel(@Valid StaffCertificateInformationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StaffCertificateInformationDO> list = staffCertificateInformationService.getStaffCertificateInformationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "叉车员工证件信息.xls", "数据", StaffCertificateInformationRespVO.class,
                        BeanUtils.toBean(list, StaffCertificateInformationRespVO.class));
    }

}
