<?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.AssRelcustomerinfoMapper">
    
    <resultMap type="AssRelcustomerinfo" id="AssRelcustomerinfoResult">
        <result property="id"    column="id"    />
        <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="isDelete"    column="is_delete"    />
        <result property="mainkunnr"    column="mainkunnr"    />
        <result property="mainname1"    column="mainname1"    />
        <result property="kunnr"    column="kunnr"    />
        <result property="name1"    column="name1"    />
        <result property="glfs"    column="glfs"    />
        <result property="glsj"    column="glsj"    />
        <result property="enddate"    column="enddate"    />
        <result property="etlCreatetime"    column="ETL_createtime"    />
        <result property="customerType" column="customer_type" />
    </resultMap>

    <sql id="selectAssRelcustomerinfoVo">
        select id, create_by, create_time, update_by, update_time, is_delete, mainkunnr, mainname1, kunnr, name1, glfs, glsj, enddate, ETL_createtime,user_id,customer_type from ass_relcustomerinfo
    </sql>

    <select id="selectAssRelcustomerinfoList" parameterType="AssRelcustomerinfo" resultMap="AssRelcustomerinfoResult">
        <include refid="selectAssRelcustomerinfoVo"/>
        <where>  
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
            <if test="mainkunnr != null  and mainkunnr != ''"> and mainkunnr = #{mainkunnr}</if>
            <if test="mainname1 != null  and mainname1 != ''"> and mainname1 = #{mainname1}</if>
            <if test="kunnr != null  and kunnr != ''"> and kunnr = #{kunnr}</if>
            <if test="name1 != null  and name1 != ''"> and name1 = #{name1}</if>
            <if test="glfs != null  and glfs != ''"> and glfs = #{glfs}</if>
            <if test="glsj != null "> and glsj = #{glsj}</if>
            <if test="enddate != null "> and enddate = #{enddate}</if>
            <if test="etlCreatetime != null "> and ETL_createtime = #{etlCreatetime}</if>
        </where>
    </select>
    
    <select id="selectAssRelcustomerinfoById" parameterType="Long" resultMap="AssRelcustomerinfoResult">
        <include refid="selectAssRelcustomerinfoVo"/>
        where id = #{id}
    </select>
    <select id="selectRelcustomerinfoListByorgCode" resultMap="AssRelcustomerinfoResult">
        <include refid="selectAssRelcustomerinfoVo"/>

        WHERE
            glfs != '开票方关联'
        AND mainkunnr IN (
            SELECT
                ar.mainkunnr
            FROM
                ass_relcustomerinfo ar
            WHERE
                1 = 1
            AND ( #{orgCode} = ar.mainkunnr OR #{orgCode} = ar.kunnr )
            GROUP BY
                ar.mainkunnr
            )
            or user_id=#{userId}
        GROUP BY
            kunnr
    </select>

    <select id="selectRelcustomerinfoByKunnr" resultMap="AssRelcustomerinfoResult">
        <include refid="selectAssRelcustomerinfoVo"/>
        where
        glfs = '用户关联'
        <if test="kunnr !=null and kunnr !=''">
           and kunnr=#{kunnr}
        </if>
        <if test="userId !=null and userId !=''">
            and user_id=#{userId}
        </if>
    </select>

    <insert id="insertAssRelcustomerinfo" parameterType="AssRelcustomerinfo" useGeneratedKeys="true" keyProperty="id">
        insert into ass_relcustomerinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="isDelete != null ">is_delete,</if>
            <if test="mainkunnr != null  and mainkunnr != ''">mainkunnr,</if>
            <if test="mainname1 != null  and mainname1 != ''">mainname1,</if>
            <if test="kunnr != null  and kunnr != ''">kunnr,</if>
            <if test="name1 != null  and name1 != ''">name1,</if>
            <if test="glfs != null  and glfs != ''">glfs,</if>
            <if test="glsj != null ">glsj,</if>
            <if test="enddate != null ">enddate,</if>
            <if test="etlCreatetime != null ">ETL_createtime,</if>
            <if test="userId != null ">user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="isDelete != null ">#{isDelete},</if>
            <if test="mainkunnr != null  and mainkunnr != ''">#{mainkunnr},</if>
            <if test="mainname1 != null  and mainname1 != ''">#{mainname1},</if>
            <if test="kunnr != null  and kunnr != ''">#{kunnr},</if>
            <if test="name1 != null  and name1 != ''">#{name1},</if>
            <if test="glfs != null  and glfs != ''">#{glfs},</if>
            <if test="glsj != null ">#{glsj},</if>
            <if test="enddate != null ">#{enddate},</if>
            <if test="etlCreatetime != null ">#{etlCreatetime},</if>
            <if test="userId != null ">#{userId},</if>
         </trim>
    </insert>

    <insert id="relationCustomerOnlineSync">

        insert into customers_start
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mainkunnr != null  and mainkunnr != ''">customers_code,</if>
            <if test="mainname1 != null and mainname1 !='' ">customers_name,</if>
                                                                online_time,
            <if test="customerType != null  and customerType !='' ">customer_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mainkunnr != null  and mainkunnr != ''">#{mainkunnr},</if>
            <if test="mainname1 != null  and mainname1 !=''">#{mainname1},</if>
                                                                #{updateTime},
            <if test="customerType != null and customerType!=''  ">#{customerType},</if>
        </trim>
    </insert>

    <select id="selectCustomerStart" resultType="java.lang.String">
        select customer_type from customers_start where 1=1
        <if test="customerCode !=null and customerCode !=''">
            and customers_code=#{customerCode}
        </if>
        <if test="customerType!=null and customerType !=''and type != 3  ">
            and customer_type=#{customerType}
        </if>
        <if test="customerType!=null and customerType !=''  and type == 3 ">
            and (customer_type=#{customerType}
            or  customer_type=3)
        </if>
        GROUP BY customer_type
    </select>

    <select id="selectCustomerByCustomerCode" resultType="java.lang.String">
        select customer_type from customers_start where 1=1
        <if test="customerCode !=null and customerCode !=''">
            and customers_code=#{customerCode}
        </if>
        GROUP BY customer_type
    </select>

    <select id="deleteCustomerStartByCustomerCode" parameterType="String" >
        delete from customers_start  where 1=1
        <if test="customerCode !=null and customerCode !=''">
            and customers_code=#{customerCode}
        </if>
    </select>
   <!--   INSERT INTO customers_start (customers_code, customers_name, online_time,customer_type)

            SELECT t.mainkunnr,t.mainname1,t1.online_time,t1.customer_type FROM (

                SELECT s.mainkunnr,s.mainname1,s.kunnr,s.customer_type from (
                    SELECT
                                ar.*
                        FROM
                                customers_start cs
                        LEFT JOIN ass_relcustomerinfo ar ON (cs.customers_code=ar.kunnr or cs.customers_code=ar.mainkunnr)
                        WHERE  ar.glfs != '开票方关联'
                        GROUP BY kunnr

                ) s LEFT JOIN customers_start c ON s.mainkunnr = c.customers_code
                WHERE c.customers_code is null
            ) t LEFT JOIN customers_start t1 ON t.kunnr = t1.customers_code

                union all

            SELECT t.kunnr,t.name1,t1.online_time,t1.customer_type FROM (
                SELECT s.kunnr,s.name1,s.mainkunnr,s.customer_type from (
                SELECT
                    ar.*
                FROM
                    customers_start cs
                LEFT JOIN ass_relcustomerinfo ar ON (cs.customers_code=ar.kunnr or cs.customers_code=ar.mainkunnr)
                WHERE  ar.glfs != '开票方关联'
                GROUP BY kunnr
            ) s LEFT JOIN customers_start c ON s.kunnr = c.customers_code
                WHERE c.customers_code is null
            ) t LEFT JOIN customers_start t1 ON t.mainkunnr = t1.customers_code-->


    <select id="selectAssRelCustomer" resultMap="AssRelcustomerinfoResult">
        <include refid="selectAssRelcustomerinfoVo"/>
        where customer_type is not  null
    </select>
    <update id="updateAssRelcustomerinfo" parameterType="AssRelcustomerinfo">
        update ass_relcustomerinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
            <if test="isDelete != null ">is_delete = #{isDelete},</if>
            <if test="mainkunnr != null  and mainkunnr != ''">mainkunnr = #{mainkunnr},</if>
            <if test="mainname1 != null  and mainname1 != ''">mainname1 = #{mainname1},</if>
            <if test="kunnr != null  and kunnr != ''">kunnr = #{kunnr},</if>
            <if test="name1 != null  and name1 != ''">name1 = #{name1},</if>
            <if test="glfs != null  and glfs != ''">glfs = #{glfs},</if>
            <if test="glsj != null ">glsj = #{glsj},</if>
            <if test="enddate != null ">enddate = #{enddate},</if>
            <if test="etlCreatetime != null ">ETL_createtime = #{etlCreatetime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAssRelcustomerinfoById" parameterType="Long">
        delete from ass_relcustomerinfo where id = #{id}
    </delete>

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

    <delete id="deleteAssRelcustomerinfoByGlfs" parameterType="String">
        delete from ass_relcustomerinfo where glfs =#{glfs}
    </delete>

    <delete id="deleteAssRelcustomerinfoByUserId" parameterType="String">
        delete from ass_relcustomerinfo where 1=1
        <if test="userId !=null and userId !='' ">
            and  user_id =#{userId}
        </if>
        <if test="customerCode !=null and customerCode !='' ">
            and kunnr=#{customerCode}
        </if>
         and glfs='用户关联'
    </delete>


    <select id="selectCustomerName" resultType="java.lang.String">
        select customer_name from order_sales_sum_tuc_customer where 1=1
        <if test="customerId !=null and customerId !='' ">
            and customer_id=#{customerId}
        </if> LIMIT 1
    </select>
    <select id="selectCustomerNameAndCode" resultMap="AssRelcustomerinfoResult">
        select  mainkunnr as mainkunnr,mainname1 as  mainname1 FROM `ass_relcustomerinfo`
        UNION select  kunnr as mainkunnr,name1 as mainname1 FROM `ass_relcustomerinfo`
    </select>
</mapper>