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

    <resultMap type="com.ruoyi.web.modules.trading.domain.BizSupplier" id="BizSupplierResult">
        <id     property="id"                  column="id"/>
        <result property="supplierCode"        column="supplier_code"/>
        <result property="supplierName"        column="supplier_name"/>
        <result property="supplierType"        column="supplier_type"/>
        <result property="identityNo"          column="identity_no"/>
        <result property="contactPhone"        column="contact_phone"/>
        <result property="businessLocation"    column="business_location"/>
        <result property="vehiclePlatesJson"   column="vehicle_plates"/>
        <result property="feeMethod"           column="fee_method"/>
        <result property="feeStandard"         column="fee_standard"/>
        <result property="transactionFeeRate"  column="transaction_fee_rate"/>
        <result property="managementFeeRate"   column="management_fee_rate"/>
        <result property="accountAssigned"     column="account_assigned"/>
        <result property="sysUserId"           column="sys_user_id"/>
        <result property="assignedLoginName"   column="assigned_login_name"/>
        <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 id, supplier_code, supplier_name, supplier_type, identity_no, contact_phone,
               business_location, vehicle_plates, fee_method, fee_standard,
               transaction_fee_rate, management_fee_rate,
               account_assigned, sys_user_id, assigned_login_name, del_flag,
               create_by, create_time, update_by, update_time, remark
        from biz_supplier
    </sql>

    <select id="selectBizSupplierById" parameterType="long" resultMap="BizSupplierResult">
        <include refid="selectVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectBizSupplierByIdAny" parameterType="long" resultMap="BizSupplierResult">
        <include refid="selectVo"/>
        where id = #{id}
    </select>

    <select id="selectBizSupplierBySysUserId" parameterType="long" resultMap="BizSupplierResult">
        <include refid="selectVo"/>
        where sys_user_id = #{sysUserId} and del_flag = '0'
        limit 1
    </select>

    <select id="selectMaxSupplierCodeForUpdate" resultType="string">
        select max(supplier_code) from biz_supplier for update
    </select>

    <select id="countBizSupplierByNameExcludeId" resultType="int">
        select count(1) from biz_supplier
        where del_flag = '0' and supplier_name = #{supplierName}
        <if test="excludeId != null">
            and id != #{excludeId}
        </if>
    </select>

    <select id="countBizSupplierByIdentityNoExcludeId" resultType="int">
        select count(1) from biz_supplier
        where del_flag = '0' and identity_no = #{identityNo}
        <if test="excludeId != null">
            and id != #{excludeId}
        </if>
    </select>

    <select id="selectBizSupplierList" parameterType="com.ruoyi.web.modules.trading.domain.BizSupplier" resultMap="BizSupplierResult">
        <include refid="selectVo"/>
        <where>
            del_flag = '0'
            <if test="supplierCode != null and supplierCode != ''">
                and supplier_code = #{supplierCode}
            </if>
            <if test="supplierName != null and supplierName != ''">
                and supplier_name = #{supplierName}
            </if>
        </where>
        order by supplier_code asc, id asc
    </select>

    <insert id="insertBizSupplier" parameterType="com.ruoyi.web.modules.trading.domain.BizSupplier" useGeneratedKeys="true" keyProperty="id">
        insert into biz_supplier (
            supplier_code, supplier_name, supplier_type, identity_no, contact_phone,
            business_location, vehicle_plates, fee_method, fee_standard,
            transaction_fee_rate, management_fee_rate,
            account_assigned, sys_user_id, assigned_login_name, del_flag,
            create_by, create_time, remark
        ) values (
            #{supplierCode}, #{supplierName}, #{supplierType}, #{identityNo}, #{contactPhone},
            #{businessLocation}, #{vehiclePlatesJson}, #{feeMethod}, #{feeStandard},
            #{transactionFeeRate}, #{managementFeeRate},
            #{accountAssigned}, #{sysUserId}, #{assignedLoginName}, #{delFlag},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizSupplier" parameterType="com.ruoyi.web.modules.trading.domain.BizSupplier">
        update biz_supplier set
            supplier_name = #{supplierName},
            supplier_type = #{supplierType},
            identity_no = #{identityNo},
            contact_phone = #{contactPhone},
            business_location = #{businessLocation},
            vehicle_plates = #{vehiclePlatesJson},
            fee_method = #{feeMethod},
            fee_standard = #{feeStandard},
            transaction_fee_rate = #{transactionFeeRate},
            management_fee_rate = #{managementFeeRate},
            account_assigned = #{accountAssigned},
            sys_user_id = #{sysUserId},
            assigned_login_name = #{assignedLoginName},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id} and del_flag = '0'
    </update>

    <update id="logicDeleteBizSupplierByIds">
        update biz_supplier set del_flag = '2', update_by = #{updateBy}, update_time = sysdate()
        where del_flag = '0' and id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
