<?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>
</mapper>
