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

    <resultMap type="com.ruoyi.web.modules.trading.domain.BizTradeOrder" id="BizTradeOrderResult">
        <id     property="id"               column="id"/>
        <result property="orderNo"          column="order_no"/>
        <result property="supplierId"       column="supplier_id"/>
        <result property="supplierName"     column="supplier_name"/>
        <result property="distributorId"    column="distributor_id"/>
        <result property="distributorName"  column="distributor_name"/>
        <result property="totalHeads"       column="total_heads"/>
        <result property="totalAmount"      column="total_amount"/>
        <result property="orderStatus"      column="order_status"/>
        <result property="payVoucherUrl"    column="pay_voucher_url"/>
        <result property="payVoucherPath"   column="pay_voucher_path"/>
        <result property="finishTime"       column="finish_time"/>
        <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 o.id, o.order_no, o.supplier_id, s.supplier_name,
               o.distributor_id, d.distributor_name,
               o.total_heads, o.total_amount, o.order_status,
               o.pay_voucher_url, o.pay_voucher_path, o.finish_time,
               o.del_flag, o.create_by, o.create_time, o.update_by, o.update_time, o.remark
        from biz_trade_order o
        left join biz_supplier s on s.id = o.supplier_id and s.del_flag = '0'
        left join biz_distributor d on d.id = o.distributor_id and d.del_flag = '0'
    </sql>

    <select id="selectBizTradeOrderById" parameterType="long" resultMap="BizTradeOrderResult">
        <include refid="selectVo"/>
        where o.id = #{id} and o.del_flag = '0'
    </select>

    <select id="selectBizTradeOrderByIdAny" parameterType="long" resultMap="BizTradeOrderResult">
        <include refid="selectVo"/>
        where o.id = #{id}
    </select>

    <select id="selectBizTradeOrderList" parameterType="com.ruoyi.web.modules.trading.domain.BizTradeOrder"
            resultMap="BizTradeOrderResult">
        <include refid="selectVo"/>
        <where>
            o.del_flag = '0'
            <if test="orderNo != null and orderNo != ''">
                and o.order_no like concat('%', #{orderNo}, '%')
            </if>
            <if test="supplierId != null">
                and o.supplier_id = #{supplierId}
            </if>
            <if test="distributorId != null">
                and o.distributor_id = #{distributorId}
            </if>
        </where>
        order by o.create_time desc, o.id desc
    </select>

    <select id="selectMaxOrderNoSuffixByDatePrefix" resultType="string">
        select max(right(order_no, 3))
        from biz_trade_order
        where del_flag = '0'
          and order_no like concat(#{datePrefix}, '%')
          and length(order_no) = 11
    </select>

    <select id="countByOrderNo" resultType="int">
        select count(1) from biz_trade_order where order_no = #{orderNo} and del_flag = '0'
    </select>

    <insert id="insertBizTradeOrder" parameterType="com.ruoyi.web.modules.trading.domain.BizTradeOrder"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_trade_order (
            order_no, supplier_id, distributor_id, total_heads, total_amount,
            order_status, del_flag, create_by, create_time, update_by, update_time, remark
        ) values (
            #{orderNo}, #{supplierId}, #{distributorId}, #{totalHeads}, #{totalAmount},
            #{orderStatus}, #{delFlag}, #{createBy}, #{createTime}, #{updateBy}, #{updateTime}, #{remark}
        )
    </insert>

    <update id="updateBizTradeOrder" parameterType="com.ruoyi.web.modules.trading.domain.BizTradeOrder">
        update biz_trade_order
        set supplier_id = #{supplierId},
            distributor_id = #{distributorId},
            total_heads = #{totalHeads},
            total_amount = #{totalAmount},
            update_by = #{updateBy},
            update_time = #{updateTime},
            remark = #{remark}
        where id = #{id} and del_flag = '0' and order_status = '0'
    </update>

    <update id="updateOrderCompletePay" parameterType="com.ruoyi.web.modules.trading.domain.BizTradeOrder">
        update biz_trade_order
        set order_status = #{orderStatus},
            pay_voucher_url = #{payVoucherUrl},
            pay_voucher_path = #{payVoucherPath},
            finish_time = #{finishTime},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id} and del_flag = '0' and order_status = '0'
    </update>

    <update id="logicDeleteBizTradeOrderByIds">
        update biz_trade_order
        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' and order_status = '0'
    </update>

    <select id="countPendingByDistributorId" resultType="long">
        select count(*)
        from biz_trade_order
        where distributor_id = #{distributorId}
          and del_flag = '0'
          and order_status = '0'
    </select>

</mapper>
