<?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.customer.CustomerSampleMapper">

    <select id="selectMaxSampleNo" resultType="Long">
        SELECT IFNULL(MAX(RIGHT (sample_no, 12)), 0)
        FROM customer_sample_info
    </select>

    <select id="measureRecordsById" resultType="com.casic.missiles.dto.customer.MeasureRecordsResponse">
        SELECT order_id AS orderId,order_code AS orderCode,order_time AS orderTime,
        customer_no AS customerCode,customer_name AS customerName,deliverer,real_deliver_time AS deliverTime
        FROM
        (
        SELECT id
        FROM customer_sample_info csi
        WHERE csi.is_del =0
        <if test="request.sampleId != null and request.sampleId !=''">
            AND csi.id=#{request.sampleId}
        </if>
        <if test="request.customerId != null and request.customerId !=''">
            AND csi.customer_id=#{request.customerId }
        </if>
        )csi
        JOIN
        (
        SELECT sample_id,order_id,real_deliver_time
        FROM business_order_sample_relation
        WHERE 1=1
        <if test="request.sampleId != null and request.sampleId !=''">
            AND sample_id=#{request.sampleId}
        </if>
        )bosr ON bosr.sample_id=csi.id
        JOIN (
        SELECT *
        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=bosr.order_id
        ORDER BY bosr.real_deliver_time DESC
    </select>

    <select id="samplesByOderId" resultType="com.casic.missiles.model.customer.CustomerSampleInfo">
        SELECT csi.*
        FROM customer_sample_info csi
        LEFT JOIN business_order_sample_relation sr ON sr.sample_id = csi.id
        WHERE csi.is_del =0
        <if test="sampleNo != null and sampleNo !=''">
            and csi.sample_no like concat('%', #{sampleNo}, '%')
        </if>
        <if test="sampleName != null and sampleName !=''">
            and csi.sample_name like concat('%', #{sampleName}, '%')
        </if>
        <if test="orderId != null and orderId !=''">
            and sr.order_id = #{orderId}
        </if>
        ORDER BY csi.create_time DESC
    </select>

    <select id="listPageByOrder"
            resultType="com.casic.missiles.dto.customer.sample.MultiItemSampleListResponse">
        SELECT id,sampleName,sampleModel,manufacturingNo,appendixDescn,measureContent,remark,customerName,measurePeriod,
        validDeadline,measureLastTime,sampleNo,customerNo,powerVoltage
        FROM (
            SELECT sample_name AS "sampleName",sample_no AS "sampleNo",sample_model AS "sampleModel",manufacturing_no AS
            "manufacturingNo",appendix_descn AS "appendixDescn",remark,customer_name AS "customerName",measure_period AS
            "measurePeriod",valid_deadline AS validDeadline,measure_last_time AS
            "measureLastTime",create_time,id,measure_content AS
            "measureContent",customer_no  AS "customerNo",power_voltage AS "powerVoltage"
            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>
            <if test="request.customerId != null and request.customerId !=''">
                AND customer_id=#{request.customerId}
            </if>
            <if test="request.customerNo != null and request.customerNo !=''">
                AND customer_no=#{request.customerNo}
            </if>
        )csi
        WHERE id NOT IN
        (
            SELECT sample_id
            FROM business_order_sample_relation bosr
            JOIN (
                SELECT id,order_code,customer_no
                FROM business_order
                WHERE is_del=0
                <if test="request.customerId != null and request.customerId !=''">
                    AND customer_id=#{request.customerId}
                </if>
                <if test="request.customerNo != null and request.customerNo !=''">
                    AND customer_no=#{request.customerNo}
                </if>
                <if test="request.orderId!= null and request.orderId !='' ">
                    AND id ! =#{request.orderId}
                </if>
                AND (status ='2' OR status ='1' OR  status ='' )     <!-- 委托书创建，引入的委托书只能是只能为接收状态-->
            )bo ON bo.id=bosr.order_id
        )
    </select>

    <select id="listPageByInterchange"
            resultType="com.casic.missiles.dto.customer.sample.MultiItemSampleListResponse">
        SELECT
        orderId,id,sampleName,sampleModel,manufacturingNo,appendixDescn,measureContent,remark,sampleStatus,customerName,measurePeriod,
        validDeadline,measureLastTime,sampleNo,orderCode,customerNo
        FROM (
            SELECT sample_name AS "sampleName",sample_no AS "sampleNo",sample_model AS "sampleModel",manufacturing_no AS
            "manufacturingNo",appendix_descn AS "appendixDescn",remark,customer_name AS "customerName",measure_period AS
            "measurePeriod",id,
            valid_deadline AS validDeadline,measure_last_time AS "measureLastTime",create_time,measure_content AS
            "measureContent",customer_no  AS  "customerNo"
            FROM customer_sample_info
            WHERE customer_id=#{request.customerId}
            <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
        JOIN (
        SELECT sample_status AS "sampleStatus",sample_id as "sampleId",order_id AS "orderId",order_code AS "orderCode"
        FROM business_order_sample_relation bosr
        JOIN (
        SELECT id,order_code
        FROM business_order
        WHERE customer_id=#{request.customerId}
        )bo ON bo.id=bosr.order_id
        WHERE interchange_id is null
        )bor ON bor.sampleId=csi.id
        ORDER BY create_time DESC
    </select>

    <select id="listPageBySubpackage"
            resultType="com.casic.missiles.dto.customer.sample.MultiItemSampleListResponse">
        SELECT
        orderId,id,sampleName,sampleModel,manufacturingNo,appendixDescn,measureContent,remark,sampleStatus,customerName,measurePeriod,
        validDeadline,measureLastTime,sampleNo,orderCode,customerNo
        FROM (
            SELECT sample_name AS "sampleName",sample_no AS "sampleNo",sample_model AS "sampleModel",manufacturing_no AS
            "manufacturingNo",appendix_descn AS "appendixDescn",remark,customer_name AS "customerName",measure_period AS
            "measurePeriod",valid_deadline AS validDeadline,measure_last_time AS
            "measureLastTime",create_time,id,measure_content AS
            "measureContent",
            customer_no AS "customerNo"
            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>
            <if test="request.customerNo != null and request.customerNo !=''">
                AND customer_no=#{request.customerNo}
            </if>
        )csi
        JOIN (
            SELECT sample_status AS "sampleStatus",sample_id as "sampleId",order_id AS "orderId",order_code AS "orderCode"
            FROM business_order_sample_relation bosr
            JOIN (
                SELECT id,order_code,customer_no
                FROM business_order
                WHERE is_del=0
                <if test="request.orderCode != null and request.orderCode !=''">
                    AND order_code=#{request.orderCode}
                </if>
                <if test="request.customerName != null and request.customerName !=''">
                    AND customer_name=#{request.customerName}
                </if>
                <if test="request.startTime != null and request.startTime !=''">
                    AND create_time <![CDATA[ >= ]]> #{request.startTime}
                </if>
                <if test="request.endTime != null and request.endTime !=''">
                    AND create_time <![CDATA[ <= ]]> #{request.endTime}
                </if>
                AND status='2'     <!-- 委托书状态只能为接收状态-->
            )bo ON bo.id=bosr.order_id
            WHERE (sample_status ='1' OR sample_status ='2')      <!-- 样品状态只能为待收入、待分发状态-->
        AND sample_id NOT IN  <include refid="Sample_Testing"/>   <!-- 样品在分包、委托书创建状态不在实验室使用状态-->
        )bor ON bor.sampleId=csi.id
    </select>

    <select id="listPageBySubpackageCertificate"
            resultType="com.casic.missiles.dto.customer.sample.MultiItemSampleListResponse">
        SELECT
        orderId,id,sampleName,sampleModel,manufacturingNo,appendixDescn,measureContent,remark,sampleStatus,customerName,measurePeriod,
        validDeadline,measureLastTime,sampleNo,orderCode,customerNo
        FROM (
            SELECT sample_name AS "sampleName",sample_no AS "sampleNo",sample_model AS "sampleModel",manufacturing_no AS
            "manufacturingNo",appendix_descn AS "appendixDescn",remark,customer_name AS "customerName",measure_period AS
            "measurePeriod",id,
            valid_deadline AS validDeadline,measure_last_time AS "measureLastTime",create_time,measure_content AS
            "measureContent",customer_no  AS  "customerNo"
            FROM customer_sample_info
            <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
        JOIN (
            SELECT sample_status AS "sampleStatus",sample_id as "sampleId",order_id AS "orderId",order_code AS "orderCode"
            FROM business_order_sample_relation bosr
            JOIN (
                SELECT id,order_code
                FROM business_order
             )bo ON bo.id=bosr.order_id
        )bor ON bor.sampleId=csi.id
        ORDER BY create_time DESC
    </select>

    <sql id="Sample_Testing">
        (
            SELECT sample_id
			 FROM (
					 SELECT sample_id,measure_status,Max(create_time)
					 FROM business_lab_executive_info
					 GROUP BY sample_id,measure_status
				  ) bo
              WHERE measure_status= '5'
        )
    </sql>

</mapper>
