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

    <resultMap type="orders" id="OrdersMap">
        <result property="ordersId" column="orders_id" jdbcType="INTEGER"/>
        <result property="orderNo" column="order_no" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
        <result property="classId" column="class_id" jdbcType="INTEGER"/>
        <result property="orderStatus" column="order_status" jdbcType="INTEGER"/>
        <result property="body" column="body" jdbcType="VARCHAR"/>
        <result property="subject" column="subject" jdbcType="VARCHAR"/>
        <result property="totalAmount" column="total_amount" jdbcType="NUMERIC"/>
        <result property="createTime" column="create_time" jdbcType="INTEGER"/>
        <result property="tradeNo" column="trade_no" jdbcType="VARCHAR"/>
        <result property="overTime" column="over_time" jdbcType="INTEGER"/>
        <result property="userInvoicesId" column="user_invoices_id" jdbcType="INTEGER"/>
        <result property="isDe" column="is_de" jdbcType="INTEGER"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="type" column="type" jdbcType="INTEGER"/>
        <result property="price" column="price" jdbcType="BIGINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        orders_id,
           order_no,
           user_id,
           class_id,
           order_status,
           `body`,
           `subject`,
           total_amount,
           create_time,
           trade_no,
           over_time,
           user_invoices_id,
           is_de,
           remark,
           type,
           price
    </sql>
    <sql id="Order_Column_List">
        o.orders_id,
           o.order_no,
           o.user_id,
           o.class_id,
           o.order_status,
           o.`body`,
           o.`subject`,
           o.total_amount,
           o.create_time,
           o.over_time,
           o.user_invoices_id,
           o.is_de,
           o.remark,
            o.price,
    </sql>

    <select id="queryByOrderNo" resultMap="OrdersMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM orders
        WHERE order_no = #{orderNo} AND is_de = 1
        ORDER BY orders_id DESC
        LIMIT 1
    </select>

    <select id="queryAllByUserId" resultMap="OrdersMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM orders
        WHERE user_id = #{userId} AND is_de = 1
        ORDER BY orders_id DESC
    </select>

    <select id="queryByUserInvoicesId" resultMap="OrdersMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM orders
        WHERE user_invoices_id = #{userInvoicesId} AND is_de = 1
        ORDER BY orders_id DESC
    </select>

    <select id="queryAllByClassId" resultMap="OrdersMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM orders
        WHERE class_id = #{classId} AND is_de = 1
        ORDER BY orders_id DESC
    </select>

    <select id="queryAllByStatus" resultMap="OrdersMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM orders
        WHERE order_status = #{status} AND is_de = 1
        ORDER BY orders_id DESC
    </select>

    <select id="countPrice" resultType="java.math.BigDecimal">
        SELECT SUM(total_amount) as value
        FROM orders
    </select>

    <select id="queryToday" resultType="java.math.BigDecimal">
        SELECT value
        FROM (SELECT FROM_UNIXTIME(create_time / 1000, "%Y-%m-%d") as name, SUM(total_amount) as value
              FROM `orders` where is_de=1
              group by name) AS time
        WHERE name = current_date
    </select>

    <select id="queryMonth" resultType="java.math.BigDecimal">
        SELECT value
        FROM (SELECT FROM_UNIXTIME(create_time / 1000, "%Y-%m") as name, SUM(total_amount) as value
              FROM `orders` where is_de=1
              group by name) AS time
        WHERE name = left(current_date, 7)
    </select>

    <select id="queryYear" resultType="java.math.BigDecimal">
        SELECT value
        FROM (SELECT FROM_UNIXTIME(create_time / 1000, "%Y") as name, SUM(total_amount) as value
              FROM `orders` where is_de=1
              group by name) AS time
        WHERE name = left(current_date, 4)
    </select>

    <select id="queryAllByClassName" resultMap="OrdersMap">
        SELECT
        <include refid="Order_Column_List"/>
        FROM orders o
        INNER JOIN class_config c ON o.class_id = c.class_config_id
        WHERE o.is_de=1 and c.class_name like "%"#{className}"%"
    </select>

    <select id="queryAllWithInvoices" resultType="com.hc.business.dto.OrderInvoicesDto">
        SELECT orders_id,order_no,o.user_id,o.class_id,order_status,`body`,`subject`,total_amount,
               o.create_time,over_time,o.user_invoices_id,is_de,remark,
               invoices_head,
               invoices_type,
               invoices_code,
               invoices_bank,
               invoices_user,
               invoices_no,
               invoices_price,
               `status`,
               `time`,
               remarks,
               remarks_sign,
               tax_code
        FROM user_invoices
                 INNER JOIN orders o on user_invoices.user_invoices_id = o.user_invoices_id
        WHERE order_status = 1 and is_de=1
        ORDER BY o.orders_id DESC
    </select>

    <select id="countOrderTotal" resultType="java.lang.Integer">
        SELECT COUNT(*) as value
        FROM orders
    </select>


    <select id="queryAllOrders" resultType="com.hc.business.dto.OrdersDto">
        select a.orders_id,
        a.class_id,
        a.user_id,
        a.order_no,
        b.class_name as className,
        a.total_amount,
        a.order_status,
        a.type,
        a.create_time,
        a.remark,
        a.price,
        cc.user_name,
        cc.user_mobile,
        (select pay_company from order_attachment where order_id=a.orders_id limit 0,1 ) as payCompany
        from orders a join class_config b on a.class_id=b.class_config_id
        join `user` cc on a.user_id = cc.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="classId != null">
            AND a.class_id = #{classId}
        </if>
        <if test="checkFlag == 0">
            AND a.order_status = 0 AND (a.remark is null or a.remark='')
        </if>
        <if test="checkFlag == 1">
            AND a.order_status != 0
        </if>
        <if test="checkFlag == 2">
            AND (a.remark is not null and a.remark!='')
        </if>
        <if test="payCompany != null and payCompany!=''" >
            AND a.orders_id = (select order_id from order_attachment where pay_company like concat('%',#{payCompany},'%') limit 0,1 )
        </if>
        ORDER BY a.create_time DESC
    </select>
</mapper>