<?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.hc.business.mapper.OrderUserLogMapper">

    <resultMap type="orderUserLog" id="OrderUserLogMap">
        <result property="orderLogId" column="order_log_id" jdbcType="INTEGER"/>
        <result property="orderId" column="order_id" jdbcType="INTEGER"/>
        <result property="reUserId" column="re_user_id" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        order_log_id,
           order_id,
           re_user_id,
           create_time
    </sql>

    <select id="queryByOrderId" resultMap="OrderUserLogMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM order_user_log
        WHERE order_id = #{ordersId}
    </select>

    <select id="queryByUserIdAndOrderId" resultMap="OrderUserLogMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM order_user_log
        WHERE order_id = #{ordersId} AND re_user_id = #{userId}
    </select>

    <select id="queryUserLogByOrderId" resultType="com.hc.business.dto.OrderUserLogDto">
        SELECT
            a.order_log_id,
            a.order_id,
            a.re_user_id,
            b.user_nick,
            b.user_name,
            b.user_mobile,
            b.user_email,
            b.user_company,
            b.user_gender
        FROM order_user_log a join user b on a.re_user_id=b.user_id
        WHERE a.order_id =#{orderId}

    </select>

    <select id="queryAllOrdersUser" resultType="com.hc.business.dto.OrdersAllUserDto">
        select
        d.user_name as userName,
        d.user_mobile as userMobile,
        d.user_gender as userGender,
        d.user_email as userEmail,
        d.user_company as userCompany,
        b.class_name as className,
        a.order_no as orderNo,
        a.order_status as orderStatus,
        a.create_time as payTime,
        a.price,
        a.type
        from orders a join class_config b on a.class_id=b.class_config_id
        join order_user_log c on a.orders_id = c.order_id
        join user d on c.re_user_id =d.user_id
        where a.is_de=1
        <if test="orderNo != null and orderNo!=''" >
            AND a.order_no = #{orderNo}
        </if>
        <if test="orderId != null">
            AND a.orders_id = #{orderId}
        </if>
        <if test="begin != null">
            AND a.create_time  &gt;= #{begin}
        </if>
        <if test="end != null">
            AND a.create_time &lt;= #{end}
        </if>
        <if test="status!=null ">
            AND a.order_status = #{status}
        </if>
        <if test="payType != null">
            AND a.type = #{payType}
        </if>
        <if test="isPay!=null ">
            <if test="isPay==1 ">
                AND (select count(1) from order_attachment where order_id=a.orders_id)>0
            </if>
            <if test="isPay==0">
                AND (select count(1) from order_attachment where order_id=a.orders_id)=0
            </if>
        </if>
        <if test="classId != null">
            AND a.class_id = #{classId}
        </if>
        ORDER BY a.create_time DESC
    </select>

</mapper>