<?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="cn.iocoder.yudao.module.base.mapper.RepairStatisticsMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="customerSource" resultType="cn.iocoder.yudao.module.base.vo.RepairStatisticsVO">
        SELECT
            COALESCE(sdd.label, '未知') AS name,
            COUNT(bcm.id) AS value
        FROM
            base_customer_main bcm
            LEFT JOIN system_dict_data sdd ON bcm.data_from = sdd.value AND sdd.dict_type = 'cus_data_from' AND sdd.deleted = 0
        WHERE
            bcm.deleted = 0
        GROUP BY
            bcm.data_from
        ORDER BY
            bcm.create_time
    </select>
    <select id="ticketCountTrend" resultType="cn.iocoder.yudao.module.base.vo.RepairStatisticsVO">
        SELECT
            DATE_FORMAT(create_time, '%Y-%m') AS name,
            COUNT(*) AS value
        FROM
            dl_repair_tickets
        WHERE
            create_time >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
        GROUP BY
            name
        ORDER BY
            name
    </select>
    <select id="ticketAmountTrend" resultType="cn.iocoder.yudao.module.base.vo.RepairStatisticsVO">
        SELECT
            DATE_FORMAT(create_time, '%Y-%m') AS name,
            SUM(total_price) AS value
        FROM
            dl_repair_tickets
        WHERE
            create_time >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
        GROUP BY
            name
        ORDER BY
            name
    </select>
    <select id="paymentWay" resultType="cn.iocoder.yudao.module.base.vo.RepairStatisticsVO">
        SELECT
            COALESCE(sdd.label, '未知') AS name,
            COUNT(roi.id) AS value
        FROM
            repair_order_info roi
                LEFT JOIN system_dict_data sdd ON roi.pay_type = sdd.value AND sdd.dict_type = 'repair_pay_type' AND sdd.deleted = 0
        WHERE
            roi.deleted = 0
        GROUP BY roi.pay_type
        ORDER BY roi.create_time
    </select>
    <select id="repairTypeStatistics" resultType="cn.iocoder.yudao.module.base.vo.RepairStatisticsVO">
        SELECT
            COALESCE(sdd.label, '未知') AS `name`,
            COUNT( drt.id ) AS `value`
        FROM
            dl_repair_tickets drt
                LEFT JOIN system_dict_data sdd ON drt.repair_type = sdd.value AND sdd.dict_type = 'repair_type' AND sdd.deleted = 0
        WHERE
            drt.deleted = 0
        GROUP BY
            drt.repair_type
        ORDER BY
            drt.create_time
    </select>
    <select id="ticketStatistics" resultType="cn.iocoder.yudao.module.base.vo.RepairTicketStatisticsVO">
        SELECT
            COUNT( drt.id ) AS totalCount,
            SUM( drt.total_price ) AS totalAmount,
            COUNT( CASE WHEN DATE ( drt.create_time ) = CURDATE() THEN 1 END ) AS todayCount,
            SUM( CASE WHEN DATE ( drt.create_time ) = CURDATE() THEN drt.total_price ELSE 0 END ) AS todayAmount
        FROM
            dl_repair_tickets drt
        WHERE
            drt.deleted = 0
    </select>
    <select id="ticketStatusCount" resultType="cn.iocoder.yudao.module.base.vo.RepairStatisticsVO">
        SELECT
            sdd.label AS `name`,
            COUNT(drt.id) AS `value`
        FROM
            system_dict_data sdd
                LEFT JOIN
            dl_repair_tickets drt ON drt.tickets_status = sdd.`value`
        WHERE
            sdd.dict_type = 'repair_tickets_status'
          AND sdd.deleted = 0
        GROUP BY
            sdd.value
        ORDER BY
            sdd.value
    </select>
    <select id="listTicketToday" resultType="cn.iocoder.yudao.module.tickets.vo.DlRepairTicketsRespVO">
        SELECT
            drt.*
        FROM
            dl_repair_tickets drt
        WHERE
            drt.deleted = 0
          AND DATE ( drt.create_time ) = CURDATE()
        ORDER BY
            drt.create_time DESC
    </select>
    <select id="listWorks" resultType="cn.iocoder.yudao.module.base.vo.RepairStatisticsVO">
        SELECT
            drw.user_name AS `name`,
            COUNT(drr.id) AS `value`
        FROM
            dl_repair_worker drw
                LEFT JOIN dl_repair_records drr ON drw.user_id = drr.deal_user_id AND drr.deleted = 0
        WHERE drr.type = 'sgwczj'
        GROUP BY drw.user_id,drr.ticket_id
        ORDER BY value DESC
    </select>
    <select id="listBusinessByCustomer" resultType="cn.iocoder.yudao.module.base.vo.QueryBusinessResp"
            parameterType="cn.iocoder.yudao.module.base.vo.QueryBusinessReqVO">
        SELECT t.*
        FROM (
        SELECT
        c.id AS customerId,
        c.cus_name AS customerName,
        c.phone_number AS customerPhone,
        r.id AS bizId,
        r.ticket_no AS bizNo,
        r.repair_type AS bizType,
        r.in_time AS bizTime,
        r.handle_name AS handleName,
        'repair' AS source,
        '维修' AS sourceStr,
        ROW_NUMBER() OVER (PARTITION BY c.id ORDER BY r.in_time DESC) AS rn
        FROM base_customer_main c
        JOIN dl_repair_tickets r ON c.id = r.user_id
        WHERE
        1 = 1 and c.deleted = 0
        <if test="reqVO.dateRange != null">
            AND r.in_time BETWEEN CONCAT(#{reqVO.dateRange[0]}, ' 00:00:00') AND CONCAT(#{reqVO.dateRange[1]}, ' 23:59:59')
        </if>
        <if test="reqVO.search != null and reqVO.search != ''">
            AND (c.cus_name LIKE CONCAT('%', #{reqVO.search}, '%')
            OR c.phone_number LIKE CONCAT('%', #{reqVO.search}, '%')
            OR r.ticket_no LIKE CONCAT('%', #{reqVO.search}, '%')
            )
        </if>
        ORDER BY r.in_time DESC
<!--
        UNION ALL

        SELECT
        c.id AS customerId,
        c.cus_name AS customerName,
        c.phone_number AS customerPhone,
        ri.id AS bizId,
        NULL AS bizNo,
        ri.rescue_type AS bizType,
        ri.rescue_time AS bizTime,
        'rescue' AS source,
        ROW_NUMBER() OVER (PARTITION BY c.id ORDER BY ri.rescue_time DESC) AS rn
        FROM base_customer_main c
        JOIN rescue_info ri ON c.user_id = ri.user_id
        WHERE
        1 = 1 and c.deleted = 0
        <if test="reqVO.dateRange != null">
            AND ri.rescue_time BETWEEN #{reqVO.dateRange[0]} AND #{reqVO.dateRange[1]}
        </if>
        <if test="reqVO.search != null and reqVO.search != ''">
            AND (c.cus_name LIKE CONCAT('%', #{reqVO.search}, '%')
            OR c.phone_number LIKE CONCAT('%', #{reqVO.search}, '%')
            )
        </if>-->
        ) t
        WHERE t.rn = 1;
    </select>
    <select id="listBusinessByCar" resultType="cn.iocoder.yudao.module.base.vo.QueryBusinessResp"
            parameterType="cn.iocoder.yudao.module.base.vo.QueryBusinessReqVO">
        SELECT t.*
        FROM (
        -- 维修工单
        SELECT
        car.id AS carId,
        car.license_number carNum,
        c.id AS customerId,
        c.cus_name AS customerName,
        c.phone_number AS customerPhone,
        r.id AS bizId,
        r.ticket_no AS bizNo,
        r.repair_type AS bizType,
        r.create_time AS bizTime,
        'repair' AS source,
        '维修' AS sourceStr,
        ROW_NUMBER() OVER (PARTITION BY car.id ORDER BY r.in_time DESC) AS rn
        FROM base_car_main car
        JOIN base_customer_car cc ON car.id = cc.car_id
        JOIN base_customer_main c ON cc.cus_id = c.id
        JOIN dl_repair_tickets r ON c.id = r.user_id

        -- UNION ALL
        --
        -- -- 救援单
        -- SELECT
        -- car.id AS car_id,
        -- car.license_number,
        -- c.id AS customer_id,
        -- c.cus_name,
        -- c.phone_number,
        -- ri.id AS biz_id,
        -- NULL AS biz_no,
        -- ri.rescue_type AS biz_type,
        -- ri.rescue_time AS biz_time,
        -- 'rescue' AS source,
        -- ROW_NUMBER() OVER (PARTITION BY car.id ORDER BY ri.rescue_time DESC) AS rn
        -- FROM base_car_main car
        -- JOIN base_customer_car cc ON car.id = cc.car_id
        -- JOIN base_customer_main c ON cc.cus_id = c.id
        -- JOIN rescue_info ri ON c.id = ri.user_id
        -- WHERE ri.rescue_time BETWEEN '2025-01-01 00:00:00' AND '2025-01-31 23:59:59'
        ) t
        WHERE t.rn = 1
        <if test="reqVO.dateRange != null">
            AND t.bizTime BETWEEN CONCAT(#{reqVO.dateRange[0]}, ' 00:00:00') AND CONCAT( #{reqVO.dateRange[1]}, ' 23:59:59')
        </if>
        <if test="reqVO.search != null and reqVO.search != ''">
            AND (t.customerName LIKE CONCAT('%', #{reqVO.search}, '%')
            OR t.customerPhone LIKE CONCAT('%', #{reqVO.search}, '%')
            OR t.carNum LIKE CONCAT('%', #{reqVO.search}, '%')
            )
        </if>
        ORDER BY t.bizTime DESC
    </select>
    <select id="pageByCustomerOrCar" resultType="cn.iocoder.yudao.module.base.vo.QueryTableResp">
        SELECT drt.*, '维修' AS sourceStr
        FROM dl_repair_tickets drt
        WHERE 1=1
        <if test="reqVO.dateRange != null">
            AND DATE(drt.create_time) BETWEEN #{reqVO.dateRange[0]} AND #{reqVO.dateRange[1]}
        </if>
        <if test="reqVO.phone != null and reqVO.phone != ''">
            AND drt.user_mobile LIKE CONCAT('%', #{reqVO.phone}, '%')
        </if>
        <if test="reqVO.carNo != null and reqVO.carNo != ''">
            AND drt.car_no LIKE CONCAT('%', #{reqVO.carNo}, '%')
        </if>
    </select>
</mapper>
