<?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.common.module.mapper.MaterialInfoMapper">
    <insert id="materialSync">
        INSERT INTO ctop_material_info (
        id,
        `code`,
        url,
        cover_url,
        watermark_code,
        material_name,
        watermark_url,
        watermark_material_name,
        user_id,
        auditor_id,
        `status`,
        material_describe,
        product_id,
        project_id,
        refuse_reason,
        creative_copywriter,
        refuse_file,
        excellent,
        type,
        confirm_time,
        create_time,
        update_time,
        toutiao_effi_type,
        kuaishou_effi_type,
        supplier_code,
        material_innovate,
        sync_kuaishou,
        sync_bytedance
        )
        SELECT UUID(),
        t1.`code`,
        t1.url,
        t1.cover_url,
        t1.watermark_code,
        t1.material_name,
        t1.watermark_url,
        t1.watermark_material_name,
        t1.user_id,
        t1.auditor_id,
        0,
        t1.material_describe,
        #{productId},
        #{toProjectId},
        t1.refuse_reason,
        t1.creative_copywriter,
        t1.refuse_file,
        t1.excellent,
        t1.type,
        t1.confirm_time,
        t1.create_time,
        t1.update_time,
        t1.toutiao_effi_type,
        t1.kuaishou_effi_type,
        t1.supplier_code,
        t1.material_innovate,
        t1.sync_kuaishou,
        t1.sync_bytedance
        FROM ctop_material_info t1
        WHERE t1.project_id = #{projectId}
        AND t1.`code` not in(
        SELECT `code`
        FROM ctop_material_info
        WHERE project_id = #{toProjectId}
        )
    </insert>


    <select id="getProjectIdByAccountId" resultType="java.lang.Long">

       select
       project_id
       from ctop_user_allocation
       where  account_id = #{accountId}

    </select>

    <select id="selectUserNameById" resultType="java.lang.String">
   select realname from  sys_user where  id = #{auditorId}
    </select>


    <select id="selectCountByMap" parameterType="java.util.Map" resultType="java.lang.Integer">

        select count(1)
        from ctop_material_info
        where 1 = 1
        <if test="userId != null">
            and user_id = #{userId}
        </if>

        <if test="status != null">
            and status = #{status}
        </if>


    </select>

    <select id="getKuaishouUpVideoCount" resultType="java.lang.Integer">
        select
        count(1)
        from
        ctop_kuaishou_video_get v
        where
        v.signature = #{code}
    </select>

    <select id="getToutiaoUpVideoCount" resultType="java.lang.Integer">
        select
        count(1)
        from
        ctop_bytedance_video_info v
        where
        v.signature = #{code}
    </select>


    <select id="getEffiSignature" resultType="string">
        select
        efficient_video_signature
        from ctop_user_efficient_video_map
        where company_id is not null
        group by efficient_video_signature

    </select>

    <select id="getMaterialInfoByCode" resultType="cn.com.ctop.common.module.entity.MaterialInfo">
            select
            id,
            code,
            url,
            watermark_code as watermarkCode,
            material_name as materialName,
            watermark_url as watermarkUrl,
            watermark_material_name as watermarkMaterialName,
            user_id as userId,
            auditor_id as auditorId,
            status as status,
            material_describe as materialDescribe,
            project_id as projectId,
            refuse_reason as refuseReason,
            creative_copywriter as creativeCopywriter,
            refuse_file as refuseFile,
            excellent,
            type
        from
        ctop_material_info
        where code = #{code}
        limit 1
    </select>

    <select id="getListByParams" resultType="cn.com.ctop.common.module.entity.MaterialInfo">
        select
        matInfo.code,
        matInfo.project_id,
        matInfo.id,
        t.slogansCount,
        matInfo.offline_flag,
        matInfo.confirm_time
        from ctop_material_info matInfo
        left JOIN (
        SELECT
        video_code,
        count(*) AS slogansCount
        FROM
        ctop_bytedance_video_slogen_info
        WHERE
        status = 1
        GROUP BY
        video_code
        ) t ON matInfo.code = t.video_code

        <if test="tagCode!=null">
            left join ctop_material_tag_info tagInfo on tagInfo.code = matInfo.code
        </if>
        <if test="ascription != null and ascription != ''">
            left join ctop_material_ascription ascription on matInfo.code = ascription.material_id
        </if>
        where 1=1
        <if test="tagCode!=null">
            and tagInfo.tag_code like concat(#{tagCode},'%')
        </if>
        <if test="clipId !=null and clipId != ''">
            and ascription.clip_id = #{clipId}
        </if>
        <if test="shotId !=null and shotId != ''">
            and ascription.shot_id = #{shotId}
        </if>
        <if test="planId !=null and planId != ''">
            and ascription.plan_id = #{planId}
        </if>
        <if test="leaderName !=null and leaderName != ''">
            and ascription.leader_name like concat('%',#{leaderName},'%')
        </if>
        <if test="status != null">
            and matInfo.status = #{status}
        </if>
        <if test="userId != null and userId != ''">
            and matInfo.user_id = #{userId}
        </if>
        <if test="code != null">
            and matInfo.code = #{code}
        </if>
        <if test="materialName != null">
            and matInfo.material_name like concat('%',#{materialName},'%')
        </if>
        <if test="projectIds != null and projectIds.size() != 0 ">
            and matInfo.project_id in
            <foreach item="item" index="index" collection="projectIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startDate!= null and startDate != '' ">
            and matInfo.create_time &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != '' ">
            and matInfo.create_time &lt;= #{endDate}
        </if>
        <if test="materialInnovate != null and materialInnovate != '' ">
            and matInfo.material_innovate = #{materialInnovate}
        </if>
        <if test="syncKuaishou != null and syncKuaishou != '' ">
            and matInfo.sync_Kuaishou = #{syncKuaishou}
        </if>

        <if test="syncBytedance != null and syncBytedance != '' ">
            and matInfo.sync_bytedance = #{syncBytedance}
        </if>

        <if test="offlineFlag!=null">
            and matInfo.offline_flag = #{offlineFlag}
        </if>
        <if test="status==1">
            order by matInfo.confirm_time desc
        </if>
        <if test="status!=1">
            order by matInfo.create_time desc
        </if>

    </select>
    <select id="getListByDate" resultType="cn.com.ctop.common.module.entity.MaterialInfo">
        select  *  from
        ctop_material_info
        where  1 = 1
        and DATE_FORMAT(create_time,'%Y-%m-%d') &gt;= #{startDate}
        and DATE_FORMAT(create_time,'%Y-%m-%d') &lt;= #{endDate}
    </select>

    <select id="getIdListByProject" resultType="java.lang.String">
        select id  from  ctop_material_info
        where project_id = #{projectId}
        and status != 2

    </select>

    <select id="getListByProduct" resultType="cn.com.ctop.common.module.entity.MaterialInfo">
        select *  from ctop_material_info
        where product_id = #{productId}
        and status = 1
    </select>

</mapper>
