package com.casic.missiles.mapper.business;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.dto.business.certificate.CertificateMonitorResponse;
import com.casic.missiles.dto.business.certificate.CertificateReportApprovalRequest;
import com.casic.missiles.dto.business.certificate.CertificateReportResponse;
import com.casic.missiles.dto.customer.CertificationResponse;
import com.casic.missiles.dto.customer.sample.CustomerSampleIdRequest;
import com.casic.missiles.model.business.BusinessCertificateReport;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 业务管理-证书报告 Mapper 接口
 * </p>
 *
 * @author wangpeng
 * @since 2023-02-02
 */
public interface BusinessCertificateReportMapper extends BaseMapper<BusinessCertificateReport> {

    List<CertificateReportResponse> selectDraftListForApproval(@Param("page")Page<CertificateReportResponse> page, @Param("request") CertificateReportApprovalRequest request);

    List<CertificateReportResponse> selectBatchForApproval(@Param("page")Page<CertificateReportResponse> page, @Param("request") CertificateReportApprovalRequest request, @Param("businessKey") List<String> businessKey);

    Page<CertificationResponse>  certificateRecordsById(Page<CertificationResponse> page, @Param("request") CustomerSampleIdRequest customerSampleIdRequest);

    Page<CertificateMonitorResponse>  certificateMonitorsById(Page<CertificateMonitorResponse> page, @Param("request") CustomerSampleIdRequest customerSampleIdRequest);

    @Select("SELECT IFNULL(max(RIGHT(certificate_report_code, 9)), 0) from  business_certificate_report where certificate_report_code like CONCAT(#{prefix},'%')")
    Long selectMaxCode(String prefix);

    BusinessCertificateReport detailById( @Param("id") Long id);
    @MapKey("id")
    List<Map<String,Object>> approvalSignName();

    @Select("SELECT count(id) from  business_certificate_report where approval_status =4 and certificate_report_code = #{sampleId} and order_id=#{orderId}")
    Long selectSampleCertifications(@Param("sampleId") Long sampleId, @Param("orderId") Long orderId);

    @Select("SELECT IFNULL(sum(require_certifications),0) FROM business_lab_executive_info blei where blei.measure_status = 4  and blei.sample_id = #{sampleId} and blei.order_id = #{orderId}")
    Long selectSampleCertificationAlls(@Param("sampleId") Long sampleId, @Param("orderId") Long orderId);


}
