<?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.nantian.cloud.jljc.mapper.EnterpriseInfoMapper">

    <!-- 通用查询映射结果
    <resultMap id="EnterpriseInfoMap" type="com.nantian.cloud.jljc.entity.EnterpriseInfo" >
        <id column="id" property="id" />
        <result column="enterprise_code" property="enterpriseCode" />
        <result column="isorg" property="isorg" />
        <result column="org_type" property="orgType" />
        <result column="org_code" property="orgCode" />
        <result column="password" property="password" />
        <result column="cmp_name" property="cmpName" />
        <result column="address" property="address" />
        <result column="zip_code" property="zipCode" />
        <result column="corporation_name" property="corporationName" />
        <result column="region_code" property="regionCode" />
        <result column="license" property="license" />
        <result column="organization" property="organization" />
        <result column="register_address" property="registerAddress" />
        <result column="register_date" property="registerDate" />
        <result column="register_principal" property="registerPrincipal" />
        <result column="type_code" property="typeCode" />
        <result column="industry_code" property="industryCode" />
        <result column="field_code" property="fieldCode" />
        <result column="energy_consume_level" property="energyConsumeLevel" />
        <result column="center" property="center" />
        <result column="jgzh" property="jgzh" />
        <result column="latitude" property="latitude" />
        <result column="longitude" property="longitude" />
        <result column="phone" property="phone" />
        <result column="fax" property="fax" />
        <result column="email" property="email" />
        <result column="url" property="url" />
        <result column="production_line" property="productionLine" />
        <result column="leading_product" property="leadingProduct" />
        <result column="group_name" property="groupName" />
        <result column="group_address" property="groupAddress" />
        <result column="apply_date" property="applyDate" />
        <result column="approval_date" property="approvalDate" />
        <result column="lastLogin_date" property="lastLoginDate" />
        <result column="state" property="state" />
        <result column="remark" property="remark" />
    </resultMap>-->

    <sql id="Base_Column_List">
        id,enterprise_code,isorg,org_type,org_code,password,cmp_name,address,zip_code,corporation_name,region_code,license,
        organization,register_address,register_date,register_principal,type_code,industry_code,field_code,energy_consume_level,
        center,jgzh,latitude,longitude,phone,fax,email,url,production_line,leading_product,group_name,group_address,apply_date,
        approval_date,last_login_date,state,remark,company_type,notice_year, userid, update_time, del_flag, dept_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="EnterpriseInfoMap2" type="com.nantian.cloud.jljc.entity.EnterpriseInfo" >
        <id column="id" property="id" />
        <result column="enterprise_code" property="enterpriseCode" />
        <result column="epc" property="epc" />
        <result column="isorg" property="isorg" />
        <result column="org_type" property="orgType" />
        <result column="org_code" property="orgCode" />
        <result column="password" property="password" />
        <result column="cmp_name" property="cmpName" />
        <result column="address" property="address" />
        <result column="zip_code" property="zipCode" />
        <result column="corporation_name" property="corporationName" />
        <result column="region_code" property="regionCode" />
        <result column="license" property="license" />
        <result column="organization" property="organization" />
        <result column="register_address" property="registerAddress" />
        <result column="register_date" property="registerDate" />
        <result column="register_principal" property="registerPrincipal" />
        <result column="type_code" property="typeCode" />
        <result column="industry_code" property="industryCode" />
        <result column="field_code" property="fieldCode" />
        <result column="energy_consume_level" property="energyConsumeLevel" />
        <result column="company_type" property="companyType" />
        <result column="center" property="center" />
        <result column="jgzh" property="jgzh" />
        <result column="latitude" property="latitude" />
        <result column="longitude" property="longitude" />
        <result column="phone" property="phone" />
        <result column="fax" property="fax" />
        <result column="email" property="email" />
        <result column="url" property="url" />
        <result column="production_line" property="productionLine" />
        <result column="leading_product" property="leadingProduct" />
        <result column="group_name" property="groupName" />
        <result column="group_address" property="groupAddress" />
        <result column="apply_date" property="applyDate" />
        <result column="approval_date" property="approvalDate" />
        <result column="last_login_date" property="lastLoginDate" />
        <result column="state" property="state" />
        <result column="remark" property="remark" />
        <result column="notice_year" property="noticeYear" />
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="dept_id" property="deptId"/>

        <result column="region name" property="regionName" />
        <result column="type_name" property="typeName" />
        <result column="field_name" property="fieldName" />
        <result column="industry_name" property="industryName" />
        <result column="energy_consume_level_name" property="energyConsumeLevelName" />
        <result column="company_type_name" property="companyTypeName" />
        <!--
        <result column="r_name" property="rName"/>
        <association property="tradeKinds" javaType="java.util.List" resultMap="tradekinding">
            <result column="id" property="id"/>
            <result column="code" property="code"/>
            <result column="name" property="name"/>
        </association>
        -->
    </resultMap>

    <resultMap id="tradekinding" type="com.nantian.cloud.jljc.entity.TradeKind">
        <id column="id" property="id" />
        <result column="category" property="category" />
        <result column="sort" property="sort" />
        <result column="code" property="code" />
        <result column="name" property="name" />
        <result column="type" property="type" />
    </resultMap>

    <select id="selectEnterpriseTradeType" resultType="com.nantian.cloud.jljc.entity.EnterpriseInfo">
        select a.enterprise_code,a.isorg,a.org_type,a.org_code,a.password,a.address,a.zip_code,a.corporation_name,a.region_code,a.license,a.organization,a.register_address,a.register_date,a.register_principal,a.type_code,a.industry_code,a.field_code,a.energy_consume_level,a.company_type,a.center,a.jgzh,a.latitude,a.longitude,a.phone,a.fax,a.email,a.url,a.production_line,a.leading_product,a.group_name,a.group_address,a.apply_date,a.approval_date,a.state,a.remark,a.id as id ,a.cmp_name as cmp_name ,a.address as address,b.`name` as r_name from enterprise_info a,trade_kind b
        <trim prefix="where" prefixOverrides="and">
            <if test="param2.address != null and param2.address != ''">
               and a.address like concat('%',#{param2.address},'%')
            </if>
            <if test="param2.cmpName != null and param2.cmpName != ''">
               and a.cmp_name like concat('%',#{param2.cmpName},'%')
            </if>
            <if test="param2.industryCode != null and param2.industryCode != ''">
              and a.industry_code like concat('%',#{param2.industryCode},'%')
            </if>
            and a.industry_code=b.`code`
        </trim>
    </select>

    <select id="getInfoByCode" resultType="com.nantian.cloud.jljc.entity.EnterpriseInfo">
        select <include refid="Base_Column_List"></include>
        from enterprise_info
        where enterprise_code = #{epc} and del_flag = '1'
    </select>

    <select id="getNameByCode" resultType="java.lang.String">
        select cmp_name
        from enterprise_info
        where enterprise_code = #{epc} and del_flag = '1'
    </select>

    <select id="getInfoNameByCode" resultType="com.nantian.cloud.jljc.entity.EnterpriseInfo">
        select  a.id, a.enterprise_code, a.isorg, a.org_type, a.org_code, a.password, a.cmp_name, a.address, a.zip_code, a.corporation_name, a.region_code,
        a.license, a.organization, a.register_address, a.register_date, a.register_principal, a.type_code, a.industry_code, a.field_code, a.energy_consume_level,
        a.center, a.jgzh, a.latitude, a.longitude, a.phone, a.fax, a.email, a.url, a.production_line, a.leading_product, a.group_name, a.group_address, a.apply_date,
        a.approval_date, a.last_login_date, a.state, a.remark, d.meaning regionName,  t.name1998 typeName,  k.name industryName,
        case a.field_code when '100' then '工业' when '200' then '交通运输、仓储和邮政业' when '300' then '住宿和餐饮业' when '400' then '批发和零售业' when '500' then '教育' else '其他' end as fieldName,
        a.company_type, p.type_name companyTypeName, l.level energyConsumeLevelName
        from enterprise_info a
        left join dict_region d on d.id=a.region_code
        left join dict_enterprise_type1998 t on t.id=a.type_code
        left join trade_kind k on k.code=a.industry_code
        left join company_type p on a.company_type = p.id
        left join energy_level l on a.energy_consume_level = l.id
		where a.enterprise_code = #{epc} and a.del_flag = '1'
    </select>

    <select id="getListByLevel" resultType="com.nantian.cloud.jljc.entity.EnterpriseInfo">
        select  a.id, a.enterprise_code, a.isorg, a.org_type, a.org_code, a.password, a.cmp_name, a.address, a.zip_code, a.corporation_name, a.region_code,
        a.license, a.organization, a.register_address, a.register_date, a.register_principal, a.type_code, a.industry_code, a.field_code, a.energy_consume_level,
        a.center, a.jgzh, a.latitude, a.longitude, a.phone, a.fax, a.email, a.url, a.production_line, a.leading_product, a.group_name, a.group_address, a.apply_date,
        a.approval_date, a.last_login_date, a.state, a.remark, d.meaning regionName,  t.name1998 typeName,  k.name industryName,
        case a.field_code when '100' then '工业' when '200' then '交通运输、仓储和邮政业' when '300' then '住宿和餐饮业' when '400' then '批发和零售业' when '500' then '教育' else '其他' end as fieldName,
        a.company_type, p.type_name companyTypeName, l.level energyConsumeLevelName
        from enterprise_info a
        left join dict_region d on d.id=a.region_code
        left join dict_enterprise_type1998 t on t.id=a.type_code
        left join trade_kind k on k.code=a.industry_code
        left join company_type p on a.company_type = p.id
        left join energy_level l on a.energy_consume_level = l.id
        <trim prefix="where" prefixOverrides="and">
            and a.del_flag = '1'
            <if test="query.name!=null and query.name!=''">
                and a.cmp_name like concat("%",#{query.name},"%")
            </if>
            <if test="query.code!=null and query.code!=''">
                and a.enterprise_code like concat("%",#{query.code},"%")
            </if>
            <if test="query.area!=null and query.area!=''">
                and a.region_code like concat(#{query.area},"%")
            </if>
            <if test="query.type!=null and query.type!=''">
                and a.company_type like concat("%",#{query.type},"%")
            </if>
            <if test="query.item!=null and query.item!=''">
                and a.energy_consume_level = #{query.item}
            </if>
        </trim>
        order by a.id
    </select>


    <!--查询条件（企业名称name、地区area、企业类别type、行业industry）-->
    <select id="queryList" resultType="java.util.Map">
        select IFNULL(a.enterprise_code,'') epc, IFNULL(a.cmp_name,'') cmpName, IFNULL(concat(left(a.region_code,4),"00"),'') area, d.meaning2 areaName, IFNULL(left(a.industry_code,3),'') ptrade, IFNULL(a.industry_code,'') trade, IFNULL(a.company_type,'') type, IFNULL(b.kind_name,'') kind, a.region_code dept_id
		from (
		    select <include refid="Base_Column_List"></include>
		    from enterprise_info
            <if test=" list!=null and list!='' ">
                where
                <foreach collection="list" index="index" item="i" separator="or">
                    company_type like concat("%",#{i},"%")
                </foreach>
            </if>
		) a left join trade_info b on a.industry_code = b.trade_num
		left join dict_region d on concat(left(a.region_code,4),"00") = d.id
        where a.del_flag = "1"
        <if test="query.name!=null and query.name!=''">
            and a.cmp_name like concat("%",#{query.name},"%")
        </if>
        <if test="query.area!=null and query.area!=''">
            and #{query.area} like concat("%",left(a.region_code,4),"%")
        </if>
        <if test="query.industry!=null and query.industry!=''">
            and #{query.industry} like concat("%",b.kind_name,"%")
        </if>
    </select>
</mapper>
