<?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.casic.missiles.mapper.business.BusinessOrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.model.business.BusinessOrder">
        <id column="id" property="id" />
        <result column="order_code" property="orderCode" />
        <result column="order_time" property="orderTime" />
        <result column="deliverer_id" property="delivererId" />
        <result column="deliverer" property="deliverer" />
        <result column="deliverer_tel" property="delivererTel" />
        <result column="deliver_time" property="deliverTime" />
        <result column="plan_deliver_time" property="planDeliverTime" />
        <result column="require_over_time" property="requireOverTime" />
        <result column="customer_id" property="customerId" />
        <result column="certification_company" property="certificationCompany" />
        <result column="certification_company_address" property="certificationCompanyAddress" />
        <result column="status" property="status" />
        <result column="remark" property="remark" />
        <result column="minio_file_name" property="minioFileName" />
        <result column="certifications" property="certifications" />
        <result column="is_urgent" property="isUrgent" />
        <result column="is_del" property="isDel" />
        <result column="create_user" property="createUser" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, order_code, order_time, deliverer_id, deliverer, deliverer_tel, deliver_time, plan_deliver_time, require_over_time, customer_id, status, remark, minio_file_name, certifications, is_urgent, is_del, create_user, create_time, update_time
    </sql>

    <select id="selectIdsByStatus" resultType="java.lang.Long">
        SELECT id
        FROM business_order
        WHERE is_del = 0 AND status = #{orderStatus}
    </select>

    <select id="ordersToday" resultType="java.lang.Integer">
        SELECT COUNT(id) AS orders
        FROM business_order
        WHERE is_del = 0 AND DATE(create_time) = CURDATE()
    </select>

    <select id="ordersThisYear" resultType="java.lang.Integer">
        SELECT COUNT(id) AS orders
        FROM business_order
        WHERE is_del = 0 AND YEAR(create_time) = YEAR(NOW())
    </select>

    <select id="ordersThisMonth" resultType="java.lang.Integer">
        SELECT COUNT(id) AS orders
        FROM business_order
        WHERE is_del = 0 AND DATE_FORMAT(create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')
    </select>

    <select id="selectOrderForCockpit" resultType="com.casic.missiles.model.business.BusinessOrder">
        SELECT *
        FROM business_order
        WHERE is_del = 0
        AND (status = 2 OR status = 4)
        ORDER BY create_time DESC
    </select>

    <select id="selectOrderCityRank" resultType="com.casic.missiles.dto.cockpit.CityTopResponse">
        SELECT ci.address_city_name AS cityName, COUNT(case when certifications=1 then 1 end) AS orderCount, COUNT(case when certifications=2 then 1 end) AS calibrationOrderCount
        FROM business_order bo
        JOIN customer_info ci ON bo.customer_id = ci.id
        WHERE bo.is_del = 0
        GROUP BY ci.address_city_name
        HAVING ci.address_city_name IS NOT NULL
        ORDER BY orderCount + calibrationOrderCount
    </select>

    <select id="selectPreMonthCountByYear" resultType="com.casic.missiles.dto.cockpit.BusTrendResponse">
        SELECT DATE_FORMAT(order_time,'%Y-%m') AS dimension, COUNT(id) AS orderCount
        FROM business_order
        WHERE is_del = 0
        AND DATE_SUB(CURDATE(), INTERVAL 365 DAY) &lt;= DATE(order_time)
        GROUP BY DATE_FORMAT(order_time,'%Y-%m')
    </select>
</mapper>
