<?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.system.mapper.SysUserExtMapper">
    
    <resultMap type="SysUserExt" id="SysUserExtResult">
        <result property="userId"    column="user_id"    />
        <result property="orgCode"    column="org_code"    />
        <result property="salesLevel"    column="sales_level"    />
        <result property="orgName"    column="org_name"    />
        <result property="sapEmployeeId"    column="sap_employee_id"    />
        <result property="bossEmployeeId"    column="boss_employee_id"    />
        <result property="bossName"    column="boss_name"    />
        <result property="customerCode"    column="customer_code"    />
        <result property="isCustomerManager"    column="is_customer_manager"    />
        <result property="postCode"    column="post_code"    />
        <result property="postName"    column="post_name"    />
        <result property="duty"    column="duty"    />
        <result property="dutyName"    column="duty_name"    />
    </resultMap>

    <sql id="selectSysUserExtVo">
        select user_id, org_code, sales_level, org_name, sap_employee_id, boss_employee_id, boss_name,customer_code,is_customer_manager,post_code,post_name,identity,duty,duty_name from sys_user_ext
    </sql>

    <select id="selectSysUserExtList" parameterType="SysUserExt" resultMap="SysUserExtResult">
        <include refid="selectSysUserExtVo"/>
        <where>  
            <if test="orgCode != null  and orgCode != ''"> and org_code = #{orgCode}</if>
            <if test="salesLevel != null  and salesLevel != ''"> and sales_level = #{salesLevel}</if>
            <if test="orgName != null  and orgName != ''"> and org_name like concat('%', #{orgName}, '%')</if>
            <if test="sapEmployeeId != null "> and sap_employee_id = #{sapEmployeeId}</if>
            <if test="bossEmployeeId != null "> and boss_employee_id = #{bossEmployeeId}</if>
            <if test="bossName != null  and bossName != ''"> and boss_name like concat('%', #{bossName}, '%')</if>
            <if test="customerCode != null and customerCode != ''"> and customer_code like concat('%', #{customerCode}, '%')</if>
            <if test="isCustomerManager != null  and isCustomerManager != ''"> and is_customer_manager = #{isCustomerManager}</if>
            <if test="postCode != null and postCode != ''"> and post_code like concat('%', #{postCode}, '%')</if>
            <if test="postName != null  and postName != ''"> and post_name like concat('%', #{postName}, '%')</if>
        </where>
    </select>
    
    <select id="selectSysUserExtById" parameterType="Long" resultMap="SysUserExtResult">
        <include refid="selectSysUserExtVo"/>
        where user_id = #{userId}
    </select>
    <select id="selectSysUserCustomerExtById" parameterType="Long" resultMap="SysUserExtResult">
        <include refid="selectSysUserExtVo"/>
        where user_id = #{userId} and is_customer_manager  NOT LIKE ('%0%,%8%' )
    </select>
        
    <insert id="insertSysUserExt" parameterType="SysUserExt">
        insert into sys_user_ext
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null ">user_id,</if>
            <if test="orgCode != null  and orgCode != ''">org_code,</if>
            <if test="salesLevel != null  and salesLevel != ''">sales_level,</if>
            <if test="orgName != null  and orgName != ''">org_name,</if>
            <if test="sapEmployeeId != null ">sap_employee_id,</if>
            <if test="bossEmployeeId != null ">boss_employee_id,</if>
            <if test="bossName != null  and bossName != ''">boss_name,</if>
            <if test="customerCode != null and customerCode != ''">  customer_code,</if>
            <if test="isCustomerManager != null  and isCustomerManager != ''">  is_customer_manager,</if>
            <if test="postCode != null and postCode != ''">  post_code ,</if>
            <if test="postName != null  and postName != ''">  post_name ,</if>
            <if test="identity != null  and identity != ''">  identity ,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null ">#{userId},</if>
            <if test="orgCode != null  and orgCode != ''">#{orgCode},</if>
            <if test="salesLevel != null  and salesLevel != ''">#{salesLevel},</if>
            <if test="orgName != null  and orgName != ''">#{orgName},</if>
            <if test="sapEmployeeId != null ">#{sapEmployeeId},</if>
            <if test="bossEmployeeId != null ">#{bossEmployeeId},</if>
            <if test="bossName != null  and bossName != ''">#{bossName},</if>
            <if test="customerCode != null and customerCode != ''"> #{customerCode},</if>
            <if test="isCustomerManager != null  and isCustomerManager != ''"> #{isCustomerManager},</if>
            <if test="postCode != null and postCode != ''">  #{postCode}, </if>
            <if test="postName != null  and postName != ''">  #{postName},</if>
            <if test="identity != null  and identity != ''">  #{identity},</if>
         </trim>
    </insert>

    <update id="updateSysUserExt" parameterType="SysUserExt">
        update sys_user_ext
        <trim prefix="SET" suffixOverrides=",">
            <if test="orgCode != null  and orgCode != ''">org_code = #{orgCode},</if>
            <if test="salesLevel != null  and salesLevel != ''">sales_level = #{salesLevel},</if>
            <if test="orgName != null  and orgName != ''">org_name = #{orgName},</if>
            <if test="sapEmployeeId != null ">sap_employee_id = #{sapEmployeeId},</if>
            <if test="bossEmployeeId != null ">boss_employee_id = #{bossEmployeeId},</if>
            <if test="bossName != null  and bossName != ''">boss_name = #{bossName},</if>
            <if test="customerCode != null and customerCode != ''"> customer_code =#{customerCode}, </if>
             is_customer_manager = #{isCustomerManager},
            <if test="postCode != null and postCode != ''">  post_code = #{postCode},</if>
            <if test="postName != null  and postName != ''">  post_name = #{postName}, </if>
            <if test="identity != null  and identity != ''">  identity = #{identity}, </if>
        </trim>
        where user_id = #{userId}
    </update>
    <update id="switchCustomer">
        update sys_user_ext
        <trim prefix="SET" suffixOverrides=",">
            <if test="orgName != null  and orgName != ''">org_name = #{orgName},</if>
            <if test="orgCode != null and orgCode != ''"> org_code =#{orgCode}, </if>
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteSysUserExtById" parameterType="Long">
        delete from sys_user_ext where user_id = #{userId}
    </delete>

    <delete id="deleteSysUserExtByIds" parameterType="String">
        delete from sys_user_ext where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <insert id="insertOrUpSysUserExt"  useGeneratedKeys="true" keyProperty="userId" keyColumn="user_id"
            parameterType="SysUserExt">
        insert INTO
        sys_user_ext
        <trim prefix="(" suffix=")" suffixOverrides=",">
            user_id,
            <if test="orgCode != null  and orgCode != ''">org_code,</if>
            <if test="salesLevel != null  and salesLevel != ''">sales_level,</if>
            <if test="orgName != null  and orgName != ''">org_name,</if>
            <if test="sapEmployeeId != null ">sap_employee_id,</if>
            <if test="bossEmployeeId != null ">boss_employee_id,</if>
            <if test="bossName != null  and bossName != ''">boss_name,</if>
            <if test="customerCode != null and customerCode != ''">  customer_code,</if>
            <if test="isCustomerManager != null  and isCustomerManager != ''">  is_customer_manager,</if>
            <if test="postCode != null and postCode != ''"> post_code ,</if>
            <if test="postName != null  and postName != ''"> post_name ,</if>
            <if test="identity != null  and identity != ''"> identity ,</if>
            <if test="duty != null  and duty != ''"> duty ,</if>
            <if test="dutyName != null  and dutyName != ''"> duty_name ,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{userId},
            <if test="orgCode != null  and orgCode != ''">#{orgCode},</if>
            <if test="salesLevel != null  and salesLevel != ''">#{salesLevel},</if>
            <if test="orgName != null  and orgName != ''">#{orgName},</if>
            <if test="sapEmployeeId != null ">#{sapEmployeeId},</if>
            <if test="bossEmployeeId != null ">#{bossEmployeeId},</if>
            <if test="bossName != null  and bossName != ''">#{bossName},</if>
            <if test="customerCode != null and customerCode != ''"> #{customerCode},</if>
            <if test="isCustomerManager != null  and isCustomerManager != ''"> #{isCustomerManager},</if>
            <if test="postCode != null and postCode != ''">  #{postCode},</if>
            <if test="postName != null  and postName != ''">#{postName}, </if>
            <if test="identity != null  and identity != ''">#{identity}, </if>
            <if test="duty != null  and duty != ''">#{duty}, </if>
            <if test="dutyName != null  and dutyName != ''">#{dutyName}, </if>
        </trim>
        ON DUPLICATE KEY UPDATE
        <trim suffixOverrides=",">
            <if test="orgCode != null  and orgCode != ''">org_code = #{orgCode},</if>
            <if test="salesLevel != null  and salesLevel != ''">sales_level = #{salesLevel},</if>
            <if test="orgName != null  and orgName != ''">org_name = #{orgName},</if>
            <if test="sapEmployeeId != null ">sap_employee_id = #{sapEmployeeId},</if>
            <if test="bossEmployeeId != null ">boss_employee_id = #{bossEmployeeId},</if>
            <if test="bossName != null  and bossName != ''">boss_name = #{bossName},</if>
            <if test="customerCode != null and customerCode != ''"> customer_code=#{customerCode},</if>
            <if test="isCustomerManager != null  and isCustomerManager != ''">is_customer_manager= #{isCustomerManager},</if>
            <if test="postCode != null and postCode != ''"> post_code = #{postCode}, </if>
            <if test="postName != null  and postName != ''">  post_name = #{postName}, </if>
            <if test="identity != null  and identity != ''">  identity = #{identity}, </if>
            <if test="duty != null  and duty != ''">  duty = #{duty}, </if>
            <if test="dutyName != null  and dutyName != ''">  duty_name = #{dutyName}, </if>
        </trim>
  </insert>

    <select id="selectEmployeeIdsByBossEmployeeId" parameterType="String" resultType="String">
        select sap_employee_id
        from sys_user_ext
        where boss_employee_id = #{bossEmployeeId,}
    </select>
    <select id="selectSysUserExtListByUserId" resultMap="SysUserExtResult">
        SELECT
            sue.user_id,
            sue.org_code,
            sue.sales_level,
            ar.name1 org_name,
            sue.sap_employee_id,
            sue.boss_employee_id,
            sue.boss_name,
            ar.kunnr customer_code,
            sue.is_customer_manager,
            sue.post_code,
            sue.post_name
        FROM
            sys_user_ext sue
        LEFT JOIN ass_relcustomerinfo ar ON sue.customer_code = ar.mainkunnr
        WHERE
            ar.glfs != '开票方关联'
        AND user_id = #{userId}
    </select>

    <select id="selectSysUserExtBySapEmployeeId" resultType="int">
        select count(1) from sys_user_ext where sap_employee_id = #{sapEmployeeId}
    </select>

    <select id="selectUserExtList" resultType="com.dgtly.system.domain.SysUserExt">
        SELECT
            e.sales_level as salesLevel,
            e.org_code as orgCode,
            e.user_id as userId,
            e.is_customer_manager as isCustomerManager
        FROM
            sys_user_ext e LEFT JOIN sys_user u on  u.user_id = e.user_id
        WHERE
            u.is_sync ='0'
              and u.quit is null
              and u.del_flag='0'
        GROUP BY
            e.user_id
    </select>
</mapper>