<?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.statisticalAnalysis.modular.kcsw.mapper.KcswMapper">

    <select id="kcswpage" parameterType="com.unis.statisticalAnalysis.modular.kcsw.param.KcswPageParam" resultType="java.util.Map">
        SELECT
            a.UnitID,
            b.org_name,
            a.pz,
            e1.enumname pzmc,
            a.hwxz,
            e2.enumname xzmc,
            a.dj,
            e3.enumname djmc,
            SUM(a.kcsl) kcsl
        FROM
            depot_qh.data_kcgl_kcsw_default a
                INNER JOIN depot_qh.org_info b ON a.UnitID = b.org_id
                INNER JOIN depot_qh.basic_enum e1 ON e1.enumId = a.pz
                INNER JOIN depot_qh.basic_enum e2 ON e2.enumId = a.hwxz
                INNER JOIN depot_qh.basic_enum e3 ON e3.enumId = a.dj
        where 1=1
            <if test="null != kcswPageParam.orgId">
                and a.UnitID = #{kcswPageParam.orgId}
            </if>
            <if test="null != kcswPageParam.lsxz">
                and a.hwxz = #{kcswPageParam.lsxz}
            </if>
            <if test="null != kcswPageParam.lspz">
                and a.pz = #{kcswPageParam.lspz}
            </if>
            <if test="null != kcswPageParam.lsdj">
                and a.dj = #{kcswPageParam.lsdj}
            </if>
        GROUP BY
            a.UnitID,
            a.pz,
            a.hwxz,
            a.dj
     </select>

    <select id="qualityStatistics" parameterType="java.lang.Integer" resultType="java.util.Map">
        SELECT
            a.UnitID,
            a.dj,
            e3.enumname djmc,
            SUM(a.kcsl) kcsl
        FROM
          depot_qh.data_kcgl_kcsw_default a
                INNER JOIN depot_qh.basic_enum e3 ON e3.enumId = a.dj
        where 1=1
         <if test="null != orgId">
             and a.UnitID = #{orgId}
         </if>
        GROUP BY
            a.dj
    </select>

    <select id="grainPropertyStatistics" parameterType="java.lang.Integer" resultType="java.util.Map">
        SELECT
            a.UnitID,
            a.hwxz,
            e2.enumname xzmc,
            SUM(a.kcsl) kcsl
        FROM
          depot_qh.data_kcgl_kcsw_default a
        INNER JOIN depot_qh.basic_enum e2 ON e2.enumId = a.hwxz
        where a.hwxz in (select enumId from depot_qh.basic_enum where parentId = 6865) -- 6865 地方储备粮
        <if test="null != orgId">
            and a.UnitID = #{orgId}
        </if>
        GROUP BY
        a.hwxz
    </select>

    <select id="yjcs" parameterType="java.lang.Integer" resultType="java.util.Map">
        SELECT
            COUNT(id) count,
            '转圈粮预警' type
        FROM
           depot_qh.storage_pre_violation
            <if test="null != orgId">
                where org_id = #{orgId} -- 转圈粮
            </if>
        UNION
        SELECT
            COUNT(id) count,
            '临近轮换期' type
        FROM
            depot_qh.Storage_Rotation_Warning where bjlx = 0  -- 临近轮换期(轮换预警)
            <if test="null != orgId">
                and org_id = #{orgId}
            </if>
        UNION
        SELECT
            COUNT(id) count,
            '应轮未轮预警' type
        FROM
            depot_qh.Storage_Rotation_Warning where bjlx = 1  -- 应轮未轮
            <if test="null != orgId">
                and org_id = #{orgId}
            </if>
        UNION
        SELECT
            COUNT(id) count,
            '超架空期预警' type
        FROM
            depot_qh.Storage_Jkq_Warning
            <if test="null != orgId">
                where org_id = #{orgId}   -- -- 超架空期
            </if>
        UNION
        SELECT
            COUNT(id) count,
            '在库时间异常' type
        FROM
            province_all.biz_pss_storage_anomaly
            <if test="null != orgId">
                where org_id = #{orgId}   -- 在库时间
            </if>
    </select>

    <select id="quantity" resultType="java.util.Map">
        select SUM(kcsl) / 1000 as count,'kczl' type from depot_qh.data_kcgl_kcsw_default
        UNION
        select COUNT(id) count,'qysl' type from province_all.biz_unit_info where delete_flag = 'NOT_DELETE'
        UNION
        select COUNT(org_id) count,'kdsl' type from depot_qh.org_info
        UNION
        select COUNT(storehouse_id) count,'cfsl' type from depot_qh.basic_storehouse where datatype = 0 -- 仓房数据
               and del_flag = 1 -- 未删除
        UNION
        select COUNT(warehouse_id) count,'hwsl' type from depot_qh.basic_warehouse where  del_flag = 1 -- 未删除
    </select>

    <select id="inventoryRatio" resultType="java.util.Map">
        SELECT
            a.pz,
            e1.enumname pzmc,
            SUM(a.kcsl) /1000 as kcsl,
            SUM(a.kcsl)  / (SELECT SUM(kcsl) from depot_qh.data_kcgl_kcsw_default) * 100  as bili
        FROM
            depot_qh.data_kcgl_kcsw_default a
                INNER JOIN depot_qh.basic_enum e1 ON e1.enumId = a.pz
        GROUP BY a.pz
    </select>

    <select id="grainStorageStatistics" resultType="java.util.Map">
        select SUM(xzklhjz)/1000 sl,DATE_FORMAT(cpzsj, '%Y') yf from depot_qh.data_crkyw_cpz_default GROUP BY DATE_FORMAT(cpzsj, '%Y') ORDER BY  DATE_FORMAT(cpzsj, '%Y') asc
    </select>

    <select id="getRkslByMonth" resultType="java.util.Map">
        select SUM(xzklhjz)/1000 rksl,DATE_FORMAT(cpzsj, '%m') yf from depot_qh.data_crkyw_cpz_default where
            DATE_FORMAT(cpzsj, '%Y') = YEAR(CURDATE()) GROUP BY DATE_FORMAT(cpzsj, '%Y') ORDER BY  DATE_FORMAT(cpzsj, '%Y') asc
    </select>

    <select id="getCkslByMonth" resultType="java.util.Map">
        select SUM(klhjz)/1000 cksl,DATE_FORMAT(jmsj, '%m') yf from depot_qh.data_crkyw_ckcmz_default where
            DATE_FORMAT(jmsj, '%Y') = YEAR(CURDATE()) GROUP BY DATE_FORMAT(jmsj, '%m') ORDER BY DATE_FORMAT(jmsj, '%Y') asc
    </select>
    <select id="getKcslByMonth" resultType="java.util.Map">
        SELECT SUM(kcsl)/1000 kcsl,DATE_FORMAT(rq, '%m') yf from depot_qh.data_kcgl_kcsw_default where DATE_FORMAT(rq, '%Y') = YEAR(CURDATE()) GROUP BY DATE_FORMAT(rq, '%m')
    </select>

    <select id="getSyjyslByMonth" resultType="java.util.Map">
        select SUM(kcsl) syjysl,DATE_FORMAT(rq, '%m') yf from(
                select UnitID,hwh,MAX(rq) rq,kcsl / 1000  kcsl from depot_qh.data_kcgl_fcbgz_default  where DATE_FORMAT(rq, '%Y') = YEAR(CURDATE()) GROUP BY hwh
            ) c GROUP BY DATE_FORMAT(rq, '%m')
   </select>
</mapper>