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

    <resultMap type="com.ruoyi.web.modules.trading.domain.BizTradeOrderLine" id="BizTradeOrderLineResult">
        <id     property="id"       column="id"/>
        <result property="orderId"    column="order_id"/>
        <result property="supplierId" column="supplier_id"/>
        <result property="earTag"     column="ear_tag"/>
        <result property="weight"   column="weight"/>
        <result property="amount"   column="amount"/>
        <result property="gradeCode" column="grade_code"/>
        <result property="gradeName" column="grade_name"/>
    </resultMap>

    <select id="selectLineListByOrderId" parameterType="long" resultMap="BizTradeOrderLineResult">
        select id, order_id, supplier_id, ear_tag, weight, amount, grade_code, grade_name
        from biz_trade_order_line
        where order_id = #{orderId}
        order by id asc
    </select>

    <select id="selectLineListByOrderIds" resultMap="BizTradeOrderLineResult">
        select id, order_id, supplier_id, ear_tag, weight, amount, grade_code, grade_name
        from biz_trade_order_line
        where order_id in
        <foreach collection="orderIds" item="orderId" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        order by order_id asc, id asc
    </select>

    <select id="selectOccupiedEarTags" resultType="string">
        select distinct l.ear_tag
        from biz_trade_order_line l
        inner join biz_trade_order o on o.id = l.order_id and o.del_flag = '0'
        where o.order_status in ('0', '1')
        <if test="excludeOrderId != null">
            and o.id != #{excludeOrderId}
        </if>
    </select>

    <delete id="deleteLineByOrderId" parameterType="long">
        delete from biz_trade_order_line where order_id = #{orderId}
    </delete>

    <insert id="batchInsertLine">
        insert into biz_trade_order_line (order_id, supplier_id, ear_tag, weight, amount, grade_code, grade_name)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.orderId}, #{item.supplierId}, #{item.earTag}, #{item.weight}, #{item.amount}, #{item.gradeCode}, #{item.gradeName})
        </foreach>
    </insert>

</mapper>
