<?php

namespace catchAdmin\alarm\model\get;

use catchAdmin\system\model\SysDictData;
use Exception;
use think\facade\Db;
trait AlarmReportGet
{
    
    /**
     * 获取告警类型名称
     */
    public function getAlarmTypeNameAttr($value)
    {
        $type = $this->getAttr('alarm_type');
        if (is_null($type)) {
            return '';
        }
        return $this->getDictValue('AlarmReason', $type);
    }
    
   
    /**
   * 在线时间时间戳
   * 
   */
  public function getAlarmTimeStrAttr($value)
  {
   return strtotime($this->alarm_time);
   
  }

   /**
   * 在线时间时间戳
   * 
   */
  public function getRealnameAttr($value)
  {
   return '测试设备';
   
  }
  /**
   * 在线时间时间戳
   * 
   */
  public function getIsSentAttr($value)
  {
    return $this->getDictValue('ReportSentStr', $value);
   
  }

    /*
    * 获取字典值(文本)
    */
   public function getDictValue($typeCode,$value)
   {
       return (new SysDictData())->getValueByCode($typeCode,$value);
   }

    /*
    * 获取推送结果
    */
    public function getSentResultAttr($value)
    {
        $id = $this->getAttr('id');
        $list=Db::table('wx_push_result_log')->where('alarm_id',$id)->select();
        $success=0;
        $fail=0;
        foreach($list as $val){
            $res=json_decode($val['result'],true);
            if($res['success'] && $res['message']=='ok'){
                $success++;
            }else{
                $fail++;
            }
        }
        return '成功'.$success.'条,失败'.$fail.'条';
    }
   
}