<?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.dbcapp.mapper.record.AccessEnterpriseInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.dbcapp.entity.AccessEnterpriseInfo">
        <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="region_name" property="regionName" />
        <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="web_url" property="webUrl" />
        <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="upload_time" property="uploadTime" />
        <result column="remark" property="remark" />
        <result column="check_upload_time" property="checkUploadTime" />
        <result column="can_upload_country" property="canUploadCountry" />
        <result column="uploaded_country" property="uploadedCountry" />
        <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="tid" property="tid" />
        <result column="ele_number" property="eleNumber" />
        <result column="icsem" property="icsem"/>
        <result column="icsem_tel" property="icsemTel"/>
        <result column="staff" property="staff"/>
        <result column="staff_tel" property="staffTel"/>
        <result column="organization_code" property="organizationCode"/>
        <result column="report_num" property="reportNum"/>
        <result column="access_note" property="accessNote"/>
    </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, web_url, production_line, leading_product, group_name,
        group_address, apply_date, approval_date, last_login_date, state, upload_time, remark, check_upload_time, can_upload_country,
        uploaded_country, userid, update_time, del_flag, dept_id, tid, ele_number, icsem, icsem_tel, staff, staff_tel
    </sql>
    <select id="getPage" resultType="com.nantian.cloud.dbcapp.entity.AccessEnterpriseInfo">
        select 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, web_url, production_line, leading_product, group_name,
        group_address, apply_date, approval_date, last_login_date, upload_time, remark, check_upload_time, can_upload_country,
        uploaded_country, userid, update_time, del_flag, dept_id, tid, ele_number, icsem, icsem_tel, staff, staff_tel,
        case when state = '1' then '未启动' when state = '2' then '推迟' when state = '3' then '正在对接' when state = '4' then '已完成' else '' end state,
        (select tname from dict_region where id = right(a.region_code,6)) region_name, organization_code, report_num, access_note
        from access_enterprise_info a
        where del_flag = "1"
        <if test="info.regionCode!=null and info.regionCode!=''">
            and region_code like  CONCAT('%',#{info.regionCode},'%')
        </if>
        <if test="info.industryCode!=null and info.industryCode!=''">
            and industry_code like  CONCAT('%',#{info.industryCode},'%')
        </if>
        <if test="info.enterpriseCode!=null and info.enterpriseCode!=''">
            and enterprise_code like  CONCAT('%',#{info.enterpriseCode},'%')
        </if>
        <if test="info.cmpName!=null and info.cmpName!=''">
            and cmp_name like  CONCAT('%',#{info.cmpName},'%')
        </if>
        <if test="info.state!=null and info.state!=''">
            and state like  CONCAT('%',#{info.state},'%')
        </if>
    </select>

    <select id="groupData" resultType="java.util.Map">
        select d.id, d.tname region, count(1) sum, count(case when state = '1' then 1 end) state1, count(case when state = '2' then 1 end) state2,
        count(case when state = '3' then 1 end) state3, count(case when state = '4' then 1 end) state4
        from access_enterprise_info a
        left join dict_region d on d.id = concat(left(right(a.region_code,6),4),'00')
        where del_flag = "1"
        <if test="info.regionCode!=null and info.regionCode!=''">
            and region_code like  CONCAT('%',#{info.regionCode},'%')
        </if>
        <if test="info.industryCode!=null and info.industryCode!=''">
            and industry_code like  CONCAT('%',#{info.industryCode},'%')
        </if>
<!--        <if test="info.enterpriseCode!=null and info.enterpriseCode!=''">-->
<!--            and enterprise_code like  CONCAT('%',#{info.enterpriseCode},'%')-->
<!--        </if>-->
<!--        <if test="info.cmpName!=null and info.cmpName!=''">-->
<!--            and cmp_name like  CONCAT('%',#{info.cmpName},'%')-->
<!--        </if>-->
        <if test="info.state!=null and info.state!=''">
            and state like  CONCAT('%',#{info.state},'%')
        </if>
        group by d.id
        union
        select '530000' id, '全省合计' region, count(1) sum, count(case when state = '1' then 1 end) state1, count(case when state = '2' then 1 end) state2,
        count(case when state = '3' then 1 end) state3, count(case when state = '4' then 1 end) state4
        from access_enterprise_info a
        where del_flag = "1"
        <if test="info.industryCode!=null and info.industryCode!=''">
            and industry_code like  CONCAT('%',#{info.industryCode},'%')
        </if>
        <if test="info.enterpriseCode!=null and info.enterpriseCode!=''">
            and enterprise_code like  CONCAT('%',#{info.enterpriseCode},'%')
        </if>
        <if test="info.cmpName!=null and info.cmpName!=''">
            and cmp_name like  CONCAT('%',#{info.cmpName},'%')
        </if>
        <if test="info.state!=null and info.state!=''">
            and state like  CONCAT('%',#{info.state},'%')
        </if>
        order by id asc
    </select>

</mapper>
