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

    <resultMap type="com.ruoyi.web.modules.trading.domain.BizDistributor" id="BizDistributorResult">
        <id     property="id"                    column="id"/>
        <result property="distributorCode"       column="distributor_code"/>
        <result property="distributorName"       column="distributor_name"/>
        <result property="distributorType"       column="distributor_type"/>
        <result property="identityNo"            column="identity_no"/>
        <result property="contactPhone"          column="contact_phone"/>
        <result property="businessLocation"      column="business_location"/>
        <result property="salesDestination"      column="sales_destination"/>
        <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, distributor_code, distributor_name, distributor_type, identity_no, contact_phone,
               business_location, sales_destination,
               account_assigned, sys_user_id, assigned_login_name, del_flag,
               create_by, create_time, update_by, update_time, remark
        from biz_distributor
    </sql>

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

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

    <select id="selectBizDistributorBySysUserId" parameterType="long" resultMap="BizDistributorResult">
        <include refid="selectVo"/>
        where sys_user_id = #{sysUserId} and del_flag = '0'
    </select>

    <select id="selectMaxDistributorCodeForUpdate" resultType="string">
        select max(distributor_code) from biz_distributor for update
    </select>

    <select id="countBizDistributorByNameExcludeId" resultType="int">
        select count(1) from biz_distributor
        where del_flag = '0' and distributor_name = #{distributorName}
        <if test="excludeId != null">
            and id != #{excludeId}
        </if>
    </select>

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

    <select id="selectBizDistributorList" parameterType="com.ruoyi.web.modules.trading.domain.BizDistributor" resultMap="BizDistributorResult">
        <include refid="selectVo"/>
        <where>
            del_flag = '0'
            <if test="distributorCode != null and distributorCode != ''">
                and distributor_code like concat('%', #{distributorCode}, '%')
            </if>
            <if test="distributorName != null and distributorName != ''">
                and distributor_name like concat('%', #{distributorName}, '%')
            </if>
        </where>
        order by distributor_code asc, id asc
    </select>

    <insert id="insertBizDistributor" parameterType="com.ruoyi.web.modules.trading.domain.BizDistributor" useGeneratedKeys="true" keyProperty="id">
        insert into biz_distributor (
            distributor_code, distributor_name, distributor_type, identity_no, contact_phone,
            business_location, sales_destination, del_flag,
            create_by, create_time, remark
        ) values (
            #{distributorCode}, #{distributorName}, #{distributorType}, #{identityNo}, #{contactPhone},
            #{businessLocation}, #{salesDestination}, #{delFlag},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizDistributor" parameterType="com.ruoyi.web.modules.trading.domain.BizDistributor">
        update biz_distributor set
            distributor_name = #{distributorName},
            distributor_type = #{distributorType},
            identity_no = #{identityNo},
            contact_phone = #{contactPhone},
            business_location = #{businessLocation},
            sales_destination = #{salesDestination},
            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="logicDeleteBizDistributorByIds">
        update biz_distributor 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>
