<?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="cn.com.ctop.kuaishou.modules.document.mapper.DocumentLibraryMapper">


    <insert id="saveDocumentLibrary" parameterType="cn.com.ctop.kuaishou.modules.document.entity.DocumentLibraryInfo">
        replace into ctop_document_library (copy_writer_id,copy_writer,account_id,status,create_time) values
        (#{copyWriterId},#{copyWriter},#{accountId},#{status},#{createTime})
    </insert>

    <insert id="insertBatch" parameterType="cn.com.ctop.kuaishou.modules.document.entity.DocumentLibraryInfo">
        replace into ctop_document_library
        (copy_writer_id,copy_writer,account_id,status,create_time)
        values
        <foreach collection="list" item="video" separator=",">
            (
            #{video.copyWriterId},#{video.copyWriter},#{video.accountId},#{video.status},#{video.createTime}
            )
        </foreach>
    </insert>


    <select id="getDocumentLibraryList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t.copy_writer_id as 'copyWriterId',t.copy_writer,t.status,COUNT(t1.creative_id) as 'relatedSize',COUNT(if(t2.status= 42 ,TRUE,NULL)) as 'refusedSize'  from
        (SELECT DISTINCT copy_writer_id,copy_writer,status,create_time from ctop_document_library
        <where>
            <if test="keywords !=null and keywords !=''">
                and copy_writer like CONCAT('%',CONCAT(#{keywords},'%'))
            </if>
            <if test="startDate !=null and startDate !='' and endDate !=null and endDate !=''">
                and create_time BETWEEN  #{startDate} and #{endDate}
            </if>
            <if test='status !=null and status !="" and status !=2'>
                and status =#{status}
            </if>
            <if test="list!=null and list.size() > 0">
                and account_id in
                <foreach item="item" collection="list" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </if>
        </where>
        ) t
        LEFT JOIN ctop_document_library_center t1 on t.copy_writer_id=t1.copy_writer_id
        LEFT JOIN ctop_kuaishou_creative t2 on t1.creative_id=t2.creative_id
        GROUP BY t.copy_writer_id
        order by t.create_time desc
    </select>


    <update id="updateStatus" >
        update  ctop_document_library set status=#{status} where copy_writer_id=#{copyWriterId}
    </update>

    <select id="getRefusedList" resultType="com.alibaba.fastjson.JSONObject" parameterType="String">
        SELECT t2.auth_name as 'accountName',t1.unit_id as 'unitId',t1.creative_name as 'creativeName',t1.review_detail as 'reviewDetail' FROM ctop_document_library_center t
        LEFT JOIN ctop_kuaishou_creative t1 on t.creative_id = t1.creative_id
        LEFT JOIN ctop_user_allocation t2 on t1.account_id = t2.account_id
        where t.copy_writer_id =#{copyWriterId} and t1.status=42
    </select>

    <select id="getAcoountId" parameterType="String" resultType="String">
        select account_id as 'accountId' from  ctop_document_library where copy_writer_id=#{copyWriterId}
    </select>

    <select id="getDataByAccountId" resultType="String">
        select id from ctop_document_library  where account_id=#{accountId} and copy_writer_id=#{copyWriterId}
     </select>

    <select id="getDocumentByAccountId" resultType="Map">
        select copy_writer_id as 'copyWriterId',copy_writer as 'copyWriter' from ctop_document_library  where account_id=#{accountId} and status=0
        <if test="copyWriterId!=null and copyWriterId!=''">
            and copy_writer_id !=#{copyWriterId}
        </if>

     </select>

</mapper>