/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.huimv.modular.collectorregister.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huimv.modular.collectorregister.entity.CollectorRegister;
import com.huimv.modular.collectorregister.entity.DeviceStatusEntity;
import com.huimv.modular.eartagdata.entity.EartagData;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;


/**
 * 设备管理
 *
 * @author yinhao
 * @date 2021-08-19 09:43:52
 */

@Repository
public interface CollectorRegisterMapper extends BaseMapper<CollectorRegister> {


   /* *//**
     * 和Mybatis使用方法一致  这样也可以的
     * @param
     * @return
     *//*
    @Select("SELECT * FROM eartag_data where eartag_no = #{eartag_no}")
    List<EartagData> selectBy(@Param("eartag_no") String eartag_no);*/

    //上传的
    @Select(" SELECT b.device_code FROM  collector_register AS b LEFT   JOIN heart_beat_record AS a ON   a.device_code = b.device_code   WHERE   a.create_time  BETWEEN  #{start_time} AND  #{end_time}  GROUP BY device_code    ")
    List<Long>  DeviceUnAble(@Param("start_time") String start_time ,@Param("end_time") String end_time);




    //上传的
    @Select("      SELECT d.device_code FROM  (  SELECT a.device_code ,  b.id  FROM  collector_register  AS  a  LEFT   JOIN   (  SELECT device_code  ,id  FROM  heart_beat_record WHERE  create_time  BETWEEN  #{start_time} AND  #{end_time}   GROUP BY device_code   )   b ON   a.device_code = b.device_code    ) d   WHERE d.id  IS NULL  ")
    List<String>  DeviceUnAble1(@Param("start_time") String start_time , @Param("end_time") String end_time);



    //上传的
    @Select("     UPDATE  collector_register   SET  can_status =  1  WHERE   device_code    #{device_codes}   ")
      void     DeviceUnAble1Set(@Param("device_codes") String device_codes );

    //找正常上传的设备
    @Select("    SELECT device_code     FROM  heart_beat_record WHERE  create_time  BETWEEN  #{start_time} AND  #{end_time}    GROUP BY device_code    ")
    List<String>  DeviceAble1(@Param("start_time") String start_time , @Param("end_time") String end_time);




    //更新采集器温度
    @Select("      UPDATE   collector_register   C   SET C.env_temp  =   \n" +
            "      \n" +
            "       (  SELECT    E.temp    FROM \n" +
            "       \n" +
            "           (    SELECT   de.create_date,  de.device_code ,  de.temp_value AS temp  FROM   device_temp  de  JOIN   ( SELECT   MAX(id) id  FROM     device_temp     ed2      GROUP BY  ed2.device_code  )   D   \n" +
            "\n" +
            "                  WHERE  de.id  = D.id   )   E      WHERE  E.device_code  = C.`device_code` )   ")
    void     updateTemp( );


}
