<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.casic.missiles.mapper.business.BusinessCertificateReportMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.model.business.BusinessCertificateReport">
        <id column="id" property="id"/>
        <result column="certificate_report_code" property="certificateReportCode"/>
        <result column="certificate_report_name" property="certificateReportName"/>
        <result column="certificate_report_category" property="certificateReportCategory"/>
        <result column="order_id" property="orderId"/>
        <result column="sample_id" property="sampleId"/>
        <result column="calibration_major" property="calibrationMajor"/>
        <result column="issuance_date" property="issuanceDate"/>
        <result column="expiration_date" property="expirationDate"/>
        <result column="template_id" property="templateId"/>
        <result column="sign_id" property="signId"/>
        <result column="approval_sign_id" property="approvalSignId"/>
        <result column="certificate_report_file" property="certificateReportFile"/>
        <result column="original_record_id" property="originalRecordId"/>
        <result column="print_status" property="printStatus"/>
        <result column="print_num" property="printNum"/>
        <result column="remark" property="remark"/>
        <result column="is_del" property="isDel"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        , certificate_report_code, certificate_report_name, certificate_report_category, calibration_major, issuance_date, expiration_date, template_id, sign_id, approval_sign_id, certificate_report_file, original_record_id, print_status, print_num, remark, is_del, create_user, create_time, update_time
    </sql>

    <select id="selectBatchForApprovalList" resultType="com.casic.missiles.dto.business.print.CertificateListResponse">
        SELECT bcr.id,bcr.certificate_report_code AS "certificateNo",bcr.certificate_report_name AS
        "certificateName",csi.sample_no AS "sampleNo",
        csi.sample_name AS "sampleName",csi.sample_model AS "sampleModel",csi.manufacturing_no AS
        "manufacturingNo",bo.order_code AS "orderNo",
        csi.measure_type AS "measureType",bcr.certificate_report_type AS "certificateType",bcr.print_status AS
        "printStatus",bcr.print_num AS "printNum",
        bcr.approval_status AS "approvalStatus",bcr.create_time AS "createTime",bcr.measure_person_id AS
        "measurePersonId"
        FROM (
        SELECT *
        FROM business_certificate_report
        WHERE is_del = 0
        <if test="request.certificateNo != null and request.certificateNo !=''">
            and certificate_report_code like concat('%',#{request.certificateNo},'%')
        </if>
        <if test="request.certificateClass != null and request.certificateClass !=''">
            and certificate_report_category like concat('%',#{request.certificateClass},'%')
        </if>
        <if test="request.printStatus != null and request.printStatus !=''">
            and print_status = #{request.printStatus}
        </if>
        <if test="businessKey != null">
            and bcr.id in
            <foreach collection="businessKey" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="request.ids != null and request.ids.size()>0">
            and bcr.id in
            <foreach collection="request.ids " item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        ) bcr
        JOIN (
        SELECT *
        FROM business_order
        WHERE is_del=0
        <if test="request.orderNo != null and request.orderNo !=''">
            and ORDER_CODE like concat('%',#{request.orderNo},'%')
        </if>
        <if test="request.customerName != null and request.customerName !=''">
            and CUSTOMER_NAME like concat('%',#{request.customerName},'%')
        </if>
        ) bo ON bo.id=bcr.order_id
        JOIN (
        SELECT id,sample_no,sample_name,sample_model,manufacturing_no,measure_type
        FROM customer_sample_info
        WHERE is_del=0
        <if test="request.sampleNo != null and request.sampleNo !=''">
            and SAMPLE_NO like concat('%',#{request.sampleNo},'%')
        </if>
        <if test="request.sampleName != null and request.sampleName !=''">
            and SAMPLE_NAME like concat('%',#{request.sampleName},'%')
        </if>
        ) csi ON csi.id=bcr.sample_id
    </select>


    <select id="selectDraftListForApproval"
            resultType="com.casic.missiles.dto.business.certificate.CertificateReportResponse">
        SELECT
        bcr.*,
        csi.sample_no AS sampleNo,
        csi.sample_name AS sampleName,
        csi.sample_model AS sampleModel,
        csi.manufacturing_no AS manufacturingNo,
        bor.measure_category AS measureCategory,
        bo.order_code AS orderCode,
        bo.customer_name as customerName,
        ms.name AS measurePersonName
        FROM business_certificate_report bcr
        LEFT JOIN business_order bo
        ON bcr.order_id=bo.id
        LEFT JOIN customer_sample_info csi
        ON bcr.sample_id=csi.id
        LEFT JOIN meter_staff ms
        ON bcr.measure_person_id=ms.id
        LEFT JOIN business_original_record bor
        ON bcr.original_record_id=bor.id
        WHERE bcr.is_del = 0 and bor.is_del = 0
        AND approval_status = #{request.approvalStatus}
        AND bcr.create_user = #{request.createUserId}
        <if test="request.certificateReportCode != null and request.certificateReportCode !=''">
            and bcr.certificate_report_code like concat('%',#{request.certificateReportCode},'%')
        </if>
        <if test="request.orderCode != null and request.orderCode !=''">
            and bo.order_code like concat('%',#{request.orderCode},'%')
        </if>
        <if test="request.customerName != null and request.customerName !=''">
            and bo.customer_name like concat('%',#{request.customerName},'%')
        </if>
        <if test="request.sampleNo != null and request.sampleNo !=''">
            and csi.sample_no like concat('%',#{request.sampleNo},'%')
        </if>
        <if test="request.sampleName != null and request.sampleName !=''">
            and csi.sample_name like concat('%',#{request.sampleName},'%')
        </if>
        <if test="request.measureCategory != null and request.measureCategory !=''">
            and bor.measure_category like concat('%',#{request.measureCategory},'%')
        </if>
        <if test="request.ids != null and request.ids.size()>0">
            and bcr.id in
            <foreach collection="request.ids" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>

    </select>

    <select id="selectBatchForApproval"
            resultType="com.casic.missiles.dto.business.certificate.CertificateReportResponse">
        SELECT
        bcr.*,
        csi.sample_no AS sampleNo,
        csi.sample_name AS sampleName,
        csi.sample_model AS sampleModel,
        csi.manufacturing_no AS manufacturingNo,
        bo.order_code AS orderCode,
        bo.customer_name as customerName,
        ms.name AS measurePersonName,
        bor.measure_category AS measureCategory
        FROM business_certificate_report bcr
        LEFT JOIN business_order bo
        ON bcr.order_id=bo.id
        LEFT JOIN customer_sample_info csi
        ON bcr.sample_id=csi.id
        LEFT JOIN meter_staff ms
        ON bcr.measure_person_id=ms.id
        LEFT JOIN business_original_record bor
        ON bcr.original_record_id=bor.id
        WHERE bcr.is_del = 0 and bor.is_del = 0
        <if test="request.certificateReportCode != null and request.certificateReportCode !=''">
            and bcr.certificate_report_code like concat('%',#{request.certificateReportCode},'%')
        </if>
        <if test="request.orderCode != null and request.orderCode !=''">
            and bo.order_code like concat('%',#{request.orderCode},'%')
        </if>
        <if test="request.customerName != null and request.customerName !=''">
            and bo.customer_name like concat('%',#{request.customerName},'%')
        </if>
        <if test="request.sampleNo != null and request.sampleNo !=''">
            and csi.sample_no like concat('%',#{request.sampleNo},'%')
        </if>
        <if test="request.sampleName != null and request.sampleName !=''">
            and csi.sample_name like concat('%',#{request.sampleName},'%')
        </if>
        <if test="request.measureCategory != null and request.measureCategory !=''">
            and bor.measure_category like concat('%',#{request.measureCategory},'%')
        </if>

        <choose>

            <when test="request.ids != null and request.ids.size()>0">
                and bcr.id in
                <foreach collection="request.ids" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </when>

            <otherwise>
                and bcr.id in
                <foreach collection="businessKey" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </otherwise>
        </choose>

    </select>


    <select id="certificateMonitorsById"
            resultType="com.casic.missiles.dto.business.certificate.CertificateMonitorResponse">
        SELECT
        bcr.id AS certificationId,
        bcr.certificate_report_code AS certificateReportCode,
        bcr.certificate_report_name AS certificateReportName,
        bcr.certificate_report_type AS certificationType,
        bcr.submit_date AS submitTime,
        bcr.update_time AS finishedTime,
        bcr.approval_status AS certificateStatus,
        ms.NAME AS createUser
        FROM
        business_certificate_report bcr
        LEFT JOIN meter_staff ms ON bcr.create_user = ms.id
        WHERE
        bcr.is_del =0
        <if test="request.sampleId != null and request.sampleId !=''">
            AND bcr.sample_id=#{request.sampleId}
        </if>
        ORDER BY bcr.update_time DESC
    </select>
    <select id="certificateRecordsById" resultType="com.casic.missiles.dto.customer.CertificationResponse">
        SELECT bcr.id AS "certificationId",certificate_report_code AS "certificationCode",certificate_report_name AS
        "certificationName", certificate_report_category AS "certificationClass",issuance_date AS
        "effectiveDate",expiration_date AS "expirationDate"
        FROM business_certificate_report  bcr
        JOIN (
            select id
            from business_order
            where is_del=0
            <if test="request.customerId != null and request.customerId !=''">
              AND customer_id=#{request.customerId }
            </if>
        )bo ON bo.id=order_id
        WHERE is_del=0
        <if test="request.sampleId != null and request.sampleId !=''">
            AND sample_id=#{request.sampleId}
        </if>
        ORDER BY issuance_date DESC
    </select>


    <select id="detailById" resultType="com.casic.missiles.model.business.BusinessCertificateReport">
        SELECT bcr.*,
               ss.sign_name     AS signName,
               st.template_name AS templateName
        FROM business_certificate_report bcr
                 LEFT JOIN system_sign ss ON bcr.sign_id = ss.id
                 LEFT JOIN system_template st ON bcr.template_id = st.id
        WHERE bcr.is_del = 0
          AND bcr.ID = #{id}
    </select>

    <select id="approvalSignName" resultType="java.util.Map">
        SELECT id,sign_name AS signName
        FROM   system_sign
    </select>

</mapper>
