<?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.BusinessCertificatePrintMapper">

<!--    &lt;!&ndash; 通用查询映射结果 &ndash;&gt;-->
<!--    <resultMap id="BaseResultMap" type="com.casic.missiles.model.business.BusinessCertificatePrint">-->
<!--        <id column="id" property="id"/>-->
<!--        <result column="certificate_report_code" property="certificateReportCode"/>-->
<!--        <result column="certificate_report_name" property="certificateReportName"/>-->
<!--        <result column="original_record_id" property="originalRecordId"/>-->
<!--        <result column="certificate_report_category" property="certificateReportCategory"/>-->
<!--        <result column="certificate_report_type" property="certificateReportType"/>-->
<!--        <result column="measure_type" property="measureType"/>-->
<!--        <result column="sample_id" property="sampleId"/>-->
<!--        <result column="order_id" property="orderId"/>-->
<!--        <result column="approval_status" property="approvalStatus"/>-->
<!--        <result column="print_num" property="printNum"/>-->
<!--        <result column="print_status" property="printStatus"/>-->
<!--        <result column="measure_dept_id" property="measureDeptId"/>-->
<!--        <result column="measure_person_id" property="measurePersonId"/>-->
<!--        <result column="approver_user" property="approverUser"/>-->
<!--        <result column="certificate_organization" property="certificateOrganization"/>-->
<!--        <result column="certificate_report_file" property="certificateReportFile"/>-->
<!--        <result column="remark" property="remark"/>-->
<!--        <result column="create_user" property="createUser"/>-->
<!--        <result column="create_time" property="createTime"/>-->
<!--        <result column="is_del" property="isDel"/>-->
<!--    </resultMap>-->

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        bcp.id, certificate_report_code, certificate_report_name, original_record_id, original_record_name, certificate_report_category, certificate_report_type,measure_content,measure_type, sample_id, sample_name, order_id, customer_no, customer_name, customer_phone, customer_email, customer_address, deliverer_id, deliverer, deliverer_tel, plan_deliver_time, require_over_time, approval_status, print_num, print_status, measure_dept_id, measure_person_id,certificate_report_file, remark, create_user, create_time, is_del
        ,sign_id,template_id,process_id
    </sql>

    <select id="selectBatchForApprovalList" resultType="com.casic.missiles.dto.business.print.CertificateListResponse">
        SELECT bcp.id,bcp.certificate_report_code AS "certificateNo",bcp.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",bcp.certificate_report_type AS "certificateType",bcp.print_status AS
            "printStatus",bcp.print_num AS "printNum",bcp.print_file_name AS "printFileName",process_id AS "processId",
            bcp.approval_status AS "approvalStatus",bcp.create_time AS "createTime",bcp.measure_person_id AS "measurePersonId"
        FROM (
            SELECT *
            FROM business_certificate_print
            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.approvalStatus != null and request.approvalStatus  !=''">
                and approval_status =#{request.approvalStatus }
            </if>
            <if test="request.printStatus != null and request.printStatus !=''">
                and print_status = #{request.printStatus}
            </if>
            <if test="businessKey != null">
                and id in
                <foreach collection="businessKey" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="request.ids != null">
                and  id in
                <foreach collection="request.ids " item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
        ) bcp
        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=bcp.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=bcp.sample_id
        ORDER BY bcp.create_time DESC
    </select>

    <select id="selectPrintDetailById" resultType="com.casic.missiles.dto.business.print.CertificatePrintDetail">
       SELECT   <include refid="Base_Column_List"/>
       FROM(
           SELECT  *
           FROM business_certificate_print
           where  is_del=0
           AND id=#{id}
       ) bcp
        LEFT JOIN(
           SELECT id,customer_name,customer_phone,customer_address,customer_no,deliverer_id,deliverer,deliverer_tel,require_over_time,plan_deliver_time,customer_id
           FROM business_order
           WHERE is_del=0
        )bo ON bo.id =order_id
        LEFT JOIN(
            SELECT id,sample_no,sample_name,measure_content,measure_type
            FROM customer_sample_info
            WHERE is_del=0
        )csi ON csi.id =sample_id
        LEFT JOIN(
            SELECT id,email as customer_email
            FROM customer_info
            WHERE is_del=0
        )ci ON ci.id =bo.customer_id
        LEFT JOIN (
            SELECT id,original_Record_Name
            FROM  business_original_record
            WHERE  is_del=0
        )boc ON boc.id=bcp.original_record_id
    </select>
</mapper>
