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

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*, b.nickname, c.nickname AS firstNickname, d.nickname AS secondNickname, e.nickname AS thirdNickname, f.rate, f.price, g.nickname AS firstDividendUserName, h.nickname AS secondDividendUserName, o.order_status AS orderStatus
        FROM shop_dealer_order a
        LEFT JOIN gxwebsoft_core.sys_user b ON a.user_id = b.user_id
        LEFT JOIN gxwebsoft_core.sys_user c ON a.first_user_id = c.user_id
        LEFT JOIN gxwebsoft_core.sys_user d ON a.second_user_id = d.user_id
        LEFT JOIN gxwebsoft_core.sys_user e ON a.third_user_id = e.user_id
        LEFT JOIN gxwebsoft_core.sys_user g ON a.first_dividend_user = g.user_id
        LEFT JOIN gxwebsoft_core.sys_user h ON a.second_dividend_user = h.user_id
        LEFT JOIN shop_order o ON a.order_no = o.order_no
        LEFT JOIN shop_dealer_user f ON a.user_id = f.user_id
        <where>
            <if test="param.id != null">
                AND a.id = #{param.id}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.resourceId != null">
              AND ( a.first_user_id = #{param.resourceId} OR a.second_user_id = #{param.resourceId} OR a.third_user_id = #{param.resourceId} OR a.first_dividend_user = #{param.resourceId} OR a.first_dividend_user = #{param.resourceId} OR a.second_dividend_user = #{param.resourceId} )
            </if>
            <if test="param.month != null">
              AND a.month = #{param.month}
            </if>
            <if test="param.orderNo != null">
                AND a.order_no = #{param.orderNo}
            </if>
            <if test="param.orderPrice != null">
                AND a.order_price = #{param.orderPrice}
            </if>
            <if test="param.firstUserId != null">
                AND a.first_user_id = #{param.firstUserId}
            </if>
            <if test="param.secondUserId != null">
                AND a.second_user_id = #{param.secondUserId}
            </if>
            <if test="param.thirdUserId != null">
                AND a.third_user_id = #{param.thirdUserId}
            </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.isInvalid != null">
                AND a.is_invalid = #{param.isInvalid}
            </if>
            <if test="param.isSettled != null">
                AND a.is_settled = #{param.isSettled}
            </if>
            <if test="param.comments != null">
              AND a.comments = #{param.comments}
            </if>
            <if test="param.settleTime != null">
                AND a.settle_time = #{param.settleTime}
            </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.keywords != null">
                AND (a.comments LIKE CONCAT('%', #{param.keywords}, '%')
                  OR a.order_no LIKE CONCAT('%', #{param.keywords}, '%')
                  OR a.title LIKE CONCAT('%', #{param.keywords}, '%')
                  OR a.user_id = #{param.keywords}
                  OR a.month = #{param.keywords}
                )
              </if>
        </where>
    </sql>

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

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

</mapper>
