<?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.iformall.mapper.WxCardSpendMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCardSpend">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="card_id" jdbcType="BIGINT" property="cardId"/>
        <result column="owner_id" jdbcType="BIGINT" property="ownerId"/>
        <result column="merchant_id" jdbcType="BIGINT" property="merchantId"/>
        <result column="order_id" jdbcType="BIGINT" property="orderId"/>
        <result column="pos_order_id" jdbcType="BIGINT" property="posOrderId"/>
        <result column="deduction_amount" jdbcType="INTEGER" property="deductionAmount"/>
        <result column="payment" jdbcType="INTEGER" property="payment"/>
        <result column="real_payment" jdbcType="INTEGER" property="realPayment"/>
        <result column="card_remain_amount" jdbcType="INTEGER" property="cardRemainAmount"/>
        <result column="card_before_amount" jdbcType="INTEGER" property="cardBeforeAmount"/>
        <result column="card_remain_real_amount" jdbcType="INTEGER" property="cardRemainRealAmount"/>
        <result column="card_before_real_amount" jdbcType="INTEGER" property="cardBeforeRealAmount"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="pay_status" jdbcType="INTEGER" property="payStatus"/>
        <result column="pay_from" jdbcType="INTEGER" property="payFrom"/>
    </resultMap>

    <sql id="allColumns">
	    `id`,`tenant_id`,`card_id`,`owner_id`,`merchant_id`,`order_id`,`pos_order_id`,
	    `deduction_amount`,`payment`,`real_payment`, `card_remain_amount`,
	    `card_before_amount`,`card_remain_real_amount`,`card_before_real_amount`,
	    `create_date`, `update_date`, `pay_status`, `pay_from`
	</sql>

    <sql id="dynamicWhereConditions">
        where 1 = 1
        <if test=" null != id ">
            and `id` = #{id}
        </if>
        <if test=" null != tenantId ">
            and `tenant_id` = #{tenantId}
        </if>
        <if test=" null != cardId ">
            and `card_id` = #{cardId}
        </if>
        <if test=" null != ownerId ">
            and `owner_id` = #{ownerId}
        </if>
        <if test=" null != merchantId ">
            and `merchant_id` = #{merchantId}
        </if>
        <if test=" null != orderId ">
            and `order_id` = #{orderId}
        </if>
        <if test=" null != posOrderId ">
            and `pos_order_id` = #{posOrderId}
        </if>
        <if test=" null != deductionAmount ">
            and `deduction_amount` = #{deductionAmount}
        </if>
        <if test=" null != payment ">
            and `payment` = #{payment}
        </if>
        <if test=" null != realPayment ">
            and `real_payment` = #{realPayment}
        </if>
        <if test=" null != cardRemainAmount ">
            and `card_remain_amount` = #{cardRemainAmount}
        </if>
        <if test=" null != cardBeforeAmount ">
            and `card_before_amount` = #{cardBeforeAmount}
        </if>
        <if test=" null != cardRemainRealAmount ">
            and `card_remain_real_amount` = #{cardRemainRealAmount}
        </if>
        <if test=" null != cardBeforeRealAmount ">
            and `card_before_real_amount` = #{cardBeforeRealAmount}
        </if>
        <if test=" null != createDate ">
            and `create_date` = #{createDate}
        </if>
        <if test=" null != updateDate ">
            and `update_date` = #{updateDate}
        </if>
        <if test=" null != payStatus ">
            and `pay_status` = #{payStatus}
        </if>
        <if test=" null != payFrom ">
            and `pay_from` = #{payFrom}
        </if>
        <if test=" null != startdate and  null!=enddate">
            and `create_date` between #{startdate} and #{enddate}
        </if>

        <if test=" null != ids ">
            and id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findList" parameterType="com.iformall.domain.po.WxCardSpend" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_card_spend
        <include refid="dynamicWhereConditions"/>
    </select>

    <select id="updateByCond" parameterType="com.iformall.domain.po.WxCardSpend">
        update wx_card_spend
        set `pay_status` = #{payStatus}, `update_date` = #{updateDate}
        <where>
            <if test=" null != tenantId ">
                and `tenant_id` = #{tenantId}
            </if>
            <if test=" null != startdate and  null!=enddate">
                and `create_date` between #{startdate} and #{enddate}
            </if>
            <if test=" null != ids ">
                and id in
                <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                    #{idItem}
                </foreach>
            </if>
        </where>
    </select>

    <delete id="deleteForPrevCancel" parameterType="com.iformall.domain.po.WxCardSpend">
        delete from wx_card_spend
        where `tenant_id` = #{tenantId} and `id` = #{id} and `pay_status` = #{payStatus}
    </delete>

    <select id="findCount" parameterType="com.iformall.domain.po.WxCardSpend" resultType="Integer">
        select count(*)
        from wx_card_spend cs
        <if test=" null != businessId ">
            inner join wx_merchant m
            on m.id = cs.merchant_id
            and m.business_id = #{businessId}
            <if test=" null != subBusinessId ">
                and m.sub_business_id = #{subBusinessId}
            </if>
        </if>
        where 1 = 1
        <if test=" null != ownerId ">
            and cs.`owner_id` = #{ownerId}
        </if>
        <if test=" null != tenantId ">
            and cs.`tenant_id` = #{tenantId}
        </if>
        <if test=" null != startdate and  null!=enddate">
            and cs.`create_date` between #{startdate} and #{enddate}
        </if>
        <if test="  0 == timeSlot">
            and date_format(cs.create_date,'%H:%m') &gt;= '06:00'
            and date_format(cs.create_date,'%H:%m') &lt;  '18:00'
            and date_format(cs.create_date,'%w') in (1,2,3,4,5)
        </if>

        <if test="  1 == timeSlot">
            and ((date_format(cs.create_date,'%H:%m') &gt;= '18:00'
            and date_format(cs.create_date,'%w') in (1,2,3,4))
            or
            ((date_format(cs.create_date,'%H:%m') &lt;  '06:00')
            and date_format(cs.create_date,'%w') in (1,2,3,4,5)))
        </if>

        <if test="  2 == timeSlot">
            and date_format(cs.create_date,'%H:%m') &gt;= '06:00'
            and date_format(cs.create_date,'%H:%m') &lt;  '18:00'
            and date_format(cs.create_date,'%w') in (0,6)
        </if>

        <if test="  3 == timeSlot">
            and ((date_format(cs.create_date,'%H:%m') &gt;= '18:00'
            and date_format(cs.create_date,'%w') in (5,6,0))
            or
            ((date_format(cs.create_date,'%H:%m') &lt;  '06:00')
            and date_format(cs.create_date,'%w') in (6,0)))
        </if>
    </select>

    <select id="getCountByBusinessId" parameterType="com.iformall.domain.po.WxCardSpend" resultType="hashmap">
        select m.business_id, count(m.business_id) bu_count
        from wx_card_spend cs
        inner join wx_merchant m on m.id = cs.merchant_id
        where 1 = 1
        <if test=" null != ownerId ">
            and cs.`owner_id` = #{ownerId}
        </if>
        <if test=" null != tenantId ">
            and cs.`tenant_id` = #{tenantId}
        </if>
        <if test=" null != startdate and  null!=enddate">
            and cs.`create_date` between #{startdate} and #{enddate}
        </if>
        <if test="  0 == timeSlot">
            and date_format(cs.create_date,'%H:%m') &gt;= '06:00'
            and date_format(cs.create_date,'%H:%m') &lt;  '18:00'
            and date_format(cs.create_date,'%w') in (1,2,3,4,5)
        </if>

        <if test="  1 == timeSlot">
            and ((date_format(cs.create_date,'%H:%m') &gt;= '18:00'
            and date_format(cs.create_date,'%w') in (1,2,3,4))
            or
            ((date_format(cs.create_date,'%H:%m') &lt;  '06:00')
            and date_format(cs.create_date,'%w') in (1,2,3,4,5)))
        </if>

        <if test="  2 == timeSlot">
            and date_format(cs.create_date,'%H:%m') &gt;= '06:00'
            and date_format(cs.create_date,'%H:%m') &lt;  '18:00'
            and date_format(cs.create_date,'%w') in (0,6)
        </if>

        <if test="  3 == timeSlot">
            and ((date_format(cs.create_date,'%H:%m') &gt;= '18:00'
            and date_format(cs.create_date,'%w') in (5,6,0))
            or
            ((date_format(cs.create_date,'%H:%m') &lt;  '06:00')
            and date_format(cs.create_date,'%w') in (6,0)))
        </if>
        group by m.business_id
    </select>

    <select id="getCountBySubBusinessId" parameterType="com.iformall.domain.po.WxCardSpend" resultType="hashmap">
        select m.sub_business_id, count(m.sub_business_id) bu_count
        from wx_card_spend cs
        <if test=" null != businessId ">
            inner join wx_merchant m on m.id = cs.merchant_id and m.business_id = #{businessId}
        </if>
        where 1 = 1
        <if test=" null != ownerId ">
            and cs.`owner_id` = #{ownerId}
        </if>
        <if test=" null != tenantId ">
            and cs.`tenant_id` = #{tenantId}
        </if>
        <if test=" null != startdate and  null!=enddate">
            and cs.`create_date` between #{startdate} and #{enddate}
        </if>
        <if test="  0 == timeSlot">
            and date_format(cs.create_date,'%H:%m') &gt;= '06:00'
            and date_format(cs.create_date,'%H:%m') &lt;  '18:00'
            and date_format(cs.create_date,'%w') in (1,2,3,4,5)
        </if>

        <if test="  1 == timeSlot">
            and ((date_format(cs.create_date,'%H:%m') &gt;= '18:00'
            and date_format(cs.create_date,'%w') in (1,2,3,4))
            or
            ((date_format(cs.create_date,'%H:%m') &lt;  '06:00')
            and date_format(cs.create_date,'%w') in (1,2,3,4,5)))
        </if>

        <if test="  2 == timeSlot">
            and date_format(cs.create_date,'%H:%m') &gt;= '06:00'
            and date_format(cs.create_date,'%H:%m') &lt;  '18:00'
            and date_format(cs.create_date,'%w') in (0,6)
        </if>

        <if test="  3 == timeSlot">
            and ((date_format(cs.create_date,'%H:%m') &gt;= '18:00'
            and date_format(cs.create_date,'%w') in (5,6,0))
            or
            ((date_format(cs.create_date,'%H:%m') &lt;  '06:00')
            and date_format(cs.create_date,'%w') in (6,0)))
        </if>
        group by m.sub_business_id
    </select>


    <resultMap id="VoResultMap" type="com.iformall.domain.vo.WxCardSpendVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="card_id" jdbcType="BIGINT" property="cardId"/>
        <result column="owner_id" jdbcType="BIGINT" property="ownerId"/>
        <result column="merchant_id" jdbcType="BIGINT" property="merchantId"/>
        <result column="order_id" jdbcType="BIGINT" property="orderId"/>
        <result column="pos_order_id" jdbcType="BIGINT" property="posOrderId"/>
        <result column="deduction_amount" jdbcType="INTEGER" property="deductionAmount"/>
        <result column="payment" jdbcType="INTEGER" property="payment"/>
        <result column="real_payment" jdbcType="INTEGER" property="realPayment"/>
        <result column="card_remain_amount" jdbcType="INTEGER" property="cardRemainAmount"/>
        <result column="card_before_amount" jdbcType="INTEGER" property="cardBeforeAmount"/>
        <result column="card_remain_real_amount" jdbcType="INTEGER" property="cardRemainRealAmount"/>
        <result column="card_before_real_amount" jdbcType="INTEGER" property="cardBeforeRealAmount"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="pay_status" jdbcType="INTEGER" property="payStatus"/>

        <result column="title" jdbcType="VARCHAR" property="title"/>

        <result column="name" jdbcType="VARCHAR" property="merchantName"/>

        <result column="subsidy" jdbcType="INTEGER" property="subsidy"/>
        <result column="real_subsidy" jdbcType="INTEGER" property="realSubsidy"/>
        <result column="status" jdbcType="INTEGER" property="status"/>

        <result column="onick_name" jdbcType="VARCHAR" property="onickName"/>
        <result column="ou_phone" jdbcType="VARCHAR" property="ouPhone"/>
    </resultMap>

    <sql id="allVoColumns">
	    cs.`id`,cs.`tenant_id`,cs.`card_id`,cs.`owner_id`,cs.`merchant_id`,cs.`order_id`,cs.`deduction_amount`,cs.`payment`,cs.`real_payment`,
	    cs.`card_remain_amount`,cs.`card_before_amount`,cs.`card_remain_real_amount`,cs.`card_before_real_amount`,
	    cs.`create_date`,cs.`update_date`, cs.`pay_status`,
	    m.`name`, c.`title`,
	    ms.`subsidy`,ms.`real_subsidy`,ms.`status`,
	    ou.`nick_name` as onick_name, ou.`phone` as ou_phone
	</sql>

    <sql id="dynamicVoWhereConditions">
        where 1 = 1
        <if test=" null != id ">
            and cs.`id` = #{id}
        </if>
        <if test=" null != tenantId ">
            and cs.`tenant_id` = #{tenantId}
        </if>
        <if test=" null != cardId ">
            and cs.`card_id` = #{cardId}
        </if>
        <if test=" null != ownerId ">
            and cs.`owner_id` = #{ownerId}
        </if>
        <if test=" null != merchantId ">
            and cs.`merchant_id` = #{merchantId}
        </if>
        <if test=" null != orderId ">
            and cs.`order_id` = #{orderId}
        </if>
        <if test=" null != deductionAmount ">
            and cs.`deduction_amount` = #{deductionAmount}
        </if>
        <if test=" null != payment ">
            and cs.`payment` = #{payment}
        </if>
        <if test=" null != realPayment ">
            and cs.`real_payment` = #{realPayment}
        </if>
        <if test=" null != cardRemainAmount ">
            and cs.`card_remain_amount` = #{cardRemainAmount}
        </if>
        <if test=" null != cardBeforeAmount ">
            and cs.`card_before_amount` = #{cardBeforeAmount}
        </if>
        <if test=" null != cardRemainRealAmount ">
            and cs.`card_remain_real_amount` = #{cardRemainRealAmount}
        </if>
        <if test=" null != cardBeforeRealAmount ">
            and cs.`card_before_real_amount` = #{cardBeforeRealAmount}
        </if>
        <if test=" null != createDate ">
            and cs.`create_date` = #{createDate}
        </if>
        <if test=" null != updateDate ">
            and cs.`update_date` = #{updateDate}
        </if>
        <if test=" null != payStatus ">
            and cs.`pay_status` = #{payStatus}
        </if>
        <if test=" null != status ">
            and ms.`status` = #{status}
        </if>
        <if test=" null != ouPhone and '' != ouPhone ">
            and ou.`phone` = #{ouPhone}
        </if>
        <if test=" null != startdate and  null!=enddate">
            and cs.`create_date` between #{startdate} and #{enddate}
        </if>
        <if test=" null != payStatusS ">
            and cs.`pay_status` in
            <foreach collection="payStatusS" index="index" item="sItem" open="(" separator="," close=")">
                #{sItem}
            </foreach>
        </if>
        <if test=" null != ids ">
            and cs.id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
    </sql>

    <select id="findCardSpendVoList" parameterType="com.iformall.domain.vo.WxCardSpendVo" resultMap="VoResultMap">
        select
        <include refid="allVoColumns"/>
        from wx_card_spend cs
        left join wx_coupon_order co on co.id = cs.card_id
        left join wx_coupon c on c.id = co.coupon_id
        left join wx_merchant m on m.id = cs.merchant_id
        left join wx_merchant_subsidy ms on ms.order_id = cs.order_id
        left join wx_c_user ou on ou.id = co.owner_id
        <include refid="dynamicVoWhereConditions"/>
        order by cs.create_date DESC
    </select>

    <select id="sumCardSpendVoForMerchant" parameterType="com.iformall.domain.vo.WxCardSpendVo" resultType="hashmap">
        select cs.merchant_id, m.name as merchantName,
        IFNULL(SUM(cs.`deduction_amount`),0) as sum_deduction_amount,
        IFNULL(SUM(cs.`payment`),0) as sum_payment,
        IFNULL(SUM(cs.`real_payment`),0) as sum_real_payment,
        IFNULL(SUM(ms.`subsidy`), 0) as sum_subsidy,
        IFNULL(SUM(ms.`real_subsidy`), 0) as sum_real_subsidy
        from wx_card_spend cs
        left join wx_coupon_order co on co.id = cs.card_id
        left join wx_coupon c on c.id = co.coupon_id
        left join wx_merchant m on m.id = cs.merchant_id
        left join wx_merchant_subsidy ms on ms.order_id = cs.order_id
        left join wx_c_user ou on ou.id = co.owner_id
        <include refid="dynamicVoWhereConditions"/>
        group by cs.merchant_id
    </select>

    <select id="sumCardSpendVoForOwner" parameterType="com.iformall.domain.vo.WxCardSpendVo" resultType="hashmap">
        select cs.owner_id,
        IFNULL(SUM(cs.`deduction_amount`),0) as sum_deduction_amount,
        IFNULL(SUM(cs.`payment`),0) as sum_payment,
        IFNULL(SUM(cs.`real_payment`),0) as sum_real_payment,
        IFNULL(SUM(ms.`subsidy`), 0) as sum_subsidy,
        IFNULL(SUM(ms.`real_subsidy`), 0) as sum_real_subsidy
        from wx_card_spend cs
        left join wx_coupon_order co on co.id = cs.card_id
        left join wx_coupon c on c.id = co.coupon_id
        left join wx_merchant m on m.id = cs.merchant_id
        left join wx_merchant_subsidy ms on ms.order_id = cs.order_id
        left join wx_c_user ou on ou.id = co.owner_id
        <include refid="dynamicVoWhereConditions"/>
        group by cs.owner_id
    </select>


</mapper>
