<?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.grainReserves.modular.reserveQuantity.mapper.ReserveQuantityMapper">
    <select id="selectPageList" parameterType="com.baomidou.mybatisplus.core.conditions.Wrapper" resultType="com.unis.grainReserves.modular.reserveQuantity.entity.ReserveQuantity">
        SELECT
            t1.zcdz_city zcdzCity,
            t1.cityName cityName,
            t1.dwmc dwmc,
            t1.org_name orgName,
            t1.UnitID unitId,
            IFNULL(ROUND(t1.kcslSum / 1000, 3), 0) kcslSum,
            IFNULL(
                ROUND(t2.kcslPSum / 1000, 3),
                0
            ) kcslPSum,
            IFNULL(
                ROUND(t3.kcslCSum / 1000, 3),
                0
            ) kcslCSum,
            IFNULL(
                ROUND(t4.kcslCLSum / 1000, 3),
                0
            ) kcslCLSum
        FROM
            (
                SELECT
                    bui.zcdz_city,
                    dd.DICT_LABEL cityName,
                    bui.dwmc,
                    oi.org_name,
                    kkd.UnitID,
                    SUM(kkd.kcsl) kcslSum
                FROM
                    data_kcgl_kcsw_default kkd
                LEFT JOIN org_info oi ON kkd.UnitID = oi.org_id
                LEFT JOIN province_all.biz_unit_info bui ON bui.id = oi.unit_info_id
                LEFT JOIN province_all.dev_dict dd ON bui.zcdz_city = dd.ID
                GROUP BY
                    kkd.UnitID
            ) t1
        LEFT JOIN (
            SELECT
                bui.zcdz_city,
                bui.dwmc,
                oi.org_name,
                kkd.UnitID,
                SUM(kkd.kcsl) kcslPSum
            FROM
                data_kcgl_kcsw_default kkd
            LEFT JOIN org_info oi ON kkd.UnitID = oi.org_id
            LEFT JOIN province_all.biz_unit_info bui ON bui.id = oi.unit_info_id
            WHERE
                hwxz = '6877'
            GROUP BY
                kkd.UnitID
        ) t2 ON t1.UnitID = t2.UnitID
        LEFT JOIN (
            SELECT
                bui.zcdz_city,
                bui.dwmc,
                oi.org_name,
                kkd.UnitID,
                SUM(kkd.kcsl) kcslCSum
            FROM
                data_kcgl_kcsw_default kkd
            LEFT JOIN org_info oi ON kkd.UnitID = oi.org_id
            LEFT JOIN province_all.biz_unit_info bui ON bui.id = oi.unit_info_id
            WHERE
                hwxz = '6878'
            GROUP BY
                kkd.UnitID
        ) t3 ON t1.UnitID = t3.UnitID
        LEFT JOIN (
            SELECT
                bui.zcdz_city,
                bui.dwmc,
                oi.org_name,
                kkd.UnitID,
                SUM(kkd.kcsl) kcslCLSum
            FROM
                data_kcgl_kcsw_default kkd
            LEFT JOIN org_info oi ON kkd.UnitID = oi.org_id
            LEFT JOIN province_all.biz_unit_info bui ON bui.id = oi.unit_info_id
            WHERE
                hwxz = '6879'
            GROUP BY
                kkd.UnitID
        ) t4 ON t1.UnitID = t4.UnitID
        ${ew.customSqlSegment}
    </select>
    <select id="storagePage" parameterType="com.baomidou.mybatisplus.core.conditions.Wrapper" resultType="com.unis.grainReserves.modular.reserveQuantity.entity.ReserveQuantityKQ">
        SELECT
            kkd.UnitID unitId,
            kkd.lkmc,
            kkd.ch,
            bs.storehouse_name storehouseName,
            kkd.hwh,
            bw.warehouse_name warehouseName,
            kkd.pz,
            be.enumname pzName,
            kkd.hwxz,
            be1.enumname hwxzName,
            kkd.dj,
            be2.enumname djName,
            IFNULL(ROUND(kkd.kcsl / 1000, 3), 0) kcslSum
        FROM
            data_kcgl_kcsw_default kkd
        LEFT JOIN basic_storehouse bs ON kkd.UnitID = bs.org_id
        AND kkd.ch = bs.storehouse_id
        LEFT JOIN basic_warehouse bw ON bs.org_id = bw.org_id AND kkd.hwh = bw.warehouse_id
        LEFT JOIN basic_enum be ON be.enumId = kkd.pz
        LEFT JOIN basic_enum be1 ON be1.enumId = kkd.hwxz
        LEFT JOIN basic_enum be2 ON be2.enumId = kkd.dj
        ${ew.customSqlSegment}
    </select>
</mapper>