<?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.zhyc.module.biosafety.mapper.SwMedicineUsageMapper">

    <resultMap type="SwMedicineUsage" id="SwMedicineUsageResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="sheepfoldId" column="sheepfold_id"/>
        <result property="sheepfoldName" column="sheepfold_name"/>
        <result property="sheepId" column="sheep_id"/>
        <result property="sheepNo" column="sheep_no"/>
        <result property="datetime" column="datetime"/>
        <result property="useType"    column="use_type"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <resultMap id="SwMedicineUsageSwMedicineUsageDetailsResult" type="SwMedicineUsage" extends="SwMedicineUsageResult">
        <collection property="swMedicineUsageDetailsList" ofType="SwMedicineUsageDetails" column="id" select="selectSwMedicineUsageDetailsList" />
    </resultMap>

    <resultMap type="SwMedicineUsageDetails" id="SwMedicineUsageDetailsResult">
        <result property="id"    column="id"    />
        <result property="mediUsage"    column="medi_usage"    />
        <result property="mediId"    column="medi_id"    />
        <result property="mediName" column="name"/>
        <result property="dosage"    column="dosage"    />
        <result property="unit"    column="unit"    />
        <result property="usageId"    column="usageId"    />
        <result property="usetime"    column="usetime"    />
        <result property="manufacturer"    column="manufacturer"    />
        <result property="batchNumber"    column="batch_number"    />
    </resultMap>

    <sql id="selectSwMedicineUsageVo">
        select smu.id, sheepfold as sheepfold_id,sheep sheep_id,name, datetime,use_type, smu.update_by, smu.update_time, smu.create_by, smu.create_time,
               ds.sheepfold_name,
               bs.manage_tags sheep_no
        from sw_medicine_usage smu
        left join da_sheepfold ds on ds.id = smu.sheepfold
        left join bas_sheep bs on bs.id = smu.sheep
    </sql>

    <select id="selectSwMedicineUsageList" parameterType="SwMedicineUsage" resultMap="SwMedicineUsageResult">
        SELECT
        smu.id,
        smu.sheepfold          AS sheepfold_id,
        smu.sheep              AS sheep_id,
        smu.name,
        smu.datetime,
        smu.use_type,
        smu.update_by,
        smu.update_time,
        smu.create_by,
        smu.create_time,
        ds.sheepfold_name,
        bs.manage_tags         AS sheep_no
        FROM sw_medicine_usage smu
        LEFT JOIN da_sheepfold ds ON ds.id = smu.sheepfold
        LEFT JOIN bas_sheep    bs ON bs.id = smu.sheep
        <where>
            <!-- 根据ids数组查询 -->
            <if test="ids != null and ids.length > 0">
                AND smu.id IN
                <foreach collection="ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <!-- 2. 使用类型 -->
            <if test="useType != null and useType != ''">
                AND smu.use_type = #{useType}
            </if>

            <!-- 3. 羊只耳号 -->
            <if test="sheepNo != null and sheepNo != ''">
                AND bs.manage_tags LIKE CONCAT('%', #{sheepNo}, '%')
            </if>
            <if test="sheepNos != null and sheepNos.length > 0">
                AND (
                <foreach collection="sheepNos" item="item" separator=" OR " open="" close="">
                    bs.manage_tags LIKE CONCAT('%', #{item}, '%')
                </foreach>
                )
            </if>
            <!-- 全部羊多耳号查询（母羊或公羊匹配即可） -->
            <if test="allEarNumbers != null and allEarNumbers.size() > 0">
                AND (
                bs.manage_tags IN
                <foreach collection="allEarNumbers" item="earNumber" open="(" separator="," close=")">
                    #{earNumber}
                </foreach>

                )
            </if>


            <!-- 4. 使用时间区间 -->
            <if test="params.beginUseTime != null and params.endUseTime != null">
                AND smu.datetime BETWEEN #{params.beginUseTime} AND #{params.endUseTime}
            </if>

            <!-- 5. 创建时间区间 -->
            <if test="params.beginCreateTime != null and params.endCreateTime != null">
                AND smu.create_time BETWEEN #{params.beginCreateTime} AND #{params.endCreateTime}
            </if>
            <!-- 按药品名称过滤 -->
            <if test="name != null and name != ''">
                AND EXISTS (
                SELECT 1
                FROM sw_medicine_usage_details mud
                JOIN sw_medicine sm ON sm.id = mud.medi_id
                WHERE mud.medi_usage = smu.id
                AND sm.name LIKE CONCAT('%', #{name}, '%')
                )
            </if>
            ${params.dataScope}
        </where>
        ORDER BY smu.datetime DESC
    </select>

    <select id="selectSwMedicineUsageById" parameterType="Integer" resultMap="SwMedicineUsageSwMedicineUsageDetailsResult">
        select id, name, use_type, update_by, update_time, create_by, create_time
        from sw_medicine_usage
        where id = #{id}
    </select>

    <select id="selectSwMedicineUsageDetailsList" resultMap="SwMedicineUsageDetailsResult">
        select smud.id, medi_usage, medi_id, dosage, unit, usageId,usetime, manufacturer, batch_number,
               sm.name
        from sw_medicine_usage_details smud
        join sw_medicine sm on smud.medi_id = sm.id
        where medi_usage = #{medi_usage}

    </select>
    <select id="selectSwMedicineUsageDetailsById" resultMap="SwMedicineUsageDetailsResult">
        SELECT
            smud.id,
            smud.medi_usage,
            smud.medi_id,
            smud.dosage,
            smud.usetime,
            smud.manufacturer,
            smud.batch_number,
            sm.name AS name,
            su.name AS usageId,
            sunit.unit AS unit
        FROM
            sw_medicine_usage_details smud
                JOIN sw_medicine sm ON smud.medi_id = sm.id
                LEFT JOIN sw_usage su ON smud.usageId = su.id
                LEFT JOIN sw_unit sunit ON smud.unit = sunit.id
        WHERE
            smud.medi_usage = #{id}
    </select>

    <insert id="insertSwMedicineUsage" parameterType="SwMedicineUsage" useGeneratedKeys="true" keyProperty="id">
        insert into sw_medicine_usage
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="useType != null">use_type,</if>
            <if test="sheepfoldId != null">sheepfold,</if>
            <if test="sheepId != null">sheep,</if>
            <if test="datetime != null">datetime,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="userId != null">user_id,</if>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="useType != null">#{useType},</if>
            <if test="sheepfoldId != null">#{sheepfoldId},</if>
            <if test="sheepId != null">#{sheepId},</if>
            <if test="datetime != null">#{datetime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateSwMedicineUsage" parameterType="SwMedicineUsage">
        update sw_medicine_usage
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="useType != null">use_type = #{useType},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSwMedicineUsageById" parameterType="Long">
        delete from sw_medicine_usage where id = #{id}
    </delete>

    <delete id="deleteSwMedicineUsageByIds" parameterType="String">
        delete from sw_medicine_usage where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteSwMedicineUsageDetailsByMediUsages" parameterType="String">
        delete from sw_medicine_usage_details where medi_usage in
        <foreach item="mediUsage" collection="array" open="(" separator="," close=")">
            #{mediUsage}
        </foreach>
    </delete>

    <delete id="deleteSwMedicineUsageDetailsByMediUsage" parameterType="Integer">
        delete from sw_medicine_usage_details where medi_usage = #{mediUsage}
    </delete>


    <!-- 修改后的正确SQL -->
    <insert id="batchSwMedicineUsageDetails" parameterType="java.util.List">
        insert into sw_medicine_usage_details(id, medi_usage, medi_id, dosage, unit, usageId, usetime, manufacturer, batch_number)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.id}, #{item.mediUsage}, #{item.mediId}, #{item.dosage},
            #{item.unit}, #{item.usageId}, #{item.usetime},
            #{item.manufacturer}, #{item.batchNumber})
        </foreach>
    </insert>

</mapper>
