<?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.mobileInfo.modular.qcqualityinspection.mapper.BusinessQcQualityInspectionMapper">
    <resultMap id="BaseResultMap" type="com.unis.mobileInfo.modular.qcqualityinspection.entity.BusinessQcQualityInspection" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="createdate" property="createdate" jdbcType="TIMESTAMP" />
        <result column="updatename" property="updatename" jdbcType="VARCHAR" />
        <result column="updatedate" property="updatedate" jdbcType="TIMESTAMP" />
        <result column="zjbgdh" property="zjbgdh" jdbcType="VARCHAR" />
        <result column="org_id" property="orgId" jdbcType="INTEGER" />
        <result column="jylb" property="jylb" jdbcType="VARCHAR" />
        <result column="basic_storehouse_id" property="basicStorehouseId" jdbcType="INTEGER" />
        <result column="warehouse_id" property="warehouseId" jdbcType="INTEGER" />
        <result column="grain_variety_id" property="grainVarietyId" jdbcType="INTEGER" />
        <result column="grain_grade_id" property="grainGradeId" jdbcType="INTEGER" />
        <result column="rkrq" property="rkrq" jdbcType="DATE" />
        <result column="receive_year" property="receiveYear" jdbcType="DATE" />
        <result column="produce_area" property="produceArea" jdbcType="VARCHAR" />
        <result column="jysj" property="jysj" jdbcType="TIMESTAMP" />
        <result column="check_result" property="checkResult" jdbcType="TINYINT" />
        <result column="jydw" property="jydw" jdbcType="VARCHAR" />
        <result column="jyr" property="jyr" jdbcType="VARCHAR" />
        <result column="jyyj" property="jyyj" jdbcType="VARCHAR" />
        <result column="zblb" property="zblb" jdbcType="VARCHAR" />
        <result column="spaqzbsfhg" property="spaqzbsfhg" jdbcType="VARCHAR" />
        <result column="qfrq" property="qfrq" jdbcType="VARCHAR" />
        <result column="bgcjsj" property="bgcjsj" jdbcType="TIMESTAMP" />
        <result column="shrxm" property="shrxm" jdbcType="VARCHAR" />
        <result column="sfzcccnx" property="sfzcccnx" jdbcType="VARCHAR" />
        <result column="zbjgpd" property="zbjgpd" jdbcType="VARCHAR" />
        <result column="qysj" property="qysj" jdbcType="TIMESTAMP" />
        <result column="qydbh" property="qydbh" jdbcType="VARCHAR" />
        <result column="qyrxm" property="qyrxm" jdbcType="VARCHAR" />
        <result column="qyqy" property="qyqy" jdbcType="VARCHAR" />
        <result column="jdrxm" property="jdrxm" jdbcType="VARCHAR" />
        <result column="ypbh" property="ypbh" jdbcType="VARCHAR" />
        <result column="ypsl" property="ypsl" jdbcType="DECIMAL" />
        <result column="dbsl" property="dbsl" jdbcType="DECIMAL" />
        <result column="ypdj" property="ypdj" jdbcType="VARCHAR" />
        <result column="sample_status" property="sampleStatus" jdbcType="VARCHAR" />
        <result column="sample_package_model" property="samplePackageModel" jdbcType="VARCHAR" />
        <result column="bz" property="bz" jdbcType="VARCHAR" />
    </resultMap>
    <sql id="Base_Column_List" >
        id, createdate, updatename, updatedate, zjbgdh, org_id, jylb, basic_storehouse_id,
    warehouse_id, grain_variety_id, grain_grade_id, rkrq, receive_year, produce_area,
    jysj, check_result, jydw, jyr, jyyj, zblb, spaqzbsfhg, qfrq, bgcjsj, shrxm, sfzcccnx,
    zbjgpd, qysj, qydbh, qyrxm, qyqy, jdrxm, ypbh, ypsl, dbsl, ypdj, sample_status, sample_package_model,
    bz
    </sql>
    <select id="selectNo" resultType="com.unis.mobileInfo.modular.qcqualityinspection.vo.ReserveQualityOrgPageVO">
        select count(t1.org_id) noPassNum,t1.org_id,t2.org_name from depot_qh.business_qc_quality_inspection t1 left join depot_qh.org_info t2 on
        t1.org_id=t2.org_id
        <where>
            t1.check_result='0'
            <if test="orgName!=null and orgName!=''">
                and t2.org_name like concat("%",#{orgName},"%")
            </if>
        </where>
    </select>
    <select id="selectPass" resultType="com.unis.mobileInfo.modular.qcqualityinspection.vo.ReserveQualityOrgPageVO">
        select count(t1.org_id) passNum,t1.org_id ,t2.org_name from depot_qh.business_qc_quality_inspection t1 left join depot_qh.org_info t2 on
        t1.org_id=t2.org_id
        <where>
            t1.check_result='1'
            <if test="orgName!=null and orgName!=''">
                and t2.org_name like concat("%",#{orgName},"%")
            </if>
        </where>
    </select>


    <select id="selectMyPage"
            resultType="com.unis.mobileInfo.modular.qcqualityinspection.vo.ReserveQualityStoreHousePageVO">
        SELECT DISTINCT
        t1.id,
        t1.receive_year,
        t1.ypdj,
        t1.ypsl,
        t2.org_name,
        t3.storehouse_name basicStorehouseName,
        t4.warehouse_name,
        t5.enumname grainVarietyName
        FROM
        depot_qh.business_qc_quality_inspection t1
        LEFT JOIN depot_qh.org_info t2 ON t1.org_id = t2.org_id
        LEFT JOIN depot_qh.basic_storehouse t3 ON t1.basic_storehouse_id = t3.storehouse_id
        LEFT JOIN depot_qh.basic_warehouse t4 ON t1.warehouse_id = t4.warehouse_id
        LEFT JOIN depot_qh.basic_enum t5 ON t1.grain_variety_id = t5.enumId
        <where>
            t1.org_id=#{orgId}
            <if test="basicStorehouseName!=null and basicStorehouseName!=''">
                and t3.storehouse_name like concat("%",#{basicStorehouseName},"%")
            </if>
            <if test="grainVarietyName!=null and grainVarietyName!=''">
                and t5.enumname like concat("%",#{grainVarietyName},"%")
            </if>
            <if test="receiveYear!=null">
                and t1.receive_year =#{receiveYear}
            </if>
            <if test="ypdj!=null and ypdj!=''">
                and t1.ypdj like concat("%",#{ypdj},"%")
            </if>
        </where>
    </select>


    <select id="selectCity"
            resultType="com.unis.mobileInfo.modular.qcqualityinspection.vo.ReserveQualityCityPageVO">

        SELECT
            t3.zcdz_city cityId
        FROM
            business_qc_quality_inspection t1
                LEFT JOIN org_info t2 ON t1.org_id = t2.org_id
                LEFT JOIN province_all.biz_unit_info t3 ON t2.unit_info_id = t3.id
        GROUP BY
            t3.zcdz_city

    </select>

    <select id="selectCityPass"
            resultType="com.unis.mobileInfo.modular.qcqualityinspection.vo.ReserveQualityCityPageVO">

        SELECT
            count(t3.zcdz_city) passNum
        FROM
            depot_qh.business_qc_quality_inspection t1
                LEFT JOIN depot_qh.org_info t2 ON t1.org_id = t2.org_id
                LEFT JOIN province_all.biz_unit_info t3 ON t2.unit_info_id = t3.id
        where t3.zcdz_city=#{cityId} and t1.check_result='1'

    </select>

    <select id="selectCityNoPass"
            resultType="com.unis.mobileInfo.modular.qcqualityinspection.vo.ReserveQualityCityPageVO">

        SELECT
            count(t3.zcdz_city) noPassNum
        FROM
            depot_qh.business_qc_quality_inspection t1
                LEFT JOIN depot_qh.org_info t2 ON t1.org_id = t2.org_id
                LEFT JOIN province_all.biz_unit_info t3 ON t2.unit_info_id = t3.id
        where t3.zcdz_city=#{cityId} and t1.check_result='0'

    </select>

    <select id="selectCityPassByCityName"
            resultType="com.unis.mobileInfo.modular.qcqualityinspection.vo.ReserveQualityCityPageVO">

        SELECT
        count(t3.zcdz_city) passNum,t4.id cityId,t4.DICT_LABEL cityName
        FROM
        depot_qh.business_qc_quality_inspection t1
        LEFT JOIN depot_qh.org_info t2 ON t1.org_id = t2.org_id
        LEFT JOIN province_all.biz_unit_info t3 ON t2.unit_info_id = t3.id
        left join province_all.dev_dict t4 on t3.zcdz_city=t4.id
        <where>
            t1.check_result='1'
            <if test="cityName!=null and cityName!=''">
                and t4.DICT_LABEL like concat("%",#{cityName},"%")
            </if>
        </where>

    </select>

    <select id="selectCityNoPassByCityName"
            resultType="com.unis.mobileInfo.modular.qcqualityinspection.vo.ReserveQualityCityPageVO">

        SELECT
        count(t3.zcdz_city) noPassNum,t4.id cityId,t4.DICT_LABEL cityName
        FROM
        depot_qh.business_qc_quality_inspection t1
        LEFT JOIN depot_qh.org_info t2 ON t1.org_id = t2.org_id
        LEFT JOIN province_all.biz_unit_info t3 ON t2.unit_info_id = t3.id
        left join province_all.dev_dict t4 on t3.zcdz_city=t4.id
        <where>
            t1.check_result='0'
            <if test="cityName!=null and cityName!=''">
                and t4.DICT_LABEL like concat("%",#{cityName},"%")
            </if>
        </where>

    </select>
    <select id="selectCitys"
            resultType="com.unis.mobileInfo.modular.qcqualityinspection.vo.ReserveQualityCityPageVO">

        SELECT
        ifnull( COUNT( CASE WHEN t1.check_result = '1' THEN 1 END ), 0 ) AS passNum,
        ifnull( COUNT( CASE WHEN t1.check_result = '0' THEN 1 END ), 0 ) AS noPassNum,
        t4.id AS cityId,
        t4.DICT_LABEL AS cityName
        FROM
        depot_qh.business_qc_quality_inspection t1
        LEFT JOIN depot_qh.org_info t2 ON t1.org_id = t2.org_id
        LEFT JOIN province_all.biz_unit_info t3 ON t2.unit_info_id = t3.id
        LEFT JOIN province_all.dev_dict t4 ON t3.zcdz_city = t4.id
        <where>
            <if test="cityName!=null and cityName!=''">
                and t4.DICT_LABEL like concat("%",#{cityName},"%")
            </if>
        </where>

        GROUP BY
        t4.id,
        t4.DICT_LABEL;
    </select>
    <select id="selectNoAndPass"
            resultType="com.unis.mobileInfo.modular.qcqualityinspection.vo.ReserveQualityOrgPageVO">
        SELECT
        ifnull( COUNT( CASE WHEN t1.check_result = '1' THEN 1 END ), 0 ) AS passNum,
        ifnull( COUNT( CASE WHEN t1.check_result = '0' THEN 1 END ), 0 ) AS noPassNum,
        t1.org_id,
        t2.org_name
        FROM
        depot_qh.business_qc_quality_inspection t1
        LEFT JOIN depot_qh.org_info t2 ON t1.org_id = t2.org_id
        LEFT JOIN province_all.biz_unit_info t3 ON t2.unit_info_id = t3.id
        <where>
            t3.zcdz_city=#{cityId}
            <if test="orgName!=null and orgName!=''">
                and t2.org_name like concat("%",#{orgName},"%")
            </if>
        </where>
        GROUP BY
        t1.org_id,
        t2.org_name;
    </select>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        select
        <include refid="Base_Column_List" />
        from business_qc_quality_inspection
        where id = #{id,jdbcType=INTEGER}
    </select>
</mapper>