<?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.boot.security.server.dao.CopyrightDao">

    <!--<select id="selectTableListMonth"  resultType="com.boot.security.server.model.BaseSettlement"  parameterType="com.boot.security.server.model.BaseSettlement">-->
        <!--SELECT-->
        <!--a.report_date AS reportDate,a.copyright_id as copyrightId,a.copyright_name as copyrightName,-->
        <!--a.total_amount as totalAmount,IFNULL(b.total_ad_amount,0) totalAdvertAmount,jiesuan_status_name statusName,jiesuan_status status,release_status_name releaseStatusName,release_status releaseStatus-->
        <!--FROM bi_report.report_book_settlement_month a-->
        <!--LEFT JOIN  bi_report.report_book_advert_month b-->
        <!--ON a.report_date=b.report_date AND a.batch_id=b.batch_id AND a.batch_name=b.batch_name-->
        <!--WHERE a.report_date>='2020-04'-->
        <!--<if test="copyrightId != null">-->
            <!--and a.copyright_id in-->
            <!--<foreach collection="copyrightId" item="item" index="index" separator="," open="(" close=")">-->
                <!--#{item.copyrightId}-->
            <!--</foreach>-->
        <!--</if>-->
        <!--<if test="status != null and status!='' ">-->
            <!--and  a.release_status = #{status}-->
        <!--</if>-->
        <!--ORDER BY a.total_amount DESC-->
        <!--limit #{page},#{limit};-->
    <!--</select>-->

    <select id="selectTableListMonth"  resultType="com.boot.security.server.model.BaseSettlement"  parameterType="com.boot.security.server.model.BaseSettlement">
        SELECT report_date,a.copyright_id,a.copyright_name,total_amount as totalAmount,
        IFNULL(total_ad_amount,0) totalAdvertAmount,jiesuan_status_name statusName,jiesuan_status status,release_status_name releaseStatusName,release_status releaseStatus,b.type,b.type_name,b.context
        FROM bi_report.report_book_settlement_month_copyright a,copyright_data.copyright_deduction b
        WHERE a.report_date>='2020-04' and a.copyright_id=b.copyright_id and a.report_date=b.create_month
        <if test="copyrightId != null">
            and a.copyright_id in
            <foreach collection="copyrightId" item="item" index="index" separator="," open="(" close=")">
                #{item.copyrightId}
            </foreach>
        </if>
        <if test="status != null and status!='' ">
            and  a.release_status = #{status}
        </if>
        ORDER BY a.report_date DESC
        limit #{page},#{limit};
    </select>

    <select id="selectTableListMonthCount"  resultType="Integer"  parameterType="com.boot.security.server.model.BaseSettlement">
        SELECT count(*)
        from(select report_date,copyright_id,copyright_name,total_amount as totalAmount,
        IFNULL(total_ad_amount,0) totalAdvertAmount,jiesuan_status_name statusName,jiesuan_status status,release_status_name releaseStatusName,release_status releaseStatus
        FROM bi_report.report_book_settlement_month_copyright a
        WHERE a.report_date>='2020-04'
        <if test="copyrightId != null">
            and a.copyright_id in
            <foreach collection="copyrightId" item="item" index="index" separator="," open="(" close=")">
                #{item.copyrightId}
            </foreach>
        </if>
        <if test="status != null and status!='' ">
            and  a.release_status = #{status}
        </if>
        ORDER BY a.report_date DESC) t
    </select>

    <!--<select id="selectTableListMonthCount"  resultType="Integer"  parameterType="com.boot.security.server.model.BaseSettlement">-->

        <!--SELECT-->
        <!--count(*)-->
        <!--FROM bi_report.report_book_settlement_month a-->
        <!--LEFT JOIN  bi_report.report_book_advert_month b-->
        <!--ON a.report_date=b.report_date AND a.batch_id=b.batch_id AND a.batch_name=b.batch_name-->
        <!--WHERE a.report_date>='2020-04'-->
        <!--<if test="copyrightId != null">-->
            <!--and a.copyright_id in-->
            <!--<foreach collection="copyrightId" item="item" index="index" separator="," open="(" close=")">-->
                <!--#{item.copyrightId}-->
            <!--</foreach>-->
        <!--</if>-->
        <!--<if test="status != null and status!='' ">-->
            <!--and  a.release_status = #{status}-->
        <!--</if>-->
        <!--ORDER BY a.total_amount DESC ;-->
    <!--</select>-->

    <!--<select id="selectTableListMonthSchema"  resultType="com.boot.security.server.model.BaseSettlement"  parameterType="com.boot.security.server.model.BaseSettlement">-->
        <!--SELECT-->
        <!--a.report_date AS reportDate,a.copyright_id as copyrightId,a.copyright_name as copyrightName,-->
        <!--a.total_amount as totalAmount,IFNULL(b.total_ad_amount,0) totalAdvertAmount,jiesuan_status_name statusName,jiesuan_status status,release_status_name releaseStatusName,release_status releaseStatus-->
        <!--FROM bi_report.report_book_settlement_month a-->
        <!--LEFT JOIN  bi_report.report_book_advert_month b-->
        <!--ON a.report_date=b.report_date AND a.batch_id=b.batch_id AND a.batch_name=b.batch_name-->
        <!--WHERE a.report_date>='2020-04'-->
        <!--<if test="copyrightId != null">-->
            <!--and a.copyright_id in-->
            <!--<foreach collection="copyrightId" item="item" index="index" separator="," open="(" close=")">-->
                <!--#{item.copyrightId}-->
            <!--</foreach>-->
        <!--</if>-->
        <!--<if test="status != null and status!='' ">-->
            <!--and  a.release_status = #{status}-->
        <!--</if>-->
        <!--ORDER BY a.total_amount DESC-->
    <!--</select>-->

    <select id="selectTableListMonthSchema"  resultType="com.boot.security.server.model.BaseSettlement"  parameterType="com.boot.security.server.model.BaseSettlement">
        SELECT report_date,a.copyright_id,a.copyright_name,total_amount as totalAmount,
        IFNULL(total_ad_amount,0) totalAdvertAmount,jiesuan_status_name statusName,jiesuan_status status,release_status_name releaseStatusName,release_status releaseStatus,b.type,b.type_name,b.context
        FROM bi_report.report_book_settlement_month_copyright a,copyright_data.copyright_deduction b
        WHERE a.report_date>='2020-04' and a.copyright_id=b.copyright_id and a.report_date=b.create_month
        <if test="copyrightId != null">
            and a.copyright_id in
            <foreach collection="copyrightId" item="item" index="index" separator="," open="(" close=")">
                #{item.copyrightId}
            </foreach>
        </if>
        <if test="status != null and status!='' ">
            and  a.release_status = #{status}
        </if>
        ORDER BY a.report_date DESC
    </select>

    <select id="selectCopyrightById"  resultType="com.boot.security.server.model.BaseSettlement"  >
        SELECT a.copyright_id as copyrightId,a.copyright_name as copyrightName
        FROM bi_report.book_banquanweihu a inner join (select copyright_id from user_copyright_map where user_id=#{userId} and status=1) b on a.copyright_id=b.copyright_id group by a.copyright_id,a.copyright_name;
    </select>

    <select id="selectUserAll"  resultType="Long"  >
        SELECT id
        FROM sys_user;
    </select>

    <select id="selectBookInfo"  resultType="com.boot.security.server.model.BaseSettlement"  parameterType="com.boot.security.server.model.BaseSettlement">
        SELECT book_id bookId,cp_id cpId,book_name bookName,authors
        FROM bi_report.books_api
        where book_id is not null
        <if test="copyrightId != null">
            and copyright_id in
            <foreach collection="copyrightId" item="item" index="index" separator="," open="(" close=")">
                #{item.copyrightId}
            </foreach>
        </if>
        limit #{page},#{limit};
    </select>

    <select id="selectBookInfoCount"  resultType="Integer"  parameterType="com.boot.security.server.model.BaseSettlement">
        SELECT count(*)
        FROM bi_report.books_api
        where book_id is not null
        <if test="copyrightId != null">
            and copyright_id in
            <foreach collection="copyrightId" item="item" index="index" separator="," open="(" close=")">
                #{item.copyrightId}
            </foreach>
        </if>
    </select>

    <insert id="updateUserCopyrightMap" parameterType="java.util.List">
        INSERT INTO user_copyright_map(user_id, copyright_id,copyright_name)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.userId},#{item.copyrightId},
            #{item.copyrightName})
        </foreach>
        ON DUPLICATE KEY UPDATE
        user_id = VALUES(user_id),
        copyright_id = VALUES(copyright_id),
        copyright_name = VALUES(copyright_name)
    </insert>

    <select id="selectBookDetail"  resultType="com.boot.security.server.model.BaseSettlement"  parameterType="java.util.List">
        select report_date reportDate,a.copyright_id copyrightId,a.copyright_name copyrightName,book_id bookId,book_name bookName,authors,total_amount totalAmount,total_ad_amount totalAdvertAmount,b.type,b.type_name,b.context
        from bi_report.report_books_settlement_month a,copyright_data.copyright_deduction b
        WHERE
        <![CDATA[
	    a.report_date>= #{startTime}  and a.report_date <= #{endTime}
	    ]]>  and a.copyright_id=b.copyright_id and a.report_date=b.create_month
        <if test="bookId != null and bookId!='' ">
            and  a.book_id = #{bookId}
        </if>
        <if test=" bookName != null and bookName !='' ">
            and  a.book_name like #{bookName}
        </if>
        <if test="copyrightIdList != null">
            and a.copyright_id in
            <foreach collection="copyrightIdList" item="item" index="index" separator="," open="(" close=")">
                #{item.copyrightId}
            </foreach>
        </if>
        limit #{page},#{limit};
    </select>

    <select id="selectBookDetailCount"  resultType="Integer"  parameterType="java.util.List">
        select count(*)
        from bi_report.report_books_settlement_month a
        WHERE
        <![CDATA[
	    a.report_date>= #{startTime}  and a.report_date <= #{endTime}
	    ]]>
        <if test="bookId != null and bookId!='' ">
            and  a.book_id = #{bookId}
        </if>
        <if test=" bookName != null and bookName !='' ">
            and  a.book_name like #{bookName}
        </if>
        <if test="copyrightIdList != null">
            and a.copyright_id in
            <foreach collection="copyrightIdList" item="item" index="index" separator="," open="(" close=")">
                #{item.copyrightId}
            </foreach>
        </if>
    </select>

</mapper>