<?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="cn.wise.sc.cement.business.mapper.SampleHandleMapper">

    <sql id="where">
        <where>
            and s.cement_code = s.parallel_code
            <if test="params.startDate != null and params.startDate != ''">
                and DATE(e.create_time) &gt;= #{params.startDate}
            </if>
            <if test="params.endDate != null and params.endDate != ''">
                and DATE(e.create_time) &lt;= #{params.endDate}
            </if>
            <if test="params.status != null">
                and t.status = #{params.status}
            </if>
            <if test="params.clientId != null">
                and e.client_id = #{params.clientId}
            </if>
            <if test="params.projectName != null and params.projectName != ''">
                and p.name like concat('%', #{params.projectName}, '%')
            </if>
            <if test="params.projectCode != null and params.projectCode != ''">
                and p.code like concat('%', #{params.projectCode}, '%')
            </if>
            <if test="params.userId != null">
                and t.user_id = #{params.userId}
            </if>
        </where>
    </sql>
    <sql id="sampleDisWhere">
        <where>
            e.status > 4 AND h.id IS NOT NULL
            <if test="params.start_time != null and params.start_time != ''">
                and h.finish_time <![CDATA[>=]]> #{params.start_time}
            </if>
            <if test="params.end_time != null and params.end_time != ''">
                and h.finish_time  <![CDATA[<=]]> #{params.end_time}
            </if>
            <if test="params.user_id != null and params.user_id != ''">
                and h.user_id = #{params.user_id}
            </if>
        </where>

    </sql>
    <select id="getSampleHandleList" resultType="cn.wise.sc.cement.business.model.vo.SampleHandleVo">
        select t.*, p.id as projectId, p.name as projectName, p.code as projectCode,
        e.sample_num as sampleNum,
        s.name as sampleName, s.sample_code as sampleCode,s.cement_code as cementCode,
        su.name as userName,
        (
        CASE t.status
        WHEN 0 THEN '未接受'
        WHEN 1 THEN '已接受'
        WHEN 2 THEN '已完成'
        ELSE ''
        END
        ) as statusValue
        from sample_handle t
        left join sys_user su on su.id = t.user_id
        left join sample s on s.id = t.sample_id
        left join entrust e on e.id = s.entrust_id
        left join project p on p.id = e.project_id
        where e.id = #{params.id} and s.cement_code = s.parallel_code
        <if test="params.userId != null">
            and t.user_id = #{params.userId}
        </if>
        order by t.id ASC
    </select>


    <select id="getDetail" resultType="cn.wise.sc.cement.business.model.vo.SampleHandleVo">
        select t.*, su.name as userName, s.name as sampleName, s.sample_code as sampleCode,
        s.cement_code as cementCode, p.id as projectId, p.name as projectName, p.code as projectCode,
        (
        CASE t.status
        WHEN 0 THEN '未接受'
        WHEN 1 THEN '已接受'
        WHEN 2 THEN '已完成'
        ELSE ''
        END
        ) as statusValue
        from sample_handle t
        left join sys_user su on su.id = t.user_id
        left join sample s on s.id = t.sample_id
        left join entrust e on e.id = s.entrust_id
        left join project p on p.id = e.project_id
        where t.id = #{id}
    </select>

    <select id="getEntrustSample" resultType="cn.wise.sc.cement.business.entity.EntrustSample">
        SELECT project_name,entrust_code,h.id as distribution_id ,s.entrust_id,h.user_id,s.id as sample_id,e.status FROM entrust e
        LEFT JOIN
        (SELECT * FROM sample) s
        ON e.id = s.entrust_id
        LEFT JOIN
        (SELECT * FROM sample_handle) h
        ON h.sample_id = s.id
        WHERE e.status > 4 AND h.id IS NOT NULL
    </select>
    <select id="getEntrustSampleOptimize" resultType="cn.wise.sc.cement.business.entity.EntrustSample">

        SELECT project_name,entrust_code,h.id,s.entrust_id,h.user_id,s.id as sample_id,e.status,h.content,h.finish_time
        FROM entrust e
        LEFT JOIN
        (SELECT * FROM sample) s
        ON e.id = s.entrust_id
        LEFT JOIN
        (SELECT * FROM sample_handle) h
        ON h.sample_id = s.id
        <include refid="sampleDisWhere"/>
    </select>


</mapper>