<?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.pluss.platform.mapper.MerchantOrderStatisticsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.MerchantOrderStatistics">
        <id column="id" property="id" />
        <result column="merchantCode" property="merchantCode" />
        <result column="totalFee" property="totalFee" />
        <result column="count" property="count" />
        <result column="retFee" property="retFee" />
        <result column="retCount" property="retCount" />
        <result column="startDate" property="startDate" />
        <result column="endDate" property="endDate" />
        <result column="createTime" property="createTime" />
    </resultMap>

    <insert id="insertStatistics">
        INSERT IGNORE INTO tb_pluss_merchant_order_statistics(`merchantCode`, `count`, `totalFee`, `retCount`, `retFee`, `startDate`, `endDate`)
        SELECT
            mbi.merchantCode,
            COUNT(IF(mo.`status` = 1, 1, NULL)) count,
            SUM(IF(mo.`status` = 1, consumeFee, 0.00)  ) totalFee,
            COUNT(IF(mo.`status` = 2, 1, NULL)) retCount,
            SUM(IF(mo.`status` = 2, consumeFee, 0.00)  ) retFee,
	        #{startDate},
	        #{endDate}
        FROM
            tb_pluss_merchant_base_info mbi
            LEFT JOIN tb_pluss_merchant_order mo ON mbi.merchantCode = mo.merchantCode
        WHERE
            createDate BETWEEN #{startDate}
          AND #{endDate}
        GROUP BY
            merchantCode;
    </insert>

    <insert id="insertAllStatics">
        INSERT IGNORE INTO tb_pluss_merchant_order_statistics(`merchantCode`, `totalFee`, `count`, `retFee`, `retCount`, `startDate`, `endDate`)
        SELECT merchantCode, SUM(totalFee) totalFee, SUM(count) count, SUM(retFee) retFee, SUM(retCount) retCount, '1970-01-01', '2099-12-31'
        FROM tb_pluss_merchant_order_statistics mos GROUP BY merchantCode;
    </insert>

    <select id="selectDirectTotal" resultType="cn.pluss.platform.entity.MerchantOrderStatistics">
        SELECT SUM(totalFee) `totalFee`, SUM(`count`) `count` FROM tb_pluss_user_app ua
            LEFT JOIN tb_pluss_merchant_order_statistics mos ON ua.merchantCode = mos.merchantCode
        WHERE ua.parentId = #{pId}
        <choose>
            <when test="month != null and month != ''">
                AND DATE_FORMAT(mos.startDate, "%Y-%m") = #{month}
            </when>
            <otherwise>
                AND mos.startDate = "1970-01-01"
            </otherwise>
        </choose>
    </select>

    <select id="selectIndirectTotal" resultType="cn.pluss.platform.entity.MerchantOrderStatistics">
        SELECT SUM(`teamTransFlow`) totalFee, SUM(`transNum`) count FROM tb_pluss_user_profit
        <where>
            userId = #{pId}
            <if test="month != null and month != ''">
                AND profitMonth = #{month}
            </if>
        </where>

    </select>
</mapper>
