<?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.younike.system.mapper.MerchantOrderMapper">

    <resultMap type="OrdersVO" id="OrderResult">
        <result property="orderId"    column="order_id"    />
        <result property="orderIndex"    column="order_index"    />
        <result property="memberId"    column="member_id"    />
        <result property="merchantId"    column="merchant_id"    />
        <result property="merchantName"    column="merchant_name"    />
        <result property="orderType"    column="order_type"    />
        <result property="orderState"    column="order_state"    />
        <result property="appointmentStartTime"    column="appointment_start_time"    />
        <result property="appointmentEndTime"    column="appointment_end_time"    />
        <result property="serviceStartTime"    column="service_start_time"    />
        <result property="serviceEndTime"    column="service_end_time"    />
        <result property="serviceLongitude"    column="service_longitude"    />
        <result property="serviceLatitude"    column="service_latitude"    />
        <result property="shopAmount"    column="shop_amount"    />
        <result property="isSettle"    column="is_settle"    />
        <result property="orderAmount"    column="order_amount"    />
        <result property="beanAmount"    column="bean_amount"    />
        <result property="memberCouponId"    column="member_coupon_id"    />
        <result property="couponAmount"    column="coupon_amount"    />
        <result property="totalAmount"    column="total_amount"    />
        <result property="payWay"    column="pay_way"    />
        <result property="memo"    column="memo"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="loginIp"    column="login_ip"    />
        <result property="loginDate"    column="login_date"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="column1"    column="column1"    />
        <result property="column2"    column="column2"    />
        <result property="column3"    column="column3"    />
        <result property="Gname"    column="name"    />
        <result property="goodsUnit"    column="goods_unit"    />
        <result property="otherUnit"    column="other_unit"    />
        <result property="memberPhone"    column="phone"    />
        <result property="categoryName"    column="category_name"    />
        <result property="goodsCounts"    column="goods_counts"    />
        <result property="serverName"    column="server_name"    />
        <result property="serverPhone"    column="server_phone"    />
        <result property="price"    column="price"    />
        <result property="addCount"    column="add_counts"    />
        <result property="refStatus"    column="ref_status"    />
        <result property="serverId"    column="server_id"    />
        <result property="addAmount"    column="add_amount"    />
        <result property="maintenanceAmount"    column="maintenance_amount"    />
        <result property="maintenanceRemark"    column="maintenance_remark"    />



    </resultMap>

    <sql id="selectOrderVo">
        select
            o.order_id, order_index,
            o.member_id,
            o.merchant_id, merchant_name, order_type, order_state, appointment_start_time, appointment_end_time, service_start_time, service_end_time, service_longitude, service_latitude, shop_amount, is_settle, order_amount, bean_amount, member_coupon_id, coupon_amount, total_amount, pay_way, memo,
            o.del_flag,
            add_amount,
            o.create_time,
            og.cate_id,
            og.NAME,
            og.goods_unit,
            og.other_unit,
            me.phone,
            cg.category_name,
            ogs.server_name,
            su.phone as server_phone,
            og.price,
            og.add_counts,
            og.goods_counts,
            ore.status as ref_status,
            ogs.server_id,
            o.maintenance_amount,
            o.maintenance_remark
            from `order` o
                LEFT JOIN goods ON goods.merchant_id = o.merchant_id
                LEFT JOIN member me ON me.member_id = o.member_id
                LEFT JOIN order_goods og ON og.order_id = o.order_id
                LEFT JOIN category cg ON cg.category_id = og.cate_id
                LEFT JOIN order_goods_server ogs ON ogs.order_id = o.order_id
                LEFT JOIN server_user su ON su.server_id = ogs.server_id
                left JOIN order_refund ore on ore.order_id=o.order_id
    </sql>

    <select id="selectOrderList" parameterType="Order" resultMap="OrderResult">
        <include refid="selectOrderVo"/>
        <where>
            <if test="orderIndex != null "> and o.order_index = #{orderIndex}</if>
            <if test="memberId != null "> and member_id = #{memberId}</if>
            <if test="merchantId != null "> and merchant_id = #{merchantId}</if>
            <if test="merchantName != null  and merchantName != ''"> and merchant_name like concat('%', #{merchantName}, '%')</if>
            <if test="orderType != null  and orderType != ''"> and order_type = #{orderType}</if>
            <if test="categoryName != null  and categoryName != ''"> and cg.category_name = #{categoryName}</if>
            <if test="orderState != null  and orderState != ''"> and order_state = #{orderState}</if>
            <if test="appointmentStartTime != null "> and appointment_start_time = #{appointmentStartTime}</if>
            <if test="appointmentEndTime != null "> and appointment_end_time = #{appointmentEndTime}</if>
            <if test="serviceStartTime != null "> and service_start_time = #{serviceStartTime}</if>
            <if test="serviceEndTime != null "> and service_end_time = #{serviceEndTime}</if>
            <if test="serviceLongitude != null "> and service_longitude = #{serviceLongitude}</if>
            <if test="serviceLatitude != null "> and service_latitude = #{serviceLatitude}</if>
            <if test="shopAmount != null "> and shop_amount = #{shopAmount}</if>
            <if test="isSettle != null "> and is_settle = #{isSettle}</if>
            <if test="orderAmount != null "> and order_amount = #{orderAmount}</if>
            <if test="beanAmount != null "> and bean_amount = #{beanAmount}</if>
            <if test="memberCouponId != null  and memberCouponId != ''"> and member_coupon_id = #{memberCouponId}</if>
            <if test="couponAmount != null "> and coupon_amount = #{couponAmount}</if>
            <if test="totalAmount != null "> and total_amount = #{totalAmount}</if>
            <if test="payWay != null  and payWay != ''"> and pay_way = #{payWay}</if>
            <if test="memo != null  and memo != ''"> and memo = #{memo}</if>
            <if test="loginIp != null  and loginIp != ''"> and login_ip = #{loginIp}</if>
            <if test="loginDate != null "> and login_date = #{loginDate}</if>
            <if test="column1 != null  and column1 != ''"> and column1 = #{column1}</if>
            <if test="column2 != null  and column2 != ''"> and column2 = #{column2}</if>
            <if test="column3 != null  and column3 != ''"> and column3 = #{column3}</if>
            <if test="serverPhone != null  and serverPhone != ''"> and  su.phone = #{serverPhone}</if>
            <if test="Gname != null  and Gname != ''"> and  og.name like concat('%', #{Gname}, '%')</if>
            <if test="memberPhone != null  and memberPhone != ''"> and   me.phone = #{memberPhone}</if>
            <if test="serverName != null  and serverName != ''"> and  ogs.server_name like concat('%', #{serverName}, '%')</if>
            <if test="serverId != null  and serverId != ''"> and  ogs.server_id = #{serverId}</if>
            <if test='addCount=="1" '> and  og.add_counts>0</if>
            <if test='addCount=="2" '> and  og.add_counts =0</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                AND date_format(o.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>
        GROUP BY o.order_id
        order by o.order_id

    </select>

    <select id="selectOrderByOrderId" parameterType="Long" resultMap="OrderResult">
        <include refid="selectOrderVo"/>
        where o.order_id = #{orderId}
    </select>

    <insert id="insertOrder" parameterType="Order">
        insert into order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="orderIndex != null">order_index,</if>
            <if test="memberId != null">member_id,</if>
            <if test="merchantId != null">merchant_id,</if>
            <if test="merchantName != null">merchant_name,</if>
            <if test="orderType != null and orderType != ''">order_type,</if>
            <if test="orderState != null and orderState != ''">order_state,</if>
            <if test="appointmentStartTime != null">appointment_start_time,</if>
            <if test="appointmentEndTime != null">appointment_end_time,</if>
            <if test="serviceStartTime != null">service_start_time,</if>
            <if test="serviceEndTime != null">service_end_time,</if>
            <if test="serviceLongitude != null">service_longitude,</if>
            <if test="serviceLatitude != null">service_latitude,</if>
            <if test="shopAmount != null">shop_amount,</if>
            <if test="isSettle != null">is_settle,</if>
            <if test="orderAmount != null">order_amount,</if>
            <if test="beanAmount != null">bean_amount,</if>
            <if test="memberCouponId != null">member_coupon_id,</if>
            <if test="couponAmount != null">coupon_amount,</if>
            <if test="totalAmount != null">total_amount,</if>
            <if test="payWay != null">pay_way,</if>
            <if test="memo != null">memo,</if>
            <if test="delFlag != null and delFlag != ''">del_flag,</if>
            <if test="loginIp != null">login_ip,</if>
            <if test="loginDate != null">login_date,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="column1 != null">column1,</if>
            <if test="column2 != null">column2,</if>
            <if test="column3 != null">column3,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="orderIndex != null">#{orderIndex},</if>
            <if test="memberId != null">#{memberId},</if>
            <if test="merchantId != null">#{merchantId},</if>
            <if test="merchantName != null">#{merchantName},</if>
            <if test="orderType != null and orderType != ''">#{orderType},</if>
            <if test="orderState != null and orderState != ''">#{orderState},</if>
            <if test="appointmentStartTime != null">#{appointmentStartTime},</if>
            <if test="appointmentEndTime != null">#{appointmentEndTime},</if>
            <if test="serviceStartTime != null">#{serviceStartTime},</if>
            <if test="serviceEndTime != null">#{serviceEndTime},</if>
            <if test="serviceLongitude != null">#{serviceLongitude},</if>
            <if test="serviceLatitude != null">#{serviceLatitude},</if>
            <if test="shopAmount != null">#{shopAmount},</if>
            <if test="isSettle != null">#{isSettle},</if>
            <if test="orderAmount != null">#{orderAmount},</if>
            <if test="beanAmount != null">#{beanAmount},</if>
            <if test="memberCouponId != null">#{memberCouponId},</if>
            <if test="couponAmount != null">#{couponAmount},</if>
            <if test="totalAmount != null">#{totalAmount},</if>
            <if test="payWay != null">#{payWay},</if>
            <if test="memo != null">#{memo},</if>
            <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
            <if test="loginIp != null">#{loginIp},</if>
            <if test="loginDate != null">#{loginDate},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="column1 != null">#{column1},</if>
            <if test="column2 != null">#{column2},</if>
            <if test="column3 != null">#{column3},</if>
        </trim>
    </insert>

    <update id="updateOrder" parameterType="Order">
        update
        `order` o,
        order_goods og
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderIndex != null">order_index = #{orderIndex},</if>
            <if test="memberId != null">member_id = #{memberId},</if>
            <if test="merchantId != null">merchant_id = #{merchantId},</if>
            <if test="merchantName != null">merchant_name = #{merchantName},</if>
            <if test="orderType != null and orderType != ''">order_type = #{orderType},</if>
            <if test="orderState != null and orderState != ''">order_state = #{orderState},</if>
            <if test="appointmentStartTime != null">appointment_start_time = #{appointmentStartTime},</if>
            <if test="appointmentEndTime != null">appointment_end_time = #{appointmentEndTime},</if>
            <if test="serviceStartTime != null">service_start_time = #{serviceStartTime},</if>
            <if test="serviceEndTime != null">service_end_time = #{serviceEndTime},</if>
            <if test="serviceLongitude != null">service_longitude = #{serviceLongitude},</if>
            <if test="serviceLatitude != null">service_latitude = #{serviceLatitude},</if>
            <if test="shopAmount != null">shop_amount = #{shopAmount},</if>
            <if test="isSettle != null">is_settle = #{isSettle},</if>
            <if test="orderAmount != null">order_amount = #{orderAmount},</if>
            <if test="beanAmount != null">bean_amount = #{beanAmount},</if>
            <if test="memberCouponId != null">member_coupon_id = #{memberCouponId},</if>
            <if test="couponAmount != null">coupon_amount = #{couponAmount},</if>
            <if test="totalAmount != null">total_amount = #{totalAmount},</if>
            <if test="payWay != null">pay_way = #{payWay},</if>
            <if test="memo != null">memo = #{memo},</if>
            <if test="delFlag != null and delFlag != ''">o.del_flag = #{delFlag},</if>
            <if test="loginIp != null">login_ip = #{loginIp},</if>
            <if test="loginDate != null">login_date = #{loginDate},</if>
            <if test="createBy != null">o.create_by = #{createBy},</if>
            <if test="createTime != null">o.create_time = #{createTime},</if>
            <if test="updateBy != null">o.update_by = #{updateBy},</if>
            <if test="updateTime != null">o.update_time = #{updateTime},</if>
            <if test="column1 != null">o.column1 = #{column1},</if>
            <if test="column2 != null">o.column2 = #{column2},</if>
            <if test="column3 != null">o.column3 = #{column3},</if>
            <if test="price != null">og.price = #{price},</if>
        </trim>
        where o.order_id = #{orderId}
        and  o.order_id=og.order_id
    </update>

    <delete id="deleteOrderByOrderId" parameterType="Long">
        delete from order where order_id = #{orderId}
    </delete>

    <delete id="deleteOrderByOrderIds" parameterType="String">
        delete from order where order_id in
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </delete>
</mapper>
