<?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.dgtly.order.mapper.DiysalesorgStartMapper">

    <resultMap type="com.dgtly.order.domain.DiySalesorgtree" id="MetaDiySalesorgtreeResult">
        <result property="org1code"    column="Org1code"    />
        <result property="org1name"    column="Org1name"    />
        <result property="org2code"    column="Org2code"    />
        <result property="org2name"    column="Org2name"    />
        <result property="org3code"    column="Org3code"    />
        <result property="org3name"    column="Org3name"    />
        <result property="org4code"    column="Org4code"    />
        <result property="org4name"    column="Org4name"    />
        <result property="org5code"    column="Org5code"    />
        <result property="org5name"    column="Org5name"    />
        <result property="org6code"    column="Org6code"    />
        <result property="org6name"    column="Org6name"    />
    </resultMap>

    <resultMap type="com.dgtly.order.domain.DiysalesorgStart" id="DiysalesorgStartResult">
        <result property="salesorgCode"    column="salesorg_code"    />
        <result property="salseorgName"    column="salseorg_name"    />
    </resultMap>

    <sql id="selectDiysalesorgStartVo">
        select salesorg_code, salseorg_name from diysalesorg_start
    </sql>

    <select id="selectDiysalesorgStartList" parameterType="com.dgtly.order.domain.DiysalesorgStart" resultMap="DiysalesorgStartResult">
        <include refid="selectDiysalesorgStartVo"/>
        <where>  
            <if test="salesorgCode != null  and salesorgCode != ''"> and salesorg_code like concat('%', #{salesorgCode}, '%')</if>
            <if test="salseorgName != null  and salseorgName != ''"> and salseorg_name like concat('%', #{salseorgName}, '%')</if>
        </where>
    </select>
    
    <select id="selectDiysalesorgStartById" parameterType="String" resultMap="DiysalesorgStartResult">
        <include refid="selectDiysalesorgStartVo"/>
        where salesorg_code = #{salesorgCode}
    </select>

    <select id="selectSalesOrgList" parameterType="com.dgtly.order.domain.DiySalesorgtree" resultMap="MetaDiySalesorgtreeResult">
        SELECT
        Org1code, Org1name, Org2code, Org2name, Org3code, Org3name, Org4code, Org4name, Org5code, Org5name, Org6code, Org6name
        from meta_diy_salesorgtree
        where Org6code IS NOT NULL
        and Org6code != ''
        and Org6name IS NOT NULL
        and Org6name != ''
        <if test="org1code != null  and org1code != ''"> and Org1code like concat('%', #{org1code}, '%')</if>
        <if test="org1name != null  and org1name != ''"> and Org1name like concat('%', #{org1name}, '%')</if>
        <if test="org2code != null  and org2code != ''"> and Org2code like concat('%', #{org2code}, '%')</if>
        <if test="org2name != null  and org2name != ''"> and Org2name like concat('%', #{org2name}, '%')</if>
        <if test="org3code != null  and org3code != ''"> and Org3code like concat('%', #{org3code}, '%')</if>
        <if test="org3name != null  and org3name != ''"> and Org3name like concat('%', #{org3name}, '%')</if>
        <if test="org4code != null  and org4code != ''"> and Org4code like concat('%', #{org4code}, '%')</if>
        <if test="org4name != null  and org4name != ''"> and Org4name like concat('%', #{org4name}, '%')</if>
        <if test="org5code != null  and org5code != ''"> and Org5code like concat('%', #{org5code}, '%')</if>
        <if test="org5name != null  and org5name != ''"> and Org5name like concat('%', #{org5name}, '%')</if>
        <if test="org6code != null  and org6code != ''"> and Org6code like concat('%', #{org6code}, '%')</if>
        <if test="org6name != null  and org6name != ''"> and Org6name like concat('%', #{org6name}, '%')</if>
        GROUP BY Org6code,Org6name
    </select>

    <insert id="insertDiysalesorgStart" parameterType="com.dgtly.order.domain.DiysalesorgStart">
        insert into diysalesorg_start
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="salesorgCode != null  and salesorgCode != ''">salesorg_code,</if>
            <if test="salseorgName != null  and salseorgName != ''">salseorg_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="salesorgCode != null  and salesorgCode != ''">#{salesorgCode},</if>
            <if test="salseorgName != null  and salseorgName != ''">#{salseorgName},</if>
         </trim>
    </insert>

    <update id="updateDiysalesorgStart" parameterType="com.dgtly.order.domain.DiysalesorgStart">
        update diysalesorg_start
        <trim prefix="SET" suffixOverrides=",">
            <if test="salseorgName != null  and salseorgName != ''">salseorg_name = #{salseorgName},</if>
        </trim>
        where salesorg_code = #{salesorgCode}
    </update>

    <delete id="deleteDiysalesorgStartById" parameterType="String">
        delete from diysalesorg_start where salesorg_code = #{salesorgCode}
    </delete>

    <delete id="deleteDiysalesorgStartByIds" parameterType="String">
        delete from diysalesorg_start where salesorg_code in 
        <foreach item="salesorgCode" collection="array" open="(" separator="," close=")">
            #{salesorgCode}
        </foreach>
    </delete>
    
</mapper>