/*
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.eartagregister.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huimv.modular.eartagregister.entity.EartagRegister;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


/**
 * 耳标注册
 *
 * @author yinhao
 * @date 2021-08-17 16:17:54
 */
@Repository
public interface EartagRegisterMapper extends BaseMapper<EartagRegister> {

     //查询过去时间内耳标数据上传次数并且批量更新上传次数
    @Select("UPDATE  eartag_register E  SET E.count_times =  (  IF (   (   SELECT B.count FROM  ( SELECT eartag_no, COUNT(*) AS COUNT\n" +
            "FROM eartag_data ea  WHERE 1=1   AND    ea.time >  DATE_SUB(NOW(), INTERVAL 12 HOUR)\n" +
            "GROUP BY eartag_no   ORDER BY  COUNT DESC ) B  WHERE E.eartag_no = B.eartag_no  )  >   0  ,    (SELECT B.count FROM  ( SELECT eartag_no, COUNT(*) AS COUNT\n" +
            "FROM eartag_data ea  WHERE 1=1   AND    ea.time >  DATE_SUB(NOW(), INTERVAL 12 HOUR)\n" +
            "GROUP BY eartag_no   ORDER BY  COUNT DESC ) B  WHERE E.eartag_no = B.eartag_no  )  ,   0    )   ) ")
    public void updateCount();



    //温度和耳标温度状态我没办法一次实现  所以这里分开来写 存储最新的温度
    @Select(" UPDATE  eartag_register E  SET E.ear_temp =  (  SELECT     C.ear_temp     FROM          (  SELECT     ed1.eartag_no , ed1.ear_temp   FROM     eartag_data   ed1    JOIN ( SELECT MAX(id) id  FROM   eartag_data ed2   GROUP BY ed2.eartag_no )     b  ON ed1.id = b.id   ORDER  BY ed1.`time`  DESC  ) C    WHERE  C.eartag_no  = e.eartag_no   )    ")
    public void updateEartagTemp();


    //更新耳标的温度状态  正常1   异常0
    @Select("     UPDATE  eartag_register E  SET  E.ear_temp_status =(    IF      (      ( SELECT    SUM(B.ear_temp +5)/10  AS  avg_temp     FROM     (    SELECT   ear_temp FROM  `eartag_data`     ORDER BY   id DESC   LIMIT 10  )      B  )  >     E.ear_temp    AND   E.ear_temp   > ( SELECT SUM(B.ear_temp -5 )/10 AS  avg_temp     FROM     (    SELECT   ear_temp FROM  `eartag_data`     ORDER BY   id DESC   LIMIT 10  )      B  )   ,     '1'      ,    '0'    )     ) ")
    public void CheckEartagTempAndSetStatus();



    //删除所有耳标号为传入耳标号的耳标
    @Delete(" DELETE   FROM    `eartag_register` WHERE    eartag_no =#{eartag_no}    ")
    void    deleteByeartagnoFromEartagregister(@Param("eartag_no") String eartag_no);
     //定时更新耳标绑定的基站信息
    

}
