<?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.finishedGrainSupervision.modular.mapper.DataKcglKcswDefaultCountMapper">


    <select id="selectMaPage" resultType="java.util.Map">
        select DISTINCT
        oi.org_name orgName,
        kc.kcsl lssl,
        Bs.storehouse_name storehouseName,
        bw.warehouse_name warehouseName,
        pz.enumname as "grainVarietyName",
        xz.enumname as "grainNatureName",
        dq.DICT_LABEL area,
        dj.enumname as "grainGradeName"

        from depot_qh.data_kcgl_cplkcsw_default kc
        LEFT JOIN depot_qh.basic_storehouse Bs ON kc.ch = Bs.storehouse_id
        LEFT JOIN  depot_qh.basic_warehouse bw on kc.hwh = bw.warehouse_id
        LEFT JOIN depot_qh.basic_enum pz on pz.enumId = kc.sjjlpzbh
        LEFT JOIN depot_qh.basic_enum xz on xz.enumId = kc.xz
        LEFT JOIN depot_qh.basic_enum dj on dj.enumId = kc.sjjldjbh
        left join depot_qh.org_info oi on oi.org_id=kc.UnitID
        left join province_all.biz_unit_info  bui on oi.unit_info_id=bui.id
        left join province_all.dev_dict dq  on dq.ID =bui.zcdz_city
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where ${ew.sqlSegment}
        </if>
    </select>
    <select id="statisticsGrainGrade" resultType="java.util.Map">
        SELECT
            dj.enumname AS "GrainGrade",
            SUM(kc.kcsl) AS TotalKilograms,
            ROUND(100.0 * SUM(kc.kcsl) / (SELECT SUM(kcsl) FROM depot_qh.data_kcgl_cplkcsw_default), 2) AS PercentageOfTotal
        FROM
            depot_qh.data_kcgl_cplkcsw_default kc
                JOIN
            depot_qh.basic_enum dj ON dj.enumId = kc.sjjldjbh

        GROUP BY
            kc.sjjldjbh

    </select>
    <select id="statisticsArea" resultType="java.util.Map">
        SELECT
            dq.DICT_LABEL AS "Area",
            ROUND(SUM(kc.kcsl) / 1000.0, 2) AS TotalInventoryInTons
        FROM
            depot_qh.data_kcgl_cplkcsw_default kc
                LEFT JOIN
            depot_qh.org_info oi ON kc.UnitID = oi.org_id
                LEFT JOIN
            province_all.biz_unit_info bui ON oi.unit_info_id = bui.id
                LEFT JOIN
            province_all.dev_dict dq ON dq.ID = bui.zcdz_city

        GROUP BY
            dq.DICT_LABEL
        ORDER BY
            TotalInventoryInTons DESC;


    </select>
    <select id="statisticsGrainVarietyName" resultType="java.util.Map">
        SELECT
            pz.enumname AS "GrainVarietyName",
            SUM(kc.kcsl)/1000 AS TotalKilograms,
            ROUND(100.0 * SUM(kc.kcsl) / (SELECT SUM(kcsl) FROM depot_qh.data_kcgl_cplkcsw_default kc), 2) AS PercentageOfTotal
        FROM
            depot_qh.data_kcgl_cplkcsw_default kc
                left  JOIN
            depot_qh.basic_enum pz ON pz.enumId = kc.sjjlpzbh

        GROUP BY
            kc.sjjlpzbh
        ORDER BY
            PercentageOfTotal DESC;

    </select>
    <select id="statisticsIn" resultType="java.util.Map">
        SELECT
            months.month_number,
            COALESCE(totals.sjjlsl, 0) AS sjjlsl
        FROM
            (SELECT 1 AS month_number UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION
             SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS months
                LEFT JOIN
            (SELECT
                 EXTRACT(MONTH FROM ywsj) AS month_number,
                 COALESCE(SUM(sjjlsl)/1000, 0) AS sjjlsl
             FROM
                 depot_qh.data_crkyw_cplrk_default
             WHERE
                 EXTRACT(YEAR FROM ywsj) = #{tjnf}
             GROUP BY
                 month_number) AS totals
            ON
                months.month_number = totals.month_number
        ORDER BY
            months.month_number;

    </select>
    <select id="statisticsOut" resultType="java.util.Map">
        SELECT
            months.month_number,
            COALESCE(totals.sjjlsl, 0) AS sjjlsl
        FROM
            (SELECT 1 AS month_number UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION
             SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS months
                LEFT JOIN
            (SELECT
                 EXTRACT(MONTH FROM ywsj) AS month_number,
                 COALESCE(SUM(sjjlsl)/1000, 0) AS sjjlsl
             FROM
                 depot_qh.data_crkyw_cplck_default
             WHERE
                 EXTRACT(YEAR FROM ywsj) = #{tjnf}
             GROUP BY
                 month_number) AS totals
            ON
                months.month_number = totals.month_number
        ORDER BY
            months.month_number;

    </select>
    <select id="statisticsPlan" resultType="java.util.Map">
        SELECT
            PlanStatus,
            SUM(PlanCount) AS TotalForStatus,
            ROUND(SUM(PlanCount) * 100.0 / (SELECT COUNT(*) FROM depot_qh.business_pg_gops_plan), 2) AS Percentage
        FROM
            (
                SELECT
                    CASE
                        WHEN plan_quantity = completion_quantity THEN '已完成计划'
                        ELSE '未完成计划'
                        END AS PlanStatus,
                    COUNT(*) AS PlanCount
                FROM
                    depot_qh.business_pg_gops_plan
                GROUP BY
                    CASE
                        WHEN plan_quantity = completion_quantity THEN '已完成计划'
                        ELSE '未完成计划'
                        END
            ) AS StatusCounts
        GROUP BY
            PlanStatus;



    </select>
    <select id="statisticsGrainNature" resultType="java.util.Map">
        SELECT
            pz.enumname AS "GrainNatureName",
            SUM(kc.kcsl)/1000 AS TotalKilograms,
            ROUND(100.0 * SUM(kc.kcsl) / (SELECT SUM(kcsl) FROM depot_qh.data_kcgl_cplkcsw_default kc), 2) AS PercentageOfTotal
        FROM
            depot_qh.data_kcgl_cplkcsw_default kc
                left  JOIN
            depot_qh.basic_enum pz ON pz.enumId = kc.xz
        where kc.xz in (6877,6878,6879)
        GROUP BY
            kc.xz
        ORDER BY
            PercentageOfTotal DESC;

    </select>
    <select id="statisticsQualitye" resultType="java.util.Map">
        SELECT
            dj.enumname AS "GrainGrade",
            ROUND(SUM(kc.kcsl)/1000,2) AS TotalKilograms,
            ROUND(100.0 * SUM(kc.kcsl) / (SELECT SUM(kcsl) FROM depot_qh.data_kcgl_cplkcsw_default), 2) AS PercentageOfTotal
        FROM
            depot_qh.data_kcgl_cplkcsw_default kc
                JOIN
            depot_qh.basic_enum dj ON dj.enumId = kc.sjjldjbh

        GROUP BY
            kc.sjjldjbh



    </select>

    <select id="findUnitPage" resultType="com.unis.finishedGrainSupervision.modular.entity.vo.UnitPageVO">
        SELECT DISTINCT
        a.id,
        a.dwmc,
        a.fddbr,
        a.tydwbm,
        e.DICT_LABEL dwlxName
        FROM
        biz_unit_info a
        LEFT JOIN depot_qh.org_info b ON a.id = b.unit_info_id
        LEFT JOIN depot_qh.basic_warehouse c ON b.org_id = c.org_id
        LEFT JOIN dev_dict e ON a.dwlx = e.ID
        WHERE c.lylx = 1
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            and ${ew.sqlSegment}
        </if>
        ORDER BY a.id
    </select>
    <select id="findOrgInfoPage" resultType="com.unis.finishedGrainSupervision.modular.entity.vo.OrgInfoPageVO">
        SELECT DISTINCT
        b.org_id,
        b.org_name,
        a.dwmc,
        b.design_capacity,
        b.depot_class,
        d.enumname  depotClaseName
        FROM
        depot_qh.org_info b
        LEFT JOIN biz_unit_info a ON a.id = b.unit_info_id
        LEFT JOIN depot_qh.basic_warehouse c ON b.org_id = c.org_id
        LEFT JOIN depot_qh.basic_enum d on d.enumId = b.depot_class
        WHERE c.lylx = 1
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            and ${ew.sqlSegment}
        </if>
        ORDER BY b.org_id
    </select>
    <select id="findStorehousePage" resultType="com.unis.finishedGrainSupervision.modular.entity.vo.StorehousePageVO">
        SELECT DISTINCT
        a.storehouse_id,
        b.org_id,
        b.org_name,
        a.storehouse_name,
        a.storehouse_code,
        a.design_capacity,
        e.enumname  storehouseTypeName
        FROM
        depot_qh.basic_storehouse a
        LEFT JOIN depot_qh.org_info b ON a.org_id = b.org_id
        LEFT JOIN depot_qh.basic_warehouse c ON b.org_id = c.org_id and a.storehouse_id = c.storehouse_id
        LEFT JOIN depot_qh.basic_enum e on e.enumId = a.storehouse_type
        WHERE c.lylx = 1
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            and ${ew.sqlSegment}
        </if>
        ORDER BY b.org_id,a.storehouse_code
    </select>
    <select id="findDevInputPage" resultType="com.unis.finishedGrainSupervision.modular.entity.vo.DevInputPageVO">
        SELECT DISTINCT
        a.id,
        b.org_id,
        b.org_name,
        c.enumname deviceTypeName,
        a.device_name,
        a.number,
        a.model,
        a.use_count
        FROM
        depot_qh.storage_deviceinput a
        LEFT JOIN depot_qh.org_info b ON a.org_id = b.org_id
        LEFT JOIN depot_qh.basic_enum c ON c.enumId = a.device_type
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            WHERE ${ew.sqlSegment}
        </if>
        ORDER BY b.org_id,a.id
    </select>
    <select id="getDevInputDetail" resultType="com.unis.finishedGrainSupervision.modular.entity.vo.DevInputDetailVO">
        SELECT DISTINCT
        a.id,
        a.device_type,
        a.device_name,
        a.model,
        a.use_count,
        a.device_manager,
        a.input_time,
        a.number,
        a.unit,
        a.device_price,
        a.storage_rack,
        a.storage_rack_cell,
        a.handler,
        a.book_keeper,
        a.producer,
        a.remark,
        a.service_life,
        a.parent_id,
        a.position,
        a.status,
        a.broken_count,
        a.produce_time,
        a.org_id,
        a.updatetime,
        b.org_name,
        c.enumname deviceTypeName
        FROM
        depot_qh.storage_deviceinput a
        LEFT JOIN depot_qh.org_info b ON a.org_id = b.org_id
        LEFT JOIN depot_qh.basic_enum c ON c.enumId = a.device_type
        WHERE a.id = #{id}
    </select>

    <select id="statisticsTransaction" resultType="java.util.Map">
        SELECT
            dq.DICT_LABEL AS "Area",
            SUM(bpgp.total_amount) AS total_amount_sum,
            SUM(bpgp.completion_amount) AS completion_amount_sum
        FROM
            depot_qh.business_pg_gops_plan bpgp
                left join depot_qh.org_info oi ON bpgp.org_id = oi.org_id
                LEFT JOIN
            province_all.biz_unit_info bui ON oi.unit_info_id = bui.id
                LEFT JOIN
            province_all.dev_dict dq ON dq.ID = bui.zcdz_city
        GROUP BY
            dq.DICT_LABEL

    </select>
    <select id="statisticsYearIn" resultType="java.util.Map">
        SELECT
            YEAR(kc.ywsj) AS Year,
            ROUND(SUM(kc.sjjlsl)/1000,3) AS TotalKCSL
        FROM
            depot_qh.data_crkyw_cplrk_default kc
        GROUP BY
            YEAR(kc.ywsj)
        ORDER BY
            Year ASC;
    </select>
    <select id="statisticsStorageFacilities" resultType="java.util.Map">

        WITH RECURSIVE enum_hierarchy AS (
            SELECT
                be.enumId,
                be.enumname,
                be.parentId,
                be.enumname AS top_parent_name  -- 初始情况下，顶层父类就是自己
            FROM
                depot_qh.basic_enum be
            WHERE
                be.enumId IN (7256, 7257, 7258, 7259, 7260, 7261, 7262) -- 顶级父类的parentId为NULL

            UNION ALL

            SELECT
                be.enumId,
                be.enumname,
                be.parentId,
                eh.top_parent_name
            FROM
                depot_qh.basic_enum be
                    JOIN
                enum_hierarchy eh ON be.parentId = eh.enumId
        )
        SELECT
            eh.top_parent_name AS type,
            COUNT(df.id) AS num
        FROM
            depot_qh.d_facilities_basics df
            JOIN
            enum_hierarchy eh ON df.ssfl = eh.enumname
        GROUP BY
            eh.top_parent_name;
    </select>



























    <select id="findDailycheckPage" resultType="com.unis.finishedGrainSupervision.modular.entity.vo.StorageDailycheckPageVO">
        SELECT DISTINCT
        a.id,
        b.org_id,
        b.org_name,
        e.storehouse_name houseName,
        f.warehouse_name warehouseName,
        c.enumname subTypeName,
        a.check_time,
        a.checker
        FROM
        depot_qh.storage_dailycheck a
        LEFT JOIN depot_qh.org_info b ON a.org_id = b.org_id

        LEFT JOIN depot_qh.basic_enum c ON c.enumId = a.sub_type
        LEFT JOIN depot_qh.basic_storehouse e on  b.org_id = e.org_id and a.house_id = e.storehouse_id
        LEFT JOIN depot_qh.basic_warehouse f ON b.org_id = f.org_id and a.warehouse_id = f.warehouse_id
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            WHERE ${ew.sqlSegment}
        </if>
        ORDER BY b.org_id,a.id
    </select>
    <select id="getDailycheckDetail" resultType="com.unis.finishedGrainSupervision.modular.entity.vo.StorageDailycheckDetailVO">
        SELECT DISTINCT
    a.id,
    a.house_id,
    a.warehouse_id,
    a.org_id,
    a.sub_type,
    a.check_time,
    a.checker,
    a.pests_position,
    a.pests_density,
    a.pests_species,
    a.rain_check,
    a.ratfinch_check,
    a.other_check,
    a.find_question,
    a.dailycard_id,
    a.flag_type,
    a.rcdoor,
    a.rcwindow,
    a.rclamp,
    a.rchygiene,
    a.rccorridor,
    a.bgy,
    a.cyry,
    a.tqqk,
    a.lw,
    a.cw,
    a.qw,
    a.cns,
    a.cws,
    a.sfjl,
    a.lsmbqk,
    a.cfywxl,
    a.sffc,
     b.org_id,
    b.org_name,
    e.storehouse_name houseName,
    f.warehouse_name warehouseName,
    c.enumname subTypeName
    FROM
    depot_qh.storage_dailycheck a
    LEFT JOIN depot_qh.org_info b ON a.org_id = b.org_id

    LEFT JOIN depot_qh.basic_enum c ON c.enumId = a.sub_type
	LEFT JOIN depot_qh.basic_storehouse e on  b.org_id = e.org_id and a.house_id = e.storehouse_id
	LEFT JOIN depot_qh.basic_warehouse f ON b.org_id = f.org_id and a.warehouse_id = f.warehouse_id
        WHERE a.id = #{id}
    </select>
    <select id="statisticsIntelligentEquipment" resultType="java.util.Map">
        (
            SELECT '粮情设备' AS device_type, COUNT(*) AS count
            FROM depot_qh.t_lqinfo
        ) UNION ALL (
            SELECT '气体设备', COUNT(*)
            FROM depot_qh.t_qtdevinfo
        ) UNION ALL (
            SELECT '虫害设备', COUNT(*)
            FROM depot_qh.t_ccinfo
        ) UNION ALL (
            SELECT '通风设备', COUNT(*)
            FROM depot_qh.t_ventilatio_info
        )
    </select>
</mapper>