<?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="cn.cecep.talroad.mapper.PCodCompanyMapper">
    <select id="queryPCodCompanyList" resultType="cn.cecep.talroad.domain.vo.PCodCompanyVo">
        SELECT
        a.company_id as companyId,
        a.company_name as companyName,
        a.company_tag as companyTag,
        b.name as companyTagName,
        a.credit_code as creditCode,
        a.pwxkz as pwxkz,
        a.company_type as companyType,
        c.name as companyTypeName,
        a.industry_code as industry,
        d.name as industryName,
        a.regist_address as registAddress,
        a.address as address,
        a.longitude as longitude,
        a.latitude as latitude,
        a.fk_air_pollutant as fkAirPollutantids,
        a.status as STATUS,
        a.province_code as provinceCode,
        a.city_code as cityCode,
        a.district_code as districtCode
        FROM
        p_cod_company a
        LEFT JOIN s_envgas_pollution_label as b ON a.company_tag = b.pk_id
        and b.region_code = #{source.areaCode}
        LEFT JOIN s_envgas_pollution_mold as c ON a.company_type = c.pk_id
        and c.region_code = #{source.areaCode}
        LEFT JOIN s_envgas_industry_category as d ON a.industry_code = d.id
        <where>
            <if test="source.companyType != null and source.companyType != 0">
                AND a.company_type = #{source.companyType}
            </if>
            <if test="source.companyIndustry != null and source.companyIndustry != 0">
                AND a.industry_code = #{source.companyIndustry}
            </if>
            <if test="source.industryCode != null and source.industryCode != ''">
                AND d.code LIKE concat(#{source.industryCode},'%')
            </if>
            <if test="source.companyTag != null and source.companyTag != 0">
                AND a.company_tag = #{source.companyTag}
            </if>
            <if test="source.companyName != null and source.companyName != ''">
                AND a.company_name LIKE concat('%',#{source.companyName},'%')
            </if>
            <if test="source.areaCode != null and source.areaCode != ''">
                AND (a.province_code = #{source.areaCode} OR a.city_code = #{source.areaCode} OR a.district_code =
                #{source.areaCode})
            </if>
            <if test="source.companyTypeList != null and source.companyTypeList.size > 0">
                AND a.company_type IN
                (
                <foreach collection="source.companyTypeList" item="item" separator=",">
                    #{item}
                </foreach>
                )
            </if>
        </where>
        ORDER BY a.createtime desc
    </select>
    <select id="checkNameIsExist" resultType="java.lang.Integer">
        SELECT count(1) from p_cod_company where company_name = #{name} AND (province_code = #{areaCode} OR city_code = #{areaCode} OR district_code = #{areaCode})
    </select>
    <select id="checkNameForUpdate" resultType="java.lang.Integer">
        SELECT count(1) from p_cod_company where company_name = #{name}
        AND (province_code = #{areaCode} OR city_code = #{areaCode} OR district_code = #{areaCode})
        AND company_id != #{companyId}
    </select>
</mapper>
