<?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.unis.lawEnforcementSupervision.modular.lesCase.mapper.LesCaseMapper">
    <select id="selectNumber" resultType="string">
        SELECT
            case_number caseNumber
        FROM
            biz_les_case
        ORDER BY
            id DESC
        LIMIT 1
    </select>
    <select id="getCaseHandler" resultType="map">
        SELECT
            id caseHandlerId,
            `name` caseHandlerName
        FROM
            biz_les_person
    </select>
    <select id="queryEntity" parameterType="string" resultType="com.unis.lawEnforcementSupervision.modular.lesCase.entity.LesCase">
        SELECT
            blc.*, blp.`name` caseHandlerName,
            be.enumname grainVarietyName
        FROM
            biz_les_case blc
        LEFT JOIN biz_les_person blp ON blc.case_handler_id = blp.id
        LEFT JOIN depot_qh.basic_enum be ON blc.grain_variety_id = be.enumId
        WHERE
            blc.id = #{id}
    </select>
    <select id="pageList" parameterType="com.baomidou.mybatisplus.core.conditions.Wrapper" resultType="com.unis.lawEnforcementSupervision.modular.lesCase.entity.LesCase">
        SELECT
            blc.*,
            be.enumname grainVarietyName
        FROM
            biz_les_case blc
        LEFT JOIN depot_qh.basic_enum be ON blc.grain_variety_id = be.enumId
        ${ew.customSqlSegment}
    </select>
    <select id="getCaseCount" resultType="com.unis.lawEnforcementSupervision.modular.lesCase.entity.LesCaseCount">
        SELECT
            COUNT(1) totalCount,
            SUM(
                CASE
                WHEN case_date = DATE_FORMAT(NOW(), '%Y-%m-%d') THEN
                    1
                ELSE
                    0
                END
            ) todayCount,
            SUM(
                CASE
                WHEN verification_progress = 2 THEN
                    1
                ELSE
                    0
                END
            ) completeCount,
            SUM(
                CASE
                WHEN verification_progress = 1 THEN
                    1
                ELSE
                    0
                END
            ) carryOnCount
        FROM
            biz_les_case
        WHERE
            case_date LIKE CONCAT(
                DATE_FORMAT(NOW(), '%Y'),
                "%"
            )
    </select>
    <select id="selectCurrentYearMonth_1" resultType="com.unis.lawEnforcementSupervision.modular.lesCase.entity.OverCurrentYearMonth">
        SELECT
            MONTH (case_date) AS currentYearMonth,
            COUNT(*) AS monthCount
        FROM
            biz_les_case
        WHERE
            verification_progress = 1
        AND YEAR (case_date) = DATE_FORMAT(NOW(), '%Y')
        GROUP BY
            MONTH (case_date)
        ORDER BY
            currentYearMonth
    </select>
    <select id="selectCurrentYearMonth_2" resultType="com.unis.lawEnforcementSupervision.modular.lesCase.entity.OverCurrentYearMonth">
        SELECT
            MONTH (case_date) AS currentYearMonth,
            COUNT(*) AS monthCount
        FROM
            biz_les_case
        WHERE
            verification_progress = 2
        AND YEAR (case_date) = DATE_FORMAT(NOW(), '%Y')
        GROUP BY
            MONTH (case_date)
        ORDER BY
            currentYearMonth
    </select>
</mapper>