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

    <resultMap type="zccmorders" 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"/>
    </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
    </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
    </sql>

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

    <select id="queryAllByUserId" resultMap="OrdersMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM zccm_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 zccm_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 zccm_orders
        WHERE class_id = #{classId}
        ORDER BY orders_id DESC
    </select>

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

    <select id="countPrice" resultType="java.math.BigDecimal">
        SELECT SUM(total_amount) as value
        FROM zccm_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 `zccm_orders`
              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 `zccm_orders`
              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 `zccm_orders`
              group by name) AS time
        WHERE name = left(current_date, 4)
    </select>

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

    <select id="queryAllWithInvoices" resultType="com.hc.business.dto.ZccmOrderInvoicesDto">
        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 zccm_user_invoices
        INNER JOIN zccm_orders o on zccm_user_invoices.user_invoices_id = o.user_invoices_id
        WHERE order_status = 1
        ORDER BY o.orders_id DESC
    </select>

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

</mapper>