<?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
        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 =#{copyrightId}
        </if>
        ORDER BY a.total_amount DESC
        limit #{page},#{limit};
    </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 =#{copyrightId}
        </if>
        ORDER BY a.total_amount DESC ;
    </select>

    <select id="selectCopyrightAll"  resultType="com.boot.security.server.model.BaseSettlement"  >
        SELECT a.copyright_id as copyrightId,a.copyright_name as copyrightName
        FROM bi_report.book_banquanweihu a group by a.copyright_id,a.copyright_name;
    </select>

    <select id="selectBookInfo"  resultType="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=#{copyrightId}
        </if>
        limit #{page},#{limit};
    </select>

    <select id="selectBookInfoCount"  resultType="Integer"  >
        SELECT count(*)
        FROM bi_report.books_api
        where book_id is not null
        <if test="copyrightId != null">
            and copyright_id=#{copyrightId}
        </if>
    </select>

    <select id="selectBookDetail"  resultType="com.boot.security.server.model.BaseSettlement"  parameterType="java.util.List">
        select reportDate,copyright_id copyrightId,copyright_name copyrightName,book_id bookId,book_name bookName,authors,sum(total_amount) totalAmount,sum(total_ad_amount) totalAdvertAmount
        from
        (SELECT
        DATE_FORMAT(a.report_date,'%Y-%m') reportDate,a.copyright_id,a.copyright_name,a.book_id,a.book_name,a.authors,a.total_amount
        ,ifnull(b.total_ad_amount,0) total_ad_amount
        FROM report_book_settlement a
        left join  report_book_advert b
        on a.report_date=b.report_date and a.book_id=b.book_id and a.batch_id=b.batch_id and a.batch_name=b.batch_name
        WHERE
        <![CDATA[
	    DATE_FORMAT(a.report_date,'%Y-%m') >= #{startTime}  and DATE_FORMAT(a.report_date,'%Y-%m') <= #{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="copyrightId != null">
            and a.copyright_id =#{copyrightId}
        </if>
        UNION

        SELECT
        DATE_FORMAT(a.report_date,'%Y-%m') reportDate,b.copyright_id,b.copyright_name,b.book_id,b.book_name,b.authors,IFNULL(a.total_amount,0) total_amount,IFNULL(b.total_ad_amount,0) total_ad_amount
        FROM report_book_settlement a
        RIGHT JOIN  report_book_advert b
        ON a.report_date=b.report_date AND a.book_id=b.book_id AND a.batch_id=b.batch_id AND a.batch_name=b.batch_name
        WHERE b.jiesuan_ad_amount>0
        <![CDATA[
	     and
	    DATE_FORMAT(b.report_date,'%Y-%m') >= #{startTime}  and DATE_FORMAT(b.report_date,'%Y-%m') <= #{endTime}
	    ]]>
        <if test="bookId != null and bookId!='' ">
            and  b.book_id = #{bookId}
        </if>
        <if test=" bookName != null and bookName !='' ">
            and  b.book_name like #{bookName}
        </if>
        <if test="copyrightId != null">
            and a.copyright_id =#{copyrightId}
        </if>) t
        group BY t.reportDate,t.copyright_id,t.copyright_name,t.book_id,t.book_name,t.authors
    </select>

</mapper>