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

    <insert id="addEnableByApply">
        insert into configure_data_collect_enable(enterpriseCode,dataCode,startTime,endTime,remark,creatTime,updateTime,delFlag,isEnable,creatUserId,updateUserId)
        <foreach collection="list" index="i" item="item" separator = "UNION">
            select #{item.enterpriseCode}, #{item.dataCode}, #{item.startTime}, #{item.endTime}, #{item.remark}, #{item.creatTime}, #{item.updateTime},"1", "1", #{item.creatUserId}, #{item.updateUserId}
        </foreach>
    </insert>

    <select id="getApplyPage" resultType="com.nantian.cloud.dbcapp.entity.ConfigureDataCollectEnableApply">
        select t1.id, t1.enterpriseCode, t1.dataCode, t1.startTime, t1.endTime, t1.remark, t1.creatTime, t1.updateTime,
         t1.delFlag, t1.creatUserId, t1.updateUserId, t1.auditUser, t1.auditType, t1.auditText, t1.auditTime, t2.collectItemName, t3.cmpName
        from configure_data_collect_enable_apply t1
        left join configure_data_collect t2 on t1.enterpriseCode = t2.enterpriseCode and t1.dataCode = t2.dataCode
        left join enterprise_info t3 on t1.enterpriseCode = t3.enterpriseCode
        where t1.delFlag = "1"
        <if test="enableApply.cmpName!=null and enableApply.cmpName!=''">
            and t3.cmpName like concat('%',#{enableApply.cmpName},'%')
        </if>
        <if test="enableApply.enterpriseCode!=null and enableApply.enterpriseCode!=''">
            and t1.enterpriseCode like concat('%',#{enableApply.enterpriseCode},'%')
        </if>
        <if test="enableApply.collectItemName!=null and enableApply.collectItemName != '' ">
            and t2.collectItemName like concat('%',#{enableApply.collectItemName},'%')
        </if>
        <if test="enableApply.dataCode!=null and enableApply.dataCode != '' ">
            and t1.dataCode like concat('%',#{enableApply.dataCode},'%')
        </if>
        <if test="enableApply.auditType!=null and enableApply.auditType != '' ">
            and #{enableApply.auditType} like concat('%',t1.auditType,'%')
        </if>
    </select>

    <select id="getOldByApply" resultType="com.nantian.cloud.dbcapp.entity.ConfigureDataCollectEnableApply">
        select t1.id, t1.enterpriseCode, t1.dataCode, t1.startTime, t1.endTime, t1.remark, t1.auditType,
         (select collectItemName from configure_data_collect where t1.enterpriseCode = enterpriseCode and t1.dataCode = dataCode) collectItemName
        from configure_data_collect_enable_apply t1
        where t1.delFlag = "1" and t1.enterpriseCode = #{enableApply.enterpriseCode} and t1.dataCode = #{enableApply.dataCode} and t1.startTime = #{enableApply.startTime}
        order by t1.id desc limit 0,1
    </select>

    <delete id="delApply">
        update configure_data_collect_enable_apply set delFlag = 4 where id = #{id}
    </delete>


    <update id="updateEnergyExceptionDataInfoByApplyStartTime">
        update energy_exception_data_info set handlerType = '0',handlerReason = '采集配置项停用删除',handlerDate = now()
        where enterpriseCode = #{enterpriseCode} and dataCode = #{dataCode} and DATE_FORMAT(statDate,'%Y-%m-%d') >= #{startTime}
    </update>

</mapper>