<?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.jly.mapper.CompanyCalculaterMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jly.entity.CompanyCalculater">
        <id column="id" property="id"/>
        <result column="enterprise_code" property="enterpriseCode"/>
        <result column="calculater_name" property="calculaterName"/>
        <result column="phone" property="phone"/>
        <result column="dept" property="dept"/>
        <result column="job" property="job"/>
        <result column="exam" property="exam"/>
        <result column="technical_post" property="technicalPost"/>
        <result column="certificate" property="certificate"/>
        <result column="certificate_name" property="certificateName"/>
        <result column="certificate_no" property="certificateNo"/>
        <result column="remark" property="remark"/>
        <result column="state" property="state"/>
        <result column="userid" property="userid" />
        <result column="update_time" property="updateTime" />
        <result column="del_flag" property="delFlag" />
        <result column="dept_id" property="deptId" />
    </resultMap>

    <select id="getCaculaterStatistics" resultType="java.util.HashMap">
       SELECT o.meaning,o.id,oa.total AS notHasCertificate,ob.total AS hasCertificate FROM dict_region o
       LEFT JOIN (SELECT COUNT(*) AS total,e.region_code FROM company_calculater g LEFT JOIN enterprise_info e ON g.enterprise_code = e.enterprise_code and g.del_flag = "1" WHERE ISNULL(g.certificate_name) GROUP BY e.region_code) oa ON o.id = oa.region_code
       LEFT JOIN (SELECT COUNT(*) AS total,e.region_code FROM company_calculater g LEFT JOIN enterprise_info e ON g.enterprise_code = e.enterprise_code and g.del_flag = "1" WHERE g.certificate_name IS NOT NULL GROUP BY e.region_code) ob ON o.id = ob.region_code
    </select>

    

    <select id="getPageData" resultType="java.util.HashMap">
        SELECT o.*,i.cmp_name,i.region_code,i.corporation_name,r.id AS countyId,r.meaning AS countyName,dr.id AS cityId,dr.meaning AS cityName,dir.id AS provinceId,dir.meaning AS provinceName FROM company_calculater o
        LEFT JOIN enterprise_info i ON o.enterprise_code = i.enterprise_code LEFT JOIN dict_region r ON r.id = i.region_code LEFT JOIN dict_region dr ON dr.id = r.pid LEFT JOIN dict_region dir ON dir.id = dr.pid  LEFT JOIN trade_category tc ON i.t_id = tc.id
        <trim prefix="where"  prefixOverrides="and">
            <if test="flag == 3 and code != null and code != ''">
               AND i.region_code = #{code}
            </if>
            <if test="flag == 2 and code != null and code != ''">
                AND dr.id = #{code}
            </if>
            <if test="flag == 1 and code != null and code != ''">
                AND dir.id = #{code}
            </if>
            <if test="industryCode != null and industryCode != ''">
                AND tc.id = #{industryCode}
            </if>
            <if test="cmpName != null and cmpName != ''">
                AND i.cmp_name LIKE CONCAT('%',#{cmpName},'%')
            </if>
            <if test="calculaterName != null and calculaterName != ''">
                AND o.calculater_name LIKE CONCAT('%',#{calculaterName},'%')
            </if>
            and  o.del_flag = "1"
        </trim>
        order by i.region_code
    </select>

    <select id="pageCmp" resultType="com.nantian.cloud.jly.entity.CompanyCalculater">
        select * from company_calculater
        <trim prefix="where"  prefixOverrides="and">
            <if test="query.calculaterName != null and query.calculaterName != ''">
                AND calculater_name LIKE CONCAT('%',#{query.calculaterName},'%')
            </if>
            <if test="query.phone != null and query.phone != ''">
                AND phone LIKE CONCAT('%',#{query.phone},'%')
            </if>
            <if test="query.dept != null and query.dept != ''">
                AND dept LIKE CONCAT('%',#{query.dept},'%')
            </if>
            <if test="query.enterpriseCode != null and query.enterpriseCode != ''">
                AND enterprise_code = #{query.enterpriseCode}
            </if>
            <if test="query.delFlag != null and query.delFlag != ''">
                AND del_flag = #{query.delFlag}
            </if>
        </trim>
    </select>
    <select id="selectByCmp" resultType="com.nantian.cloud.jly.entity.CompanyCalculater">
        select * from company_calculater where enterprise_code = #{query.enterpriseCode}
    </select>
</mapper>
