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

    <resultMap type="com.ruoyi.web.modules.trading.domain.BizEntryIsolation" id="BizEntryIsolationResult">
        <id     property="id"                    column="id"/>
        <result property="entryTime"             column="entry_time"/>
        <result property="supplierId"            column="supplier_id"/>
        <result property="certFileUrl"           column="cert_file_url"/>
        <result property="certFilePath"          column="cert_file_path"/>
        <result property="certNo"                column="cert_no"/>
        <result property="quantity"              column="quantity"/>
        <result property="earTags"               column="ear_tags"/>
        <result property="isolationDays"         column="isolation_days"/>
        <result property="expectedReleaseDate"   column="expected_release_date"/>
        <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"/>
        <result property="supplierName"          column="supplier_name"/>
        <result property="penAllocCount"         column="pen_alloc_count"/>
    </resultMap>

    <sql id="selectVo">
        select e.id, e.entry_time, e.supplier_id, e.cert_file_url, e.cert_file_path, e.cert_no,
               e.quantity, e.ear_tags, e.isolation_days, e.expected_release_date, e.del_flag,
               e.create_by, e.create_time, e.update_by, e.update_time, e.remark,
               s.supplier_name,
               (select count(1) from biz_entry_isolation_alloc a where a.entry_id = e.id) as pen_alloc_count
        from biz_entry_isolation e
        left join biz_supplier s on e.supplier_id = s.id
    </sql>

    <select id="selectBizEntryIsolationById" parameterType="long" resultMap="BizEntryIsolationResult">
        <include refid="selectVo"/>
        where e.id = #{id} and e.del_flag = '0'
    </select>

    <select id="selectBizEntryIsolationByIdAny" parameterType="long" resultMap="BizEntryIsolationResult">
        <include refid="selectVo"/>
        where e.id = #{id}
    </select>

    <select id="selectBizEntryIsolationList" parameterType="com.ruoyi.web.modules.trading.domain.BizEntryIsolation"
            resultMap="BizEntryIsolationResult">
        <include refid="selectVo"/>
        <where>
            e.del_flag = '0'
            <if test="entryDateStart != null and entryDateStart != ''">
                and date(e.entry_time) &gt;= #{entryDateStart}
            </if>
            <if test="entryDateEnd != null and entryDateEnd != ''">
                and date(e.entry_time) &lt;= #{entryDateEnd}
            </if>
            <if test="certNo != null and certNo != ''">
                and e.cert_no like concat('%', #{certNo}, '%')
            </if>
            <if test="supplierId != null">
                and e.supplier_id = #{supplierId}
            </if>
        </where>
        order by e.entry_time desc, e.id desc
    </select>

    <insert id="insertBizEntryIsolation" parameterType="com.ruoyi.web.modules.trading.domain.BizEntryIsolation"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_entry_isolation (
            entry_time, supplier_id, cert_file_url, cert_file_path, cert_no, quantity, ear_tags,
            isolation_days, expected_release_date, del_flag,
            create_by, create_time, update_by, update_time, remark
        ) values (
            #{entryTime}, #{supplierId}, #{certFileUrl}, #{certFilePath}, #{certNo}, #{quantity}, #{earTags},
            #{isolationDays}, #{expectedReleaseDate}, #{delFlag},
            #{createBy}, #{createTime}, #{updateBy}, #{updateTime}, #{remark}
        )
    </insert>

    <update id="updateBizEntryIsolation" parameterType="com.ruoyi.web.modules.trading.domain.BizEntryIsolation">
        update biz_entry_isolation
        set entry_time = #{entryTime},
            supplier_id = #{supplierId},
            cert_file_url = #{certFileUrl},
            cert_file_path = #{certFilePath},
            cert_no = #{certNo},
            quantity = #{quantity},
            ear_tags = #{earTags},
            isolation_days = #{isolationDays},
            expected_release_date = #{expectedReleaseDate},
            update_by = #{updateBy},
            update_time = #{updateTime},
            remark = #{remark}
        where id = #{id} and del_flag = '0'
    </update>

    <update id="logicDeleteBizEntryIsolationByIds">
        update biz_entry_isolation
        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="selectEndedMainEarTagRowsBySupplier" resultType="string">
        select ear_tags
        from biz_entry_isolation
        where del_flag = '0'
          and supplier_id = #{supplierId}
          and expected_release_date &lt;= curdate()
    </select>
</mapper>
