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

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*,b.nickname,c.order_status,c.pay_status,c.is_temporary,c.order_status,c.deleted,c.week,c.delivery_time as deliveryTime
        FROM shop_order_goods a
        LEFT JOIN sys_user b ON a.user_id = b.user_id
        LEFT JOIN shop_order c ON a.order_id = c.order_id
        <where>
            <if test="param.orderGoodsId != null">
                AND a.order_goods_id = #{param.orderGoodsId}
            </if>
            <if test="param.goodsId != null">
                AND a.goods_id = #{param.goodsId}
            </if>
            <if test="param.goodsName != null">
                AND a.goods_name LIKE CONCAT('%', #{param.goodsName}, '%')
            </if>
            <if test="param.categoryId != null">
              AND a.category_id LIKE CONCAT('%', #{param.categoryId}, '%')
            </if>
            <if test="param.temporary != null">
              AND a.temporary = #{param.temporary}
            </if>
            <if test="param.payStatus != null">
              AND c.pay_status = #{param.payStatus}
            </if>
            <if test="param.imageId != null">
                AND a.image_id = #{param.imageId}
            </if>
            <if test="param.deductStockType != null">
                AND a.deduct_stock_type = #{param.deductStockType}
            </if>
            <if test="param.specType != null">
                AND a.spec_type = #{param.specType}
            </if>
            <if test="param.goodsSkuId != null">
                AND a.goods_sku_id LIKE CONCAT('%', #{param.goodsSkuId}, '%')
            </if>
            <if test="param.goodsProps != null">
                AND a.goods_props LIKE CONCAT('%', #{param.goodsProps}, '%')
            </if>
            <if test="param.content != null">
                AND a.content LIKE CONCAT('%', #{param.content}, '%')
            </if>
            <if test="param.goodsNo != null">
                AND a.goods_no LIKE CONCAT('%', #{param.goodsNo}, '%')
            </if>
            <if test="param.goodsPrice != null">
                AND a.goods_price = #{param.goodsPrice}
            </if>
            <if test="param.linePrice != null">
                AND a.line_price = #{param.linePrice}
            </if>
            <if test="param.goodsWeight != null">
                AND a.goods_weight LIKE CONCAT('%', #{param.goodsWeight}, '%')
            </if>
            <if test="param.isUserGrade != null">
                AND a.is_user_grade = #{param.isUserGrade}
            </if>
            <if test="param.gradeRatio != null">
                AND a.grade_ratio = #{param.gradeRatio}
            </if>
            <if test="param.gradeGoodsPrice != null">
                AND a.grade_goods_price = #{param.gradeGoodsPrice}
            </if>
            <if test="param.gradeTotalMoney != null">
                AND a.grade_total_money = #{param.gradeTotalMoney}
            </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.pointsBonus != null">
                AND a.points_bonus = #{param.pointsBonus}
            </if>
            <if test="param.totalNum != null">
                AND a.total_num = #{param.totalNum}
            </if>
            <if test="param.hasNum != null">
              AND a.total_num > 0
            </if>
            <if test="param.totalPrice != null">
                AND a.total_price = #{param.totalPrice}
            </if>
            <if test="param.totalPayPrice != null">
                AND a.total_pay_price = #{param.totalPayPrice}
            </if>
            <if test="param.deliveryStatus != null">
                AND a.delivery_status = #{param.deliveryStatus}
            </if>
            <if test="param.deliveryTime != null">
              AND c.delivery_time = #{param.deliveryTime}
            </if>
            <if test="param.deliveryNum != null">
                AND a.delivery_num = #{param.deliveryNum}
            </if>
            <if test="param.isIndDealer != null">
                AND a.is_ind_dealer = #{param.isIndDealer}
            </if>
            <if test="param.dealerMoneyType != null">
                AND a.dealer_money_type = #{param.dealerMoneyType}
            </if>
            <if test="param.firstMoney != null">
                AND a.first_money = #{param.firstMoney}
            </if>
            <if test="param.secondMoney != null">
                AND a.second_money = #{param.secondMoney}
            </if>
            <if test="param.thirdMoney != null">
                AND a.third_money = #{param.thirdMoney}
            </if>
            <if test="param.isComment != null">
                AND a.is_comment = #{param.isComment}
            </if>
            <if test="param.orderId != null">
                AND a.order_id = #{param.orderId}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.goodsSourceId != null">
                AND a.goods_source_id = #{param.goodsSourceId}
            </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 AND c.deleted = 0
            </if>
            <if test="param.merchantCode != null">
                AND a.merchant_code LIKE CONCAT('%', #{param.merchantCode}, '%')
            </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.deliveryTimeStart != null">
              AND c.delivery_time &gt;= #{param.deliveryTimeStart}
            </if>
            <if test="param.deliveryTimeEnd != null">
              AND c.delivery_time &lt;= #{param.deliveryTimeEnd}
            </if>
            <if test="param.orderStatus != null">
              AND c.order_status = #{param.orderStatus}
            </if>
            <if test="param.gear != null">
              AND a.gear = #{param.gear}
            </if>
            <if test="param.nickname != null">
              AND a.nickname LIKE CONCAT('%', #{param.nickname}, '%')
            </if>
            <if test="param.organizationId != null">
              AND c.user_id IN (SELECT user_id FROM sys_user WHERE organization_id = #{param.organizationId})
            </if>
            <if test="param.week != null">
              AND c.week = #{param.week}
            </if>
            <if test="param.keywords != null">
              AND (
              a.order_goods_id = #{param.keywords}
              OR a.order_id = #{param.keywords}
              OR b.nickname = #{param.keywords}
              )
            </if>
        </where>
    </sql>

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

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

    <select id="selectByOrderIds" resultType="com.gxwebsoft.shop.entity.OrderGoods">
      SELECT a.order_id, b.*
      FROM shop_order a
      LEFT JOIN shop_order_goods b ON a.order_id = b.order_id
      WHERE a.order_id IN
      <foreach collection="orderIds" open="(" close=")" separator="," item="orderId">
        #{orderId}
      </foreach>
      AND b.deleted = 0
    </select>

    <select id="selectCountBySettled" resultType="com.gxwebsoft.shop.entity.OrderGoods">
      SELECT COUNT(*)
      FROM shop_order_goods
      <where>
        <if test="param.categoryId != null">
          AND category_id = #{param.categoryId}
        </if>
          AND deleted = 0
      </where>
    </select>

</mapper>
