<?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.ruixuan.jinniu.mapper.KuaishouAccountMapper">

    <resultMap type="com.ruixuan.jinniu.entity.KuaishouAccount" id="KuaishouAccountResult">
        <result property="id" column="id"/>
        <result property="accountId" column="account_id"/>
        <result property="accountName" column="account_name"/>
        <result property="projectId" column="project_id"/>
        <result property="projectName" column="project_name"/>
        <result property="mediaCode" column="media_code"/>
        <result property="ksId" column="ks_id"/>
        <result property="ksName" column="ks_name"/>
        <result property="operatorId" column="operator_id"/>
        <result property="operatorName" column="operator_name"/>
        <result property="productName" column="product_name"/>
        <result property="industry" column="industry"/>
        <result property="secondIndustry" column="second_industry"/>
        <result property="corporationName" column="corporation_name"/>
        <result property="accountStatus" column="account_status"/>
        <result property="accountType" column="account_type"/>
        <result property="bindTime" column="bind_time"/>
        <result property="advertiserId" column="advertiser_id"/>
        <result property="accountCreateTime" column="account_create_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectKuaishouAccountVo">
        select id,
               account_id,
               account_name,
               project_id,
               project_name,
               media_code,
               ks_id,
               ks_name,
               operator_id,
               operator_name,
               product_name,
               industry,
               second_industry,
               corporation_name,
               account_status,
               account_type,
               advertiser_id,
               bind_time,
               account_create_time,
               create_time,
               update_time
        from kuaishou_account
    </sql>

    <select id="selectKuaishouAccountList" parameterType="com.ruixuan.jinniu.entity.KuaishouAccount"
            resultMap="KuaishouAccountResult">
        <include refid="selectKuaishouAccountVo"/>
        <where>
            <if test="accountId != null ">
                and account_id = #{accountId}
            </if>
            <if test="accountName != null  and accountName != ''">
                and account_name like concat('%', #{accountName}, '%')
            </if>
            <if test="userList != null and userList.size() >0 ">
                and operator_id in
                <foreach collection="userList" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
            <if test="projectName != null  and projectName != ''">
                and project_name like concat('%', #{projectName}, '%')
            </if>
            <if test="operatorName != null  and operatorName != ''">
                and operator_name like concat('%', #{operatorName}, '%')
            </if>
        </where>
        order by create_time desc
    </select>


    <select id="getAccountByProjectId" parameterType="com.ruixuan.jinniu.entity.KuaishouAccount"
            resultMap="KuaishouAccountResult">
        <include refid="selectKuaishouAccountVo"/>
        where project_id = #{projectId}
        order by create_time desc
    </select>

    <select id="getAccountNameByProjectId" resultType="com.alibaba.fastjson.JSONObject">
        select account_id as 'accountId', account_name as 'accountName', operator_name as 'userName', product_name as 'productName', account_type as 'accountType'
        from kuaishou_account
        where project_id = #{projectId}
          AND account_status = 1
        order by create_time desc
    </select>

    <select id="selectKuaishouAccountById" parameterType="Long" resultMap="KuaishouAccountResult">
        <include refid="selectKuaishouAccountVo"/>
        where id = #{id}
    </select>

    <select id="selectKuaishouAccountByAccountId" parameterType="Long" resultMap="KuaishouAccountResult">
        <include refid="selectKuaishouAccountVo"/>
        where account_id = #{accountId}
    </select>

    <insert id="insertKuaishouAccount" parameterType="com.ruixuan.jinniu.entity.KuaishouAccount" useGeneratedKeys="true"
            keyProperty="id">
        insert into kuaishou_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="accountId != null">account_id,</if>
            <if test="accountName != null">account_name,</if>
            <if test="projectId != null">project_id,</if>
            <if test="projectName != null">project_name,</if>
            <if test="mediaCode != null">media_code,</if>
            <if test="ksId != null">ks_id,</if>
            <if test="ksName != null">ks_name,</if>
            <if test="operatorId != null">operator_id,</if>
            <if test="operatorName != null">operator_name,</if>
            <if test="productName != null">product_name,</if>
            <if test="industry != null">industry,</if>
            <if test="secondIndustry != null">second_industry,</if>
            <if test="corporationName != null">corporation_name,</if>
            <if test="accountStatus != null">account_status,</if>
            <if test="accountType != null">account_type,</if>
            <if test="bindTime != null">bind_time,</if>
            <if test="advertiserId != null">advertiser_id,</if>
            <if test="accountCreateTime != null">account_create_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="accountId != null">#{accountId},</if>
            <if test="accountName != null">#{accountName},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="projectName != null">#{projectName},</if>
            <if test="mediaCode != null">#{mediaCode},</if>
            <if test="ksId != null">#{ksId},</if>
            <if test="ksName != null">#{ksName},</if>
            <if test="operatorId != null">#{operatorId},</if>
            <if test="operatorName != null">#{operatorName},</if>
            <if test="productName != null">#{productName},</if>
            <if test="industry != null">#{industry},</if>
            <if test="secondIndustry != null">#{secondIndustry},</if>
            <if test="corporationName != null">#{corporationName},</if>
            <if test="accountStatus != null">#{accountStatus},</if>
            <if test="accountType != null">#{accountType},</if>
            <if test="bindTime != null">#{bindTime},</if>
            <if test="advertiserId != null">#{advertiserId},</if>
            <if test="accountCreateTime != null">#{accountCreateTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateKuaishouAccount" parameterType="com.ruixuan.jinniu.entity.KuaishouAccount">
        update kuaishou_account
        <trim prefix="SET" suffixOverrides=",">
            <if test="accountId != null">account_id = #{accountId},</if>
            <if test="accountName != null">account_name = #{accountName},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="mediaCode != null">media_code = #{mediaCode},</if>
            <if test="ksId != null">ks_id = #{ksId},</if>
            <if test="ksName != null">ks_name = #{ksName},</if>
            <if test="operatorId != null">operator_id = #{operatorId},</if>
            <if test="operatorName != null">operator_name = #{operatorName},</if>
            <if test="productName != null">product_name = #{productName},</if>
            <if test="industry != null">industry = #{industry},</if>
            <if test="secondIndustry != null">second_industry = #{secondIndustry},</if>
            <if test="corporationName != null">corporation_name = #{corporationName},</if>
            <if test="accountStatus != null">account_status = #{accountStatus},</if>
            <if test="accountType != null">account_type = #{accountType},</if>
            <if test="bindTime != null">bind_time = #{bindTime},</if>
            <if test="advertiserId != null">advertiser_id = #{advertiserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateKuaishouAccountByAccountId" parameterType="com.ruixuan.jinniu.entity.KuaishouAccount">
        update kuaishou_account
        <trim prefix="SET" suffixOverrides=",">
            <if test="accountId != null">account_id = #{accountId},</if>
            <if test="accountName != null">account_name = #{accountName},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="mediaCode != null">media_code = #{mediaCode},</if>
            <if test="ksId != null">ks_id = #{ksId},</if>
            <if test="ksName != null">ks_name = #{ksName},</if>
            <if test="operatorId != null">operator_id = #{operatorId},</if>
            <if test="operatorName != null">operator_name = #{operatorName},</if>
            <if test="productName != null">product_name = #{productName},</if>
            <if test="industry != null">industry = #{industry},</if>
            <if test="secondIndustry != null">second_industry = #{secondIndustry},</if>
            <if test="corporationName != null">corporation_name = #{corporationName},</if>
            <if test="accountStatus != null">account_status = #{accountStatus},</if>
            <if test="accountType != null">account_type = #{accountType},</if>
            <if test="bindTime != null">bind_time = #{bindTime},</if>
            <if test="advertiserId != null">advertiser_id = #{advertiserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteKuaishouAccountById" parameterType="Long">
        delete
        from kuaishou_account
        where id = #{id}
    </delete>

    <delete id="deleteKuaishouAccountByProjectId" parameterType="Long">
        delete
        from kuaishou_account
        where project_id = #{projectId}
    </delete>

    <select id="selectUnboundAccountList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT *
        FROM (
        <if test="status == null or status == '' ">
            ( SELECT
            t1.account_id as 'accountId',
            t1.account_name as 'accountName',
            'JN' as 'type',
            t1.user_id as 'ksId',
            t1.user_name as 'ksName',
            date_format(t1.account_create_time,'%Y-%m-%d') as 'accountCreateTime',
            t1.product_name as 'productName',
            '-' as 'operatorId',
            '-' as 'operatorName'
            FROM kuaishou_agent_account_list t1
            WHERE NOT EXISTS
            (SELECT 1 FROM kuaishou_account t2 WHERE t2.account_id = t1.account_id)
            <if test="accountId != null and accountId != '' ">
                AND t1.account_id = #{accountId}
            </if>
            <if test="accountName != null and accountName != '' ">
                AND t1.account_name Like concat('%', #{accountName}, '%')
            </if>
            <if test="ksId != null and ksId != '' ">
                AND t1.user_id = #{ksId}
            </if>
            )
            union all
            (SELECT
            account_id as 'accountId',
            account_name as 'accountName',
            media_code as 'type',
            ks_id as 'ksId',
            ks_name as 'ksName',
            date_format(account_create_time,'%Y-%m-%d') as 'accountCreateTime',
            product_name as 'productName',
            operator_id as 'operatorId',
            operator_name as 'operatorName'
            FROM kuaishou_account
            <where>
                <if test="accountId != null and accountId != '' ">
                    AND account_id = #{accountId}
                </if>
                <if test="accountName != null and accountName != '' ">
                    AND account_name Like concat('%', #{accountName}, '%')
                </if>
                <if test="ksId != null and ksId != '' ">
                    AND ks_id = #{ksId}
                </if>
            </where>
            )
        </if>
        <if test="status == 2 ">
            SELECT
            t1.account_id as 'accountId',
            t1.account_name as 'accountName',
            'JN' as 'type',
            t1.user_id as 'ksId',
            t1.user_name as 'ksName',
            date_format(t1.account_create_time,'%Y-%m-%d') as 'accountCreateTime',
            t1.product_name as 'productName',
            '-' as 'operatorId',
            '-' as 'operatorName'
            FROM kuaishou_agent_account_list t1
            WHERE NOT EXISTS
            (SELECT 1 FROM kuaishou_account t2 WHERE t2.account_id = t1.account_id)
            <if test="accountId != null and accountId != '' ">
                AND t1.account_id = #{accountId}
            </if>
            <if test="accountName != null and accountName != '' ">
                AND t1.account_name Like concat('%', #{accountName}, '%')
            </if>
            <if test="ksId != null and ksId != '' ">
                AND t1.user_id = #{ksId}
            </if>
        </if>
        <if test="status == 1">
            SELECT
            account_id as 'accountId',
            account_name as 'accountName',
            media_code as 'type',
            ks_id as 'ksId',
            ks_name as 'ksName',
            date_format(account_create_time,'%Y-%m-%d') as 'accountCreateTime',
            product_name as 'productName',
            operator_id as 'operatorId',
            operator_name as 'operatorName'
            FROM kuaishou_account
            <where>
                <if test="accountId != null and accountId != '' ">
                    AND account_id = #{accountId}
                </if>
                <if test="accountName != null and accountName != '' ">
                    AND account_name Like concat('%', #{accountName}, '%')
                </if>
                <if test="ksId != null and ksId != '' ">
                    AND ks_id = #{ksId}
                </if>
            </where>
        </if>
        ) t  ORDER BY accountCreateTime DESC
    </select>

    <insert id="insertRecord">
        insert into kuaishou_account_bind_record
        (account_id,
         bind_user_id,
         user_id,
         bind_type)
            value ( #{accountId},
            #{bindUserId},
            #{userId},
            #{bindType}
            )
    </insert>

</mapper>