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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.model.business.BusinessOrderSampleRelation">
        <id column="id" property="id"/>
        <result column="order_id" property="orderId"/>
        <result column="sample_id" property="sampleId"/>
        <result column="sample_status" property="sampleStatus"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        , order_id, sample_id, sample_status
    </sql>

    <!-- 各状态列表查询结果列 -->
    <sql id="Status_Column_List">
        csi.id AS sample_id, csi.sample_no, csi.sample_name, csi.sample_model, csi.manufacturing_no, bo.order_code AS orderNo,
        bo.id AS orderId, bo.customer_id, bo.customer_no, bo.customer_name, bo.deliverer, bo.is_urgent, csi.sample_belong,
        bo.require_over_time, bosr.sample_status, bosr.real_deliver_time, bo.remark
    </sql>

    <!-- 详情查询结果列 -->
    <sql id="Detail_Column_List">
        bosr.sample_id, csi.sample_no, csi.sample_name, csi.sample_model, csi.manufacturing_no, csi.manufacturer, csi.manufacturer_country,
        csi.manufacturing_date, csi.ABC, bo.customer_id, bo.customer_no, bo.customer_name, bo.customer_phone, bo.customer_address, bo.id AS orderId,
        bo.order_code, bo.deliverer, bo.deliverer_tel, bo.plan_deliver_time, bo.require_over_time, bo.certifications, bosr.measure_content, csi.measure_period,
        csi.measure_last_time AS measureLastDate, csi.valid_deadline AS effectiveDate, bosr.remark, csi.minio_file_name, bosr.measure_type, csi.label_bind
    </sql>

    <select id="getSampleListByStatus" resultType="com.casic.missiles.dto.business.dispatch.DeviceDispatchVO">
        SELECT bosr.id, csi.id AS sampleId, csi.sample_no, csi.sample_name, csi.sample_model,
        csi.manufacturing_no, bo.order_code, bo.id AS orderId, bo.customer_id, bo.customer_name,
        bo.customer_no, bo.deliverer, bo.is_urgent, csi.sample_belong, bo.require_over_time,
        bosr.sample_status, bosr.real_deliver_time, csi.measure_type, bo.is_urgent, bo.plan_deliver_time,
        bo.require_over_time, bo.deliverer, bosr.return_time, csi.remark
        FROM
        (
        SELECT *
        FROM business_order bo
        WHERE bo.is_del = 0
        <if test="request.sampleStatus == 8"><!--超期状态，样品在待分发和检测中下的超期-->
            AND bo.require_over_time &lt; NOW()
        </if>
        <if test="request.orderNo != null and request.orderNo !=''">
            AND bo.order_code LIKE concat('%',#{request.orderNo},'%')
        </if>
        <if test="request.customerNo != null and request.customerNo !=''">
            AND bo.customer_no LIKE concat('%',#{request.customerNo},'%')
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            AND bo.plan_deliver_time &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            AND bo.plan_deliver_time &lt; #{request.endTime}
        </if>
        <if test="request.isUrgent != null and request.isUrgent !=2 ">
            AND bo.is_urgent = #{request.isUrgent}
        </if>
        ) bo
        JOIN
        (
        SELECT *
        FROM business_order_sample_relation bosr
        WHERE 1=1
        <if test="request.sampleStatus == 2"><!--超期状态，样品在待分发和检测中下的超期-->
            AND (bosr.sample_status = 2 OR bosr.sample_status = 3 OR bosr.sample_status = 4 )
        </if>
        <if test="request.sampleStatus == 8"><!--超期状态，样品在待分发和检测中下的超期-->
            AND bosr.sample_status = 3
        </if>

        <if test="request.sampleStatus != null and request.sampleStatus != 8  and request.sampleStatus != 2"><!--非超期状态的其它状态-->
            AND bosr.sample_status = #{request.sampleStatus}
        </if>

        <if test="request.ids != null">
            and id in
            <foreach collection="request.ids" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        ) bosr ON bo.id = bosr.order_id
        JOIN (
        SELECT *
        FROM customer_sample_info csi
        WHERE is_del = 0
        <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.sampleBelong != null and request.sampleBelong !=''and request.sampleBelong !=2 ">
            AND csi.sample_belong = #{request.sampleBelong}
        </if>
        ) csi ON csi.id = bosr.sample_id
        ORDER BY bosr.create_time DESC
    </select>


    <select id="isBindLabel" resultType="java.lang.Long">
        SELECT id
        FROM customer_sample_info csi
        WHERE is_del = 0
        <if test="label != null and label !=''">
            and label_bind =#{label}
        </if>
    </select>

    <update id="bindLabel">
         UPDATE  customer_sample_info
         SET label_bind=#{request.label}
         WHERE is_del = 0
         AND  id=#{request.sampleId}
    </update>

    <select id="selectSampleListByStatus"
            resultType="com.casic.missiles.dto.business.labExecutive.SampleHandOutListResponse">
        SELECT
        <include refid="Status_Column_List"/>
        FROM business_order bo
        JOIN business_order_sample_relation bosr ON bo.id = bosr.order_id
        JOIN customer_sample_info csi ON csi.id = bosr.sample_id
        WHERE bo.is_del = 0
        AND bosr.measure_type = 1<!--检定方式：内检-->
        <if test="request.sampleStatus == 8"><!--超期状态，样品在待分发和检测中下的超期-->
            AND (bosr.sample_status = 2 OR bosr.sample_status = 3)
            AND bo.require_over_time &lt; NOW()
        </if>
        <if test="request.sampleStatus != 8"><!--非超期状态的其它状态-->
            AND bosr.sample_status = #{request.sampleStatus}
        </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.orderNo != null and request.orderNo !=''">
            AND bo.order_code LIKE concat('%',#{request.orderNo},'%')
        </if>
        <if test="request.customerNo != null and request.customerNo !=''">
            AND bo.customer_no LIKE concat('%',#{request.customerNo},'%')
        </if>
        <if test="request.customerName != null and request.customerName !=''">
            AND bo.customer_name LIKE concat('%',#{request.customerName},'%')
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            AND bo.require_over_time &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            AND bo.require_over_time &lt; #{request.endTime}
        </if>
        <if test="request.isUrgent != null and request.isUrgent !=''">
            AND bo.is_urgent = #{request.isUrgent}
        </if>
        <if test="request.sampleBelong != null and request.sampleBelong !=''">
            AND csi.sample_belong = #{request.sampleBelong}
        </if>
        <if test="request.ids != null and request.ids.size() > 0">
            AND csi.id IN
            <foreach collection="request.ids" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ORDER BY bosr.create_time DESC
    </select>

    <select id="selectSampleDetail"
            resultType="com.casic.missiles.dto.business.labExecutive.SampleHandOutDetailResponse">
        SELECT
        <include refid="Detail_Column_List"/>
        FROM business_order_sample_relation bosr
        JOIN customer_sample_info csi ON bosr.sample_id = csi.id
        JOIN business_order bo ON bosr.order_id = bo.id
        WHERE bo.is_del = 0 AND csi.is_del = 0
        AND bosr.order_id = #{orderId}
        AND bosr.sample_id = #{sampleId}
    </select>

    <update id="updateByOrderIdAndSampleId">
        UPDATE business_order_sample_relation
        SET sample_status = #{status}
        WHERE order_id = #{orderId}
          AND sample_id = #{sampleId}
    </update>


    <select id="getInterChangeSampleList" resultType="com.casic.missiles.dto.customer.sample.CustomerSampleListVO">
        SELECT sampleNo,
               sampleName,
               sampleModel,
               manufacturingNo,
               appendixDescn,
               id,
               remark,
               sampleId,
               measureContent,
               orderId
        FROM (SELECT sample_no        AS "sampleNo",
                     sample_name      AS "sampleName",
                     sample_model     AS "sampleModel",
                     manufacturing_no AS "manufacturingNo",
                     appendix_descn   AS "appendixDescn",
                     id,
                     remark
              FROM customer_sample_info
              WHERE is_del = 0
                AND id = #{sampleId}) bo
                 JOIN (SELECT measure_content AS "measureContent", sample_id AS "sampleId", order_id AS "orderId"
                       FROM business_order_sample_relation
                       WHERE order_id = #{orderId}
                         AND sample_id = #{sampleId}) bosr ON bosr.sampleId = bo.id
    </select>

    <update id="updateTypeBatchByOrderAndSample">
        <foreach collection="orderSampleDTOS" item="item" separator=";">
            UPDATE business_order_sample_relation
            SET measure_type = 2
            WHERE order_id = #{item.orderId} AND sample_id = #{item.sampleId}
        </foreach>
    </update>

    <update id="updateStatusBatchByOrderAndSample">
        <foreach collection="orderSampleDTOS" item="item" separator=";">
            UPDATE business_order_sample_relation
            SET sample_status = 4
            WHERE order_id = #{item.orderId} AND sample_id = #{item.sampleId}
        </foreach>
    </update>

    <update id="updatePriceBatchById">
        <foreach collection="samplePriceList" item="item" separator=";">
            UPDATE business_order_sample_relation
            SET posted_price = #{item.price}
            WHERE id = #{item.relationId}
        </foreach>
    </update>

    <select id="selectSampleListForSettlement" resultType="com.casic.missiles.dto.finance.SettlementSampleDTO">
        SELECT bosr.id, csi.sample_no, csi.sample_name, csi.sample_model, csi.manufacturing_no, bosr.measure_content, bosr.posted_price
        FROM customer_sample_info csi
        JOIN business_order_sample_relation bosr ON csi.id = bosr.sample_id
        WHERE bosr.order_id = #{orderId}
    </select>

    <select id="selectIdBySampleNo" resultType="com.casic.missiles.dto.business.outsource.OrderSampleDTO">
        SELECT bosr.sample_id, bosr.order_id
        FROM customer_sample_info csi
        JOIN business_order_sample_relation bosr ON csi.id = bosr.sample_id
        WHERE csi.sample_no = #{sampleNo}
        ORDER BY bosr.create_time DESC
        LIMIT 1
    </select>

    <select id="selectSampleMonitorListByStatus"
            resultType="com.casic.missiles.dto.business.workbench.SampleMonitorResponse">
        SELECT bosr.sample_id, csi.sample_no, csi.sample_name, bo.id AS orderId
        FROM business_order bo
        JOIN business_order_sample_relation bosr ON bo.id = bosr.order_id
        JOIN customer_sample_info csi ON csi.id = bosr.sample_id
        WHERE bo.is_del = 0
        AND bosr.measure_type = 1<!--检定方式：内检-->
        AND bosr.sample_status = #{status}
        ORDER BY bosr.create_time DESC
    </select>

    <select id="getSampleListByStatusMonth" resultType="com.casic.missiles.dto.business.dispatch.DeviceDispatchVO">
        SELECT bosr.id, csi.id AS sampleId, csi.sample_no, csi.sample_name, csi.sample_model,
        csi.manufacturing_no, bo.order_code, bo.id AS orderId, bo.customer_id, bo.customer_name,
        bo.customer_no, bo.deliverer, bo.is_urgent, csi.sample_belong, bo.require_over_time,
        bosr.sample_status, bosr.real_deliver_time, csi.measure_type, bo.is_urgent, bo.plan_deliver_time,
        bo.require_over_time, bo.deliverer, bosr.return_time, csi.remark
        FROM
        (
        SELECT *
        FROM business_order bo
        WHERE bo.is_del = 0 AND DATE_FORMAT(bo.plan_deliver_time, '%Y%m') = DATE_FORMAT(CURDATE() ,'%Y%m')
        <if test="request.sampleStatus == 8"><!--超期状态，样品在待分发和检测中下的超期-->
            AND bo.require_over_time &lt; NOW()
        </if>
        <if test="request.orderNo != null and request.orderNo !=''">
            AND bo.order_code LIKE concat('%',#{request.orderNo},'%')
        </if>
        <if test="request.customerNo != null and request.customerNo !=''">
            AND bo.customer_no LIKE concat('%',#{request.customerNo},'%')
        </if>
        <if test="request.isUrgent != null and request.isUrgent !=2 ">
            AND bo.is_urgent = #{request.isUrgent}
        </if>
        ) bo
        JOIN
        (
        SELECT *
        FROM business_order_sample_relation bosr
        WHERE 1=1
        <if test="request.sampleStatus == 2"><!--超期状态，样品在待分发和检测中下的超期-->
            AND (bosr.sample_status = 2 OR bosr.sample_status = 3 OR bosr.sample_status = 4 )
        </if>
        <if test="request.sampleStatus == 8"><!--超期状态，样品在待分发和检测中下的超期-->
            AND bosr.sample_status = 3
        </if>

        <if test="request.sampleStatus != null and request.sampleStatus != 8  and request.sampleStatus != 2"><!--非超期状态的其它状态-->
            AND bosr.sample_status = #{request.sampleStatus}
        </if>

        <if test="request.ids != null">
            and id in
            <foreach collection="request.ids" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        ) bosr ON bo.id = bosr.order_id
        JOIN (
        SELECT *
        FROM customer_sample_info csi
        WHERE is_del = 0
        <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.sampleBelong != null and request.sampleBelong !=''and request.sampleBelong !=2 ">
            AND csi.sample_belong = #{request.sampleBelong}
        </if>
        ) csi ON csi.id = bosr.sample_id
    </select>

    <select id="selectSampleListByStatusMonth"
            resultType="com.casic.missiles.dto.business.labExecutive.SampleHandOutListResponse">
        SELECT
        <include refid="Status_Column_List"/>
        FROM business_order bo
        JOIN business_order_sample_relation bosr ON bo.id = bosr.order_id
        JOIN customer_sample_info csi ON csi.id = bosr.sample_id
        WHERE bo.is_del = 0
        AND bosr.measure_type = 1<!--检定方式：内检-->
        <if test="request.sampleStatus == 8"><!--超期状态，样品在待分发和检测中下的超期-->
            AND (bosr.sample_status = 2 OR bosr.sample_status = 3)
            AND bo.require_over_time &lt; NOW()
        </if>
        <if test="request.sampleStatus != 8"><!--非超期状态的其它状态-->
            AND bosr.sample_status = #{request.sampleStatus}
        </if>
        AND DATE_FORMAT(bo.plan_deliver_time, '%Y%m') = DATE_FORMAT(CURDATE() ,'%Y%m')
        ORDER BY bosr.create_time DESC
    </select>

    <select id="selectCountThisMonthForMeasureWork" resultType="java.lang.Integer">
        SELECT COUNT(bosr.id)
        FROM business_order bo
        JOIN business_order_sample_relation bosr ON bo.id = bosr.order_id
        JOIN customer_sample_info csi ON csi.id = bosr.sample_id
        WHERE bo.is_del = 0
        AND bosr.measure_type = 1<!--检定方式：内检-->
        AND (bosr.sample_status = 4 OR bosr.sample_status = 5 OR bosr.sample_status = 6)
        AND DATE_FORMAT(bosr.update_time, '%Y%m') = DATE_FORMAT(CURDATE() ,'%Y%m')
    </select>

    <select id="selectCountLastMonthForMeasureWork" resultType="java.lang.Integer">
        SELECT COUNT(bosr.id)
        FROM business_order bo
        JOIN business_order_sample_relation bosr ON bo.id = bosr.order_id
        JOIN customer_sample_info csi ON csi.id = bosr.sample_id
        WHERE bo.is_del = 0
        AND bosr.measure_type = 1<!--检定方式：内检-->
        AND (bosr.sample_status = 4 OR bosr.sample_status = 5 OR bosr.sample_status = 6)
        AND DATE_FORMAT(bosr.update_time, '%Y%m') = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y%m')
    </select>

    <select id="selectCountForTrafficStatisticsByMonths"
            resultType="com.casic.missiles.dto.cockpit.TrafficStatisticsResponse">
        WITH RECURSIVE months(months,num) as (
        SELECT DATE_FORMAT((CURDATE()), '%Y-%m') AS 'months' , 0 as num
        UNION ALL
        SELECT DATE_FORMAT((CURDATE() - INTERVAL num+1 MONTH), '%Y-%m') AS 'months', num+1 as num from months where num &lt; #{months}-1
        )
        SELECT m.`months` AS `month`, IFNULL(b.`traffic`, 0) AS `traffic`
        FROM (SELECT months FROM months) AS m
        LEFT JOIN (
            SELECT DATE_FORMAT(bosr.update_time, '%Y-%m') AS `month`, COUNT(bosr.id) AS `traffic`
            FROM business_order bo
            JOIN business_order_sample_relation bosr ON bo.id = bosr.order_id
            JOIN customer_sample_info csi ON csi.id = bosr.sample_id
            WHERE bo.is_del = 0
            AND bosr.measure_type = 1
            AND (bosr.sample_status = 4 OR bosr.sample_status = 5 OR bosr.sample_status = 6)
            AND DATE_FORMAT(bosr.update_time, '%Y%m') &gt;= DATE_FORMAT(DATE_SUB(NOW(), INTERVAL #{months} MONTH), '%Y%m')
            GROUP BY `month`
        ) AS b
        ON m.`months` = b.`month`
    </select>

    <select id="selectCountForCockpit" resultType="java.lang.Integer">
        SELECT COUNT(bosr.id)
        FROM business_order bo
        JOIN business_order_sample_relation bosr ON bo.id = bosr.order_id
        JOIN customer_sample_info csi ON csi.id = bosr.sample_id
        WHERE bo.is_del = 0
        <if test="type == 1">
            AND bosr.sample_status != 6 AND bo.require_over_time &lt; NOW()
        </if>
        <if test="type == 2">
            AND bosr.sample_status = 6 AND bo.require_over_time &lt; NOW()
        </if>
        <if test="type == 3">
            AND bosr.sample_status = 6 AND bo.require_over_time &gt;= NOW()
        </if>
        <if test="type == 4">
            AND bo.require_over_time &gt;= NOW()
        </if>
        <if test="dimension != null and dimension != '' and dimension == 'month'">
            AND DATE_FORMAT(bo.require_over_time, '%Y%m') = DATE_FORMAT(CURDATE( ),'%Y%m')
        </if>
    </select>

    <select id="selectPreMonthCountByYear" resultType="com.casic.missiles.dto.cockpit.BusTrendResponse">
        SELECT DATE_FORMAT(bo.order_time,'%Y-%m') AS dimension, COUNT(bosr.id) AS sampleCount
        FROM business_order bo
        JOIN business_order_sample_relation bosr ON bo.id = bosr.order_id
        JOIN customer_sample_info csi ON csi.id = bosr.sample_id
        WHERE bo.is_del = 0
        AND DATE_SUB(CURDATE(), INTERVAL 365 DAY) &lt;= DATE(bo.order_time)
        GROUP BY DATE_FORMAT(bo.order_time,'%Y-%m')
    </select>

    <select id="selectCountByMonth" resultType="com.casic.missiles.dto.cockpit.WorkRatioResponse">
        SELECT COUNT(bosr.id) AS measuredSampleCount
        FROM business_order bo
        JOIN business_order_sample_relation bosr ON bo.id = bosr.order_id
        JOIN customer_sample_info csi ON csi.id = bosr.sample_id
        WHERE bo.is_del = 0
        AND (bosr.sample_status = 4 OR bosr.sample_status = 5)
        AND DATE_FORMAT(bosr.update_time, '%Y%m') = DATE_FORMAT(#{date},'%Y%m')
    </select>
</mapper>
