<?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="com.younike.system.domain.vo.OrdersVO" id="OrderResultVO">
        <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="groupTimes"    column="group_times"    />
        <result property="groupRemainingTimes"    column="group_remaining_times"    />
        <result property="column1"    column="column1"    />
        <result property="column2"    column="column2"    />
        <result property="column3"    column="column3"    />
        <result property="goodsName"    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"/>
        <result property="categoryId"    column="category_id"/>
        <result property="parentName"    column="parentName"/>
        <result property="singleTime"    column="single_time"/>
        <result property="goodsId"    column="goods_id"/>
        <result property="goodsSpecificationId"    column="goods_specification_id"/>
    </resultMap>

    <resultMap type="com.younike.system.domain.vo.ReconciliationVo" id="ReconciliationResult">
        <result property="orderId"    column="order_id"    />
        <result property="orderIndex"    column="order_index"    />
        <result property="orderAmount"    column="order_amount"    />
        <result property="totalAmount"    column="total_amount"    />
        <result property="beanAmount"    column="bean_amount"    />
        <result property="addAmount"    column="addAmount"    />
        <result property="addShopAmount"    column="add_shop_amount"    />
        <result property="shopAmount" column="shop_amount"/>
        <result property="addPayStatus" column="add_pay_status"/>
        <result property="isSettle" column="is_settle"/>
        <result property="payTime" column="payTime"/>
        <result property="orderType" column="order_type"/>
        <result property="orderState" column="order_state"/>
        <result property="payWay" column="pay_way"/>
        <result property="memberPhone" column="memberPhone"/>
        <result property="goodsId" column="goods_id"/>
        <result property="statusName" column="statusName"/>
        <result property="merchantName" column="merchant_name"/>
        <result property="merchantId" column="merchant_id"/>
        <result property="merchantIndex" column="merchant_index"/>
        <result property="goodsIndex" column="goods_index"/>

    </resultMap>

    <resultMap id="UjdChangeRecordResult" type="UjdChangeRecord">
        <id column="ujd_change_record_id" property="ujdChangeRecordId" />
        <result column="change_qty" property="changeQty" />
        <result column="change_type" property="changeType" />
        <result column="change_property" property="changeProperty" />
        <result column="merchant_id" property="merchantId" />
        <result column="merchant_index" property="merchantIndex" />
        <result column="merchant_name" property="merchantName" />
        <result column="goods_id" property="goodsId" />
        <result column="goods_index" property="goodsIndex" />
        <result column="goods_name" property="goodsName" />
        <result column="trade_no" property="tradeNo" />
        <result column="change_time" property="changeTime" />
        <result column="del_flag" property="delFlag" />
        <result column="create_by" property="createBy" />
        <result column="create_time" property="createTime" />
        <result column="update_by" property="updateBy" />
        <result column="update_time" property="updateTime" />
        <result column="remark" property="remark" />
    </resultMap>


    <sql id="selectOrderVO">
        select
            o.order_id, order_index,
            o.member_id,
            o.merchant_id, 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, maintenance_amount, pay_way, memo,
            o.merchant_name,
            o.del_flag,
            o.add_amount,
            o.group_times,
            o.group_remaining_times,
            o.order_verification_code,
            o.create_time,
            og.cate_id,
            og.name,
            og.goods_unit,
            og.other_unit,
            og.goods_id,
            og.goods_specification_id,
            me.phone,
            cg.category_name,
            ogs.server_name,
            su.phone as server_phone,
            og.price,
            og.add_counts,
            og.goods_counts,
            sc.decode_value as ref_status,
            ogs.server_id,
            o.maintenance_amount,
            o.maintenance_remark,
            cy.category_name as parentName,
            ogs.single_time,
            m.merchant_index,
            m.merchant_type,
            g.goods_index
            from `order` o
            LEFT JOIN member me ON me.member_id = o.member_id AND me.del_flag = '0'
            LEFT JOIN merchant m ON m.merchant_id = o.merchant_id AND m.del_flag = '0'
            LEFT JOIN order_goods og ON og.order_id = o.order_id AND og.del_flag = '0'
            LEFT JOIN goods g ON g.goods_id = og.goods_id AND g.del_flag = '0'
            LEFT JOIN category cg ON cg.category_id = og.cate_id AND cg.del_flag = '0'
            LEFT JOIN category cy ON cg.parent_id = cy.category_id AND cy.del_flag = '0'
            LEFT JOIN order_goods_server ogs ON ogs.order_id = o.order_id AND ogs.del_flag = '0'
            LEFT JOIN server_user su ON su.server_id = ogs.server_id AND su.del_flag = '0'
            LEFT JOIN sys_user u ON u.user_id = m.admin_id AND u.del_flag = '0'
            left join sys_code sc on o.order_state = sc.decode_id and code_id = '003'
    </sql>

    <select id="selectOrderList" parameterType="OrdersVO" resultMap="OrderResultVO">
        <include refid="selectOrderVO"/>
        <where>
            o.del_flag = '0'
            <if test="orderIndex != null and orderIndex != ''"> and order_index like concat('%', #{orderIndex}, '%')</if>
            <if test="merchantIndex != null and merchantIndex != ''"> and m.merchant_index like concat('%', #{merchantIndex}, '%')</if>
            <if test="merchantType != null"> and m.merchant_type = #{merchantType}</if>
            <if test="memberId != null "> and member_id = #{memberId}</if>
            <if test="merchantId != null "> and o.merchant_id = #{merchantId}</if>
            <if test="merchantName != null  and merchantName != ''"> and o.merchant_name like concat('%', #{merchantName}, '%')</if>
            <if test="orderType != null  and orderType != ''"> and order_type = #{orderType}</if>
            <if test="orderVerificationCode != null  and orderVerificationCode != ''"> and order_verification_code = #{orderVerificationCode}</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="goodsName != null  and goodsName != ''"> and  og.name like concat('%', #{goodsName}, '%')</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="categoryId != null  and categoryId != ''">
                and (cg.category_id =  #{categoryId} or cg.parent_id =  #{categoryId} )
            </if>
            <if test="homeAdd != null  and homeAdd != '' and homeAdd == 1"> and og.add_counts > 0 </if>
            <if test="homeAdd != null  and homeAdd != '' and homeAdd == 0"> and (og.add_counts = 0 or og.add_counts is null)</if>
            <if test="repairAdd != null  and repairAdd != '' and repairAdd == 1"> and o.maintenance_amount > 0</if>
            <if test="repairAdd != null  and repairAdd != '' and repairAdd == 0"> and (o.maintenance_amount = 0 or o.maintenance_amount is null)</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>
        order by o.create_time DESC

    </select>

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

    <insert id="insertOrder" parameterType="OrdersVO">
        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="OrdersVO">
        update
        `order` o
        <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>
        </trim>
        where o.order_id = #{orderId}
    </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>
    <sql id="selectReconciliationOrder">
        select
                o.order_id,
                o.order_index,
                o.order_amount,
                o.total_amount,
                IFNULL(o.bean_amount,0) as bean_amount,
                o.add_amount,
                o.add_shop_amount,
                o.add_pay_status,
                o.is_settle,
                o.pay_way,
                o.order_state,
                o.order_type,
                o.shop_amount,
                od.create_time as payTime,
                oa.phone as memberPhone,
                og.goods_id,
                sc.Decode_Value as statusName,
                m.merchant_id,
                m.merchant_name,
                m.merchant_index,
                g.goods_index
                from `order` o
                LEFT JOIN order_record od ON o.order_id= od.order_id and od.pre_state = '000' and od.now_state= '001'
                LEFT JOIN order_address oa ON oa.order_id = o.order_id AND oa.del_flag = '0'
                LEFT JOIN order_goods og ON og.order_id = o.order_id AND og.del_flag = '0'
                LEFT JOIN goods g ON og.goods_id = g.goods_id
                LEFT JOIN  sys_code sc ON sc.Decode_id= o.order_state and sc.Code_id = '003'
                LEFT JOIN merchant m ON m.merchant_id = o.merchant_id and m.del_flag = '0'
    </sql>
    <select id="selectReconciliationOrderList" parameterType="com.younike.system.domain.vo.ReconciliationVo" resultMap="ReconciliationResult">
        <include refid="selectReconciliationOrder"/>
        <where>
            o.del_flag = '0'
            <if test="orderId != null and orderId != ''">and o.order_id like concat('%', #{orderId}, '%')</if>
            <if test="goodsId != null and goodsId != ''">and og.goods_id like concat('%', #{goodsId}, '%')</if>
            <if test="orderIndex != null and orderIndex != ''">and o.order_index like concat('%', #{orderIndex}, '%')
            </if>
            <if test="goodsIndex != null and goodsIndex != ''">and g.goods_index like concat('%', #{goodsIndex}, '%')
            </if>
            <if test="memberPhone != null and memberPhone != ''">and oa.phone like concat('%', #{memberPhone}, '%')</if>
            <if test="payWay != null and payWay != ''">and o.pay_way = #{payWay}</if>
            <if test="orderState != null and orderState != ''">and o.order_state = #{orderState}</if>
            <if test="merchantId != null and merchantId != ''">and o.merchant_id = #{merchantId}</if>
            <if test="merchantName != null and merchantName != ''">and m.merchant_name like concat('%', #{merchantName},
                '%')
            </if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(od.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(od.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>

        GROUP BY o.order_id
        order by o.create_time DESC

    </select>

    <!--    查询店铺实收金额统计 主订单店铺收入-->
    <select id="getOrderStatistics" parameterType="com.younike.system.domain.vo.ReconciliationVo"
            resultType="com.younike.system.domain.vo.ReconciliationVo">
        select
        SUM( o.shop_amount) as shopMoney,
        SUM(o.total_amount) as totalAmount
        from `order` o
        LEFT JOIN order_record od ON o.order_id= od.order_id and od.pre_state = '000' and od.now_state= '001'
        LEFT JOIN order_address oa ON oa.order_id = o.order_id AND oa.del_flag = '0'
        LEFT JOIN order_goods og ON og.order_id = o.order_id AND og.del_flag = '0'
        LEFT JOIN goods g ON og.goods_id = g.goods_id
        LEFT JOIN merchant m ON m.merchant_id = o.merchant_id and m.del_flag = '0'
        where o.del_flag = '0'
        and o.is_settle = 1
        and (o.order_state='004' or o.order_state='007')
        <if test="orderId != null and orderId != ''">and o.order_id like concat('%', #{orderId}, '%')</if>
        <if test="goodsId != null and goodsId != ''">and og.goods_id like concat('%', #{goodsId}, '%')</if>
        <if test="orderIndex != null and orderIndex != ''">and o.order_index like concat('%', #{orderIndex}, '%')</if>
        <if test="goodsIndex != null and goodsIndex != ''">and g.goods_index like concat('%', #{goodsIndex}, '%')</if>
        <if test="memberPhone != null and memberPhone != ''">and oa.phone like concat('%', #{memberPhone}, '%')</if>
        <if test="payWay != null and payWay != ''">and o.pay_way = #{payWay}</if>
        <if test="orderState != null and orderState != ''">and o.order_state = #{orderState}</if>
        <if test="merchantId != null and merchantId != ''">and o.merchant_id = #{merchantId}</if>
        <if test="merchantName != null and merchantName != ''">and m.merchant_name like concat('%', #{merchantName},
            '%')
        </if>

        <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(od.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(od.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
        </if>
    </select>

    <!--    查询追加服务店铺可得金额-->
    <select id="getOrderAddStatistics" parameterType="com.younike.system.domain.vo.ReconciliationVo"
            resultType="com.younike.system.domain.vo.ReconciliationVo">
        select
        SUM( o.add_shop_amount) as addShopAmount
        from `order` o
        LEFT JOIN order_record od ON o.order_id= od.order_id and od.pre_state = '000' and od.now_state= '001'
        LEFT JOIN order_address oa ON oa.order_id = o.order_id AND oa.del_flag = '0'
        LEFT JOIN order_goods og ON og.order_id = o.order_id AND og.del_flag = '0'
        LEFT JOIN goods g ON og.goods_id = g.goods_id
        LEFT JOIN merchant m ON m.merchant_id = o.merchant_id and m.del_flag = '0'
        where o.del_flag = '0'
        and o.is_settle = 1
        and o.add_pay_status = 1
        and (o.order_state='004' or o.order_state='007')
        <if test="orderId != null and orderId != ''">and o.order_id like concat('%', #{orderId}, '%')</if>
        <if test="goodsId != null and goodsId != ''">and og.goods_id like concat('%', #{goodsId}, '%')</if>
        <if test="orderIndex != null and orderIndex != ''">and o.order_index like concat('%', #{orderIndex}, '%')</if>
        <if test="goodsIndex != null and goodsIndex != ''">and g.goods_index like concat('%', #{goodsIndex}, '%')</if>
        <if test="memberPhone != null and memberPhone != ''">and oa.phone like concat('%', #{memberPhone}, '%')</if>
        <if test="payWay != null and payWay != ''">and o.pay_way = #{payWay}</if>
        <if test="orderState != null and orderState != ''">and o.order_state = #{orderState}</if>
        <if test="merchantId != null and merchantId != ''">and o.merchant_id = #{merchantId}</if>
        <if test="merchantName != null and merchantName != ''">and m.merchant_name like concat('%', #{merchantName},
            '%')
        </if>
        <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(od.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(od.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
        </if>
    </select>
    <!--    运营管理：财务对账：查询订单账单-->
    <select id="getReconciliationOrderList" parameterType="com.younike.system.domain.vo.ReconciliationVo"
            resultMap="ReconciliationResult">
        <include refid="selectReconciliationOrder"/>
        <where>
            o.del_flag = '0'
            <if test="orderId != null and orderId != ''">and o.order_id like concat('%', #{orderId}, '%')</if>
            <if test="goodsId != null and goodsId != ''">and og.goods_id like concat('%', #{goodsId}, '%')</if>
            <if test="orderIndex != null and orderIndex != ''">and o.order_index like concat('%', #{orderIndex}, '%')
            </if>
            <if test="goodsIndex != null and goodsIndex != ''">and g.goods_index like concat('%', #{goodsIndex}, '%')
            </if>
            <if test="memberPhone != null and memberPhone != ''">and oa.phone like concat('%', #{memberPhone}, '%')</if>
            <if test="payWay != null and payWay != ''">and o.pay_way = #{payWay}</if>
            <if test="orderState != null and orderState != ''">and o.order_state = #{orderState}</if>
            <if test="merchantId != null and merchantId != ''">and o.merchant_id like concat('%', #{merchantId}, '%')
            </if>
            <if test="merchantIndex != null and merchantIndex != ''">and m.merchant_index like concat('%',
                #{merchantIndex}, '%')
            </if>
            <if test="merchantName != null and merchantName != ''">and m.merchant_name like concat('%', #{merchantName},
                '%')
            </if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(od.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(od.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>
        GROUP BY o.order_id
        order by o.create_time DESC

    </select>
    <!--    运营管理:查询店铺实收金额统计 主订单店铺收入-->
    <select id="getAdminOrderStatistics" parameterType="com.younike.system.domain.vo.ReconciliationVo"
            resultType="com.younike.system.domain.vo.ReconciliationVo">
        select
        SUM( o.shop_amount) as shopMoney,
        SUM(o.total_amount) as totalAmount
        from `order` o
        LEFT JOIN order_record od ON o.order_id= od.order_id and od.pre_state = '000' and od.now_state= '001'
        LEFT JOIN order_address oa ON oa.order_id = o.order_id AND oa.del_flag = '0'
        LEFT JOIN order_goods og ON og.order_id = o.order_id AND og.del_flag = '0'
        LEFT JOIN goods g ON og.goods_id = g.goods_id
        LEFT JOIN merchant m ON m.merchant_id = o.merchant_id and m.del_flag = '0'
        where o.del_flag = '0'
        and o.is_settle = 1
        and (o.order_state='004' or o.order_state='007')
        <if test="orderId != null and orderId != ''">and o.order_id like concat('%', #{orderId}, '%')</if>
        <if test="goodsId != null and goodsId != ''">and og.goods_id like concat('%', #{goodsId}, '%')</if>
        <if test="orderIndex != null and orderIndex != ''">and o.order_index like concat('%', #{orderIndex}, '%')</if>
        <if test="goodsIndex != null and goodsIndex != ''">and g.goods_index like concat('%', #{goodsIndex}, '%')</if>
        <if test="memberPhone != null and memberPhone != ''">and oa.phone like concat('%', #{memberPhone}, '%')</if>
        <if test="payWay != null and payWay != ''">and o.pay_way = #{payWay}</if>
        <if test="orderState != null and orderState != ''">and o.order_state = #{orderState}</if>
        <if test="merchantId != null and merchantId != ''">and o.merchant_id like concat('%', #{merchantId}, '%')</if>
        <if test="merchantIndex != null and merchantIndex != ''">and m.merchant_index like concat('%', #{merchantIndex},
            '%')
        </if>
        <if test="merchantName != null and merchantName != ''">and m.merchant_name like concat('%', #{merchantName},
            '%')
        </if>

        <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(od.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(od.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
        </if>
    </select>

    <!--    运营管理:查询追加服务店铺收入 -->
    <select id="getAdminOrderAddStatistics" parameterType="com.younike.system.domain.vo.ReconciliationVo"
            resultType="com.younike.system.domain.vo.ReconciliationVo">
        select
        SUM( o.add_shop_amount) as addShopAmount
        from `order` o
        LEFT JOIN order_record od ON o.order_id= od.order_id and od.pre_state = '000' and od.now_state= '001'
        LEFT JOIN order_address oa ON oa.order_id = o.order_id AND oa.del_flag = '0'
        LEFT JOIN order_goods og ON og.order_id = o.order_id AND og.del_flag = '0'
        LEFT JOIN goods g ON og.goods_id = g.goods_id
        LEFT JOIN merchant m ON m.merchant_id = o.merchant_id and m.del_flag = '0'
        where o.del_flag = '0'
        and o.is_settle = 1
        and o.add_pay_status = 1
        and (o.order_state='004' or o.order_state='007')
        <if test="orderId != null and orderId != ''">and o.order_id like concat('%', #{orderId}, '%')</if>
        <if test="goodsId != null and goodsId != ''">and og.goods_id like concat('%', #{goodsId}, '%')</if>
        <if test="orderIndex != null and orderIndex != ''">and o.order_index like concat('%', #{orderIndex}, '%')</if>
        <if test="goodsIndex != null and goodsIndex != ''">and g.goods_index like concat('%', #{goodsIndex}, '%')</if>
        <if test="memberPhone != null and memberPhone != ''">and oa.phone like concat('%', #{memberPhone}, '%')</if>
        <if test="payWay != null and payWay != ''">and o.pay_way = #{payWay}</if>
        <if test="orderState != null and orderState != ''">and o.order_state = #{orderState}</if>
        <if test="merchantId != null and merchantId != ''">and o.merchant_id like concat('%', #{merchantId}, '%')</if>
        <if test="merchantIndex != null and merchantIndex != ''">and m.merchant_index like concat('%', #{merchantIndex},
            '%')
        </if>
        <if test="merchantName != null and merchantName != ''">and m.merchant_name like concat('%', #{merchantName},
            '%')
        </if>
        <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(od.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(od.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
        </if>
    </select>

    <select id="reconciliationlist"  resultMap="UjdChangeRecordResult">
        select u.ujd_change_record_id, u.change_qty, u.change_type, u.change_property, u.merchant_id, u.merchant_index, u.merchant_name, u.goods_id, u.goods_index, u.goods_name, u.trade_no, u.change_time, u.del_flag, u.create_by, u.create_time, u.update_by, u.update_time, u.remark from ujd_change_record u
        <where> del_flag=0
            <!--<if test="merchantId != null and merchantId = ''">and u.merchant_id like concat('%',#{merchantId},'%')</if>-->
            <if test="merchantIndex != null and merchantIndex != ''">and u.merchant_index like concat('%',#{merchantIndex},'%')</if>
            <if test="merchantName != null and merchantName != ''">and u.merchant_name like concat('%',#{merchantName},'%')</if>
            <if test="goodsIndex != null and goodsIndex !='' ">and u.goods_index like concat('%',#{goodsIndex},'%') </if>
            <if test="goodsName != null and goodsName != ''">and u.goods_name like concat('%',#{goodsName},'%')</if>
            <if test="changeType != null and changeType != ''">and u.change_type = #{changeType}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(u.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(u.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>

    </select>

    <select id="listOrder" parameterType="com.younike.system.param.OrderQueryParam" resultMap="OrderResultVO">
        <include refid="selectOrderVO"/>
        <where>
            o.del_flag = '0'
            <if test="merchantId != null "> and o.merchant_id = #{merchantId}</if>
            <if test="merchantIndex != null and merchantIndex != ''"> and m.merchant_index like concat('%', #{merchantIndex}, '%')</if>
            <if test="orderIndex != null and orderIndex != ''"> and order_index like concat('%', #{orderIndex}, '%')</if>
            <if test="orderState != null  and orderState != ''"> and order_state = #{orderState}</if>
            <if test="orderType != null  and orderType != ''"> and order_type = #{orderType}</if>
            <if test="merchantName != null  and merchantName != ''"> and o.merchant_name like concat('%', #{merchantName}, '%')</if>
            <if test="goodsName != null  and goodsName != ''"> and  og.name like concat('%', #{goodsName}, '%')</if>
            <if test="serverName != null  and serverName != ''"> and  ogs.server_name like concat('%', #{serverName}, '%')</if>
            <if test="serverPhone != null  and serverPhone != ''"> and  su.phone = #{serverPhone}</if>
            <if test="memberPhone != null  and memberPhone != ''"> and   me.phone = #{memberPhone}</if>
            <if test="categoryId != null  and categoryId != ''">
                and (cg.category_id =  #{categoryId} or cg.parent_id =  #{categoryId} )
            </if>
            <if test="merchantType != null"> and m.merchant_type = #{merchantType}</if>
            <if test="orderVerificationCode != null  and orderVerificationCode != ''"> and order_verification_code = #{orderVerificationCode}</if>
            <if test="homeAdd != null  and homeAdd != '' and homeAdd == 1"> and og.add_counts > 0 </if>
            <if test="homeAdd != null  and homeAdd != '' and homeAdd == 0"> and (og.add_counts = 0 or og.add_counts is null)</if>
            <if test="repairAdd != null  and repairAdd != '' and repairAdd == 1"> and o.maintenance_amount > 0</if>
            <if test="repairAdd != null  and repairAdd != '' and repairAdd == 0"> and (o.maintenance_amount = 0 or o.maintenance_amount is null)</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{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(#{endTime},'%y%m%d')
            </if>
        </where>
        order by o.create_time DESC
    </select>

    <select id="getOrderByVerificationCode" parameterType="java.lang.String" resultMap="OrderResultVO">
        <include refid="selectOrderVO"/>
        where o.order_verification_code = #{orderVerificationCode}
    </select>
</mapper>
