<?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.QzStorePromMapper">
    <select id="queryStorePromList" resultType="map">
        select
        `prom_id` 'promId',
        `store_id`,
        `prom_beg_time`,
        `prom_end_time`,
        `is_finished`
        from mk_store_prom a
        where 1=1 AND a.prom_end_time <![CDATA[ < ]]> now() AND is_finished = 1
    </select>
    <select id="queryStorePromByNotStartList" resultType="map">
        select
        `prom_id` 'promId',
        `store_id`,
        `prom_beg_time` 'promBegTime',
        `prom_end_time`,
        `is_finished`
        from mk_store_prom a
        where 1=1 AND is_finished = 2
    </select>
    <update id="updateStoreProm" parameterType="map">
        update mk_store_prom set is_finished = #{isFinished}, mod_time = now() where prom_id = #{promId}
    </update>

    <select id="queryStorePromByPaySuccess" resultType="map">
        SELECT
            s.prom_stat_id promStatId,
            r.prom_id promId,
            g.goods_id goodsId,
            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_prom_stat s
            LEFT JOIN mk_store_prom_order_real r ON s.prom_id = r.prom_id
            LEFT JOIN mall_order o ON r.order_id = o.id
            LEFT JOIN mall_order_goods g ON o.id = g.order_id
            AND g.id = r.order_goods_id
        WHERE 1=1
            <if test="statusList != null">
                AND o.order_status IN
                <foreach item="statusList" collection="statusList" open="(" separator="," close=")">
                    #{statusList}
                </foreach>
            </if>
        and is_prom_stat_scan = 0
        GROUP BY
            goods_id
    </select>
    <select id="getStatDataByGoodsId" resultType="map">
        SELECT
            count( DISTINCT o.order_sn ) payOrderNum,
            count( DISTINCT r.user_id ) payerNum,
            sum( o.goods_price ) receivMoney,
            sum( g.number ) payCaseNum ,
	        sum( o.actual_price ) orderActualPrice
        FROM
            mall_order o
            INNER JOIN mall_order_goods g ON o.id = g.order_id
            INNER JOIN mk_store_prom_order_real r ON r.order_goods_id = g.id
            AND r.order_id = o.id
        WHERE
            g.goods_id = #{goodsId}
            <if test="statusList != null">
                AND o.order_status IN
                <foreach item="statusList" collection="statusList" open="(" separator="," close=")">
                    #{statusList}
                </foreach>
            </if>
    </select>

    <select id="getActMoneyDataByGoodsId" resultType="map" parameterType="map">
        SELECT
            sum( g.retail_price ) actMoney
        FROM
            mall_order o
            INNER JOIN mall_order_goods g ON o.id = g.order_id
        WHERE
            g.order_id IN (
        SELECT
            g.order_id
        FROM
            mall_order o
            INNER JOIN mall_order_goods g ON o.id = g.order_id
            INNER JOIN mk_store_prom_order_real r ON r.order_goods_id = g.id
            AND r.order_id = o.id
        WHERE
            g.goods_id = #{goodsId}
            <if test="statusListOne != null">
                AND o.order_status IN
                <foreach item="statusListOne" collection="statusListOne" open="(" separator="," close=")">
                    #{statusListOne}
                </foreach>
            </if>
            )
            AND g.id NOT IN (
        SELECT
            g.id
        FROM
            mall_order o
            INNER JOIN mall_order_goods g ON o.id = g.order_id
            INNER JOIN mk_store_prom_order_real r ON r.order_goods_id = g.id
            AND r.order_id = o.id
        WHERE
            g.goods_id = #{goodsId}
            <if test="statusListTwo != null">
                AND o.order_status IN
                <foreach item="statusListTwo" collection="statusListTwo" open="(" separator="," close=")">
                    #{statusListTwo}
                </foreach>
            </if>
            )
    </select>
    <update id="updateStorePromStat" parameterType="map">
        update mk_store_prom_stat set
          pay_order_num = #{payOrderNum} ,
          payer_num = #{payerNum} ,
          receiv_money = #{receivMoney} ,
          pay_case_num = #{payCaseNum} ,
          act_money = #{actMoney}
        where prom_stat_id = #{promStatId}
    </update>


    <select id="getStoreRealByGoodsIdList" resultType="map">
        SELECT
            r.prom_order_real_id
        FROM
            mall_order o
            INNER JOIN mall_order_goods g ON o.id = g.order_id
            INNER JOIN mk_store_prom_order_real r ON r.order_goods_id = g.id
            AND r.order_id = o.id
        WHERE
            g.goods_id = #{goodsId}
            <if test="statusList != null">
                AND o.order_status IN
                <foreach item="statusList" collection="statusList" open="(" separator="," close=")">
                    #{statusList}
                </foreach>
            </if>
          and is_prom_stat_scan = 0
    </select>
    <update id="updatePromRealIsPromScan" parameterType="map">
        update mk_store_prom_order_real set is_prom_stat_scan = #{isPromStatScan} where prom_order_real_id=#{promOrderRealId}
    </update>

</mapper>