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

    <select id="listState" resultType="com.nantian.cloud.dbcapp.vo.ConfigureGaugeVo">
        SELECT
        cg.id,cg.enterprise_code,cg.next_align_date,cg.align_org,cg.un_align_reason,cg.location,cg.installer,cg.install_time,cg.link_sys,cg.current_state,cg.state_change_time,cg.ser_no,cg.bar_code,cg.meter_name,cg.meter_type,cg.meter_level,cg.params,cg.data_code,cg.report_arithmetic,cg.report_ratio,cg.manufacturer,cg.model,cg.precision_level,cg.ranges,cg.manage_no,cg.align_state,cg.align_cycle,cg.last_align_date,cg.state,cg.remark,cg.params_type,IF(cgr.id IS NULL,"0","1") relevanceState, cgr.audit_status auditStatus
        FROM configure_gauge cg
        LEFT JOIN configure_gauge_relevance cgr ON cgr.is_deleted = 0 AND cgr.gauge_id = cg.id AND cgr.data_code = #{dataCode}
        WHERE cg.enterprise_code = #{epc}
        <if test="type == '2' ">
            HAVING relevanceState = "1"
        </if>
        ORDER BY relevanceState DESC, auditStatus ASC
    </select>

    <select id="getList" resultType="com.nantian.cloud.dbcapp.entity.ConfigureGaugeRelevance">
        SELECT
        cgr.id,cgr.enterprise_code enterpriseCode,cgr.gauge_id gaugeId,cgr.data_code dataCode,cgr.creator,cgr.audit_status auditStatus,cgr.gmt_audit gmtAudit,cgr.auditor,cgr.audit_description auditDescription,cgr.gmt_create gmtCreate,cgr.gmt_modified gmtModified,cgr.is_deleted isDeleted,
        ei.cmpName enterpriseName,cdc.collectItemName,
        cg.meter_name meterName,cg.meter_type meterType,cg.meter_level meterLevel
        FROM configure_gauge_relevance cgr
        INNER JOIN configure_gauge cg ON cgr.gauge_id = cg.id
        INNER JOIN energy_record.configure_data_collect cdc ON cgr.enterprise_code = cdc.enterpriseCode AND cgr.data_code = cdc.dataCode
        INNER JOIN energy_record.enterprise_info ei ON cg.enterprise_code = ei.enterpriseCode
        WHERE cg.del_flag = "1" AND cgr.is_deleted = 0
        <if test="epc != null and epc != ''">
            AND (ei.enterpriseCode = #{epc} OR ei.cmpName LIKE CONCAT("%",#{epc},"%"))
        </if>
        <if test="dataCode != null and dataCode != ''">
            AND cgr.data_code = #{dataCode}
        </if>
        <if test="auditStatus != null and auditStatus != ''">
            AND cgr.audit_status = #{auditStatus}
        </if>

    </select>

    <select id="getGaugeByRelevance" resultType="com.nantian.cloud.dbcapp.vo.ConfigureGaugeVo">
        SELECT
        cg.id,ei.cmp_name,cg.enterprise_code,cg.next_align_date,cg.align_org,cg.un_align_reason,cg.location,cg.installer,cg.install_time,cg.link_sys,cg.current_state,cg.state_change_time,cg.ser_no,cg.bar_code,cg.meter_name,cg.meter_type,cg.meter_level,cg.params,cg.data_code,cg.report_arithmetic,cg.report_ratio,cg.manufacturer,cg.model,cg.precision_level,cg.ranges,cg.manage_no,cg.align_state,cg.align_cycle,cg.last_align_date,cg.state,cg.remark,cg.params_type
        FROM configure_gauge  cg
        INNER JOIN configure_gauge_relevance cgr ON cgr.gauge_id = cg.id AND cgr.is_deleted = 0 AND cgr.audit_status = "1"
        INNER JOIN enterprise_info ei ON cg.enterprise_code = ei.enterprise_code
        WHERE cg.del_flag = "1" AND cgr.enterprise_code = #{epc} AND cgr.data_code = #{dataCode}
    </select>

    <select id="getConfigByRelevance" resultType="com.nantian.cloud.dbcapp.entity.ConfigureDataCollect">
        SELECT
            cdc.id,ei.cmpName,cdc.enterpriseCode,cdc.dataCode,cdc.collectItemName,cdc.processCode,cdc.processUnitCode,cdc.equipmentCode,cdc.equipmentUnitCode,cdc.energyClassCode,cdc.energyTypeCode,cdc.dataUsageCode,cdc.inputType,cdc.statType,cdc.collectSystemName,cdc.scope,cdc.state,cdc.remark,cdc.isEnable
        FROM energy_record.configure_data_collect cdc
        INNER JOIN configure_gauge_relevance cgr  ON cgr.enterprise_code = cdc.enterpriseCode AND cgr.data_code = cdc.dataCode
        INNER JOIN energy_record.enterprise_info ei ON cdc.enterpriseCode = ei.enterpriseCode
        WHERE cgr.is_deleted = 0 AND cgr.gauge_id = #{gaugeId}
    </select>

    <update id="updateDelete">
        update configure_gauge_relevance set is_deleted = "1" where id in
        <foreach collection="ids" item="id" open="(" separator=","  close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateDeleteByEpcAndDataCode">
        UPDATE configure_gauge_relevance
        SET is_deleted = CASE
                         WHEN audit_status = "1" and is_deleted != 0 THEN CONVERT(is_deleted, UNSIGNED INTEGER) + 1
                         ELSE "1" END,
            remark = CASE
                     WHEN audit_status = "1" and is_deleted != 0 THEN "取消该关系审核，重新建立关联关系。"
                     ELSE "已配置新关联，删除旧关联。" END
        WHERE enterprise_code = #{epc} AND data_code = #{dataCode}
    </update>

    <select id="relevanceState" resultType="com.nantian.cloud.dbcapp.vo.RelevanceStateVo">
        SELECT
            cdc.enterpriseCode,cdc.dataCode,
            CASE WHEN auditStatus like "%2%" THEN "2" WHEN auditStatus like "%0%" THEN "0" WHEN auditStatus IS NULL THEN "3" ELSE "1" END auditStatus,
            IF(relevanceState IS NULL,"0","1") relevanceState
        FROM
            (SELECT enterpriseCode, dataCode FROM energy_record.configure_data_collect WHERE enterpriseCode = #{epc}) cdc
                LEFT JOIN
            (SELECT enterprise_code,data_code, group_concat(id) relevanceState,group_concat(audit_status) auditStatus FROM configure_gauge_relevance WHERE enterprise_code = #{epc} and is_deleted = 0 GROUP BY enterprise_code,data_code) cgr
            ON cdc.enterpriseCode = cgr.enterprise_code AND cdc.dataCode = cgr.data_code
        ORDER BY dataCode
    </select>

    <select id="listGroupByEpcAndDataCode" resultType="com.nantian.cloud.dbcapp.entity.ConfigureGaugeRelevance">
        SELECT r1.*, cdc.collectItemName
        FROM ( SELECT
        cgr.id,cgr.enterprise_code enterpriseCode,cgr.data_code dataCode,cgr.creator,
        CASE WHEN GROUP_CONCAT(cgr.audit_status) like "%2%" THEN "2" WHEN GROUP_CONCAT(cgr.audit_status) like "%0%" THEN "0" WHEN GROUP_CONCAT(cgr.audit_status) IS NULL THEN "3" ELSE "1" END auditStatus,
        cgr.gmt_audit gmtAudit,cgr.auditor,cgr.audit_description auditDescription,cgr.gmt_create gmtCreate,cgr.gmt_modified gmtModified,cgr.is_deleted isDeleted,
        ei.cmpName enterpriseName,
        GROUP_CONCAT(cg.meter_name) meterName,GROUP_CONCAT(cg.meter_type) meterType, GROUP_CONCAT(cg.meter_level) meterLevel,
        (case when SUBSTR(cgr.data_code,1,8) = "00000000" then "1"
        when SUBSTR(cgr.data_code,5,4) = "0000" then "2"
        else "3" end) meterLevelNew
        FROM configure_gauge_relevance cgr
        INNER JOIN configure_gauge cg ON cgr.gauge_id = cg.id
        INNER JOIN energy_record.enterprise_info ei ON cg.enterprise_code = ei.enterpriseCode
        WHERE cg.del_flag = "1" AND cgr.is_deleted = 0
        <if test="epc != null and epc != ''">
            AND (ei.enterpriseCode = #{epc} OR ei.cmpName LIKE CONCAT("%",#{epc},"%"))
        </if>
        <if test="dataCode != null and dataCode != ''">
            AND cgr.data_code = #{dataCode}
        </if>
        GROUP BY cgr.enterprise_code,cgr.data_code) r1
        INNER JOIN energy_record.configure_data_collect cdc ON r1.enterpriseCode = cdc.enterpriseCode AND r1.dataCode = cdc.dataCode
        WHERE 1=1
        <if test="auditStatus != null and auditStatus != ''">
            AND auditStatus = #{auditStatus}
        </if>
    </select>

    <update id="auditGroup">
        UPDATE configure_gauge_relevance SET audit_status = #{auditStatus}, auditor = #{username}, audit_description = #{auditRemark}, gmt_audit = NOW()
        WHERE is_deleted = 0 AND enterprise_code = #{epc} AND data_code = #{dataCode}
    </update>

    <insert id="autoMatchRelevance">
        INSERT INTO jly.configure_gauge_relevance (enterprise_code,gauge_id,data_code,creator,audit_status,gmt_create,gmt_modified,remark)
        SELECT
            cg.enterprise_code, cg.id, cg.data_code, "system" creator, "0" audit_status, NOW() gmt_create, NOW() gmt_modified, "系统默认匹配" remark
        FROM configure_gauge cg WHERE cg.data_code IS NOT NULL AND del_flag = "1"
        AND NOT EXISTS (
            SELECT 1 FROM jly.configure_gauge_relevance cgr
            WHERE cgr.enterprise_code = cg.enterprise_code
            AND cgr.gauge_id = cg.id
            AND cgr.data_code = cg.data_code)
    </insert>

    <select id="getGaugeIds" resultType="java.lang.Long">
        SELECT gauge_id FROM jly.configure_gauge_relevance cgr
        WHERE cgr.enterprise_code = #{epc} AND cgr.data_code = #{dataCode} AND cgr.is_deleted = "0"
    </select>

    <update id="updateMeterLevel">
        UPDATE jly.configure_gauge SET meter_level = (
        CASE
        WHEN FIND_IN_SET('1', CONCAT(IFNULL(meter_level, case when SUBSTR(data_code,1,8) = "00000000" then "1" when SUBSTR(data_code,5,4) = "0000" then "2" else "3" end),",", #{meterLevel})) AND FIND_IN_SET('2', CONCAT(meter_level,",", #{meterLevel})) AND FIND_IN_SET('3', CONCAT(meter_level,",", #{meterLevel})) THEN 123
        WHEN FIND_IN_SET('1', CONCAT(IFNULL(meter_level, case when SUBSTR(data_code,1,8) = "00000000" then "1" when SUBSTR(data_code,5,4) = "0000" then "2" else "3" end),",", #{meterLevel})) AND FIND_IN_SET('2', CONCAT(meter_level,",", #{meterLevel})) THEN 12
        WHEN FIND_IN_SET('1', CONCAT(IFNULL(meter_level, case when SUBSTR(data_code,1,8) = "00000000" then "1" when SUBSTR(data_code,5,4) = "0000" then "2" else "3" end),",", #{meterLevel})) AND FIND_IN_SET('3', CONCAT(meter_level,",", #{meterLevel})) THEN 13
        WHEN FIND_IN_SET('2', CONCAT(IFNULL(meter_level, case when SUBSTR(data_code,1,8) = "00000000" then "1" when SUBSTR(data_code,5,4) = "0000" then "2" else "3" end),",", #{meterLevel})) AND FIND_IN_SET('3', CONCAT(meter_level,",", #{meterLevel})) THEN 12
        WHEN FIND_IN_SET('1', CONCAT(IFNULL(meter_level, case when SUBSTR(data_code,1,8) = "00000000" then "1" when SUBSTR(data_code,5,4) = "0000" then "2" else "3" end),",", #{meterLevel})) THEN 1
        WHEN FIND_IN_SET('2', CONCAT(IFNULL(meter_level, case when SUBSTR(data_code,1,8) = "00000000" then "1" when SUBSTR(data_code,5,4) = "0000" then "2" else "3" end),",", #{meterLevel})) THEN 2
        WHEN FIND_IN_SET('3', CONCAT(IFNULL(meter_level, case when SUBSTR(data_code,1,8) = "00000000" then "1" when SUBSTR(data_code,5,4) = "0000" then "2" else "3" end),",", #{meterLevel})) THEN 3
        END),
        data_code = ( CASE
        WHEN FIND_IN_SET(#{dataCode}, data_code) THEN data_code
        ELSE CONCAT(data_code, ",", #{dataCode}) END)
        WHERE id in
        <foreach collection="gaugeIds" open="(" separator="," close=")" item="id">
            #{id}
        </foreach>
    </update>

    <select id="getIdAndNewDataCodeMap" resultType="com.nantian.cloud.dbcapp.vo.MapVo">
        SELECT cg.id `key`, cg.data_code `value`
        FROM jly.configure_gauge cg
                 INNER JOIN jly.configure_gauge_relevance cgr ON cgr.gauge_id = cg.id
        WHERE cgr.enterprise_code = #{enterpriseCode}
          AND cgr.data_code = #{dataCode}
          AND audit_status = '1'
          AND cg.data_code NOT LIKE CONCAT('%', cgr.data_code , '%')
          AND is_deleted = 1
    </select>

    <update id="recoverGauges">
        UPDATE configure_gauge cg INNER JOIN configure_gauge_relevance cgr
        ON cgr.gauge_id = cg.id SET cg.data_code = CASE WHEN RIGHT (cg.data_code, LENGTH (cgr.data_code)) = cgr.data_code THEN REPLACE(cg.data_code, CONCAT(',', cgr.data_code), '') ELSE CONCAT(cgr.data_code, ',') END
        WHERE cgr.enterprise_code = #{enterpriseCode}
          AND cgr.data_code = #{dataCode}
          AND audit_status = '1'
          AND cg.data_code LIKE CONCAT('%', cgr.data_code , '%')
          AND is_deleted = 1
    </update>

    <update id="recoverGaugesLevel">
        <if test="list != null and list.size() > 0">
            <foreach collection="list" item="item" separator=";">
                UPDATE jly.configure_gauge SET meter_level = #{item.value} WHERE id = #{item.key}
            </foreach>
        </if>
    </update>


</mapper>