datacaps.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package service
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. "log"
  6. "strconv"
  7. "time"
  8. "ydMonitoring/config"
  9. "ydMonitoring/databases"
  10. "ydMonitoring/util"
  11. )
  12. /*
  13. *
  14. DataCaps
  15. *得到所有设备的SN并且通过SN得到表名
  16. *z_device_data_sn
  17. *查找该表内最新的一条数据信息并且查询该设数据上传时间,并且通过改时间
  18. 判断前5分钟内的数据是否缺失,如有缺失,直接推送短信告警,并记录在数据库里面(时间、客户名称、SN、数据缺失的时间、是否处理、处理人、处理时间)
  19. *
  20. */
  21. func DataCaps(davice []Device) {
  22. message := make([]string, 0)
  23. //文件名
  24. var row = 2
  25. filename := fmt.Sprintf("%s.xlsx", time.Now().Format("2006-01-02"))
  26. file, err := excelize.OpenFile(filename)
  27. if err != nil {
  28. file = excelize.NewFile()
  29. //创建工作区
  30. sheet := "sheet1"
  31. file.NewSheet(sheet)
  32. //设置列标题
  33. file.SetCellValue(sheet, "A1", "设备SN")
  34. file.SetCellValue(sheet, "B1", "公司名称")
  35. file.SetCellValue(sheet, "C1", "设备名称")
  36. file.SetCellValue(sheet, "D1", "探头")
  37. file.SetCellValue(sheet, "E1", "数据缺失开始时间")
  38. } else {
  39. //获取最后一行
  40. lastRow, err := getLastRow(file, "sheet1")
  41. if err != nil {
  42. log.Println("获取Excel文件失败:", err)
  43. return
  44. }
  45. row = lastRow + 1
  46. }
  47. sqllist := make([]string, 0)
  48. for _, v := range davice {
  49. log.Println(v.Tsn)
  50. //sql := fmt.Sprintf("select t_sn,t_id,t_pid,t_name from device_sensor where t_sn = %s")
  51. sql := fmt.Sprintf("select t_sn,t_id,t_pid,t_name from device_sensor where t_sn = %s", v.Tsn)
  52. sqllist = append(sqllist, sql)
  53. }
  54. deviceSensor := make([]DeviceSensor, 0)
  55. for _, sql := range sqllist {
  56. query, _ := databases.Db.Query(sql)
  57. var sn, t_id, t_pid, t_name string
  58. if query != nil {
  59. for query.Next() {
  60. query.Scan(&sn, &t_id, &t_pid, &t_name)
  61. deviceSensor = append(deviceSensor, DeviceSensor{sn, t_id, t_pid, t_name})
  62. }
  63. }
  64. }
  65. //查询最新的两条数据以及保存时间,比较最近两条时间是否有超过保存时间
  66. for _, sensor := range deviceSensor {
  67. var t_time string
  68. var t_save_t string
  69. var companyname string
  70. var id string
  71. sql := fmt.Sprintf("SELECT t_time FROM z_device_data_%s ORDER BY t_time DESC LIMIT 1", sensor.Tsn)
  72. sqls := fmt.Sprintf("SELECT t_save_t FROM device_parameter WHERE t_sn='%s' ORDER BY update_time DESC LIMIT 1", sensor.Tsn)
  73. company := fmt.Sprintf("SELECT t_name,id FROM company WHERE id='%s'", sensor.TPid)
  74. databases.Db.QueryRow(sqls).Scan(&t_save_t)
  75. databases.Db.QueryRow(company).Scan(&companyname, &id)
  76. queryRow := databases.Db.QueryRow(sql)
  77. if queryRow != nil {
  78. queryRow.Scan(&t_time)
  79. }
  80. // 解析时间字符串为time.Time对象
  81. timeFormat := "2006-01-02 15:04:05" // 假设这是时间字符串的格式
  82. //fmt.Println("时间不能为空")
  83. if t_time != "" {
  84. t1, err := time.Parse(timeFormat, t_time)
  85. if err != nil {
  86. log.Println("解析时间失败:", err)
  87. continue
  88. }
  89. now := time.Now().Format(timeFormat)
  90. nows, _ := time.Parse(timeFormat, now)
  91. // 计算时间差
  92. timeDiff := nows.Sub(t1).Seconds()
  93. //timeDiff := t1.Sub(now).Seconds()
  94. fmt.Println("最后一条数据时间:", t_time)
  95. fmt.Println("系统当前时间:", now)
  96. //比较时间差
  97. fmt.Println("时间差:", timeDiff)
  98. fmt.Println("系统设置时间差:", t_save_t)
  99. // 获取今天的开始时间
  100. todayStart := time.Now().Truncate(24 * time.Hour)
  101. // 确保t1表示的时间为今天
  102. if t1.After(todayStart) || t1.Equal(todayStart) {
  103. float, _ := strconv.ParseFloat(t_save_t, 64)
  104. if timeDiff > float {
  105. //log.Printf("实际时间大于系统时间Sn:%s=>>%s=>>探头%s,上传时间%s", sensor.Tsn, companyname, sensor.TId, t_time[0])
  106. message = append(message, fmt.Sprintf("Sn:%s==>%s==>设备:%s==>探头%s==>数据缺失上传时间:%s \n", sensor.Tsn, companyname+"["+id+"]", sensor.TName, sensor.TId, t_time))
  107. // 将相关信息记录到Excel文件中
  108. file.SetCellValue("sheet1", fmt.Sprintf("A%d", row), sensor.Tsn)
  109. file.SetCellValue("sheet1", fmt.Sprintf("B%d", row), companyname+"["+id+"]")
  110. file.SetCellValue("sheet1", fmt.Sprintf("C%d", row), sensor.TName)
  111. file.SetCellValue("sheet1", fmt.Sprintf("D%d", row), sensor.TId)
  112. file.SetCellValue("sheet1", fmt.Sprintf("E%d", row), t_time) // 数据缺失开始时间
  113. row++
  114. }
  115. }
  116. if err := file.SaveAs(filename); err != nil {
  117. log.Println("保存Excel文件失败:", err)
  118. }
  119. }
  120. }
  121. fmt.Println(message)
  122. lenght := len(message)
  123. phones := config.Config.GetStringSlice("phone")
  124. for _, phone := range phones {
  125. err := util.SendModel(phone, message, lenght)
  126. if err != nil {
  127. log.Println("发送失败:", err)
  128. }
  129. }
  130. }
  131. // 获取最后一个有数据的行号
  132. func getLastRow(f *excelize.File, sheet string) (int, error) {
  133. rows, err := f.GetRows(sheet)
  134. if err != nil {
  135. return 0, err
  136. }
  137. return len(rows), nil
  138. }