<?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.basic.info.modular.orgInfo.mapper.OrgInfoMapper">

    <select id="findPage" resultType="java.util.Map">
        select a.org_id orgId, a.org_name orgName, a.short_name shortName, a.org_code orgCode, a.org_class_id orgClassId, a.company_nature companyNature, a.busi_type busiType,
        a.address, a.contact, a.fax, a.charge_person chargePerson, a.longitude, a.latitude, a.regist_capital registCapital, a.work_number workNumber,
        a.area_code areaCode, a.area_name areaName,a.post, a.depot_class depotClass,a.effective_capacity effectiveCapacity, a.depot_area depotArea,
        a.complete_date completeDate, a.use_date useDate, a.company_org_code companyOrgCode, a.credit_code creditCode, a.regist_type registType, a.qualification,
        a.independent_corporation independentCorporation, a.legal_person legalPerson, a.whether_rent whetherRent, a.info_regist infoRegist, a.regist_date registDate, a.remark,
        a.parent_id parentId, a.status, a.del_flag, a.birds_eye birdsEye, a.birds_Remote birdsRemote, a.brief_introduction briefIntroduction, a.invoice_title invoiceTitle,
        a.regist_number registNumber, a.bank_name bankName, a.account_name accountName, a.account_number accountNumber,a.level_code levelCode,a.total_assets totalAssets,a.corporate_capacity corporateCapacity,
        a.office_phone officePhone,a.registered_address registeredAddress,a.mail_box mailBox,a.official_website officialWebsite,a.house_number houseNumber,a.tank_number tankNumber,a.depot_property depotProperty,
        a.updatetime, a.cityCouncil_code cityCouncilCode,a.frlxfs,a.yxgr,a.kqbm,a.sfzk, a.kq_number, a.zc_province, a.zc_city zcCity, a.zc_county zcCounty,a.unit_info_id unitInfoId,
        b.dwmc as qymc
        FROM
        depot_qh.org_info a
        LEFT JOIN province_all.biz_unit_info b on a.unit_info_id = b.id
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
        order  by a.org_id desc
    </select>
    <select id="selectOrgInfoBySsSafeProductInspectionId"
            resultType="com.unis.basic.info.modular.orgInfo.entity.OrgInfo">

        select t2.*
        from province_all.biz_ss_safe_product_inspection t1
                 left join depot_qh.org_info t2 on t1.org_id = t2.org_id
        where t1.id = #{ssSafeProductInspectionId}

    </select>
    <select id="findCountPage" resultType="java.lang.Integer">
        select count(org_id) as num from org_info
    </select>
    <select id="selectStoreHousePage" resultType="java.util.Map">
        SELECT
            bui.dwmc AS dwmc,
            SUM(DISTINCT oi.depot_area) AS depotArea,
            COUNT(DISTINCT bw.warehouse_id) AS warehouseCount,
        COALESCE(SUM(DISTINCT bt.tank_capacity)/1000, 0) AS tankCapacitySum,
        COALESCE(SUM(DISTINCT bs.design_capacity)/1000, 0) AS designCapacitySum,
        bui.ygs AS ygs,
        COALESCE(SUM(DISTINCT bs.actual_capacity)/1000, 0) AS actualCapacitySum,
        COALESCE((SUM(DISTINCT bs.design_capacity) - SUM(DISTINCT bs.actual_capacity))/1000, 0) AS emptySum
        FROM
            province_all.biz_unit_info bui
                INNER JOIN depot_qh.org_info oi ON oi.unit_info_id = bui.id
                LEFT JOIN depot_qh.basic_tank bt ON bt.org_id = oi.org_id AND bt.del_flag = 1
                LEFT JOIN depot_qh.basic_storehouse bs ON bs.org_id = oi.org_id AND bs.del_flag = 1
                LEFT JOIN depot_qh.basic_warehouse bw ON bw.org_id = oi.org_id AND bw.del_flag = 1
        where bui.delete_flag='NOT_DELETE'
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            and  ${ew.sqlSegment}
        </if>
        GROUP BY
            bui.id

    </select>
    <select id="selectStoreHouseCount" resultType="java.lang.Integer">
        select count(storehouse_id)as num from depot_qh.basic_storehouse where del_flag = 1
    </select>
    <select id="selectInventoryCount" resultType="java.lang.Integer">
        select ROUND(sum(kcsl)/1000,2) num from data_kcgl_kcsw_default
    </select>
    <select id="selectGrainNatureName" resultType="java.util.Map">
        SELECT DISTINCT
            oi.longitude,
            oi.latitude,
            oi.org_id orgId,
            oi.org_name orgName
        FROM
            depot_qh.data_kcgl_kcsw_default dkkd
                LEFT JOIN depot_qh.org_info oi on dkkd.UnitID=oi.org_id
        <if test="dataPageParam.type != null and dataPageParam.type != ''">
            where dkkd.hwxz=#{dataPageParam.type}
        </if>
    </select>
    <select id="selectKcCountPage" resultType="java.util.Map">

        SELECT
            kkd.UnitID unitId,
            oi.org_name orgName,
            bs.storehouse_name storehouseName,
            bw.warehouse_name warehouseName,
            be.enumname pzName,
            be1.enumname hwxzName,

            be2.enumname djName,
            kkd.kcsl
        FROM
            depot_qh.data_kcgl_kcsw_default kkd
                LEFT JOIN depot_qh.org_info oi on oi.org_id=kkd.UnitID

                LEFT JOIN depot_qh.basic_storehouse bs ON kkd.UnitID = bs.org_id
                AND kkd.ch = bs.storehouse_id
                LEFT JOIN depot_qh.basic_warehouse bw ON bs.org_id = bw.org_id
                AND kkd.hwh = bw.warehouse_id
                LEFT JOIN depot_qh.basic_enum be ON be.enumId = kkd.pz
                LEFT JOIN depot_qh.basic_enum be1 ON be1.enumId = kkd.hwxz
                LEFT JOIN depot_qh.basic_enum be2 ON be2.enumId = kkd.dj
            where kkd.UnitID =#{dataPageParam.orgId}
    </select>
    <select id="selectKcChart" resultType="java.util.Map">
        SELECT
            dj.enumname AS "GrainGrade",
            ROUND(SUM(kc.kcsl)/1000,2)AS TotalKilograms

        FROM
            depot_qh.data_kcgl_kcsw_default kc
                left JOIN
            depot_qh.basic_enum dj ON dj.enumId = kc.dj
        where kc.UnitID =#{dataPageParam.orgId}
        GROUP BY
            kc.dj

    </select>
    <select id="selectKcGrainNatureChart" resultType="java.util.Map">
        SELECT
            hwxz.enumname AS "GrainGrade",
            ROUND(SUM(kc.kcsl)/1000,2)AS TotalKilograms

        FROM
            depot_qh.data_kcgl_kcsw_default kc
                left JOIN
            depot_qh.basic_enum hwxz ON hwxz.enumId = kc.hwxz
        where kc.hwxz in (6877,6878,6879)
            and kc.UnitID=#{dataPageParam.orgId}
        GROUP BY
            kc.hwxz


    </select>
    <select id="selectKcSumRepot" resultType="java.util.Map">
        SELECT
            oi.org_name orgName,
            ROUND(SUM(kkd.kcsl)/1000,2) AS totalStock

        FROM
            depot_qh.data_kcgl_kcsw_default kkd
                LEFT JOIN depot_qh.org_info oi ON oi.org_id = kkd.UnitID
        where kkd.UnitID=#{dataPageParam.orgId}

    </select>
    <select id="selectKcGrainGradeRepot" resultType="java.util.Map">
        SELECT
            pz.enumname AS "GrainGrade",
            ROUND(SUM(kc.kcsl)/1000,2)AS TotalKilograms

        FROM
            depot_qh.data_kcgl_kcsw_default kc
                JOIN
            depot_qh.basic_enum pz ON pz.enumId = kc.pz
        where kc.UnitID=#{dataPageParam.orgId}
        GROUP BY
            kc.pz
    </select>
    <select id="selectKcGrainNatureRepot" resultType="java.util.Map">
        SELECT
            hwxz.enumname AS "GrainNature",
            ROUND(SUM(kc.kcsl)/1000,2)AS TotalKilograms

        FROM
            depot_qh.data_kcgl_kcsw_default kc
                JOIN
            depot_qh.basic_enum hwxz ON hwxz.enumId = kc.hwxz
        where  kc.hwxz in (6877,6878,6879) and kc.UnitID=#{dataPageParam.orgId}
        GROUP BY
            kc.hwxz


    </select>
    <select id="selectKcYearRepot" resultType="java.util.Map">
        SELECT
            YEAR(kkd.rq) AS inventoryYear,
            SUM(kkd.kcsl)/1000 AS yearlyStock
        FROM
            depot_qh.data_kcgl_kcsw_default kkd
        where kkd.UnitID=#{dataPageParam.orgId}
        GROUP BY
            YEAR(kkd.rq)
    </select>

    <select id="selectByOrgInfo" resultType="java.util.Map">
        SELECT DISTINCT
            a.org_id orgId,
            a.org_name orgName,
            a.short_name shortName,
            a.org_class_id orgClassId,
            a.company_nature companyNature,
            a.busi_type busiType,
            a.address,
            a.contact,
            a.fax,
            a.charge_person chargePerson,
            a.longitude,
            a.latitude,
            a.regist_capital registCapital,
            a.work_number workNumber,
            a.area_code areaCode,
            a.area_name areaName,
            a.post,
            a.depot_class depotClass,
            a.effective_capacity effectiveCapacity,
            a.depot_area depotArea,
            a.complete_date completeDate,
            a.use_date useDate,
            a.company_org_code companyOrgCode,
            a.credit_code creditCode,
            a.regist_type registType,
            a.qualification,
            a.independent_corporation independentCorporation,
            a.legal_person legalPerson,
            a.whether_rent whetherRent,
            a.info_regist infoRegist,
            a.regist_date registDate,
            a.remark,
            a.parent_id parentId,
            a.STATUS,
            a.del_flag,
            a.birds_eye birdsEye,
            a.birds_Remote birdsRemote,
            a.brief_introduction briefIntroduction,
            a.invoice_title invoiceTitle,
            a.regist_number registNumber,
            a.bank_name bankName,
            a.account_name accountName,
            a.account_number accountNumber,
            a.level_code levelCode,
            a.total_assets totalAssets,
            a.corporate_capacity corporateCapacity,
            a.office_phone officePhone,
            a.registered_address registeredAddress,
            a.mail_box mailBox,
            a.official_website officialWebsite,
            a.house_number houseNumber,
            a.tank_number tankNumber,
            a.depot_property depotProperty,
            a.updatetime,
            a.cityCouncil_code cityCouncilCode,
            a.frlxfs,
            a.yxgr,
            a.kqbm,
            a.sfzk,
            a.kq_number,
            a.zc_province,
            a.zc_city zcCity,
            a.zc_county zcCounty,
            a.unit_info_id unitInfoId,
            count(DISTINCT bs.storehouse_id)stoehouseCount,
            count(DISTINCT bw.warehouse_id)warehouseCount,
            COALESCE(SUM(DISTINCT bt.tank_capacity)/1000, 0) AS tankCapacitySum,
            COALESCE(SUM(DISTINCT bs.design_capacity)/1000, 0) AS designCapacitySum,
            count(DISTINCT bt.id)tankCount
        FROM
            depot_qh.org_info a
                LEFT JOIN depot_qh.basic_storehouse bs ON bs.org_id = a.org_id AND bs.del_flag = 1
                LEFT JOIN depot_qh.basic_warehouse bw ON bw.org_id = a.org_id AND bw.del_flag = 1
                LEFT JOIN depot_qh.basic_tank bt ON bt.org_id = a.org_id AND bt.del_flag = 1
        where a.org_id=#{dataPageParam.orgId}


    </select>



    <select id="statisticalAnalysisPage"
            resultType="com.unis.equipmentOnlineRateAssessment.modular.monitorCamera.vo.StatisticalAnalysisPageVO">

        SELECT DISTINCT
        oi.org_id,
        oi.org_name,
        bui.zcdz_city zcdzCityId,
        dd.DICT_LABEL zcdzCityName,
        t1.nvrNum,
        t2.cameraNum,
        t3.cameraAbnormalNum
        FROM
        depot_qh.org_info oi
        LEFT JOIN province_all.biz_unit_info bui ON oi.unit_info_id = bui.id
        LEFT JOIN province_all.dev_dict dd ON bui.zcdz_city = dd.ID
        LEFT JOIN (
        SELECT
        oi.org_id,
        count( mn.id ) nvrNum
        FROM
        depot_qh.org_info oi
        LEFT JOIN depot_qh.monitor_nvr mn ON oi.org_id = mn.org_id
        GROUP BY
        oi.org_id
        ) t1 ON oi.org_id = t1.org_id
        LEFT JOIN (
        SELECT
        oi.org_id,
        count( mc.id ) cameraNum
        FROM
        depot_qh.org_info oi
        LEFT JOIN depot_qh.monitor_camera mc ON oi.org_id = mc.org_id
        GROUP BY
        oi.org_id
        ) t2 ON oi.org_id = t2.org_id
        LEFT JOIN (
        SELECT
        oi.org_id,
        count( mc.id ) cameraAbnormalNum
        FROM
        depot_qh.org_info oi
        LEFT JOIN depot_qh.monitor_camera mc ON oi.org_id = mc.org_id
        WHERE
        mc.`status` != '1'
        GROUP BY
        oi.org_id
        ) t3 ON oi.org_id = t3.org_id

        <where>
            <if test="zcdzCityName!=null and zcdzCityName!=null">
               and dd.DICT_LABEL =#{zcdzCityName}
            </if>
        </where>

    </select>


    <select id="statisticalAnalysis"
            resultType="com.unis.equipmentOnlineRateAssessment.modular.monitorCamera.vo.StatisticalAnalysisVO">
        SELECT DISTINCT
            oi.org_id,
            oi.org_name,
            bui.zcdz_city zcdzCityId,
            dd.DICT_LABEL zcdzCityName,
            t1.nvrNum,
            t2.cameraNum,
            t3.cameraAbnormalNum
        FROM
            depot_qh.org_info oi
                LEFT JOIN province_all.biz_unit_info bui ON oi.unit_info_id = bui.id
                LEFT JOIN province_all.dev_dict dd ON bui.zcdz_city = dd.ID
                LEFT JOIN (
                SELECT
                    oi.org_id,
                    count( mn.id ) nvrNum
                FROM
                    depot_qh.org_info oi
                        LEFT JOIN depot_qh.monitor_nvr mn ON oi.org_id = mn.org_id
                GROUP BY
                    oi.org_id
            ) t1 ON oi.org_id = t1.org_id
                LEFT JOIN (
                SELECT
                    oi.org_id,
                    count( mc.id ) cameraNum
                FROM
                    depot_qh.org_info oi
                        LEFT JOIN depot_qh.monitor_camera mc ON oi.org_id = mc.org_id
                GROUP BY
                    oi.org_id
            ) t2 ON oi.org_id = t2.org_id
                LEFT JOIN (
                SELECT
                    oi.org_id,
                    count( mc.id ) cameraAbnormalNum
                FROM
                    depot_qh.org_info oi
                        LEFT JOIN depot_qh.monitor_camera mc ON oi.org_id = mc.org_id
                WHERE
                    mc.`status` != '1'
                GROUP BY
                    oi.org_id
            ) t3 ON oi.org_id = t3.org_id
    </select>
</mapper>