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


    <insert id="insertConfigureGauge">
	    insert into configure_gauge(enterprise_code, next_align_date, align_org, un_align_reason, location, installer, install_time, link_sys, current_state, state_change_time, ser_no, bar_code, meter_name, meter_type, meter_level, params, data_code, report_arithmetic, report_ratio, manufacturer, model, precision_level, ranges, manage_no, align_state, align_cycle, last_align_date, state, remark, params_type, uploaded_country, digest, quoted, del_flag)
	    <foreach collection="list" item="item" separator="union">
			select #{item.enterpriseCode} as enterpriseCode, #{item.nextAlignDateTmp} as nextAlignDate, #{item.alignOrg} as alignOrg, #{item.unAlignReason} as unAlignReason, #{item.location} as location, #{item.installer} as installer, #{item.installTimeTmp} as installTime, #{item.linkSys} as linkSys, #{item.currentState} as currentState, #{item.stateChangeTimeTmp} as stateChangeTime,
			#{item.serNo} as serNo, #{item.barCode} as barCode, #{item.meterName} as meterName, #{item.meterType} as meterType, #{item.meterLevel} as meterLevel, #{item.params} as params, #{item.dataCode} as dataCode, #{item.reportArithmetic} as reportArithmetic, #{item.reportRatio} as reportRatio, #{item.manufacturer} as manufacturer, #{item.model} as model, #{item.precisionLevel} as precisionLevel,
			#{item.ranges} as ranges, #{item.manageNo} as manageNo, #{item.alignState} as alignState, #{item.alignCycle} as alignCycle, #{item.LastAlignDate} as LastAlignDate, #{item.state} as state, #{item.remark} as remark, #{item.paramsType} as paramsType, #{item.uploadedCountry} as uploadedCountry, #{item.digest} as digest, #{item.quoted} as quoted, '1' as del_flag
		</foreach>
    </insert>

    <insert id="insertCalculater">
        insert into company_calculater(enterprise_code, calculater_name, phone, dept, job, technical_post, certificate, certificate_name, certificate_no, remark, state, digest, quoted, del_flag)
         <foreach collection="list" item="item" separator="union">
			select #{item.enterpriseCode} as enterpriseCode, #{item.calculaterName} as calculaterName, #{item.phone} as phone, #{item.dept} as dept, #{item.job} as job, #{item.technicalPost} as technicalPost, #{item.certificate} as certificate,
			#{item.certificateName} as certificateName, #{item.certificateNo} as certificateNo, #{item.remark} as remark, #{item.state} as state, #{item.digest} as digest, #{item.quoted} as quoted, '1' as del_flag
		</foreach>
    </insert>

    <insert id="insertDevice">
        insert into configure_device(enterprise_code, device_name, device_no, model, location, dept, device_type, using_date, current_state, manufacturer, energy_code, energy_value, process_unit_code, state, remark, quoted, digest)
        <foreach collection="list" item="item" separator="union">
            select #{item.enterpriseCode} as enterpriseCode, #{item.deviceName} as deviceName, #{item.deviceNo} as deviceNo, #{item.model} as model, #{item.location} as location, #{item.dept} as dept, #{item.deviceType} as deviceType, #{item.usingDate} as usingDate,
            #{item.currentState} as currentState, #{item.manufacturer} as manufacturer, #{item.energyCode} as energyCode, #{item.energyValue} as energyValue, #{item.processUnitCode} as processUnitCode,  #{item.state} as state, #{item.remark} as remark, #{item.quoted} as quoted, #{item.digest} as digest
        </foreach>
    </insert>

    <insert id="etlGauges">
        delete from configure_gauge where enterprise_code = #{epc};
        insert into configure_gauge(enterprise_code, meter_name, model, meter_level, ranges, manufacturer, ser_no, meter_type, params, manage_no, location, align_cycle, align_state, userid, update_time, del_flag, dept_id)
        select epc, gauge_name, gauge_type, accuracy_levels, test_region, product_factory, num_factrory, gauge_class, energy_class, manager_num, location, check_time, gauge_status, userid, update_time, del_flag, dept_id
        from check_gauges where epc = #{epc} and check_plan_id = ${planId} and del_flag = '1';
    </insert>

    <insert id="etlCompanyCalculater">
        delete from company_calculater where enterprise_code = #{epc};
        insert into company_calculater(enterprise_code, calculater_name, dept, job, technical_post, exam, certificate_no, certificate, remark, userid, update_time, del_flag, dept_id)
        select epc, name, dept, duties, major_dulties, test_flag, cert_num, cert_file, comments, userid, update_time, del_flag, dept_id
        from check_persons where epc = #{epc} and check_plan_id = ${planId} and del_flag = '1';
    </insert>


    <select id="getInfoEpc" resultType="java.lang.String">
        select distinct enterprise_code from company_calculater
    </select>

    <select id="getGaugesByEtl" resultType="com.nantian.cloud.dbcapp.vo.ConfigureGaugeVo">
        select epc enterpriseCode, gauge_name meterName, gauge_type model, accuracy_levels meterLevel, test_region ranges, product_factory manufacturer, num_factrory serNo, gauge_class meterType,
        energy_class params, manager_num manageNo, location location, check_time alignCycle, gauge_status alignState, del_flag
        from check_gauges where epc = #{epc} and check_plan_id = ${planId} and del_flag = '1'
    </select>

    <select id="getIdsByGauge" resultType="java.lang.String">
        select quoted from configure_gauge where quoted is not null
    </select>

    <select id="getIdsByCalculater" resultType="java.lang.String">
         select quoted from company_calculater where quoted is not null
    </select>

    <select id="getIdsByDevice" resultType="java.lang.String">
         select quoted from configure_device where quoted is not null
    </select>
    <select id="selectToolByAreaAndIndustry" resultType="int">
        select count(id) from configure_gauge where enterprise_code in
        (
        select distinct enterprise_code from enterprise_info a where a.region_code like #{area}
        <if test="industry!=null">
            and a.tid = #{industry}
        </if>
        )
        and del_flag = "1"  and meter_level in (1,2,3)
    </select>
    <select id="getMeasureTol" resultType="java.util.Map">
        SELECT t.sumCz as cz,
         t1.sumWcz as wcz,
         t.sumCz+t1.sumWcz as total
         from
        ( select count(a.id) as sumCz from company_calculater a left join enterprise_info b on a.enterprise_code = b.enterprise_code  where  certificate is not null and a.del_flag =1 and b.region_code like concat(#{areaCode},'%'))  t
        left join
        (select count(a.id) as sumWcz from company_calculater  a left join enterprise_info b on a.enterprise_code = b.enterprise_code where certificate is  null and a.del_flag =1 and b.region_code like concat(#{areaCode},'%')) t1 on 1=1
    </select>
    <select id="selectInstrumentsNum" resultType="java.util.Map">
        select count(id) as total,
        COUNT( CASE WHEN  meter_level = '1' THEN 1  ELSE NULL END) AS level1,
        COUNT( CASE WHEN  meter_level = '2' THEN 1  ELSE NULL END) AS level2,
        COUNT( CASE WHEN  meter_level = '3' THEN 1  ELSE NULL END) AS level3
        from configure_gauge where enterprise_code in
        (
        select distinct enterprise_code from enterprise_info a where a.region_code like concat(#{area},'%')
        )
        and del_flag = "1"  and meter_level in (1,2,3)
    </select>
    <select id="getSumTask" resultType="java.lang.Integer">
        SELECT count(*) from check_plan where del_flag =1 and dept_id like concat(#{areaCode},'%')
    </select>

    <select id="getTaskList" resultType="com.nantian.cloud.dbcapp.vo.MobileTaskVO">
        SELECT
        id ,name,num as No,year as year,
        check_type  as type,
        begin_time as starTime,
        end_time as endTime
        FROM
        check_plan
        WHERE
        del_flag =1
        <if test="name!=null">
            and name like #{name}
        </if>

        <if test="type!=null">
            and check_type = #{type}
        </if>

    </select>
    <!--通过任务id查询企业-->
    <select id="getCompListByPlanId" resultType="com.nantian.cloud.dbcapp.vo.MobileCompVO">
        select
         com_name as  name,
         epc as  enterpriseCode,
         check_status as  state
        from check_enterprice where check_plan_id=#{id}
    </select>

</mapper>