<?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.HookBindMapper">
    
    <resultMap type="HookBind" id="HookBindResult">
        <result property="id"    column="id"    />
        <result property="batchNo"    column="batch_no"    />
        <result property="hookNo"    column="hook_no"    />
        <result property="slaughterCode"    column="slaughter_code"    />
        <result property="distributeBatchId"    column="distribute_batch_id"    />
        <result property="isBind"    column="is_bind"    />
        <result property="bindTime"    column="bind_time"    />
        <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 id="HookBindBatchListResult" type="com.ruoyi.app.DTO.HookBindBatchListDTO">
        <result property="batchNo"    column="batch_no"    />
        <result property="slaughterCode"    column="slaughter_code"    />
        <result property="hookNoList"    column="hook_no_list"    />
        <result property="total"    column="total"    />
        <result property="bindTime"    column="bind_time"    />
        <result property="purchaserName"    column="purchaser_name"    />
        <result property="supplierName"    column="supplier_name"    />
        <result property="animalCertNo"    column="animal_cert_no"    />
        <result property="distributeBatchId"    column="distribute_batch_id"    />
    </resultMap>
    <resultMap id="HookBindDetailResult" type="com.ruoyi.app.DTO.HookBindDetailDTO">
        <result property="slaughterCode"    column="slaughter_code"    />
        <result property="hookNo"    column="hook_no"    />
        <result property="distributeBatchId"    column="distribute_batch_id"    />
        <result property="entranceBatchId"    column="entrance_batch_id"    />
        <result property="purchaserName"    column="purchaser_name"    />
        <result property="supplierName"    column="supplier_name"    />
        <result property="animalCertNo"    column="animal_cert_no"    />
    </resultMap>

    <sql id="selectHookBindVo">
        select id, batch_no,hook_no, slaughter_code,distribute_batch_id, is_bind,bind_time, create_time, create_by, update_time, update_by, del_flag from hook_bind
    </sql>

    <select id="selectHookBindList" parameterType="HookBind" resultMap="HookBindResult">
        <include refid="selectHookBindVo"/>
        <where>

            <if test="hookNo != null  and hookNo != ''"> and hook_no = #{hookNo}</if>
            <if test="distributeBatchId != null "> and distribute_batch_id = #{distributeBatchId}</if>
            <if test="isBind != null  and isBind != ''"> and is_bind = #{isBind}</if>
        </where>
    </select>

    <select id="selectHookBindListByBatch" parameterType="HookBind" resultMap="HookBindBatchListResult">
        select a.batch_no,a.slaughter_code,GROUP_CONCAT(a.hook_no SEPARATOR ',') as hook_no_list,COUNT(a.id) AS total,a.bind_time,
               c.purchaser_name,d.supplier_name,e.animal_cert_no
        from hook_bind as a
        left join distribute_batch as b on b.id = a.distribute_batch_id and b.del_flag = '0'
        left join purchaser as c on c.id = b.purchaser_id and c.del_flag = '0'
        left join supplier as d on d.id = b.supplier_id and d.del_flag = '0'
        left join entrance_batch as e on e.id = b.entrance_batch_id and e.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="slaughterCode != null  and slaughterCode != ''"> and a.slaughter_code like concat('%', #{slaughterCode}, '%')</if>
        </where>
        group by a.batch_no,a.slaughter_code,a.distribute_batch_id,a.bind_time
    </select>
    
    <select id="selectHookBindByBatch" parameterType="String" resultMap="HookBindBatchListResult">
        select a.batch_no,a.slaughter_code,a.distribute_batch_id,GROUP_CONCAT(a.hook_no SEPARATOR ',') as hook_no_list,COUNT(a.id) AS total,a.bind_time,
               c.purchaser_name,d.supplier_name,e.animal_cert_no
        from hook_bind as a
                 left join distribute_batch as b on b.id = a.distribute_batch_id and b.del_flag = '0'
                 left join purchaser as c on c.id = b.purchaser_id and c.del_flag = '0'
                 left join supplier as d on d.id = b.supplier_id and d.del_flag = '0'
                 left join entrance_batch as e on e.id = b.entrance_batch_id and e.del_flag = '0'
        where a.batch_no = #{batchNo} and a.del_flag = '0'
        group by a.batch_no,a.slaughter_code,a.distribute_batch_id,a.bind_time
        limit 1
    </select>

        <select id="selectHookBindDetail" parameterType="String" resultMap="HookBindDetailResult">
        select a.slaughter_code,a.hook_no,a.distribute_batch_id,c.purchaser_name,b.entrance_batch_id,d.supplier_name,e.animal_cert_no
        from hook_bind as a
                 left join distribute_batch as b on b.id = a.distribute_batch_id and b.del_flag = '0'
                 left join purchaser as c on c.id = b.purchaser_id and c.del_flag = '0'
                 left join supplier as d on d.id = b.supplier_id and d.del_flag = '0'
                 left join entrance_batch as e on e.id = b.entrance_batch_id and e.del_flag = '0'
        where a.hook_no = #{hookNo} and a.del_flag = '0'
        order by a.create_time desc limit 1
    </select>

    <!--判断相同时间下该批次号是否已绑定-->
    <select id="checkHookNoUnique" parameterType="HookBind"  resultMap="HookBindResult">
        select id,batch_no, hook_no from hook_bind
        where hook_no = #{hookNo} and bind_time = #{bindTime} and del_flag = '0'
        limit 1
    </select>

    <insert id="insertHookBind" parameterType="HookBind" useGeneratedKeys="true" keyProperty="id">
        insert into hook_bind
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="batchNo != null and batchNo != ''">batch_no,</if>
            <if test="hookNo != null and hookNo != ''">hook_no,</if>
            <if test="slaughterCode != null and slaughterCode != ''">slaughter_code,</if>
            <if test="distributeBatchId != null">distribute_batch_id,</if>
            <if test="isBind != null">is_bind,</if>
            <if test="bindTime != null">bind_time,</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="batchNo != null and batchNo != ''">#{batchNo},</if>
            <if test="hookNo != null and hookNo != ''">#{hookNo},</if>
            <if test="slaughterCode != null and slaughterCode != ''">#{slaughterCode},</if>
            <if test="distributeBatchId != null">#{distributeBatchId},</if>
            <if test="isBind != null">#{isBind},</if>
            <if test="bindTime != null">#{bindTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <insert id="insertHookBindBatch" parameterType="list" >
        INSERT INTO hook_bind (batch_no, hook_no,slaughter_code, distribute_batch_id, bind_time, create_time, create_by,del_flag)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.batchNo}, #{item.hookNo},#{item.slaughterCode},#{item.distributeBatchId}, #{item.bindTime},#{item.createTime}, #{item.createBy},'0')
        </foreach>
    </insert>

    <update id="updateHookBind" parameterType="HookBind">
        update hook_bind
        <trim prefix="SET" suffixOverrides=",">
            <if test="slaughterCode != null and slaughterCode != ''">slaughter_code = #{slaughterCode},</if>
            <if test="distributeBatchId != null">distribute_batch_id = #{distributeBatchId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where batch_no = #{batchNo} and del_flag = '0'
    </update>

    <delete id="deleteHookBindById" parameterType="String">
        update hook_bind set del_flag = '2' where batch_no = #{batchNo}
    </delete>

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