<?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.gxwebsoft.shop.mapper.OrderMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*,
        b.nickname,b.alias,b.username,b.user_id,b.avatar,b.phone,b.email,
        c.equipment_name,c.equipment_code,c.equipment_category,c.equipment_avatar,c.battery_model,c.equipment_id,
        d.merchant_name,d.merchant_code,
        e.organization_id,e.organization_name
        FROM shop_order a
        LEFT JOIN sys_user b ON a.user_id = b.user_id
        LEFT JOIN apps_equipment c ON a.equipment_id = c.equipment_id
        LEFT JOIN shop_merchant d ON a.merchant_code = d.merchant_code
        LEFT JOIN sys_organization e ON b.organization_id = e.organization_id
        <where>
            <if test="param.orderId != null">
                AND a.order_id = #{param.orderId}
            </if>
            <if test="param.orderNo != null">
                AND a.order_no LIKE CONCAT('%', #{param.orderNo}, '%')
            </if>
            <if test="param.totalPrice != null">
                AND a.total_price = #{param.totalPrice}
            </if>
            <if test="param.orderPrice != null">
                AND a.order_price = #{param.orderPrice}
            </if>
            <if test="param.couponId != null">
                AND a.coupon_id = #{param.couponId}
            </if>
            <if test="param.couponMoney != null">
                AND a.coupon_money = #{param.couponMoney}
            </if>
            <if test="param.pointsMoney != null">
                AND a.points_money = #{param.pointsMoney}
            </if>
            <if test="param.pointsNum != null">
                AND a.points_num = #{param.pointsNum}
            </if>
            <if test="param.payPrice != null">
                AND a.pay_price = #{param.payPrice}
            </if>
            <if test="param.updatePrice != null">
                AND a.update_price = #{param.updatePrice}
            </if>
            <if test="param.buyerRemark != null">
                AND a.buyer_remark LIKE CONCAT('%', #{param.buyerRemark}, '%')
            </if>
            <if test="param.payType != null">
                AND a.pay_type = #{param.payType}
            </if>
            <if test="param.payMethod != null">
                AND a.pay_method LIKE CONCAT('%', #{param.payMethod}, '%')
            </if>
            <if test="param.payStatus != null">
                AND a.pay_status = #{param.payStatus}
            </if>
            <if test="param.payTime != null">
                AND a.pay_time = #{param.payTime}
            </if>
            <if test="param.tradeId != null">
                AND a.trade_id = #{param.tradeId}
            </if>
            <if test="param.deliveryType != null">
                AND a.delivery_type = #{param.deliveryType}
            </if>
            <if test="param.extractShopId != null">
                AND a.extract_shop_id = #{param.extractShopId}
            </if>
            <if test="param.extractClerkId != null">
                AND a.extract_clerk_id = #{param.extractClerkId}
            </if>
            <if test="param.expressPrice != null">
                AND a.express_price = #{param.expressPrice}
            </if>
            <if test="param.expressId != null">
                AND a.express_id = #{param.expressId}
            </if>
            <if test="param.expressNo != null">
                AND a.express_no LIKE CONCAT('%', #{param.expressNo}, '%')
            </if>
            <if test="param.deliveryStatus != null">
                AND a.delivery_status = #{param.deliveryStatus}
            </if>
            <if test="param.deliveryTime != null">
                AND a.delivery_time = #{param.deliveryTime}
            </if>
            <if test="param.receiptStatus != null">
                AND a.receipt_status = #{param.receiptStatus}
            </if>
            <if test="param.receiptTime != null">
                AND a.receipt_time = #{param.receiptTime}
            </if>
            <if test="param.orderStatus != null">
                AND a.order_status = #{param.orderStatus}
            </if>
            <if test="param.rentOrderId != null">
              AND a.rent_order_id = #{param.rentOrderId}
            </if>
            <if test="param.pointsBonus != null">
                AND a.points_bonus = #{param.pointsBonus}
            </if>
            <if test="param.merchantRemark != null">
                AND a.merchant_remark LIKE CONCAT('%', #{param.merchantRemark}, '%')
            </if>
            <if test="param.isSettled != null">
                AND a.is_settled = #{param.isSettled}
            </if>
            <if test="param.transactionId != null">
                AND a.transaction_id LIKE CONCAT('%', #{param.transactionId}, '%')
            </if>
            <if test="param.isComment != null">
                AND a.is_comment = #{param.isComment}
            </if>
            <if test="param.orderSource != null">
                AND a.order_source = #{param.orderSource}
            </if>
            <if test="param.orderSourceId != null">
                AND a.order_source_id = #{param.orderSourceId}
            </if>
            <if test="param.orderSourceData != null">
                AND a.order_source_data LIKE CONCAT('%', #{param.orderSourceData}, '%')
            </if>
            <if test="param.platform != null">
                AND a.platform LIKE CONCAT('%', #{param.platform}, '%')
            </if>
            <if test="param.isRenew != null">
              AND a.is_renew = #{param.isRenew}
            </if>
            <if test="param.week != null">
              AND a.week = #{param.week}
            </if>
            <if test="param.isTemporary != null">
              AND a.is_temporary = #{param.isTemporary}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.userIds != null">
              AND a.user_id IN
              <foreach collection="param.userIds" item="item" separator="," open="(" close=")">
                #{item}
              </foreach>
            </if>
            <if test="param.shopId != null">
                AND a.shop_id = #{param.shopId}
            </if>
            <if test="param.sortNumber != null">
                AND a.sort_number = #{param.sortNumber}
            </if>
            <if test="param.comments != null">
                AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.deleted != null">
                AND a.deleted = #{param.deleted}
            </if>
            <if test="param.deleted == null">
                AND a.deleted = 0
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.merchantName != null">
              AND a.merchant_name LIKE CONCAT('%', #{param.merchantName}, '%')
            </if>
            <if test="param.merchantCode != null">
              AND a.merchant_code = #{param.merchantCode}
            </if>
            <if test="param.keywords != null">
              AND (
              a.order_no = #{param.keywords}
              OR a.order_id = #{param.keywords}
              OR a.user_id = #{param.keywords}
              OR a.comments LIKE CONCAT('%', #{param.keywords}, '%')
              OR b.nickname LIKE CONCAT('%', #{param.keywords}, '%')
              OR b.phone = #{param.keywords}
              OR c.equipment_code = #{param.keywords}
              OR d.merchant_name LIKE CONCAT('%', #{param.keywords}, '%')
              )
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.shop.entity.Order">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.shop.entity.Order">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询单条数据 -->
    <select id="getOne" resultType="com.gxwebsoft.shop.entity.Order">
      SELECT * FROM shop_order WHERE order_no = #{param.orderNo}
    </select>

    <select id="updatePayStatus" resultType="com.gxwebsoft.shop.entity.Order">
      UPDATE shop_order SET pay_status = #{param.payStatus} WHERE order_no = #{param.orderNo}
    </select>

    <update id="updateByIdRel">
      UPDATE shop_order SET pay_status = #{param.payStatus},pay_method = #{param.payMethod},receipt_amount = #{param.receiptAmount},pay_time = #{param.payTime},trade_id = #{param.tradeId},subject = #{param.subject},expiration_time = #{param.expirationTime} WHERE order_id = #{param.orderId}
    </update>

    <select id="selectPageSettled" resultType="com.gxwebsoft.shop.entity.Order">
      <include refid="selectSql"></include>
    </select>

    <update id="updateByIdSettled">
      UPDATE shop_order SET is_settled = #{param.isSettled},settled_time = #{param.settledTime} WHERE order_id = #{param.orderId}
    </update>


</mapper>
