<?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.ruoyi.app.mapper.HarmlessTreatmentMapper">
    
    <resultMap type="HarmlessTreatment" id="HarmlessTreatmentResult">
        <result property="id"    column="id"    />
        <result property="entranceBatchId"    column="entrance_batch_id"    />
        <result property="illnessDeadAmount"    column="illness_dead_amount"    />
        <result property="illnessDiseaseAmount"    column="illness_disease_amount"    />
        <result property="snapKillAmount"    column="snap_kill_amount"    />
        <result property="unqualifiedWeight"    column="unqualified_weight"    />
        <result property="illnessPigsDetail"    column="illness_pigs_detail"    />
        <result property="dealReason"    column="deal_reason"    />
        <result property="dealWay"    column="deal_way"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <resultMap type="com.ruoyi.app.DTO.HarmlessSumDTO" id="HarmlessSumDTOResult">
        <result property="illnessDeadAmountSum"    column="illness_dead_amount_sum"    />
        <result property="illnessDiseaseAmountSum"    column="illness_disease_amount_sum"    />
        <result property="unqualifiedWeightSum"    column="unqualified_weight_sum"    />
        <result property="snapKillAmountSum"    column="snap_kill_amount_sum"    />
    </resultMap>

    <sql id="selectHarmlessTreatmentVo">
        select id, entrance_batch_id, illness_dead_amount, illness_disease_amount, snap_kill_amount, unqualified_weight, illness_pigs_detail, deal_reason, deal_way, create_time, create_by, update_time, update_by, del_flag from harmless_treatment
    </sql>

    <select id="selectHarmlessTreatmentList" parameterType="HarmlessTreatment" resultMap="HarmlessTreatmentResult">
        <include refid="selectHarmlessTreatmentVo"/>
        <where>
            del_flag = '0'
            <if test="entranceBatchId != null "> and entrance_batch_id = #{entranceBatchId}</if>
            <if test="illnessDeadAmount != null "> and illness_dead_amount = #{illnessDeadAmount}</if>
            <if test="illnessDiseaseAmount != null "> and illness_disease_amount = #{illnessDiseaseAmount}</if>
            <if test="snapKillAmount != null "> and snap_kill_amount = #{snapKillAmount}</if>
            <if test="unqualifiedWeight != null "> and unqualified_weight = #{unqualifiedWeight}</if>
            <if test="illnessPigsDetail != null  and illnessPigsDetail != ''"> and illness_pigs_detail = #{illnessPigsDetail}</if>
            <if test="dealReason != null  and dealReason != ''"> and deal_reason = #{dealReason}</if>
            <if test="dealWay != null  and dealWay != ''"> and deal_way = #{dealWay}</if>
        </where>
    </select>
    
    <select id="selectHarmlessTreatmentById" parameterType="Long" resultMap="HarmlessTreatmentResult">
        <include refid="selectHarmlessTreatmentVo"/>
        where entrance_batch_id = #{id} and del_flag = '0'
    </select>

    <insert id="insertHarmlessTreatment" parameterType="HarmlessTreatment" useGeneratedKeys="true" keyProperty="id">
        insert into harmless_treatment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="entranceBatchId != null">entrance_batch_id,</if>
            <if test="illnessDeadAmount != null">illness_dead_amount,</if>
            <if test="illnessDiseaseAmount != null">illness_disease_amount,</if>
            <if test="snapKillAmount != null">snap_kill_amount,</if>
            <if test="unqualifiedWeight != null">unqualified_weight,</if>
            <if test="illnessPigsDetail != null">illness_pigs_detail,</if>
            <if test="dealReason != null">deal_reason,</if>
            <if test="dealWay != null">deal_way,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            del_flag
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="entranceBatchId != null">#{entranceBatchId},</if>
            <if test="illnessDeadAmount != null">#{illnessDeadAmount},</if>
            <if test="illnessDiseaseAmount != null">#{illnessDiseaseAmount},</if>
            <if test="snapKillAmount != null">#{snapKillAmount},</if>
            <if test="unqualifiedWeight != null">#{unqualifiedWeight},</if>
            <if test="illnessPigsDetail != null">#{illnessPigsDetail},</if>
            <if test="dealReason != null">#{dealReason},</if>
            <if test="dealWay != null">#{dealWay},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <update id="updateHarmlessTreatment" parameterType="HarmlessTreatment">
        update harmless_treatment
        <trim prefix="SET" suffixOverrides=",">
            <if test="entranceBatchId != null">entrance_batch_id = #{entranceBatchId},</if>
            <if test="illnessDeadAmount != null">illness_dead_amount = #{illnessDeadAmount},</if>
            <if test="illnessDiseaseAmount != null">illness_disease_amount = #{illnessDiseaseAmount},</if>
            <if test="snapKillAmount != null">snap_kill_amount = #{snapKillAmount},</if>
            <if test="unqualifiedWeight != null">unqualified_weight = #{unqualifiedWeight},</if>
            <if test="illnessPigsDetail != null">illness_pigs_detail = #{illnessPigsDetail},</if>
            <if test="dealReason != null">deal_reason = #{dealReason},</if>
            <if test="dealWay != null">deal_way = #{dealWay},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where entrance_batch_id = #{entranceBatchId} and del_flag = '0'
    </update>

    <delete id="deleteHarmlessTreatmentById" parameterType="Long">
        update harmless_treatment set del_flag = '2' where entrance_batch_id = #{id}
    </delete>

    <delete id="deleteHarmlessTreatmentByIds" parameterType="String">
        update harmless_treatment set del_flag = '2' where entrance_batch_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!--根据时间范围获取死淘数量-->
    <select id="selectHarmlessSumByTime" parameterType="com.ruoyi.app.domain.request.TimeRangeParam" resultMap="HarmlessSumDTOResult">
        select IFNULL(sum(illness_dead_amount),0) as illness_dead_amount_sum,IFNULL(sum(illness_disease_amount),0) as illness_disease_amount_sum,
        IFNULL(sum(snap_kill_amount),0) as snap_kill_amount_sum,IFNULL(sum(unqualified_weight),0) as unqualified_weight_sum
        from harmless_treatment as a
        left join entrance_batch as b on b.id = a.entrance_batch_id
        <where>
            a.del_flag = '0' and b.del_flag = '0'
            <if test="startTime != null "> and b.entrance_time <![CDATA[ >= ]]> #{startTime}</if>
            <if test="endTime != null "> and b.entrance_time <![CDATA[ <= ]]> #{endTime}</if>
        </where>
    </select>
</mapper>