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

    <resultMap type="com.ruoyi.web.modules.trading.domain.BizMarketEntry" id="BizMarketEntryResult">
        <id     property="id"           column="id"/>
        <result property="entryTime"    column="entry_time"/>
        <result property="stallId"      column="stall_id"/>
        <result property="stallCode"    column="stall_code"/>
        <result property="supplierId"   column="supplier_id"/>
        <result property="supplierName" column="supplier_name"/>
        <result property="quantity"     column="quantity"/>
        <result property="earTags"      column="ear_tags"/>
        <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 e.id, e.entry_time, e.stall_id, e.stall_code, e.supplier_id,
               s.supplier_name, e.quantity,
               (select group_concat(t.ear_tag order by t.id separator ',')
                from biz_market_entry_ear_tag t where t.entry_id = e.id) as ear_tags,
               e.del_flag,
               e.create_by, e.create_time, e.update_by, e.update_time, e.remark
        from biz_market_entry e
        left join biz_supplier s on s.id = e.supplier_id and s.del_flag = '0'
    </sql>

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

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

    <select id="selectBizMarketEntryList" parameterType="com.ruoyi.web.modules.trading.domain.BizMarketEntry"
            resultMap="BizMarketEntryResult">
        <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="stallCode != null and stallCode != ''">
                and e.stall_code like concat('%', #{stallCode}, '%')
            </if>
            <if test="supplierId != null">
                and e.supplier_id = #{supplierId}
            </if>
            <if test="earTag != null and earTag != ''">
                and exists (
                    select 1 from biz_market_entry_ear_tag t
                    where t.entry_id = e.id and t.ear_tag like concat('%', #{earTag}, '%')
                )
            </if>
        </where>
        order by e.entry_time desc, e.id desc
    </select>

    <insert id="insertBizMarketEntry" parameterType="com.ruoyi.web.modules.trading.domain.BizMarketEntry"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_market_entry (
            entry_time, stall_id, stall_code, supplier_id, quantity, del_flag,
            create_by, create_time, update_by, update_time, remark
        ) values (
            #{entryTime}, #{stallId}, #{stallCode}, #{supplierId}, #{quantity}, #{delFlag},
            #{createBy}, #{createTime}, #{updateBy}, #{updateTime}, #{remark}
        )
    </insert>

    <update id="updateBizMarketEntry" parameterType="com.ruoyi.web.modules.trading.domain.BizMarketEntry">
        update biz_market_entry
        set entry_time = #{entryTime},
            stall_id = #{stallId},
            stall_code = #{stallCode},
            supplier_id = #{supplierId},
            quantity = #{quantity},
            update_by = #{updateBy},
            update_time = #{updateTime},
            remark = #{remark}
        where id = #{id} and del_flag = '0'
    </update>

    <update id="logicDeleteBizMarketEntryByIds">
        update biz_market_entry
        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>

</mapper>
