<?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="org.jeecg.ctop.finance.settlement.mapper.SettlementFileInfoMapper">

    <!-- 查询 财务结算-结算单列表
    <select id="getSettlementSheetList" resultType="java.util.LinkedHashMap">
        SELECT
           t.*,
           MAX(t.create_time) as imageCreateTIme
        from (
              SELECT
              f.id,
              cwjs.id as cwId,
              f.create_time,
              f.upload_years as uploadYears,
              cwjs.advertiser_id as advertiserId,
              ( SELECT ad.NAME FROM ctop_advertiser ad WHERE ad.id = cwjs.advertiser_id LIMIT 1 ) advertiserName,
              cwjs.product_id as productId,
              ( SELECT cp.product_name FROM ctop_product cp WHERE cp.id = cwjs.product_id LIMIT 1 ) productName,
            cwjs.project_id as projectId,
            ( SELECT project.project_name FROM ctop_project project WHERE project.id = cwjs.project_id LIMIT 1 ) projectName,
              cwjs.account_id as accountId,
              cwjs.account_name as accountName,
              cwjs.media_id as mediaId,
              cwjs.operate_type as operateType,
              cwjs.status as status,
              cwjs.del_flag as delFlag,
              cwjs.create_user_id as saleUserId,
              ( SELECT su.realname FROM sys_user su WHERE su.id = cwjs.create_user_id LIMIT 1 ) saleUserName,
              f.create_user_id as operateUserId,
              ( SELECT su.realname FROM sys_user su WHERE su.id = f.create_user_id LIMIT 1 ) operateUserName
            FROM
            ctop_cwjs_settlement_info cwjs,
            ctop_cwjs_settlement_file_info f
        WHERE
            cwjs.del_flag = 0
          and f.del_flag = 0
          and cwjs.advertiser_id = f.advertiser_id
          and cwjs.product_id = f.product_id
          and cwjs.media_id = f.media_id
        ORDER BY f.create_time DESC LIMIT 999999) t

        <where>
            <if test="advertiserName !=null and advertiserName != ''">
                AND advertiserName like concat ('%',#{advertiserName},'%')
            </if>
            <if test="productName !=null and productName != ''">
                AND productName like concat ('%',#{productName},'%')
            </if>
            <if test="projectName !=null and projectName != ''">
                AND projectName like concat ('%',#{projectName},'%')
            </if>
            <if test="accountName !=null and accountName != ''">
                AND accountName like concat ('%',#{accountName},'%')
            </if>
            <if test="mediaId !=null and mediaId != ''">
                AND media_id = #{mediaId}
            </if>
            <if test="startTime !=null and startTime != ''">
                AND imageCreateTIme &gt;= #{startTime}
            </if>
            <if test="endTime !=null and endTime != ''">
                AND imageCreateTIme &lt;= #{endTime}
            </if>

            <if test="operatorUserIds !=null and operatorUserIds != ''">
                AND operateUserId in
                <foreach collection="operatorUserIds" item="item" separator=","
                         open="(" close=")">
                    #{item}
                </foreach>
            </if>

            <if test="saleUserIds !=null and saleUserIds != ''">
                AND saleUserId in
                <foreach collection="saleUserIds" item="item" separator=","
                         open="(" close=")">
                    #{item}
                </foreach>
            </if>


        </where>

        GROUP BY t.advertiserId,t.productId,t.uploadYears,t.operateUserId
        ORDER BY imageCreateTIme DESC

    </select>
-->

    <!-- 查询 财务结算-结算单列表-->
    <select id="getSettlementSheetList" resultType="java.util.LinkedHashMap">
        SELECT t.*
        FROM
        (
        SELECT
        f.id,
        f.upload_years AS uploadYears,
        f.media_id AS mediaId,
        f.advertiser_id AS advertiserId,
        ad.NAME as advertiserName,
        f.product_id AS productId,
        cp.product_name as productName,
        IFNULL(u.project_id, cwjs.project_id ) AS projectId,
        project.project_name AS projectName,
        IFNULL(cwjs.account_id, f.account_id) AS accountId,
        IFNULL(u.auth_name,cwjs.account_name) AS accountName,
        f.del_flag,
        IFNULL(project.sale_id,cwjs.create_user_id) AS saleUserId,
        IFNULL((SELECT su.realname FROM sys_user su WHERE project.sale_id = su.id LIMIT 1),
        (SELECT su.realname FROM sys_user su  LEFT JOIN ctop_cwjs_settlement_info cwjs1  on su.id = cwjs1.create_user_id LIMIT 1 )
        ) as saleUserName,
        f.create_user_id AS operateUserId,
        (SELECT su.realname FROM sys_user su  where su.id = f.create_user_id LIMIT 1 ) as operateUserName,
        MAX(f.create_time) create_time,
        MAX(f.update_time) update_time
        FROM
        ctop_cwjs_settlement_file_info f
        LEFT JOIN ctop_cwjs_settlement_info cwjs ON f.account_id = cwjs.account_id
        LEFT JOIN ctop_user_allocation u on u.account_id = f.account_id
        LEFT JOIN ctop_advertiser ad on ad.id = f.advertiser_id
        LEFT JOIN ctop_product cp on cp.id = f.product_id
        LEFT JOIN ctop_project project  on project.id = u.project_id
        WHERE
            f.del_flag = 0
        GROUP BY
            f.account_id,
            f.upload_years,
            f.create_user_id
        ) t
        <where>
            <if test="advertiserName !=null and advertiserName != ''">
                AND t.advertiserName like concat ('%',#{advertiserName},'%')
            </if>
            <if test="productName !=null and productName != ''">
                AND t.productName like concat ('%',#{productName},'%')
            </if>
            <if test="projectName !=null and projectName != ''">
                AND t.projectName like concat ('%',#{projectName},'%')
            </if>
            <if test="accountName !=null and accountName != ''">
                AND t.accountName like concat ('%',#{accountName},'%')
            </if>
            <if test="mediaId !=null and mediaId != ''">
                AND t.mediaId = #{mediaId}
            </if>
            <if test="startTime !=null and startTime != ''">
                AND t.uploadYears &gt;= #{startTime}
            </if>
            <if test="endTime !=null and endTime != ''">
                AND t.uploadYears &lt;= #{endTime}
            </if>
            <if test="createUserName !=null and createUserName != ''">
                AND t.operateUserName like concat ('%',#{createUserName},'%')
            </if>

            <if test="saleUserIds !=null and saleUserIds.size() >0 ">
                AND (t.operateUserId in
                <foreach collection="saleUserIds" item="item" separator=","
                         open="(" close=")">
                    #{item}
                </foreach>
                or t.saleUserId in
                <foreach collection="saleUserIds" item="item" separator=","
                         open="(" close=")">
                    #{item}
                </foreach>
                )
            </if>
           <!-- <if test="createUserId !=null and createUserId != ''">
                AND (operateUserId = #{createUserId} or saleUserId = #{createUserId})
            </if>-->
        </where>
        ORDER BY t.update_time DESC,t.projectId,t.accountId
    </select>



    <!-- 删除财务结算图片 -->
    <update id="deleteSettlementFile">
        UPDATE ctop_cwjs_settlement_file_info
        SET del_flag = 1
        WHERE advertiser_id = #{advertiserId}
          and product_id = #{productId}
          and account_id = #{accountId}
          and media_id = #{mediaId}
          and upload_years = #{uploadYears}
          and create_user_id = #{createUserId}
    </update>

    <!-- 查询账户数量以及图片数量 -->
    <select id="getAccountNumAndImgNum" resultType="java.util.Map">

        SELECT
        COUNT( DISTINCT c.account_id ) AS imageNum
        FROM
        ctop_cwjs_settlement_file_info c
        LEFT JOIN ctop_user_allocation u ON c.account_id = u.account_id
        WHERE
        del_flag = 0
        <if test="advertiserId !=null and advertiserId != ''">
            AND c.advertiser_id = #{advertiserId}
        </if>
        <if test="productId !=null and productId != ''">
            AND c.product_id = #{productId}
        </if>
        <if test="mediaId !=null and mediaId != ''">
            AND c.media_id = #{mediaId}
        </if>
        <if test="projectId !=null and projectId != ''">
            AND u.project_id = #{projectId}
        </if>
       <if test="uploadYears !=null and uploadYears != ''">
            AND c.upload_years = #{uploadYears}
        </if>
    </select>




    <!-- 查询该账户是否已绑定 或开户-->
    <select id="getSettlementAccount" resultType="java.util.Map">
        SELECT
            u.account_id AS accountId,
            u.auth_name AS accountName
        FROM
            ctop_cwjs_settlement_info f,
            ctop_user_allocation u
        WHERE
            f.account_id = u.account_id
          -- AND u.account_status = 0
          AND u.account_id = #{accountId}
          and f.media_id = #{mediaId}
            LIMIT 1

    </select>



</mapper>
