<?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.sync.mapper.MetaDiySalesorgtreeMapper">
    
    <resultMap type="MetaDiySalesorgtree" id="MetaDiySalesorgtreeResult">
        <result property="versioncode"    column="VersionCode"    />
        <result property="versionname"    column="VersionName"    />
        <result property="currentflag"    column="currentFlag"    />
        <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>

    <sql id="selectMetaDiySalesorgtreeVo">
        select VersionCode, VersionName, currentFlag, Org1code, Org1name, Org2code, Org2name, Org3code, Org3name, Org4code, Org4name, Org5code, Org5name, Org6code, Org6name from meta_diy_salesorgtree
    </sql>

    <select id="selectMetaDiySalesorgtreeList" parameterType="MetaDiySalesorgtree" resultMap="MetaDiySalesorgtreeResult">
        <include refid="selectMetaDiySalesorgtreeVo"/>
        <where>  
            <if test="versioncode != null  and versioncode != ''"> and VersionCode = #{versioncode}</if>
            <if test="versionname != null  and versionname != ''"> and VersionName = #{versionname}</if>
            <if test="currentflag != null  and currentflag != ''"> and currentFlag = #{currentflag}</if>
            <if test="org1code != null  and org1code != ''"> and Org1code = #{org1code}</if>
            <if test="org1name != null  and org1name != ''"> and Org1name = #{org1name}</if>
            <if test="org2code != null  and org2code != ''"> and Org2code = #{org2code}</if>
            <if test="org2name != null  and org2name != ''"> and Org2name = #{org2name}</if>
            <if test="org3code != null  and org3code != ''"> and Org3code = #{org3code}</if>
            <if test="org3name != null  and org3name != ''"> and Org3name = #{org3name}</if>
            <if test="org4code != null  and org4code != ''"> and Org4code = #{org4code}</if>
            <if test="org4name != null  and org4name != ''"> and Org4name = #{org4name}</if>
            <if test="org5code != null  and org5code != ''"> and Org5code = #{org5code}</if>
            <if test="org5name != null  and org5name != ''"> and Org5name = #{org5name}</if>
            <if test="org6code != null  and org6code != ''"> and Org6code = #{org6code}</if>
            <if test="org6name != null  and org6name != ''"> and Org6name = #{org6name}</if>
        </where>
    </select>
    
    <select id="selectMetaDiySalesorgtreeById" parameterType="String" resultMap="MetaDiySalesorgtreeResult">
        <include refid="selectMetaDiySalesorgtreeVo"/>
        where VersionCode = #{versioncode}
    </select>
        
    <insert id="insertMetaDiySalesorgtree" parameterType="MetaDiySalesorgtree">
        insert into meta_diy_salesorgtree
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="versioncode != null  and versioncode != ''">VersionCode,</if>
            <if test="versionname != null  and versionname != ''">VersionName,</if>
            <if test="currentflag != null  and currentflag != ''">currentFlag,</if>
            <if test="org1code != null  and org1code != ''">Org1code,</if>
            <if test="org1name != null  and org1name != ''">Org1name,</if>
            <if test="org2code != null  and org2code != ''">Org2code,</if>
            <if test="org2name != null  and org2name != ''">Org2name,</if>
            <if test="org3code != null  and org3code != ''">Org3code,</if>
            <if test="org3name != null  and org3name != ''">Org3name,</if>
            <if test="org4code != null  and org4code != ''">Org4code,</if>
            <if test="org4name != null  and org4name != ''">Org4name,</if>
            <if test="org5code != null  and org5code != ''">Org5code,</if>
            <if test="org5name != null  and org5name != ''">Org5name,</if>
            <if test="org6code != null  and org6code != ''">Org6code,</if>
            <if test="org6name != null  and org6name != ''">Org6name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="versioncode != null  and versioncode != ''">#{versioncode},</if>
            <if test="versionname != null  and versionname != ''">#{versionname},</if>
            <if test="currentflag != null  and currentflag != ''">#{currentflag},</if>
            <if test="org1code != null  and org1code != ''">#{org1code},</if>
            <if test="org1name != null  and org1name != ''">#{org1name},</if>
            <if test="org2code != null  and org2code != ''">#{org2code},</if>
            <if test="org2name != null  and org2name != ''">#{org2name},</if>
            <if test="org3code != null  and org3code != ''">#{org3code},</if>
            <if test="org3name != null  and org3name != ''">#{org3name},</if>
            <if test="org4code != null  and org4code != ''">#{org4code},</if>
            <if test="org4name != null  and org4name != ''">#{org4name},</if>
            <if test="org5code != null  and org5code != ''">#{org5code},</if>
            <if test="org5name != null  and org5name != ''">#{org5name},</if>
            <if test="org6code != null  and org6code != ''">#{org6code},</if>
            <if test="org6name != null  and org6name != ''">#{org6name},</if>
         </trim>
    </insert>

    <update id="updateMetaDiySalesorgtree" parameterType="MetaDiySalesorgtree">
        update meta_diy_salesorgtree
        <trim prefix="SET" suffixOverrides=",">
            <if test="versionname != null  and versionname != ''">VersionName = #{versionname},</if>
            <if test="currentflag != null  and currentflag != ''">currentFlag = #{currentflag},</if>
            <if test="org1code != null  and org1code != ''">Org1code = #{org1code},</if>
            <if test="org1name != null  and org1name != ''">Org1name = #{org1name},</if>
            <if test="org2code != null  and org2code != ''">Org2code = #{org2code},</if>
            <if test="org2name != null  and org2name != ''">Org2name = #{org2name},</if>
            <if test="org3code != null  and org3code != ''">Org3code = #{org3code},</if>
            <if test="org3name != null  and org3name != ''">Org3name = #{org3name},</if>
            <if test="org4code != null  and org4code != ''">Org4code = #{org4code},</if>
            <if test="org4name != null  and org4name != ''">Org4name = #{org4name},</if>
            <if test="org5code != null  and org5code != ''">Org5code = #{org5code},</if>
            <if test="org5name != null  and org5name != ''">Org5name = #{org5name},</if>
            <if test="org6code != null  and org6code != ''">Org6code = #{org6code},</if>
            <if test="org6name != null  and org6name != ''">Org6name = #{org6name},</if>
        </trim>
        where VersionCode = #{versioncode}
    </update>

    <delete id="deleteMetaDiySalesorgtreeById" parameterType="String">
        delete from meta_diy_salesorgtree where VersionCode = #{versioncode}
    </delete>

    <delete id="deleteMetaDiySalesorgtreeByIds" parameterType="String">
        delete from meta_diy_salesorgtree where VersionCode in 
        <foreach item="versioncode" collection="array" open="(" separator="," close=")">
            #{versioncode}
        </foreach>
    </delete>

    <select id="selectSalesorgByName"  resultType="Map">
      select
        ${fildName} as orgname,${fildCode} as orgcode
      from meta_diy_salesorgtree
      where
        ${fildName} = #{orgName}
      group by ${fildName}
    </select>

    <select id="selectSalesTreeByLevel"  resultType="Map" parameterType="Integer">
      select
        Org${level}name as orgname,Org${level}code as orgcode
      from meta_diy_salesorgtree
      where Org${level}code is not null and business_unit = #{type}
      group by orgname
    </select>

    <select id="selectSalesTreeByOrgCode"  resultType="Map" parameterType="Integer">
      select
        Org${level}name as orgname,Org${level}code as orgcode
      from meta_diy_salesorgtree
      where Org3code = #{orgCode}
      group by orgname
    </select>
    
</mapper>