package cn.iocoder.yudao.module.supplier.controller.admin.product;

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.supplier.controller.admin.product.vo.CertificatePageReqVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.CertificateRespVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.CertificateSaveReqVO;
import cn.iocoder.yudao.module.supplier.dal.dataobject.product.CertificateDO;
import cn.iocoder.yudao.module.supplier.service.product.CertificateService;
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.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 供应商证件资质")
@RestController
@RequestMapping("/supplier/certificate")
@Validated
public class CertificateController {

    @Resource
    private CertificateService certificateService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商证件资质")
    @PreAuthorize("@ss.hasPermission('supplier:certificate:create')")
    public CommonResult<Long> createCertificate(@Valid @RequestBody CertificateSaveReqVO createReqVO) {
        return success(certificateService.createCertificate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商证件资质")
    @PreAuthorize("@ss.hasPermission('supplier:certificate:update')")
    public CommonResult<Boolean> updateCertificate(@Valid @RequestBody CertificateSaveReqVO updateReqVO) {
        certificateService.updateCertificate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商证件资质")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('supplier:certificate:delete')")
    public CommonResult<Boolean> deleteCertificate(@RequestParam("id") Long id) {
        certificateService.deleteCertificate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商证件资质")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('supplier:certificate:query')")
    public CommonResult<CertificateRespVO> getCertificate(@RequestParam("id") Long id) {
        CertificateDO certificate = certificateService.getCertificate(id);
        return success(BeanUtils.toBean(certificate, CertificateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商证件资质分页")
    @PreAuthorize("@ss.hasPermission('supplier:certificate:query')")
    public CommonResult<PageResult<CertificateRespVO>> getCertificatePage(@Valid CertificatePageReqVO pageReqVO) {
        PageResult<CertificateDO> pageResult = certificateService.getCertificatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CertificateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商证件资质 Excel")
    @PreAuthorize("@ss.hasPermission('supplier:certificate:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCertificateExcel(@Valid CertificatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CertificateDO> list = certificateService.getCertificatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商证件资质.xls", "数据", CertificateRespVO.class,
                        BeanUtils.toBean(list, CertificateRespVO.class));
    }

}
