<?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.modules.trading.mapper.BizIsolationPatrolMapper">

    <resultMap type="com.ruoyi.web.modules.trading.domain.BizIsolationPatrol" id="BizIsolationPatrolResult">
        <id     property="id"           column="id"/>
        <result property="patrolTime"   column="patrol_time"/>
        <result property="penId"        column="pen_id"/>
        <result property="penCode"      column="pen_code"/>
        <result property="sickDeadQty"  column="sick_dead_qty"/>
        <result property="contentDesc"  column="content_desc"/>
        <result property="handleDesc"   column="handle_desc"/>
        <result property="earTags"      column="ear_tags"/>
        <result property="photoUrl"     column="photo_url"/>
        <result property="photoPath"    column="photo_path"/>
        <result property="delFlag"      column="del_flag"/>
        <result property="createBy"     column="create_by"/>
        <result property="createTime"   column="create_time"/>
        <result property="updateBy"     column="update_by"/>
        <result property="updateTime"   column="update_time"/>
        <result property="remark"       column="remark"/>
    </resultMap>

    <sql id="selectVo">
        select id, patrol_time, pen_id, pen_code, sick_dead_qty, content_desc, handle_desc,
               ear_tags, photo_url, photo_path, del_flag,
               create_by, create_time, update_by, update_time, remark
        from biz_isolation_patrol
    </sql>

    <select id="selectBizIsolationPatrolById" parameterType="long" resultMap="BizIsolationPatrolResult">
        <include refid="selectVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectBizIsolationPatrolByIdAny" parameterType="long" resultMap="BizIsolationPatrolResult">
        <include refid="selectVo"/>
        where id = #{id}
    </select>

    <select id="selectBizIsolationPatrolList" parameterType="com.ruoyi.web.modules.trading.domain.BizIsolationPatrol"
            resultMap="BizIsolationPatrolResult">
        <include refid="selectVo"/>
        <where>
            del_flag = '0'
            <if test="patrolDateStart != null and patrolDateStart != ''">
                and date(patrol_time) &gt;= #{patrolDateStart}
            </if>
            <if test="patrolDateEnd != null and patrolDateEnd != ''">
                and date(patrol_time) &lt;= #{patrolDateEnd}
            </if>
            <if test="penCode != null and penCode != ''">
                and pen_code like concat('%', #{penCode}, '%')
            </if>
        </where>
        order by patrol_time desc, id desc
    </select>

    <insert id="insertBizIsolationPatrol" parameterType="com.ruoyi.web.modules.trading.domain.BizIsolationPatrol"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_isolation_patrol (
            patrol_time, pen_id, pen_code, sick_dead_qty, content_desc, handle_desc,
            ear_tags, photo_url, photo_path, del_flag,
            create_by, create_time, update_by, update_time, remark
        ) values (
            #{patrolTime}, #{penId}, #{penCode}, #{sickDeadQty}, #{contentDesc}, #{handleDesc},
            #{earTags}, #{photoUrl}, #{photoPath}, #{delFlag},
            #{createBy}, #{createTime}, #{updateBy}, #{updateTime}, #{remark}
        )
    </insert>

    <update id="updateBizIsolationPatrol" parameterType="com.ruoyi.web.modules.trading.domain.BizIsolationPatrol">
        update biz_isolation_patrol
        set patrol_time = #{patrolTime},
            pen_id = #{penId},
            pen_code = #{penCode},
            sick_dead_qty = #{sickDeadQty},
            content_desc = #{contentDesc},
            handle_desc = #{handleDesc},
            ear_tags = #{earTags},
            photo_url = #{photoUrl},
            photo_path = #{photoPath},
            update_by = #{updateBy},
            update_time = #{updateTime},
            remark = #{remark}
        where id = #{id} and del_flag = '0'
    </update>

    <update id="logicDeleteBizIsolationPatrolByIds">
        update biz_isolation_patrol
        set del_flag = '2', update_by = #{updateBy}, update_time = now()
        where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and del_flag = '0'
    </update>

    <select id="selectInStockPenOptions" resultType="com.ruoyi.web.modules.trading.domain.vo.IsolationPenOptionVo">
        select distinct p.id as penId, p.pen_code as penCode
        from biz_isolation_pen p
        inner join biz_entry_isolation_alloc a on a.pen_id = p.id
        inner join biz_entry_isolation e on e.id = a.entry_id
        where p.del_flag = '0'
          and e.del_flag = '0'
          and e.expected_release_date &gt;= curdate()
        order by p.pen_code asc
    </select>

    <select id="countPenInStock" resultType="int">
        select count(distinct p.id)
        from biz_isolation_pen p
        inner join biz_entry_isolation_alloc a on a.pen_id = p.id
        inner join biz_entry_isolation e on e.id = a.entry_id
        where p.del_flag = '0'
          and e.del_flag = '0'
          and e.expected_release_date &gt;= curdate()
          and p.id = #{penId}
    </select>

    <select id="selectAllocEarTagRowsForPen" resultType="string">
        select a.ear_tags
        from biz_entry_isolation_alloc a
        inner join biz_entry_isolation e on e.id = a.entry_id
        where a.pen_id = #{penId}
          and e.del_flag = '0'
          and e.expected_release_date &gt;= curdate()
    </select>

    <select id="selectSickDeadEarTagRows" resultType="string">
        select p.ear_tags
        from biz_isolation_patrol p
        where p.del_flag = '0'
          and p.sick_dead_qty &gt; 0
          and ifnull(p.ear_tags, '') &lt;&gt; ''
    </select>
</mapper>
