<?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.ConfigureGaugeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jly.entity.ConfigureGauge">
        <id column="id" property="id"/>
        <result column="enterpriseCode" property="enterpriseCode"/>
        <result column="nextAlignDate" property="nextAlignDate"/>
        <result column="alignOrg" property="alignOrg"/>
        <result column="unAlignReason" property="unAlignReason"/>
        <result column="location" property="location"/>
        <result column="installer" property="installer"/>
        <result column="installTime" property="installTime"/>
        <result column="linkSys" property="linkSys"/>
        <result column="currentState" property="currentState"/>
        <result column="stateChangeTime" property="stateChangeTime"/>
        <result column="serNo" property="serNo"/>
        <result column="barCode" property="barCode"/>
        <result column="meterName" property="meterName"/>
        <result column="meterType" property="meterType"/>
        <result column="meterLevel" property="meterLevel"/>
        <result column="params" property="params"/>
        <result column="dataCode" property="dataCode"/>
        <result column="reportArithmetic" property="reportArithmetic"/>
        <result column="reportRatio" property="reportRatio"/>
        <result column="manufacturer" property="manufacturer"/>
        <result column="model" property="model"/>
        <result column="precisionLevel" property="precisionLevel"/>
        <result column="ranges" property="ranges"/>
        <result column="manageNo" property="manageNo"/>
        <result column="alignState" property="alignState"/>
        <result column="alignCycle" property="alignCycle"/>
        <result column="lastAlignDate" property="lastAlignDate"/>
        <result column="state" property="state"/>
        <result column="remark" property="remark"/>
        <result column="paramsType" property="paramsType"/>
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="dept_id" property="deptId"/>
    </resultMap>

    <resultMap id="BaseResultMapVo" type="com.nantian.cloud.jly.vo.StatisticsVo">
        <result column="meaning" property="meaning"/>
        <result column="id" property="id"/>
        <result column="access" property="access"/>
        <result column="secondary" property="secondary"/>
        <result column="equipment" property="equipment"/>
        <result column="replaceNum" property="replaceNum"/>
        <result column="varifyNum" property="varifyNum"/>
    </resultMap>

    <update id="updateConfigureGaugeState">
        UPDATE configure_gauge SET align_state = "0" WHERE id in (SELECT id FROM configure_gauge WHERE DATE(next_align_date) <![CDATA[ < ]]> DATE(NOW()))
    </update>

    <select id="getGaugesStatistics" resultType="com.nantian.cloud.jly.vo.StatisticsVo">
        SELECT o.meaning,o.id,oa.total AS access,ob.total AS secondary,oc.total AS equipment,od.total AS replaceNum,oe.total AS varifyNum
        FROM dict_region o
        LEFT JOIN (SELECT COUNT(*) AS total,e.region_code FROM configure_gauge g LEFT JOIN enterprise_info e ON g.enterprise_Code = e.enterprise_code WHERE g.meter_level = 1 and g.del_flag = "1" GROUP BY e.region_code) oa ON o.id = oa.region_code
        LEFT JOIN (SELECT COUNT(*) AS total,e.region_code FROM configure_gauge g LEFT JOIN enterprise_info e ON g.enterprise_Code = e.enterprise_code WHERE g.meter_level = 2 and g.del_flag = "1" GROUP BY e.region_code) ob ON o.id = ob.region_code
        LEFT JOIN (SELECT COUNT(*) AS total,e.region_code FROM configure_gauge g LEFT JOIN enterprise_info e ON g.enterprise_Code = e.enterprise_code WHERE g.meter_level = 3 and g.del_flag = "1" GROUP BY e.region_code) oc ON o.id = oc.region_code
        LEFT JOIN (SELECT COUNT(*) AS total,e.region_code FROM configure_gauge_replace cgr LEFT JOIN enterprise_info e ON cgr.enterprise_code = e.enterprise_code WHERE YEAR(cgr.install_time)=YEAR(NOW()) GROUP BY e.region_code) od ON o.id = od.region_code
        LEFT JOIN (SELECT COUNT(*) AS total,e.region_code FROM configure_gauge_verify cgv LEFT JOIN enterprise_info e ON cgv.enterprise_code = e.enterprise_code WHERE YEAR(cgv.verify_time)=YEAR(NOW()) GROUP BY e.region_code) oe ON o.id = oe.region_code
    </select>

    <select id="getPageData" resultType="java.util.HashMap">
        SELECT o.*,i.cmp_name,i.region_code,i.address,r.id AS countyId,r.meaning AS countyName,dr.id AS cityId,dr.meaning AS
        cityName,dir.id AS provinceId,dir.meaning AS provinceName, t1.name as meterTypeStr, t2.name as meterNameStr,d1.type FROM
        configure_gauge 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 and o.del_flag = "1"
        left join t_measuring_instruments t1 on o.meter_type = t1.code
        left join t_measuring_instruments t2 on o.meter_name = t2.code
        left join dict_gauge_type d1 on o.meter_type = d1.code
        <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 i.industry_code = #{industryCode}
            </if>
            <if test="meterLevel != null and meterLevel != ''">
                AND o.meter_level = #{meterLevel}
            </if>
            <if test="location != null and location != ''">
                AND o.location LIKE CONCAT('%',#{location},'%')
            </if>
            <if test="cmpName != null and cmpName != ''">
                AND i.cmp_name LIKE CONCAT('%',#{cmpName},'%')
            </if>
            <if test="cmp != null and cmp != ''">
                AND o.enterprise_code = #{cmp}
            </if>
            <if test="type != null and type != ''">
                AND o.meter_type = #{type}
            </if>
        </trim>
        order by i.region_code
    </select>

    <select id="pageCmp" resultType="com.nantian.cloud.jly.entity.ConfigureGauge">
        SELECT o.*,t1.name as meterTypeStr, t2.name as meterNameStr,i.cmp_name,d1.type FROM configure_gauge o
        LEFT JOIN enterprise_info i ON o.enterprise_Code = i.enterprise_code
        left join dict_gauge_type d1 on o.meter_type = d1.code
        left join t_measuring_instruments t1 on o.meter_type = t1.code
        left join t_measuring_instruments t2 on o.meter_name = t2.code
        <trim prefix="where" prefixOverrides="and">
            <if test="query.alignState != null and query.alignState != ''">
                AND o.align_state = #{query.alignState}
            </if>
            <if test="query.location != null and query.location != ''">
                AND o.location LIKE CONCAT('%',#{query.location},'%')
            </if>
            <if test="query.meterLevel != null and query.meterLevel != ''">
                <choose>
                    <when test="query.meterLevel=='4'.toString">
                        AND (o.meter_level NOT IN (1, 2, 3) or o.meter_level is null or o.meter_level = '')
                    </when>
                    <otherwise>
                        AND o.meter_level LIKE CONCAT('%',#{query.meterLevel},'%')
                    </otherwise>
                </choose>
            </if>
            <if test="query.meterName != null and query.meterName != ''">
                AND o.meter_name LIKE CONCAT('%',#{query.meterName},'%')
            </if>
            <if test="query.meterType != null and query.meterType != ''">
                AND o.meter_type LIKE CONCAT('%',#{query.meterType},'%')
            </if>
            <if test="query.enterpriseCode != null and query.enterpriseCode != ''">
                AND o.enterprise_code = #{query.enterpriseCode}
            </if>
            <if test="query.delFlag != null and query.delFlag != ''">
                AND o.del_flag = #{query.delFlag}
            </if>
        </trim>
    </select>
    <select id="getNotCertified" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM configure_gauge WHERE enterprise_code = #{epc} AND align_state !="1" AND del_flag = 1
    </select>
    <select id="getList" resultType="com.nantian.cloud.jly.entity.ConfigureGauge">
        SELECT o.*,d1.type
        from configure_gauge o
        left join dict_gauge_type d1 on o.meter_type = d1.code
        where enterprise_code = #{epc}
    </select>

</mapper>
