<?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.MemberOrderMapper">

    <insert id="saveMemberOrder" useGeneratedKeys="true" keyProperty="id" keyColumn="id"
            parameterType="cn.pluss.platform.entity.MemberOrder">
        insert into tb_pluss_member_order(merchantCode, merchantName, storeId, storeName, memberCode, nickName,
                                          createDt, orderType, orderFee, orderStatus, rechargeType, staffCode,
                                          staffName, orderNumber, weChatTransNo, aliPayTransNo)
        VALUES (#{merchantCode}, #{merchantName}, #{storeId}, #{storeName}, #{memberCode}, #{nickName}, #{createDt},
                #{orderType}, #{orderFee}, #{orderStatus}, #{rechargeType}, #{staffCode}, #{staffName}, #{orderNumber},
                #{weChatTransNo}, #{aliPayTransNo})
    </insert>

    <insert id="saveMemberOrderBatch">
        insert into
        tb_pluss_member_order(merchantCode,merchantName,storeId,storeName,memberCode,nickName,createDt,orderType,orderFee,orderStatus,rechargeType,staffCode,staffName)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{merchantCode},#{merchantName},#{storeId},#{storeName},#{memberCode},#{nickName},#{createDt},#{orderType},#{orderFee},#{orderStatus},#{rechargeType},#{staffCode},#{staffName}
            )
        </foreach>
    </insert>

    <select id="queryMemberOrder" parameterType="cn.pluss.platform.entity.MemberOrder"
            resultType="cn.pluss.platform.entity.MemberOrder">
        SELECT * from tb_pluss_member_order
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="storeId!= null">
                and storeId = #{storeId}
            </if>
            <if test="storeName!= null">
                and storeName = #{storeName}
            </if>
            <if test="memberCode!= null">
                and memberCode = #{memberCode}
            </if>
            <if test="nickName!= null">
                and nickName = #{nickName}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="orderType!= null">
                and orderType = #{orderType}
            </if>
            <if test="orderFee!= null">
                and orderFee = #{orderFee}
            </if>
            <if test="orderStatus!= null">
                and orderStatus = #{orderStatus}
            </if>
            <if test="rechargeType!= null">
                and rechargeType = #{rechargeType}
            </if>
            <if test="staffCode!= null">
                and staffCode = #{staffCode}
            </if>
            <if test="staffName!= null">
                and staffName = #{staffName}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="weChatTransNo!= null">
                and weChatTransNo = #{weChatTransNo}
            </if>
            <if test="aliPayTransNo!= null">
                and aliPayTransNo = #{aliPayTransNo}
            </if>
            <if test="channelNo!= null">
                and channelNo = #{channelNo}
            </if>
        </where>
    </select>

    <select id="queryMemberOrderList" parameterType="cn.pluss.platform.entity.MemberOrder"
            resultType="cn.pluss.platform.entity.MemberOrder">
        SELECT * from tb_pluss_member_order
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="storeId!= null">
                and storeId = #{storeId}
            </if>
            <if test="storeName!= null">
                and storeName = #{storeName}
            </if>
            <if test="memberCode!= null">
                and memberCode = #{memberCode}
            </if>
            <if test="nickName!= null">
                and nickName = #{nickName}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="orderType!= null">
                and orderType = #{orderType}
            </if>
            <if test="orderFee!= null">
                and orderFee = #{orderFee}
            </if>
            <if test="orderStatus!= null">
                and orderStatus = #{orderStatus}
            </if>
            <if test="rechargeType!= null">
                and rechargeType = #{rechargeType}
            </if>
            <if test="staffCode!= null">
                and staffCode = #{staffCode}
            </if>
            <if test="rechargeType!= null">
                and staffName = #{staffName}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="weChatTransNo!= null">
                and weChatTransNo = #{weChatTransNo}
            </if>
            <if test="aliPayTransNo!= null">
                and aliPayTransNo = #{aliPayTransNo}
            </if>
            <if test="channelNo!= null">
                and channelNo = #{channelNo}
            </if>

        </where>
    </select>

    <update id="updateMemberOrder" parameterType="cn.pluss.platform.entity.MemberOrder">
        update tb_pluss_member_order
        <set>
            <if test="merchantCode!= null">
                merchantCode = #{merchantCode},
            </if>
            <if test="merchantName!= null">
                merchantName = #{merchantName},
            </if>
            <if test="storeId!= null">
                storeId = #{storeId},
            </if>
            <if test="storeName!= null">
                storeName = #{storeName},
            </if>
            <if test="memberCode!= null">
                memberCode = #{memberCode},
            </if>
            <if test="nickName!= null">
                nickName = #{nickName},
            </if>
            <if test="createDt!= null">
                createDt = #{createDt},
            </if>
            <if test="orderType!= null">
                orderType = #{orderType},
            </if>
            <if test="orderFee!= null">
                orderFee = #{orderFee},
            </if>
            <if test="orderStatus!= null">
                orderStatus = #{orderStatus},
            </if>
            <if test="rechargeType!= null">
                rechargeType = #{rechargeType},
            </if>
            <if test="staffCode!= null">
                staffCode = #{staffCode},
            </if>
            <if test="staffName!= null">
                staffName = #{staffName},
            </if>
            <if test="orderNumber!= null">
                orderNumber = #{orderNumber},
            </if>
            <if test="weChatTransNo!= null">
                weChatTransNo = #{weChatTransNo},
            </if>
            <if test="aliPayTransNo!= null">
                aliPayTransNo = #{aliPayTransNo},
            </if>
            <if test="channelNo!= null">
                channelNo = #{channelNo}
            </if>

        </set>
        where id=#{id}
    </update>

    <delete id="deleteMemberOrder" parameterType="cn.pluss.platform.entity.MemberOrder">
        DELETE
        FROM tb_pluss_member_order
        where id = #{id}
    </delete>

    <select id="queryMemberOrderPage" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.MemberOrder">
        SELECT * from tb_pluss_member_order
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="storeId!= null">
                and storeId = #{storeId}
            </if>
            <if test="storeName!= null">
                and storeName = #{storeName}
            </if>
            <if test="memberCode!= null">
                and memberCode = #{memberCode}
            </if>
            <if test="nickName!= null">
                and nickName = #{nickName}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="orderType!= null">
                and orderType = #{orderType}
            </if>
            <if test="orderFee!= null">
                and orderFee = #{orderFee}
            </if>
            <if test="orderStatus!= null">
                and orderStatus = #{orderStatus}
            </if>
            <if test="rechargeType!= null">
                and rechargeType = #{rechargeType}
            </if>
            <if test="staffCode!= null">
                and staffCode = #{staffCode}
            </if>
            <if test="rechargeType!= null">
                and staffName = #{staffName}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="weChatTransNo!= null">
                and weChatTransNo = #{weChatTransNo}
            </if>
            <if test="aliPayTransNo!= null">
                and aliPayTransNo = #{aliPayTransNo}
            </if>
        </where>
        order by id desc limit #{pageSize} offset #{offset}
    </select>

    <select id="queryMemberOrderPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_member_order
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="storeId!= null">
                and storeId = #{storeId}
            </if>
            <if test="storeName!= null">
                and storeName = #{storeName}
            </if>
            <if test="memberCode!= null">
                and memberCode = #{memberCode}
            </if>
            <if test="nickName!= null">
                and nickName = #{nickName}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="orderType!= null">
                and orderType = #{orderType}
            </if>
            <if test="orderFee!= null">
                and orderFee = #{orderFee}
            </if>
            <if test="orderStatus!= null">
                and orderStatus = #{orderStatus}
            </if>
            <if test="rechargeType!= null">
                and rechargeType = #{rechargeType}
            </if>
            <if test="staffCode!= null">
                and staffCode = #{staffCode}
            </if>
            <if test="rechargeType!= null">
                and staffName = #{staffName}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="weChatTransNo!= null">
                and weChatTransNo = #{weChatTransNo}
            </if>
            <if test="aliPayTransNo!= null">
                and aliPayTransNo = #{aliPayTransNo}
            </if>
        </where>
    </select>

    <select id="queryMemberOrderTotalOrderFeeByTime" parameterType="java.util.Map" resultType="java.lang.Double">
        SELECT sum(orderFee) from tb_pluss_member_order
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="storeId!= null">
                and storeId = #{storeId}
            </if>
            <if test="storeName!= null">
                and storeName = #{storeName}
            </if>
            <if test="memberCode!= null">
                and memberCode = #{memberCode}
            </if>
            <if test="nickName!= null">
                and nickName = #{nickName}
            </if>
            <if test="orderType!= null">
                and orderType = #{orderType}
            </if>
            <if test="orderFee!= null">
                and orderFee = #{orderFee}
            </if>
            <if test="orderStatus!= null">
                and orderStatus = #{orderStatus}
            </if>
            <if test="rechargeType!= null">
                and rechargeType = #{rechargeType}
            </if>
            <if test="staffCode!= null">
                and staffCode = #{staffCode}
            </if>
            <if test="rechargeType!= null">
                and staffName = #{staffName}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>

            <if test="weChatTransNo!= null">
                and weChatTransNo = #{weChatTransNo}
            </if>
            <if test="aliPayTransNo!= null">
                and aliPayTransNo = #{aliPayTransNo}
            </if>

        </where>
        and createDt &gt; #{startTime} and createDt &lt; #{endTime}
    </select>

    <select id="queryMemberOrderPageCountByTime" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_member_order
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="storeId!= null">
                and storeId = #{storeId}
            </if>
            <if test="storeName!= null">
                and storeName = #{storeName}
            </if>
            <if test="memberCode!= null">
                and memberCode = #{memberCode}
            </if>
            <if test="nickName!= null">
                and nickName = #{nickName}
            </if>
            <if test="orderType!= null">
                and orderType = #{orderType}
            </if>
            <if test="orderFee!= null">
                and orderFee = #{orderFee}
            </if>
            <if test="orderStatus!= null">
                and orderStatus = #{orderStatus}
            </if>
            <if test="rechargeType!= null and rechargeType!=''">
                and rechargeType = #{rechargeType}
            </if>
            <if test="staffCode!= null">
                and staffCode = #{staffCode}
            </if>
            <if test="staffName!= null">
                and staffName = #{staffName}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="weChatTransNo!= null">
                and weChatTransNo = #{weChatTransNo}
            </if>
            <if test="aliPayTransNo!= null">
                and aliPayTransNo = #{aliPayTransNo}
            </if>
            <if test="startTime!= null and startTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')>=DATE_FORMAT(#{startTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="endTime!= null and endTime!='' ">
                <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="staffCodeList!= null">
                and (
                <foreach collection="staffCodeList" item="item" index="index" separator="or">
                    staffCode = #{item}
                </foreach>
                )
            </if>

            <if test="storeIdList!= null">
                and (
                <foreach collection="storeIdList" item="item" index="index" separator="or">
                    storeId = #{item}
                </foreach>
                )
            </if>
            <if test="isPay!= null and isPay!=''">
                and orderStatus <![CDATA[<> ]]> 0
            </if>

            <!--  and createDt &gt; #{startTime} and createDt &lt; #{endTime} -->
        </where>

    </select>

    <select id="queryMemberOrderPageByTime" parameterType="java.util.Map"
            resultType="cn.pluss.platform.entity.MemberOrder">
        SELECT * from tb_pluss_member_order
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="storeId!= null">
                and storeId = #{storeId}
            </if>
            <if test="storeName!= null">
                and storeName = #{storeName}
            </if>
            <if test="memberCode!= null">
                and memberCode = #{memberCode}
            </if>
            <if test="nickName!= null">
                and nickName = #{nickName}
            </if>
            <if test="orderType!= null">
                and orderType = #{orderType}
            </if>
            <if test="orderFee!= null">
                and orderFee = #{orderFee}
            </if>
            <if test="orderStatus!= null">
                and orderStatus = #{orderStatus}
            </if>
            <if test="rechargeType!= null and rechargeType!=''">
                and rechargeType = #{rechargeType}
            </if>
            <if test="staffCode!= null">
                and staffCode = #{staffCode}
            </if>
            <if test="staffName!= null">
                and staffName = #{staffName}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="startTime!= null and startTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')>=DATE_FORMAT(#{startTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="endTime!= null and endTime!='' ">
                <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="staffCodeList!= null">
                and (
                <foreach collection="staffCodeList" item="item" index="index" separator="or">
                    staffCode = #{item}
                </foreach>
                )
            </if>
            <if test="storeIdList!= null">
                and (
                <foreach collection="storeIdList" item="item" index="index" separator="or">
                    storeId = #{item}
                </foreach>
                )
            </if>
        </where>
        <!--  and createDt &gt; #{startTime} and createDt &lt; #{endTime} -->
        order by id desc limit #{pageSize} offset #{offset}
    </select>

    <!-- 统计商家的会员消费金额  充值金额 -->
    <select id="sumMemberOrderMoney" parameterType="java.util.Map" resultType="java.lang.Double">
        SELECT IFNULL(SUM(orderFee),0) FROM tb_pluss_member_order
        WHERE merchantCode=#{merchantCode} AND orderStatus='1' AND orderType=#{orderType}
        <if test="merchantName!= null">
            and merchantName = #{merchantName}
        </if>
        <if test="storeId!= null">
            and storeId = #{storeId}
        </if>
        <if test="storeName!= null">
            and storeName = #{storeName}
        </if>
        <if test="memberCode!= null">
            and memberCode = #{memberCode}
        </if>
        <if test="nickName!= null">
            and nickName = #{nickName}
        </if>
        <if test="orderType!= null">
            and orderType = #{orderType}
        </if>
        <if test="orderFee!= null">
            and orderFee = #{orderFee}
        </if>
        <if test="orderStatus!= null">
            and orderStatus = #{orderStatus}
        </if>
        <if test="rechargeType!= null">
            and rechargeType = #{rechargeType}
        </if>

        <if test="startTime!= null and startTime!=''">
            <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')>=DATE_FORMAT(#{startTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
        </if>
        <if test="endTime!= null and endTime!='' ">
            <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
        </if>
        <if test="staffCodeList!= null">
            and (
            <foreach collection="staffCodeList" item="item" index="index" separator="or">
                staffCode = #{item}
            </foreach>
            )
        </if>
        <if test="storeIdList!= null">
            and (
            <foreach collection="storeIdList" item="item" index="index" separator="or">
                storeId = #{item}
            </foreach>
            )
        </if>
    </select>

    <select id="queryMemberOrderCountByTimeGroupData" resultType="java.util.Map">
        SELECT count(1) count, orderType from tb_pluss_member_order
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="storeId!= null">
                and storeId = #{storeId}
            </if>
            <if test="storeName!= null">
                and storeName = #{storeName}
            </if>
            <if test="memberCode!= null">
                and memberCode = #{memberCode}
            </if>
            <if test="nickName!= null">
                and nickName = #{nickName}
            </if>
            <if test="orderType!= null">
                and orderType = #{orderType}
            </if>
            <if test="orderFee!= null">
                and orderFee = #{orderFee}
            </if>
            <if test="orderStatus!= null">
                and orderStatus = #{orderStatus}
            </if>
            <if test="rechargeType!= null and rechargeType!=''">
                and rechargeType = #{rechargeType}
            </if>
            <if test="staffCode!= null">
                and staffCode = #{staffCode}
            </if>
            <if test="staffName!= null">
                and staffName = #{staffName}
            </if>
            <if test="orderNumber!= null">
                and orderNumber = #{orderNumber}
            </if>
            <if test="weChatTransNo!= null">
                and weChatTransNo = #{weChatTransNo}
            </if>
            <if test="aliPayTransNo!= null">
                and aliPayTransNo = #{aliPayTransNo}
            </if>
            <if test="startTime!= null and startTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')>=DATE_FORMAT(#{startTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="endTime!= null and endTime!='' ">
                <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="staffCodeList!= null">
                and (
                <foreach collection="staffCodeList" item="item" index="index" separator="or">
                    staffCode = #{item}
                </foreach>
                )
            </if>

            <if test="storeIdList!= null">
                and (
                <foreach collection="storeIdList" item="item" index="index" separator="or">
                    storeId = #{item}
                </foreach>
                )
            </if>
            <if test="isPay!= null and isPay!=''">
                and orderStatus <![CDATA[<> ]]> 0
            </if>

            <!--  and createDt &gt; #{startTime} and createDt &lt; #{endTime} -->
        </where>
        GROUP BY orderType
    </select>


</mapper>
