<?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.statisticalReport.info.modular.conReportinfo.mapper.ConReportinfoMapper">
    <select id="getReserveChartDatas" resultType="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.ReserveReportinfo">
        <choose>
            <when test="param.type==1">
                SELECT
                a.pz,
                b.enumname as pzName,
                SUM(a.kcsl)/1000 as kcsl
                FROM
                depot_qh.data_kcgl_kcsw_default a
                LEFT JOIN depot_qh.basic_enum b ON a.pz = b.enumId
                GROUP BY	a.pz,b.enumname
                ORDER BY a.pz
            </when>
            <when test="param.type==2">
                SELECT
                a.hwxz,
                b.enumname as hwxzName,
                SUM(a.kcsl)/1000 as kcsl
                FROM
                depot_qh.data_kcgl_kcsw_default a
                LEFT JOIN depot_qh.basic_enum b ON a.hwxz = b.enumId
                GROUP BY	a.hwxz,b.enumname
                ORDER BY a.hwxz
            </when>
            <otherwise>
                SELECT
                a.scnf,
                b.enumname as scnfName,
                SUM(a.kcsl)/1000 as kcsl
                FROM
                depot_qh.data_kcgl_kcsw_default a
                LEFT JOIN depot_qh.basic_enum b ON a.scnf = b.enumId
                LEFT JOIN depot_qh.basic_enum c ON a.pz = c.enumId
                where c.enumname LIKE '%${param.pzName}%'
                GROUP BY	a.scnf,b.enumname
                ORDER BY a.scnf
            </otherwise>
        </choose>

    </select>
    <select id="findStorehousePage" resultType ="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.ReserveReportStorehouseinfo">
        SELECT DISTINCT
        d.org_id AS orgId,
        a.pz,
        a.hwxz,
        e.storehouse_id as storehouseId,
        f.warehouse_id as warehouseId,
        d.org_name AS orgName,
        b.enumname AS pzName,
        c.enumname AS hwxzName,
        e.storehouse_name as storehouseName,
        f.warehouse_name as warehouseName,
        a.kcsl/1000 kcsl
        FROM
        depot_qh.data_kcgl_kcsw_default a
        LEFT JOIN depot_qh.basic_enum b ON a.pz = b.enumId
        LEFT JOIN depot_qh.basic_enum c ON a.hwxz = c.enumId
        LEFT JOIN depot_qh.org_info d ON a.UnitID = d.org_id
        LEFT JOIN depot_qh.basic_storehouse e ON a.ch = e.storehouse_id
        LEFT JOIN depot_qh.basic_warehouse f on a.hwh = f.warehouse_id
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
        ORDER BY a.UnitID,a.ch,a.hwh
    </select>
    <select id="findLqData" resultType ="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.LqData">
        SELECT
    b.storehouse_name,
    a.gather_time,
    a.in_temp,
    a.in_h,
    a.out_temp,
    a.out_h,
    a.temp_max,
    a.temp_min,
    a.temp_avg
    FROM
        depot_qh.t_lq_data a
    inner JOIN depot_qh.basic_storehouse b on a.org_id = b.org_id and a.storehouse_code = b.storehouse_code
    where a.org_id = #{orgId} and b.storehouse_id = #{storehouseId}  and a.iLq_yq = 0
    ORDER BY gather_time desc LIMIT 1
    </select>
    <select id="findQualityInspection" resultType ="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.QualityInspection">
  SELECT
    Bs.storehouse_name as storehouseName,
    bw.warehouse_name as warehouseName,
    pz.enumname AS grainVarietyName,
    xz.enumname AS grainNatureName,
	dj.enumname as grainGradeName,
    dw.org_name as orgName,
    bqq.id,
    bqq.createdate,
    bqq.updatename,
    bqq.updatedate,
    bqq.org_id,
    bqq.zjbgdh,
    bqq.jylb,
    bqq.basic_storehouse_id,
    bqq.warehouse_id,
    bqq.grain_variety_id,
    bqq.grain_grade_id,
    bqq.grain_nature_id,
    bqq.rkrq,
    bqq.receive_year,
    bqq.produce_area,
    bqq.jysj,
    bqq.check_result,
    bqq.jydw,
    bqq.jyr,
    bqq.jyyj,
    bqq.zblb,
    bqq.spaqzbsfhg,
    bqq.qfrq,
    bqq.bgcjsj,
    bqq.shrxm,
    bqq.sfzcccnx,
    bqq.zbjgpd,
    bqq.qysj,
    bqq.qydbh,
    bqq.qyrxm,
    bqq.qyqy,
    bqq.jdrxm,
    bqq.ypbh,
    bqq.ypsl,
    bqq.dbsl,
    bqq.ypdj,
    bqq.sample_status,
    bqq.sample_package_model,
    bqq.bz
    FROM
        depot_qh.business_qc_quality_inspection bqq
    LEFT JOIN  depot_qh.basic_storehouse Bs ON bqq.basic_storehouse_id = Bs.storehouse_id
    LEFT JOIN  depot_qh.basic_warehouse bw ON bw.warehouse_id = bqq.warehouse_id
    LEFT JOIN depot_qh.basic_enum xz ON xz.enumId = bqq.grain_nature_id
    LEFT JOIN depot_qh.basic_enum pz ON pz.enumId = bqq.grain_variety_id
		LEFT JOIN depot_qh.basic_enum dj ON dj.enumId = bqq.grain_grade_id
    LEFT JOIN depot_qh.org_info dw ON dw.org_id = bqq.org_id
    WHERE
        bqq.warehouse_id = #{warehouseId}  and bqq.org_id =#{orgId}
    ORDER BY bqq.jysj DESC
    LIMIT 1
    </select>
    <select id="findQualityInspectionItems" resultType ="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.QualityInspectionItem">
  SELECT
	a.id,
	a.createdate,
	a.updatename,
	a.updatedate,
	a.qc_quality_inspection_id,
	a.inspection_item_id,
	mx.enumname as inspection_item_name,
	a.inspection_value,
	a.single_conclusion,
	a.inspection_method
FROM
	depot_qh.business_qc_quality_inspection_item a
LEFT JOIN depot_qh.basic_enum mx ON mx.enumId = a.inspection_item_id
where a.qc_quality_inspection_id = #{qualityId}
ORDER BY a.createdate
    </select>
    <select id="findCrkDataList" resultType ="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.CrkData">
  SELECT * FROM (SELECT
	pz.enumname as lspzName,
	xz.enumname as lsxzName,
	dw.org_name as orgName,
	unit.dwmc as unitName,
	a.BizNo as bizNo,
	a.cph,
	'入库' as ywlx,
	a.cyr,
	a.ywdjsj,
	a.djr
FROM
	depot_qh.data_crkyw_rmdj_default a
LEFT JOIN depot_qh.basic_enum xz ON xz.enumId = a.lsxz
LEFT JOIN depot_qh.basic_enum pz ON pz.enumId = a.lspz
LEFT JOIN depot_qh.org_info dw ON dw.org_id = a.UnitID
LEFT JOIN province_all.biz_unit_info unit on dw.unit_info_id = unit.id
INNER JOIN depot_qh.data_crkyw_cpz_default b on a.BizNo =b.BizNo
where a.UnitID =  #{orgId}  and b.sjhwID =  #{warehouseId}
UNION ALL
SELECT
	pz.enumname as lspzName,
	xz.enumname as lsxzName,
	dw.org_name as orgName,
	unit.dwmc as unitName,
	a.BizNo as bizNo,
	a.cph,
	'出库' as ywlx,
	a.cyr,
	a.rmsj as ywdjsj,
	a.djr
FROM
	depot_qh.data_crkyw_ckrmdj_default a
LEFT JOIN depot_qh.basic_enum xz ON xz.enumId = a.lyxz
LEFT JOIN depot_qh.basic_enum pz ON pz.enumId = a.lspz
LEFT JOIN depot_qh.org_info dw ON dw.org_id = a.UnitID
LEFT JOIN province_all.biz_unit_info unit on dw.unit_info_id = unit.id
INNER JOIN depot_qh.data_crkyw_ckcmz_default b on a.BizNo =b.BizNo
where a.UnitID = #{orgId}  and b.sjhwh =  #{warehouseId}
) x
ORDER BY ywdjsj
    </select>
    <select id="getStorehouseChartDatas" resultType ="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.StorehouseCapacityReportinfo">
       SELECT
        d.DICT_LABEL AS cityName,
        SUM(IFNULL(a.design_capacity,0.0)) designCapacity,
        SUM(IFNULL(a.actual_capacity,0.0)) actualCapacity
    FROM
        depot_qh.basic_storehouse a
    INNER JOIN depot_qh.org_info b ON a.org_id = b.org_id
    LEFT JOIN province_all.biz_unit_info c ON c.id = b.unit_info_id
    LEFT JOIN province_all.dev_dict d ON d.ID = c.zcdz_city
    WHERE
        a.del_flag = 1 -- 未删除
    GROUP BY
        d.DICT_LABEL
    ORDER BY  c.zcdz_city
    </select>

    <select id="findStorehouseCapacityPage" resultType ="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.StorehouseCapacityReportinfo">
        SELECT DISTINCT
        c.dwmc as unitName,
        b.org_name as orgName,
        a.storehouse_name storehouseName,
        zt.enumname storehouseStateName,
        IFNULL(SUM(kcsl),0.0)/1000 as kcsl,
        SUM(IFNULL(a.design_capacity,0.0)) designCapacity,
        SUM(IFNULL(a.actual_capacity,0.0)) actualCapacity,
        SUM(IFNULL(a.actual_capacity,0.0))-IFNULL(SUM(kcsl),0.0)/1000 actualNoneCapacity
        FROM
        depot_qh.basic_storehouse a
        INNER JOIN depot_qh.org_info b ON a.org_id = b.org_id
        LEFT JOIN province_all.biz_unit_info c ON c.id = b.unit_info_id
        LEFT JOIN depot_qh.data_kcgl_kcsw_default d on a.org_id = d.UnitID and a.storehouse_id = d.ch
        LEFT JOIN depot_qh.basic_enum zt on zt.enumId = a.storehouse_state
        where 1=1
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
             and ${ew.sqlSegment}
        </if>
        and a.del_flag = 1
        GROUP BY
        c.dwmc,b.org_name,a.storehouse_name
        ORDER BY  c.dwmc,b.org_name,a.storehouse_name,a.storehouse_state

    </select>
    <select id="findEsNetworkEntryPage" resultType ="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.EsNetworkEntryPageReportinfo">
        SELECT
        id,
        create_time,
        credit_code,
        enterprise_name,
        enterprise_contact,
        contact_number,
        city_state,
        type
        FROM
        (
        SELECT
        a.id,
        a.create_time,
        a.credit_code,
        a.enterprise_name,
        a.contact_person AS enterprise_contact,
        a.contact_number,
        a.city_state,
        '1' AS type
        FROM
        biz_es_network_entry a
        UNION ALL
        SELECT
        a.id,
        a.create_time,
        a.credit_code,
        a.enterprise_name,
        a.enterprise_contact,
        a.contact_number,
        a.city_state,
        '2' AS type
        FROM
        biz_es_process_enterprise a
        UNION ALL
        SELECT
        a.id,
        a.create_time,
        a.credit_code,
        a.enterprise_name,
        a.enterprise_contact,
        a.contact_number,
        a.city_state,
        '3' AS type
        FROM
        biz_es_delivery_enterprise a
        UNION ALL
        SELECT
        a.id,
        a.create_time,
        a.credit_code,
        a.enterprise_name,
        a.enterprise_contact,
        a.contact_number,
        a.city_state,
        '4' AS type
        FROM
        biz_es_storage_enterprise a
        ) x
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
        ORDER BY  type,create_time

    </select>
    <select id="findBuyPlanPage" resultType ="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.PlanReportinfo">
        SELECT
        id,
        orgName,
        planNumber,
        planName,
        planType,
        startDate,
        endDate,
        planQuantity,
        completedQuantity,
        completedQuantity/planQuantity*100 as planRate -- 占比
        FROM (SELECT
        c.id,
        d.org_name as orgName,
        e.plan_number as planNumber,
        c.plan_name as planName,
        c.plan_type as planType,
        e.start_date startDate,
        e.end_date endDate,
        SUM(f.plan_quantity) as planQuantity, -- 计划数量
        -- 计划数量
        (
        SELECT
        IFNULL(sum(m1.srsl), 0.0)/1000 AS "rksl"
        FROM
        depot_qh.data_kcgl_fcbgz_default m1
        INNER JOIN depot_qh.data_crkyw_cpz_default m2 ON m1.DataID = m2.DataID
        INNER JOIN depot_qh.data_crkyw_rmdj_default m3 ON m3.bizno = m2.bizno
        WHERE
        m3.jhdh = c.id
        -- 计划id
        ) AS "completedQuantity"
        -- 轮入-本期-数量

        FROM
        depot_qh.business_rp_report c
        inner JOIN depot_qh.business_rp_inbound f ON c.id = f.rp_report_id
        LEFT JOIN depot_qh.org_info d on f.org_id = d.org_id
        INNER JOIN province_all.biz_rp_issuance e on e.rp_report_id = c.id
        GROUP BY
        c.id,
        d.org_name,
        e.plan_number,
        c.plan_name,
        c.plan_type,
        e.start_date,
        e.end_date
        ) x
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
        ORDER BY  id

    </select>
    <select id="findCurrentPlanPage" resultType ="com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.PlanReportinfo">
        SELECT
        NOW() as currentDate,
        id,
        orgName,
        storehouseName,
        warehouseName,
        warehouseId,
        planNumber,
        planName,
        planType,
        startDate,
        endDate,
        planQuantity,
        completedQuantity,
        completedQuantity/planQuantity*100 as planRate -- 占比
        FROM (SELECT
        c.id,
        d.org_name as orgName,
        st.storehouse_name as storehouseName,
        wa.warehouse_name as warehouseName,
        f.warehouse_id as warehouseId,
        e.plan_number as planNumber,
        c.plan_name as planName,
        c.plan_type as planType,
        e.start_date startDate,
        e.end_date endDate,
        SUM(f.plan_quantity) as planQuantity, -- 计划数量
        -- 计划数量
        (
        SELECT
        IFNULL(sum(m1.zcsl), 0.0)/1000 AS "zcsl"
        FROM
        depot_qh.data_kcgl_fcbgz_default m1
        INNER JOIN depot_qh.data_crkyw_ckcpz_default m2 ON m1.DataID = m2.DataID
        INNER JOIN depot_qh.data_crkyw_ckrmdj_default m3 ON m3.bizno = m2.bizno

        WHERE
        m3.jhdID = c.id and m1.ch = f.basic_storehouse_id and m1.hwh = f.warehouse_id
        AND DATE_FORMAT(m1.rq,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
        -- 计划id
        ) AS "completedQuantity"
        -- 轮出-本期-数量

        FROM
        depot_qh.business_rp_report c
        inner JOIN depot_qh.business_rp_outbound f ON c.id = f.rp_report_id
        LEFT JOIN depot_qh.org_info d on f.org_id = d.org_id
        INNER JOIN province_all.biz_rp_issuance e on e.rp_report_id = c.id
        LEFT JOIN depot_qh.basic_storehouse st on f.basic_storehouse_id = st.storehouse_id
        LEFT JOIN depot_qh.basic_warehouse wa on f.warehouse_id = wa.warehouse_id
        GROUP BY
        c.id,
        d.org_name,
        st.storehouse_name,
        wa.warehouse_name,
        f.warehouse_id,
        e.plan_number,
        c.plan_name,
        c.plan_type,
        e.start_date,
        e.end_date
        ) x
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
        ORDER BY currentDate,id

    </select>
</mapper>