<?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="org.jeecg.modules.database.mapper.ExperimentMapper">


    <select id="join" resultType="org.jeecg.modules.database.entity.Experiment">
        select DISTINCT e.*
        from experiment e
        LEFT JOIN experiment_sample_info s ON s.experiment_id = e.id
        left join experiment_deviation_equipment d on d.experiment_id = e.id
        left join experiment_report r on r.experiment_id = e.id
        <where>
            <choose>
                <when test="experiment.pageName == 'audit'">
                    AND ((r.auditor=#{loginUser.id} and r.status='PROOFREAD_PASS') or (r.proofreader=#{loginUser.id} and r.status='INIT')
                    or (r.confirmer=#{loginUser.id} and r.status='AUDIT_PASS' ))
                </when>
                <otherwise>
                    AND ( e.supervisor=#{loginUser.id} or e.create_by=#{loginUser.username} or 'admin'=#{loginUser.username}
                    or (r.auditor=#{loginUser.id} and r.status='PROOFREAD_PASS') or (r.proofreader=#{loginUser.id} and r.status='INIT')
                    or (r.confirmer=#{loginUser.id} and r.status='AUDIT_PASS' ))
                </otherwise>
            </choose>

            <if test="experiment.name != null and experiment.name != ''">
                and e.name like concat('%', #{experiment.name}, '%')
            </if>
            <if test="experiment.clientName != null and experiment.clientName != ''">
                and e.client_name like concat('%', #{experiment.clientName}, '%')
            </if>
            <if test="experiment.type != null and experiment.type != ''">
                and e.type =#{experiment.type}
            </if>
            <if test="experiment.sampleType != null and experiment.sampleType != ''">
                and s.sample_type like concat('%', #{experiment.sampleType}, '%')
            </if>
            <if test="experiment.sampleModel != null and experiment.sampleModel != ''">
                and s.sample_model like concat('%', #{experiment.sampleModel}, '%')
            </if>

            <if test="experiment.sampleName != null and experiment.sampleName != ''">
                and s.sample_name like concat('%', #{experiment.sampleName}, '%')
            </if>

            <if test="experiment.equipmentModel != null and experiment.equipmentModel != ''">
                and d.equipment_model like concat('%', #{experiment.equipmentModel}, '%')
            </if>
            <if test="experiment.startDate != null and experiment.startDate != ''">
                and e.start_date like concat('%', #{experiment.startDate}, '%')
            </if>
            <if test="experiment.status != null and experiment.status != ''">
                and e.status like concat('%', #{experiment.status}, '%')
            </if>

            <if test="experiment.status != null and experiment.status != ''">
                and e.status like concat('%', #{experiment.status}, '%')
            </if>
            <if test="experiment.statusList != null and experiment.statusList != ''">
                and e.status in
                <foreach item="item" collection="experiment.statusList" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>


        </where>
        <if test="loginUser.postText != null and loginUser.postText != ''">
            order by ${loginUser.postText}
        </if>
    </select>
</mapper>