<?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.AppDistributorYakMapper">

    <resultMap type="com.ruoyi.web.modules.trading.domain.vo.AppDistributorEntryIsolationVo" id="EntryIsolationVoResult">
        <id     property="id"                   column="id"/>
        <result property="entryTime"            column="entry_time"/>
        <result property="supplierId"           column="supplier_id"/>
        <result property="supplierName"         column="supplier_name"/>
        <result property="supplierContactPhone" column="supplier_contact_phone"/>
        <result property="certNo"               column="cert_no"/>
        <result property="expectedReleaseDate"  column="expected_release_date"/>
        <result property="earTags"              column="ear_tags"/>
    </resultMap>

    <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"/>
    </resultMap>

    <select id="selectEntryBySupplierAndEarTag" resultMap="EntryIsolationVoResult">
        select e.id, e.entry_time, e.supplier_id, s.supplier_name, s.contact_phone as supplier_contact_phone,
               e.cert_no, e.expected_release_date, e.ear_tags
        from biz_entry_isolation e
        left join biz_supplier s on s.id = e.supplier_id and s.del_flag = '0'
        where e.del_flag = '0'
          and e.supplier_id = #{supplierId}
          and (
              FIND_IN_SET(#{earTag}, REPLACE(e.ear_tags, '，', ',')) &gt; 0
              or exists (
                  select 1 from biz_entry_isolation_alloc a
                  where a.entry_id = e.id
                    and FIND_IN_SET(#{earTag}, REPLACE(a.ear_tags, '，', ',')) &gt; 0
              )
          )
        order by e.entry_time desc
        limit 1
    </select>

    <select id="selectPatrolsDuringEntryIsolation" resultMap="BizIsolationPatrolResult">
        select distinct p.id, p.patrol_time, p.pen_id, pen.pen_code,
               p.sick_dead_qty, p.content_desc, p.handle_desc,
               p.ear_tags, p.photo_url, p.photo_path
        from biz_isolation_patrol p
        inner join biz_entry_isolation_alloc a on a.pen_id = p.pen_id and a.entry_id = #{entryId}
        left join biz_isolation_pen pen on pen.id = p.pen_id and pen.del_flag = '0'
        where p.del_flag = '0'
          and ifnull(p.sick_dead_qty, 0) = 0
          and (p.ear_tags is null or trim(p.ear_tags) = '')
          and p.patrol_time &gt;= #{entryTime}
          and p.patrol_time &lt;= #{releaseEnd}
        order by p.patrol_time asc, p.id asc
    </select>

</mapper>
