<?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.yiqi.order.mapper.OrderRechargeMapper">

    <resultMap type="OrderRecharge" id="OrderRechargeResult">
        <result property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="orgId" column="org_id"/>
        <result property="appUserId" column="app_user_id"/>
        <result property="payTime" column="pay_time"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="payType" column="pay_type"/>
        <result property="outTradeOrderNo" column="out_trade_order_no"/>
        <result property="rechargeAmount" column="recharge_amount"/>
        <result property="giveAmount" column="give_amount"/>
        <result property="givePointAmount" column="give_point_amount"/>
        <result property="activityId" column="activity_id"/>
        <result property="rechargeTypeId" column="recharge_type_id"/>
        <result property="orderStatus" column="order_status"/>
        <result property="refundApplyTime" column="refund_apply_time"/>
        <result property="refundTime" column="refund_time"/>
        <result property="refundAmount" column="refund_amount"/>
        <result property="refundDescribe" column="refund_describe"/>
        <result property="delFlag" column="del_flag"/>
        <result property="title" column="title"/>
        <result property="orderSource" column="order_source"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectOrderRechargeVo">
        select * from order_recharge
    </sql>

    <select id="selectOrderRechargeList"  resultType="com.yiqi.order.domain.vo.OrderRechargeListVO">
        select a.*,
               u.nick_name as  nickName ,u.real_name as realName, u.phone_number as phoneNumber ,
               s.name as storeName
        from order_recharge a
        left join app_user u on a.app_user_id = u.id
        left join sys_store s on a.store_id = s.id
        <where>
            and a.del_flag='0' and a.refund_status != '3'
            <if test="orderNo != null  and orderNo != ''">
                and a.order_no = #{orderNo}
            </if>
            <if test="storeName != null ">
                and s.name = #{storeName}
            </if>
            <if test="phoneNumber != null ">
                and u.phone_number = #{phoneNumber}
            </if>
            <if test="realName != null ">
                and u.real_name = #{realName}
            </if>
            <if test="orderStatus != null  and orderStatus != ''">
                and a.order_status = #{orderStatus}
            </if>
            <if test="orgId != null ">
                and a.store_id = #{orgId}
            </if>
            <if test="createTimeFrom != null and createTimeFrom !=''">
                <![CDATA[and a.create_time >= concat(#{createTimeFrom}, ' 00:00:00')]]>
            </if>
            <if test="createTimeTo != null and createTimeTo !=''">
                <![CDATA[and a.create_time <= concat(#{createTimeTo}, ' 23:59:59')]]>
            </if>
            <if test="beginPayTime != null and beginPayTime !=''">
                <![CDATA[and a.pay_time >= concat(#{beginPayTime}, ' 00:00:00')]]>
            </if>
            <if test="endPayTime != null and endPayTime !=''">
                <![CDATA[and a.pay_time <= concat(#{endPayTime}, ' 23:59:59')]]>
            </if>
            order by a.create_time DESC

            <if test="pageSizeExport != 0">
                limit #{pageSizeExport}
            </if>
        </where>

    </select>

    <select id="selectRefundList"  resultType="com.yiqi.order.domain.vo.OrderRechargeListVO">
        select  a.id as id, a.order_no as orderNo,
                a.recharge_amount as rechargeAmount,
                a.give_amount as giveAmount,
                a.refund_amount as refundAmount, a.refund_time as refundTime,
                a.refund_describe as refundDescribe, a.order_status as orderStatus,
                a.refund_apply_time as refundApplyTime,
                a.remark as remark,

                u.id as appUserId, u.nick_name as nickName ,u.real_name as realName,
                u.phone_number as phoneNumber ,

                s.name as storeName
        from order_recharge a
        left join app_user u on a.app_user_id = u.id
        left join sys_store s on a.store_id = s.id
        <where>
            and a.del_flag='0'
            and a.order_status IN ('4','5','6')
            <if test="orderNo != null  and orderNo != ''">
                and a.order_no = #{orderNo}
            </if>
            <if test="storeName != null ">
                and s.name = #{storeName}
            </if>
            <if test="phoneNumber != null ">
                and u.phone_number = #{phoneNumber}
            </if>
            <if test="realName != null ">
                and u.real_name = #{realName}
            </if>
            order by refund_time DESC
        </where>
    </select>

    <select id="selectOrderRechargeById" parameterType="Long"
            resultMap="OrderRechargeResult">
        <include refid="selectOrderRechargeVo"/>
        where id = #{id}
        and del_flag='0'
    </select>

    <select id="getPayDetailById" resultType="com.yiqi.order.domain.OrderRechargeInstalment">
        select b.* from order_recharge a left join order_recharge_instalment b on a.order_no = b.mstr_order_no
        where
            a.id = #{orderId}
          and a.del_flag = '0'
        <if test="orderStatus != null and orderStatus != ''">
            and b.order_status = #{orderStatus}
        </if>
        order by a.create_time DESC
    </select>
    <select id="getRechargeRecordByUserId" resultType="com.yiqi.order.domain.vo.OrderRechargeRecordVO">
        select  a.order_no as orderNo,
        a.recharge_amount as rechargeAmount,
        a.give_amount as giveAmount,
        a.pay_time as payTime, a.pay_Type as payType,
        a.create_time as createTime,
        a.welfare_amount as welfareAmount,
        u.real_name as userName, u.phone_number as phoneNumber ,
        s.name as storeName
        from order_recharge a
        left join app_user u on a.app_user_id = u.id
        left join sys_store s on a.store_id = s.id
        <where>
        and a.del_flag='0'
        and a.order_status in ('1','2')
        and a.app_user_id = #{userId}
        order by pay_time DESC
        </where>
    </select>
    <select id="refundRechargeOrderList" resultType="com.yiqi.order.domain.vo.OrderRechargeRefundListVO">
        select a.id as id, a.order_no as orderNo,
        a.pay_amount as payAmount ,a.recharge_amount as rechargeAmount,
        a.give_amount as giveAmount, a.give_point_amount as givePointAmount,
        a.pay_time as payTime, a.pay_Type as payType,
        a.create_time as createTime, a.order_status as orderStatus,
        a.welfare_amount as welfareAmount,
        u.nick_name as  nickName ,u.real_name as realName, u.phone_number as phoneNumber ,
        s.name as storeName, a.refund_status as refundStatus, a.refund_time as refundTime,
        a.refund_apply_time as refundApplyTime, a.refund_describe as refundDescribe,
        a.refund_amount as refundAmount
        from order_recharge a
        left join app_user u on a.app_user_id = u.id
        left join sys_store s on a.store_id = s.id
        <where>
            and a.del_flag='0'
            and a.refund_status in ('1','2')
            <if test="orderNo != null  and orderNo != ''">
                and a.order_no = #{orderNo}
            </if>
            <if test="storeName != null ">
                and s.name = #{storeName}
            </if>
            <if test="phoneNumber != null ">
                and u.phone_number = #{phoneNumber}
            </if>
            <if test="realName != null ">
                and u.real_name = #{realName}
            </if>
            <if test="orgId != null ">
                and a.store_id = #{orgId}
            </if>

            <if test="createTimeFrom != null and createTimeFrom !=''">
                <![CDATA[and a.create_time >= concat(#{createTimeFrom}, ' 00:00:00')]]>
            </if>
            <if test="createTimeTo != null and createTimeTo !=''">
                <![CDATA[and a.create_time <= concat(#{createTimeTo}, ' 23:59:59')]]>
            </if>
            <if test="beginRefundTime != null and beginRefundTime !=''">
                <![CDATA[and a.refund_time >= concat(#{beginRefundTime}, ' 00:00:00')]]>
            </if>
            <if test="endRefundTime != null and endRefundTime !=''">
                <![CDATA[and a.refund_time <= concat(#{endRefundTime}, ' 23:59:59')]]>
            </if>
            order by pay_time DESC
            <if test="pageSizeExport != 0">
                limit #{pageSizeExport}
            </if>
        </where>
    </select>
    <select id="listFinancialStatisticsByRechargeOrderToday"
            resultType="com.yiqi.order.domain.vo.OrderRechargeFinancialVO">
        select a.order_no as orderNo , a.pay_amount as payAmount
        from order_recharge a
        <where>
            a.order_status IN ('2','3') and a.refund_status = '0' and a.del_flag='0'
            and a.store_id = #{orgId}
            <if test="orderFinancialDayQueryDTO.beginCreateTime != null and orderFinancialDayQueryDTO.beginCreateTime != ''"><!-- 开始时间检索 -->
                and date_format(a.create_time,'%y%m%d') &gt;= date_format(#{orderFinancialDayQueryDTO.beginCreateTime},'%y%m%d')
            </if>
            <if test="orderFinancialDayQueryDTO.endCreateTime != null and orderFinancialDayQueryDTO.endCreateTime != ''"><!-- 结束时间检索 -->
                and date_format(a.create_time,'%y%m%d') &lt;= date_format(#{orderFinancialDayQueryDTO.endCreateTime},'%y%m%d')
            </if>
        </where>
        order by a.create_time desc
    </select>
    <select id="listFinancialStatisticsByRechargeOrderMTD"
            resultType="com.yiqi.order.domain.vo.OrderRechargeFinancialVO">
        select a.order_no as orderNo , a.pay_amount as payAmount
        from order_recharge a
        <where>
            a.order_status IN ('2','3') and a.refund_status = '0' and a.del_flag='0'
            and a.store_id = #{orgId}
            and a.create_time BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND LAST_DAY(NOW())
        </where>
        order by a.create_time desc
    </select>

    <insert id="insertOrderRecharge" parameterType="OrderRecharge" useGeneratedKeys="true"
            keyProperty="id">
        insert into order_recharge
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">order_no,
            </if>
            <if test="orgId != null">org_id,
            </if>

            <if test="appUserId != null">app_user_id,
            </if>
            <if test="payTime != null">pay_time,
            </if>
            <if test="payAmount != null">pay_amount,
            </if>
            <if test="payType != null">pay_type,
            </if>
            <if test="outTradeOrderNo != null">out_trade_order_no,
            </if>
            <if test="rechargeAmount != null">recharge_amount,
            </if>
            <if test="giveAmount != null">give_amount,
            </if>
            <if test="givePointAmount != null">give_point_amount,
            </if>
            <if test="activityId != null">activity_id,
            </if>
            <if test="rechargeTypeId != null">recharge_type_id,
            </if>
            <if test="orderStatus != null">order_status,
            </if>
            <if test="refundApplyTime != null">refund_apply_time,
            </if>
            <if test="refundTime != null">refund_time,
            </if>
            <if test="refundAmount != null">refund_amount,
            </if>
            <if test="refundDescribe != null">refund_describe,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="title != null">title,
            </if>
            <if test="orderSource != null">order_source,
            </if>
            <if test="createById != null">create_by_id,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateById != null">update_by_id,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="remark != null">remark,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">#{orderNo},
            </if>
            <if test="orgId != null">#{orgId},
            </if>
            <if test="sourceType != null">#{sourceType},
            </if>
            <if test="appUserId != null">#{appUserId},
            </if>
            <if test="payTime != null">#{payTime},
            </if>
            <if test="payAmount != null">#{payAmount},
            </if>
            <if test="payType != null">#{payType},
            </if>
            <if test="outTradeOrderNo != null">#{outTradeOrderNo},
            </if>
            <if test="rechargeAmount != null">#{rechargeAmount},
            </if>
            <if test="giveAmount != null">#{giveAmount},
            </if>
            <if test="givePointAmount != null">#{givePointAmount},
            </if>
            <if test="activityId != null">#{activityId},
            </if>
            <if test="rechargeTypeId != null">#{rechargeTypeId},
            </if>
            <if test="orderStatus != null">#{orderStatus},
            </if>
            <if test="refundApplyTime != null">#{refundApplyTime},
            </if>
            <if test="refundTime != null">#{refundTime},
            </if>
            <if test="refundAmount != null">#{refundAmount},
            </if>
            <if test="refundDescribe != null">#{refundDescribe},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="title != null">#{title},
            </if>
            <if test="orderSource != null">#{orderSource},
            </if>
            <if test="createById != null">#{createById},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateById != null">#{updateById},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
        </trim>
    </insert>

    <update id="updateOrderRecharge" parameterType="OrderRecharge">
        update order_recharge
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNo != null">order_no =
                #{orderNo},
            </if>
            <if test="orgId != null">org_id =
                #{orgId},
            </if>
            <if test="appUserId != null">app_user_id =
                #{appUserId},
            </if>
            <if test="payTime != null">pay_time =
                #{payTime},
            </if>
            <if test="payAmount != null">pay_amount =
                #{payAmount},
            </if>
            <if test="payType != null">pay_type =
                #{payType},
            </if>
            <if test="outTradeOrderNo != null">out_trade_order_no =
                #{outTradeOrderNo},
            </if>
            <if test="rechargeAmount != null">recharge_amount =
                #{rechargeAmount},
            </if>
            <if test="giveAmount != null">give_amount =
                #{giveAmount},
            </if>
            <if test="givePointAmount != null">give_point_amount =
                #{givePointAmount},
            </if>
            <if test="activityId != null">activity_id =
                #{activityId},
            </if>
            <if test="rechargeTypeId != null">recharge_type_id =
                #{rechargeTypeId},
            </if>
            <if test="orderStatus != null">order_status =
                #{orderStatus},
            </if>
            <if test="refundApplyTime != null">refund_apply_time =
                #{refundApplyTime},
            </if>
            <if test="refundTime != null">refund_time =
                #{refundTime},
            </if>
            <if test="refundAmount != null">refund_amount =
                #{refundAmount},
            </if>
            <if test="refundDescribe != null">refund_describe =
                #{refundDescribe},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="title != null">title =
                #{title},
            </if>
            <if test="orderSource != null">order_source =
                #{orderSource},
            </if>
            <if test="createById != null">create_by_id =
                #{createById},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateById != null">update_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
        </trim>
        where id = #{id}
    </update>
    <update id="updatePayAmount">
        update order_recharge set pay_amount = pay_amount + #{payAmount} where order_no = #{orderNo}
    </update>


    <delete id="deleteOrderRechargeById" parameterType="Long">
        update order_recharge set del_flag='2' where id = #{id}
    </delete>

    <delete id="deleteOrderRechargeByIds" parameterType="String">
        update order_recharge set del_flag='2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>



</mapper>