<?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.kczl.mapper.KczlMapper">

    <select id="findPage" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT
        SUM(a.kcsl) kcsl,
        b.org_name orgName,
        b.org_id orgId,
        b.address,
        c.zcdz_city city,
        b.design_capacity designCapacity,
        SUM(CASE WHEN d.check_result = 0 THEN dbsl ELSE 0 END) AS bhgsl,
        SUM(CASE WHEN d.check_result = 1 THEN dbsl ELSE 0 END) AS hgsl
        FROM
        depot_qh.data_kcgl_kcsw_default a
        LEFT JOIN depot_qh.org_info b on a.UnitID = b.org_id
        LEFT JOIN province_all.biz_unit_info c on c.id = b.unit_info_id
        left JOIN depot_qh.business_qc_quality_inspection d on d.org_id = b.org_id
       where 1=1
             <if test=" null != map.orgName">
                 and b.org_name like concat("%",#{map.orgName},"%")
             </if>
             <if test="null != map.city">
                 and c.zcdz_city = #{map.city}
             </if>
             <if test="null != map.designCapacity">
                 and b.design_capacity like concat("%",#{map.designCapacity},"%")
             </if>
        GROUP BY a.UnitID
    </select>

    <select id="detailsPage"  resultType="java.util.Map">
        SELECT
            qqi.id,
            qqi.org_id orgId,
            oi.org_name orgName,
            qqi.basic_storehouse_id basicStorehouseId,
            bs.storehouse_name storeHouseName,
            qqi.warehouse_id warehouseId,
            bw.warehouse_name warehouseName,
            qqi.grain_variety_id pz,
            be.enumname pzName,
            qqi.grain_nature_id xz,
            be1.enumname xzName,
            qqi.grain_grade_id dj,
            be2.enumname djName,
            qqi.dbsl dbsl
        FROM
        depot_qh.business_qc_quality_inspection qqi
        LEFT JOIN depot_qh.org_info oi ON qqi.org_id = oi.org_id
        LEFT JOIN depot_qh.basic_storehouse bs ON qqi.org_id = bs.org_id
        AND qqi.basic_storehouse_id = bs.storehouse_id
        LEFT JOIN depot_qh.basic_warehouse bw ON qqi.org_id = bw.org_id
        AND qqi.warehouse_id = bw.warehouse_id
        AND qqi.basic_storehouse_id = bs.storehouse_id
        LEFT JOIN depot_qh.basic_enum be ON qqi.grain_variety_id = be.enumId
        LEFT JOIN depot_qh.basic_enum be1 ON qqi.grain_nature_id = be1.enumId
        LEFT JOIN depot_qh.basic_enum be2 ON qqi.grain_grade_id = be2.enumId
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
        order  by qqi.basic_storehouse_id desc
    </select>

    <select id="selectZjByPrimaryKey" resultType="com.unis.statisticalAnalysis.modular.kczl.entity.BusinessQcQualityInspection" parameterType="java.lang.String" >
        select
            a.id, a.createdate, a.updatename, a.updatedate, a.zjbgdh, a.org_id, a.jylb, a.basic_storehouse_id,
            a.warehouse_id, a.grain_variety_id, a.grain_nature_id, a.grain_grade_id, a.rkrq, a.receive_year, a.produce_area,
            a.jysj, a.check_result, a.jydw, a.jyr, a.jyyj, a.zblb, a.spaqzbsfhg, a.qfrq, a.bgcjsj, a.shrxm, a.sfzcccnx,
            a.zbjgpd, a.qysj, a.qydbh, a.qyrxm, a.qyqy, a.jdrxm, a.ypbh, a.ypsl, a.dbsl, a.ypdj, a.sample_status, a.sample_package_model,
            a.bz,b.org_name orgName,bs.storehouse_name storeHouseName, bw.warehouse_name warehouseName,be.enumname grainVarietyName,
            be1.enumname grainNatureName,be2.enumname grainGradeName,b.registered_address registeredAddress
        from depot_qh.business_qc_quality_inspection a
        left join depot_qh.org_info b on a.org_id = b.org_id
        LEFT JOIN depot_qh.basic_storehouse bs ON a.org_id = bs.org_id  AND a.basic_storehouse_id = bs.storehouse_id
        LEFT JOIN depot_qh.basic_warehouse bw ON a.org_id = bw.org_id AND a.warehouse_id = bw.warehouse_id
        LEFT JOIN depot_qh.basic_enum be ON a.grain_variety_id = be.enumId
        LEFT JOIN depot_qh.basic_enum be1 ON a.grain_nature_id = be1.enumId
        LEFT JOIN depot_qh.basic_enum be2 ON a.grain_grade_id = be2.enumId
        where id = #{id}
    </select>

    <select id="selectZjMXByMainKey" resultType="com.unis.statisticalAnalysis.modular.kczl.entity.BusinessQcQualityInspectionItem" parameterType="java.lang.String" >
        select
            a.id, a.createdate, a.updatename, a.updatedate, a.qc_quality_inspection_id, a.inspection_item_id,a.parent_inspection_item_id,
            a.inspection_value, a.single_conclusion, a.inspection_method,be.enumname inspectionItemName,be1.enumname parentInspectionItemName
        from depot_qh.business_qc_quality_inspection_item a
        LEFT JOIN depot_qh.basic_enum be ON a.inspection_item_id = be.enumId
        LEFT JOIN depot_qh.basic_enum be1 ON a.parent_inspection_item_id = be1.enumId
        where qc_quality_inspection_id = #{id}
    </select>
</mapper>