<?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.web.v1.mapper.HookBindMapper">
    
    <resultMap type="com.ruoyi.web.v1.model.HookBind" id="HookBindResult">
        <result property="id"    column="id"    />
        <result property="batchNo"    column="batch_no"    />
        <result property="deviceSerial"    column="device_serial"    />
        <result property="epcNo"    column="epc_no"    />
        <result property="hookNo"    column="hook_no"    />
        <result property="slaughterCode"    column="slaughter_code"    />
        <result property="distributeBatchId"    column="distribute_batch_id"    />
        <result property="hasBind"    column="has_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.web.v1.DTO.HookBindBatchListDTO">
        <result property="batchNo"    column="batch_no"    />
        <result property="groupId"    column="group_id"    />
        <result property="slaughterCode"    column="slaughter_code"    />
        <result property="hookNoList"    column="hook_no_list"    />
        <result property="total"    column="total"    />
        <result property="hasBind"    column="has_bind"    />
        <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.web.v1.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,epc_no,hook_no, slaughter_code,distribute_batch_id, has_bind,bind_time, create_time, create_by, update_time, update_by, del_flag from hook_bind
    </sql>

    <select id="selectHookBindList" parameterType="com.ruoyi.web.v1.model.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="hasBind != null  and hasBind != ''"> and has_bind = #{hasBind}</if>
        </where>
    </select>
    <select id="selectHookNoByBatchNo" parameterType="String" resultType="String">
        select hook_no from hook_bind where del_flag = '0' and batch_no = #{batchNo}
    </select>
    <!--原设计根据入库的时间间隔如下20秒内的视为同一批-->
    <!--<select id="selectHookBindListByBatch" parameterType="com.ruoyi.web.v1.domain.request.ReqHookBindList" resultMap="HookBindBatchListResult">
        select
            GROUP_CONCAT(a.id SEPARATOR ',') AS group_id,
            GROUP_CONCAT(a.hook_no SEPARATOR ',') AS hook_no_list,
            COUNT(a.id) AS total,
            MAX(a.id) as max_id,
            a.slaughter_code,
            a.has_bind,
            a.bind_time,
            c.purchaser_name,
            d.supplier_name,
            e.animal_cert_no
        from (
            select
                t.*,
                @group := IF(TIMESTAMPDIFF(SECOND, @prev_time, create_time) <![CDATA[ <= ]]> 20, @group, @group + 1) as grp,
                @prev_time := create_time
            from
                hook_bind t,
                (select @group := 0, @prev_time := NULL) as vars
            <where>
                t.del_flag = '0'
                <if test="bindTime != null"> and DATE(t.bind_time) = DATE(#{bindTime})</if>
            </where>
            order by
                create_time
        ) 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="keyword != null  and keyword != ''"> and (a.slaughter_code like concat('%', #{keyword}, '%') or e.animal_cert_no like concat('%', #{keyword}, '%'))</if>
            <if test="slaughterCode != null  and slaughterCode != ''"> and a.slaughter_code like concat('%', #{slaughterCode}, '%')</if>
            <if test="animalCertNo != null  and animalCertNo != ''"> and e.animal_cert_no like concat('%', #{animalCertNo}, '%')</if>
            <if test="bindTime != null"> and DATE(a.bind_time) = DATE(#{bindTime})</if>
        </where>
        group by a.grp,a.slaughter_code,a.has_bind,a.bind_time,c.purchaser_name,d.supplier_name,e.animal_cert_no
        order by a.has_bind asc,a.bind_time desc,max_id desc
    </select>-->

    <!--新设计根据手动入库的吊钩绑定，根据生成的吊钩批次号聚合-->
    <select id="selectHookBindListByBatch" parameterType="com.ruoyi.web.v1.model.request.ReqHookBindList" resultMap="HookBindBatchListResult">
        select
        a.batch_no,
        GROUP_CONCAT(a.id SEPARATOR ',') AS group_id,
        GROUP_CONCAT(a.hook_no SEPARATOR ',') AS hook_no_list,
        COUNT(a.id) AS total,
        MAX(a.id) as max_id,
        a.slaughter_code,
        a.has_bind,
        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="keyword != null  and keyword != ''"> and (a.slaughter_code like concat('%', #{keyword}, '%') or e.animal_cert_no like concat('%', #{keyword}, '%'))</if>
            <if test="slaughterCode != null  and slaughterCode != ''"> and a.slaughter_code like concat('%', #{slaughterCode}, '%')</if>
            <if test="animalCertNo != null  and animalCertNo != ''"> and e.animal_cert_no like concat('%', #{animalCertNo}, '%')</if>
            <if test="bindTime != null"> and DATE(a.bind_time) = DATE(#{bindTime})</if>
        </where>
        group by a.batch_no,a.slaughter_code,a.has_bind,a.bind_time,c.purchaser_name,d.supplier_name,e.animal_cert_no
        order by a.has_bind asc,a.bind_time desc,max_id desc
    </select>

    <!--原设计根据多个记录ID获取批次信息-->
    <!--<select id="selectHookBindByBatch" parameterType="String" resultMap="HookBindBatchListResult">
        select a.batch_no,GROUP_CONCAT(a.id SEPARATOR ',') AS group_id,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.del_flag = '0'
        and a.id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
        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,GROUP_CONCAT(a.id SEPARATOR ',') AS group_id,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.del_flag = '0'
        and a.batch_no = #{batchNo}
        group by a.batch_no,a.slaughter_code,a.distribute_batch_id,a.bind_time
    </select>

    <!--查询吊钩绑定详情，仅为查看吊钩详情场景使用，无其他逻辑-->
    <select id="selectHookBindOnlyDetail" parameterType="com.ruoyi.web.v1.model.request.ReqHookBind" 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.del_flag = '0'
            <if test="hookNo != null  and hookNo != ''"> and a.hook_no = #{hookNo}</if>
            <if test="batchNo != null  and batchNo != ''"> and a.batch_no = #{batchNo}</if>
            <if test="id != null  and id > 0"> and a.id = #{id} </if>
            <if test="ids != null  and ids != ''"> and a.id in
                <foreach item="id" collection = "ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by a.create_time desc
        <if test="hookNo != null  and hookNo != ''">
            limit 1
        </if>
    </select>

    <!--查询吊钩短期内关联的信息-->
    <select id="selectHookBindDetail" parameterType="com.ruoyi.web.v1.model.request.ReqHookBind" 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.del_flag = '0' and a.has_bind = '1'
            <if test="hookNo != null  and hookNo != ''"> and a.hook_no = #{hookNo}</if>
            <if test="batchNo != null  and batchNo != ''"> and a.batch_no = #{batchNo}</if>
            <if test="id != null  and id > 0"> and a.id = #{id} </if>
            <if test="ids != null  and ids != ''"> and a.id in
                <foreach item="id" collection = "ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
            order by a.create_time desc
        <if test="hookNo != null  and hookNo != ''">
            limit 1
        </if>
    </select>

    <!--判断相同时间下该批次号是否已绑定-->
    <select id="checkHookNoUnique" parameterType="com.ruoyi.web.v1.model.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="com.ruoyi.web.v1.model.HookBind" useGeneratedKeys="true" keyProperty="id">
        insert into hook_bind
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="batchNo != null and batchNo != ''">batch_no,</if>
            <if test="deviceSerial != null and deviceSerial != ''">device_serial,</if>
            <if test="epcNo != null and epcNo != ''">epc_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="hasBind != null">has_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="deviceSerial != null and deviceSerial != ''">#{deviceSerial},</if>
            <if test="epcNo != null and epcNo != ''">#{epcNo},</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="hasBind != null">#{hasBind},</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,device_serial,epc_no,hook_no,slaughter_code, distribute_batch_id, has_bind,bind_time, create_time, create_by,del_flag)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.batchNo}, #{item.deviceSerial}, #{item.epcNo}, #{item.hookNo},#{item.slaughterCode},#{item.distributeBatchId},'1', #{item.bindTime},#{item.createTime}, #{item.createBy},'0')
        </foreach>
    </insert>

    <update id="updateHookBind" parameterType="com.ruoyi.web.v1.model.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="hasBind != null and hasBind != ''">has_bind = #{hasBind},</if>
            <if test="bindTime != null">bind_time = #{bindTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id} and del_flag = '0'
    </update>

    <!--原设计根据多个ID批量修改-->
    <!--<update id="updateHookBindBatch" parameterType="com.ruoyi.web.v1.domain.request.EditHookBindBatch">
        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="hasBind != null and hasBind != ''">has_bind = #{hasBind},</if>
            <if test="bindTime != null">bind_time = #{bindTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id in
        <foreach item="id" collection="groupIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>-->
    <!--新设计根据生成的批次编号修改多个-->
    <update id="updateHookBindBatch" parameterType="com.ruoyi.web.v1.model.request.EditHookBindBatch">
        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="hasBind != null and hasBind != ''">has_bind = #{hasBind},</if>
            <if test="bindTime != null">bind_time = #{bindTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where batch_no = #{batchNo}
    </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>

    <!--根据血码统计吊钩绑定数量-->
    <select id="countHookBindBySlaughterCode" parameterType="String" resultType="Integer">
        select count(1) from hook_bind
        where slaughter_code = #{slaughterCode} and del_flag = '0'
    </select>

    <!--清除绑定关系-->
    <delete id="cleanHookBind">
        update hook_bind set has_bind = '0' where has_bind = '1'
    </delete>
</mapper>