<?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.kmall.schedule.dao.QzStoreTopicStatisMapper">

    <select id="queryTicketStoreTopicByPaySuccess" resultType="map">
        SELECT
        s.store_topic_stat_id storeTopicStatId,
        s.store_topic_id storeTopicId,
        s.pay_order_num payOrderNum,
        s.payer_num payerNum,
        s.receiv_money receivMoney,
        s.pay_case_num payCaseNum,
        s.act_money actMoney
        FROM
        mk_store_topic_stat s
        LEFT JOIN mall_order o ON s.store_topic_id = o.activity_id
        left join mall_store_topic t on s.store_topic_id = t.id
        LEFT JOIN mk_store_ticket_discount d ON t.id = d.store_topic_id
        WHERE 1=1 and d.is_valid = 0 and d.is_past = 0
        <if test="statusList != null">
            AND o.order_status IN
            <foreach item="statusList" collection="statusList" open="(" separator="," close=")">
                #{statusList}
            </foreach>
        </if>
    </select>

    <select id="queryCampStoreTopicByPaySuccess" resultType="map">
        SELECT
        s.store_topic_stat_id storeTopicStatId,
        s.store_topic_id storeTopicId,
        s.pay_order_num payOrderNum,
        s.payer_num payerNum,
        s.receiv_money receivMoney,
        s.pay_case_num payCaseNum,
        s.act_money actMoney
        FROM
        mk_store_topic_stat s
        LEFT JOIN mall_order o ON s.store_topic_id = o.activity_id
        left join mall_store_topic t on s.store_topic_id = t.id
        LEFT JOIN mk_store_camp_minus d ON t.id = d.store_topic_id
        WHERE 1=1 and d.is_valid = 0 and d.is_past = 0
        <if test="statusList != null">
            AND o.order_status IN
            <foreach item="statusList" collection="statusList" open="(" separator="," close=")">
                #{statusList}
            </foreach>
        </if>
    </select>

    <select id="getStatDataByStoreTopicId" resultType="map">
        SELECT
        count( DISTINCT o.order_sn ) payOrderNum,
        ( SELECT count( * ) FROM mall_order WHERE user_id = o.user_id AND activity_id = o.activity_id
        <if test="statusListOne != null">
            AND order_status IN
            <foreach item="statusListOne" collection="statusListOne" open="(" separator="," close=")">
                #{statusListOne}
            </foreach>
        </if>) payerNum,
        sum( o.goods_price ) receivMoney,
        (select sum(number) from mall_order_goods where order_id=o.id) payCaseNum,
        sum( o.actual_price ) orderActualPrice
        FROM
        mall_order o
        INNER JOIN mk_store_topic_stat s ON o.activity_id = s.store_topic_id
        WHERE
            s.store_topic_id = #{storeTopicId}
            <if test="statusListTwo != null">
                AND o.order_status IN
                <foreach item="statusListTwo" collection="statusListTwo" open="(" separator="," close=")">
                    #{statusListTwo}
                </foreach>
            </if>
    </select>

    <update id="updateStoreTopicStat" parameterType="map">
        update mk_store_topic_stat set
          pay_order_num = #{payOrderNum} ,
          payer_num = #{payerNum} ,
          receiv_money = #{receivMoney} ,
          pay_case_num = #{payCaseNum} ,
          act_money = #{actMoney}
        where store_topic_stat_id = #{storeTopicStatId}
    </update>

</mapper>