|
@@ -3,6 +3,7 @@ package Warning
|
|
|
import (
|
|
|
"Cold_Api/conf"
|
|
|
"Cold_Api/controllers/lib"
|
|
|
+ db "Cold_Api/initialize"
|
|
|
"Cold_Api/models/Account"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
@@ -13,8 +14,11 @@ import (
|
|
|
"github.com/beego/beego/v2/core/logs"
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
"github.com/gomodule/redigo/redis"
|
|
|
+ "gorm.io/gorm"
|
|
|
+ "sort"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
+ "sync"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -25,20 +29,20 @@ var (
|
|
|
|
|
|
// 模板
|
|
|
type Warning struct {
|
|
|
- Id int64 `orm:"column(ID);size(11);auto;pk"`
|
|
|
- T_pid int `orm:"size(256);null"` // Account.Company 绑定公司
|
|
|
- T_tp int `orm:"size(200);null"` // 报警类型 ->WarningList
|
|
|
- T_sn string `orm:"index;size(256);null"` // 设备序列号
|
|
|
- T_D_name string `orm:"size(256);null"` // 设备名称
|
|
|
- T_id int `orm:"size(200);null"` // 传感器 ID
|
|
|
- T_DS_name string `orm:"size(256);null"` // 传感器名称
|
|
|
- T_Remark string `orm:"type(text);null"` // 采集内容
|
|
|
- T_Ut time.Time `orm:"type(timestamp);null;"` // 采集时间
|
|
|
- T_fUt time.Time `orm:"type(timestamp);null;"` // 首次采集时间
|
|
|
-
|
|
|
- T_Text string `orm:"type(text);null"` // 处理备注
|
|
|
- T_Log string `orm:"type(text);null"` // 通知日志
|
|
|
- T_Msid int64 `orm:"size(256);null"` // 消息ID
|
|
|
+ Id int64 `orm:"column(ID);size(11);auto;pk" gorm:"column:ID"`
|
|
|
+ T_pid int `orm:"size(256);null" gorm:"column:t_pid"` // Account.Company 绑定公司
|
|
|
+ T_tp int `orm:"size(200);null" gorm:"column:t_tp"` // 报警类型 ->WarningList
|
|
|
+ T_sn string `orm:"index;size(256);null" gorm:"column:t_sn"` // 设备序列号
|
|
|
+ T_D_name string `orm:"size(256);null" gorm:"column:t__d_name"` // 设备名称
|
|
|
+ T_id int `orm:"size(200);null" gorm:"column:t_id"` // 传感器 ID
|
|
|
+ T_DS_name string `orm:"size(256);null" gorm:"column:t__d_s_name"` // 传感器名称
|
|
|
+ T_Remark string `orm:"type(text);null" gorm:"column:t__remark"` // 采集内容
|
|
|
+ T_Ut time.Time `orm:"type(timestamp);null;" gorm:"column:t__ut"` // 采集时间
|
|
|
+ T_fUt time.Time `orm:"type(timestamp);null;" gorm:"column:t_f_ut"` // 首次采集时间
|
|
|
+
|
|
|
+ T_Text string `orm:"type(text);null" gorm:"column:t__text"` // 处理备注
|
|
|
+ T_Log string `orm:"type(text);null" gorm:"column:t__Log"` // 通知日志
|
|
|
+ T_Msid int64 `orm:"size(256);null" gorm:"column:t__msid"` // 消息ID
|
|
|
|
|
|
T_State int `orm:"size(2);default(2)"` // 0 删除 1 不处理 2 已处理 3 未处理
|
|
|
CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
|
|
@@ -80,7 +84,9 @@ type Warning_R struct {
|
|
|
T_State int // 0 删除 1 未处理 2 已处理
|
|
|
T_history int // 0 40天 1 历史数据
|
|
|
CreateTime string // 创建时间
|
|
|
+ UpdateTime string // 更新时间
|
|
|
}
|
|
|
+
|
|
|
type CompanyWarning_R struct {
|
|
|
Id int64
|
|
|
T_pid int // Account.Company 绑定公司
|
|
@@ -100,6 +106,7 @@ type CompanyWarning_R struct {
|
|
|
T_State int // 0 删除 1 未处理 2 已处理
|
|
|
T_history int // 0 40天 1 历史数据
|
|
|
CreateTime string // 创建时间
|
|
|
+ UpdateTime string // 更新时间
|
|
|
}
|
|
|
type Warning_Applet struct {
|
|
|
Id int64
|
|
@@ -122,8 +129,9 @@ type Warning_Applet struct {
|
|
|
}
|
|
|
|
|
|
type Warning_Count struct {
|
|
|
- Key string
|
|
|
- Count int64
|
|
|
+ Key string
|
|
|
+ Count int64
|
|
|
+ TableCount map[string]int64
|
|
|
}
|
|
|
|
|
|
func (t *Warning) TableName() string {
|
|
@@ -148,14 +156,15 @@ func init() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func Redis_WarningCount_Set(id int, key string, count int64) (err error) {
|
|
|
+func Redis_WarningCount_Set(id int, key string, count int64, TableCount map[string]int64) (err error) {
|
|
|
//json序列化
|
|
|
idKey := strconv.Itoa(id)
|
|
|
-
|
|
|
+ TableCount["SaveTime"] = time.Now().Unix()
|
|
|
warningCount, err := Redis_WarningCount_Get(idKey)
|
|
|
warningCount = append(warningCount, Warning_Count{
|
|
|
- Key: key,
|
|
|
- Count: count,
|
|
|
+ Key: key,
|
|
|
+ Count: count,
|
|
|
+ TableCount: TableCount,
|
|
|
})
|
|
|
|
|
|
str, err := json.Marshal(warningCount)
|
|
@@ -188,11 +197,12 @@ func Redis_WarningCount_Get(userId string) (count []Warning_Count, err error) {
|
|
|
}
|
|
|
return count, redis.ErrNil
|
|
|
}
|
|
|
-func Redis_WarningCount_GetOne(id int, key string) (count int64, err error) {
|
|
|
+func Redis_WarningCount_GetOne(id int, key string) (count int64, tableCount map[string]int64, err error) {
|
|
|
countList := []Warning_Count{}
|
|
|
+ tableCount = make(map[string]int64)
|
|
|
idKey := strconv.Itoa(id)
|
|
|
if !redis_Warning_Count.IsExist(idKey) {
|
|
|
- return count, redis.ErrNil
|
|
|
+ return count, tableCount, redis.ErrNil
|
|
|
}
|
|
|
//println("找到key:",key)
|
|
|
v := redis_Warning_Count.Get(idKey)
|
|
@@ -200,15 +210,15 @@ func Redis_WarningCount_GetOne(id int, key string) (count int64, err error) {
|
|
|
err = json.Unmarshal(v.([]byte), &countList)
|
|
|
if err != nil {
|
|
|
logs.Error(lib.FuncName(), err)
|
|
|
- return count, err
|
|
|
+ return count, tableCount, err
|
|
|
}
|
|
|
|
|
|
for _, c := range countList {
|
|
|
if c.Key == key {
|
|
|
- return c.Count, nil
|
|
|
+ return c.Count, c.TableCount, nil
|
|
|
}
|
|
|
}
|
|
|
- return count, redis.ErrNil
|
|
|
+ return count, tableCount, redis.ErrNil
|
|
|
|
|
|
}
|
|
|
|
|
@@ -246,6 +256,7 @@ func WarningToWarning_R(T_history int, t Warning) (r Warning_R) {
|
|
|
r.T_State = t.T_State
|
|
|
r.T_history = T_history
|
|
|
r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
|
|
|
+ r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
|
|
|
return r
|
|
|
}
|
|
|
func WarningToCompanyWarning_R(T_history int, t Warning) (r CompanyWarning_R) {
|
|
@@ -272,6 +283,7 @@ func WarningToCompanyWarning_R(T_history int, t Warning) (r CompanyWarning_R) {
|
|
|
r.T_State = t.T_State
|
|
|
r.T_history = T_history
|
|
|
r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
|
|
|
+ r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
|
|
|
return r
|
|
|
}
|
|
|
|
|
@@ -366,7 +378,7 @@ func Update_Warning_Backups(r Warning, T_year string, T_month string) bool {
|
|
|
o := orm.NewOrm()
|
|
|
Wtab := "warning_" + T_year + "_" + T_month
|
|
|
|
|
|
- num, err := o.Raw("UPDATE "+Wtab+" SET `t__text` = ?,`t__state` = ? WHERE `ID` = ?", r.T_Text, r.T_State, r.Id).Exec()
|
|
|
+ num, err := o.Raw("UPDATE "+Wtab+" SET `t__text` = ?,`t__state` = ?, `update_time` = ? WHERE `ID` = ?", r.T_Text, r.T_State, r.UpdateTime, r.Id).Exec()
|
|
|
|
|
|
if err != nil {
|
|
|
logs.Error(lib.FuncName(), err)
|
|
@@ -436,14 +448,14 @@ func Read_Warning_List(T_pid int, bindSN, tpList []string, T_name string, T_hand
|
|
|
var err error
|
|
|
|
|
|
// 从redis获取报警统计数量
|
|
|
- cnt, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
+ cnt, _, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
if err != nil {
|
|
|
cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
|
|
|
if err != nil {
|
|
|
logs.Error(lib.FuncName(), err)
|
|
|
return
|
|
|
}
|
|
|
- Redis_WarningCount_Set(T_pid, countRedisKey, cnt)
|
|
|
+ Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
|
|
|
}
|
|
|
if cnt == 0 {
|
|
|
return r, 0
|
|
@@ -527,14 +539,14 @@ func Read_Admin_Warning_List(admin Account.Admin, T_tp []string, T_name string,
|
|
|
}
|
|
|
var err error
|
|
|
// 从redis获取报警统计数量
|
|
|
- cnt, err = Redis_WarningCount_GetOne(admin.Id, countRedisKey)
|
|
|
+ cnt, _, err = Redis_WarningCount_GetOne(admin.Id, countRedisKey)
|
|
|
if err != nil {
|
|
|
cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
|
|
|
if err != nil {
|
|
|
logs.Error(lib.FuncName(), err)
|
|
|
return
|
|
|
}
|
|
|
- Redis_WarningCount_Set(admin.Id, countRedisKey, cnt)
|
|
|
+ Redis_WarningCount_Set(admin.Id, countRedisKey, cnt, map[string]int64{})
|
|
|
}
|
|
|
if cnt == 0 {
|
|
|
return r, 0
|
|
@@ -613,7 +625,7 @@ func Read_Warning_Backups(T_pid int, bindSN []string, T_year string, T_month str
|
|
|
sql_WHERE = strings.TrimLeft(sql_WHERE, " AND")
|
|
|
|
|
|
// 从redis获取报警统计数量
|
|
|
- cnt, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
+ cnt, _, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
if err != nil {
|
|
|
sql := "SELECT COUNT(*) FROM " + Wtab + " WHERE " + sql_WHERE
|
|
|
fmt.Println(sql)
|
|
@@ -627,7 +639,7 @@ func Read_Warning_Backups(T_pid int, bindSN []string, T_year string, T_month str
|
|
|
}
|
|
|
cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
|
|
|
|
|
|
- Redis_WarningCount_Set(T_pid, countRedisKey, cnt)
|
|
|
+ Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
|
|
|
}
|
|
|
if cnt == 0 {
|
|
|
return r, 0
|
|
@@ -717,7 +729,7 @@ func Read_Admin_Warning_Backups(admin Account.Admin, T_year string, T_month stri
|
|
|
sql_WHERE = strings.TrimLeft(sql_WHERE, " AND")
|
|
|
|
|
|
// 从redis获取报警统计数量
|
|
|
- cnt, err := Redis_WarningCount_GetOne(admin.Id, countRedisKey)
|
|
|
+ cnt, _, err := Redis_WarningCount_GetOne(admin.Id, countRedisKey)
|
|
|
if err != nil {
|
|
|
sql := "SELECT COUNT(*) FROM " + Wtab + " WHERE " + sql_WHERE
|
|
|
fmt.Println(sql)
|
|
@@ -731,7 +743,7 @@ func Read_Admin_Warning_Backups(admin Account.Admin, T_year string, T_month stri
|
|
|
}
|
|
|
cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
|
|
|
|
|
|
- Redis_WarningCount_Set(admin.Id, countRedisKey, cnt)
|
|
|
+ Redis_WarningCount_Set(admin.Id, countRedisKey, cnt, map[string]int64{})
|
|
|
}
|
|
|
if cnt == 0 {
|
|
|
return r, 0
|
|
@@ -761,7 +773,8 @@ func Read_Admin_Warning_Backups(admin Account.Admin, T_year string, T_month stri
|
|
|
|
|
|
}
|
|
|
|
|
|
-func Read_Warning_ALL_T_State_Count(T_pid int, T_pids []int, T_handle int, bindSN []string, T_Warning string, Is_Today bool) (cnt int64) {
|
|
|
+// 用户首页统计
|
|
|
+func Read_Warning_ALL_T_State_Count2(T_pid int, T_pids []int, T_handle int, bindSN []string, T_Warning string, Is_Today bool) (cnt int64) {
|
|
|
o := orm.NewOrm()
|
|
|
qs := o.QueryTable(new(Warning))
|
|
|
cond := orm.NewCondition()
|
|
@@ -804,6 +817,53 @@ func Read_Warning_ALL_T_State_Count(T_pid int, T_pids []int, T_handle int, bindS
|
|
|
return cnt
|
|
|
}
|
|
|
|
|
|
+// 用户首页统计,使用gorm查询
|
|
|
+func Read_Warning_ALL_T_State_Count(T_pid int, T_pids []int, T_handle int, bindSN []string, T_Warning string, Is_Today bool) (cnt int64) {
|
|
|
+
|
|
|
+ tableName := "warning"
|
|
|
+ if conf.UseNewWarningQuery {
|
|
|
+ tableName = fmt.Sprintf("warning_%s_%s", time.Now().Format("2006"), time.Now().Format("01"))
|
|
|
+ }
|
|
|
+ gormDB := db.DB.Table(tableName)
|
|
|
+ gormDB.Where("t__state > 0 ")
|
|
|
+ if T_pid > 0 {
|
|
|
+ gormDB.Where("t_pid = ?", T_pid)
|
|
|
+ }
|
|
|
+ if T_pid == 0 {
|
|
|
+ gormDB.Where("t_pid in (?)", T_pids)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(bindSN) > 0 {
|
|
|
+ gormDB.Where("t_sn in (?)", bindSN)
|
|
|
+ }
|
|
|
+
|
|
|
+ if Is_Today {
|
|
|
+ startTime := time.Now().Format("2006-01-02") + " 00:00:00"
|
|
|
+ endTime := time.Now().Format("2006-01-02") + " 23:59:59"
|
|
|
+ gormDB.Where("t__ut between ? and ?", startTime, endTime)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(T_Warning) > 0 && T_Warning != "*" {
|
|
|
+ list := lib.SplitStringIds(T_Warning, "W")
|
|
|
+ gormDB.Where("t_tp in (?)", list)
|
|
|
+ }
|
|
|
+
|
|
|
+ // T_State 0 删除 1 不处理 2 已处理 3 未处理
|
|
|
+ // T_handle 1:默认 2:全部记录 3 未处理
|
|
|
+ if T_handle == 1 {
|
|
|
+ gormDB.Where("t__state > ?", 1)
|
|
|
+ }
|
|
|
+ if T_handle == 3 {
|
|
|
+ gormDB.Where("t__state = ?", 3)
|
|
|
+ }
|
|
|
+ err := gormDB.Count(&cnt).Error
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(lib.FuncName(), err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return cnt
|
|
|
+}
|
|
|
+
|
|
|
// 设备日志
|
|
|
type DeviceLogs struct {
|
|
|
Id int64
|
|
@@ -897,7 +957,7 @@ func Read_Warning_List_By_DS_T_type(admin *Account.Admin, T_pid int, bindSN []st
|
|
|
"LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_type FROM device_sensor) AS ds " +
|
|
|
"ON w.t_sn = ds.tsn AND w.t_id = ds.tid " +
|
|
|
"WHERE " + sql_WHERE
|
|
|
- cnt, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
+ cnt, _, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
if err != nil {
|
|
|
fmt.Println(sql)
|
|
|
_, err = o.Raw(sql).ValuesList(&maps_z)
|
|
@@ -910,7 +970,7 @@ func Read_Warning_List_By_DS_T_type(admin *Account.Admin, T_pid int, bindSN []st
|
|
|
}
|
|
|
cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
|
|
|
|
|
|
- Redis_WarningCount_Set(T_pid, countRedisKey, cnt)
|
|
|
+ Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
|
|
|
}
|
|
|
|
|
|
if cnt == 0 {
|
|
@@ -1019,14 +1079,14 @@ func Read_Company_Warning_List(T_pids []int, T_tp []string, T_name string, T_han
|
|
|
|
|
|
var err error
|
|
|
// 从redis获取报警统计数量
|
|
|
- cnt, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
+ cnt, _, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
if err != nil {
|
|
|
cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
|
|
|
if err != nil {
|
|
|
logs.Error(lib.FuncName(), err)
|
|
|
return
|
|
|
}
|
|
|
- Redis_WarningCount_Set(T_pid, countRedisKey, cnt)
|
|
|
+ Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
|
|
|
}
|
|
|
if cnt == 0 {
|
|
|
return r, 0
|
|
@@ -1106,7 +1166,7 @@ func Read_Company_Warning_Backups(T_pids []int, T_year string, T_month string, T
|
|
|
sql_WHERE = strings.TrimLeft(sql_WHERE, " AND")
|
|
|
|
|
|
// 从redis获取报警统计数量
|
|
|
- cnt, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
+ cnt, _, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
if err != nil {
|
|
|
sql := "SELECT COUNT(*) FROM " + Wtab + " WHERE " + sql_WHERE
|
|
|
fmt.Println(sql)
|
|
@@ -1120,7 +1180,7 @@ func Read_Company_Warning_Backups(T_pids []int, T_year string, T_month string, T
|
|
|
}
|
|
|
cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
|
|
|
|
|
|
- Redis_WarningCount_Set(T_pid, countRedisKey, cnt)
|
|
|
+ Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
|
|
|
}
|
|
|
if cnt == 0 {
|
|
|
return r, 0
|
|
@@ -1145,3 +1205,511 @@ func Read_Company_Warning_Backups(T_pids []int, T_year string, T_month string, T
|
|
|
return r, cnt
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+func GormReadWarningListScopes(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string) func(db *gorm.DB) *gorm.DB {
|
|
|
+ return func(db *gorm.DB) *gorm.DB {
|
|
|
+ if len(bindSN) > 0 {
|
|
|
+ db.Where("t_sn in (?)", bindSN)
|
|
|
+ }
|
|
|
+
|
|
|
+ if T_pid > 0 {
|
|
|
+ db.Where("t_pid = ?", T_pid)
|
|
|
+ }
|
|
|
+ if len(tpList) > 0 {
|
|
|
+ min, max, is := checkStringListContinuous(tpList)
|
|
|
+ if is {
|
|
|
+ db.Where("t_tp between ? and ?", min, max)
|
|
|
+ } else {
|
|
|
+ db.Where("t_tp in (?)", tpList)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(T_name) > 0 {
|
|
|
+ if len(T_name) == 16 {
|
|
|
+ db.Where("t_sn = ?", T_name)
|
|
|
+ } else {
|
|
|
+ likeName := "%" + T_name + "%"
|
|
|
+ db.Where("t_sn like ? or t__d_name like ? or t_id like ? or t__d_s_name like ? ", likeName, likeName, likeName, likeName)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(Time_start_) > 0 {
|
|
|
+ db.Where("t__ut >= ?", Time_start_)
|
|
|
+ }
|
|
|
+ if len(Time_end_) > 0 {
|
|
|
+ db.Where("t__ut <= ?", Time_end_)
|
|
|
+ }
|
|
|
+ // T_State 0 删除 1 不处理 2 已处理 3 未处理
|
|
|
+ // T_handle 1:默认 2:全部记录 3 未处理
|
|
|
+ if T_handle == 1 {
|
|
|
+ db.Where("t__state > ?", 1)
|
|
|
+ } else if T_handle == 3 {
|
|
|
+ db.Where("t__state = ?", 3)
|
|
|
+ } else {
|
|
|
+ db.Where("t__state > ?", 0)
|
|
|
+ }
|
|
|
+ return db
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 获取列表
|
|
|
+func Gorm_Read_Warning_List(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
|
|
|
+ countRedisKey string) (r []Warning_R, cnt int64) {
|
|
|
+ tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
|
|
|
+ var err error
|
|
|
+ var warnings []Warning
|
|
|
+ tableCount := make(map[string]int64)
|
|
|
+ gormReadWarningListScopes := GormReadWarningListScopes(T_pid, bindSN, tpList, T_name, T_handle, Time_start_, Time_end_)
|
|
|
+ // 从redis获取报警统计数量
|
|
|
+ cnt, tableCount, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
+ if err != nil {
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ tableDB := db.DB.Table(tableName)
|
|
|
+ var count int64
|
|
|
+
|
|
|
+ tableDB.Scopes(gormReadWarningListScopes).Count(&count)
|
|
|
+ tableCount[tableName] = count
|
|
|
+ cnt += count
|
|
|
+ }
|
|
|
+ Redis_WarningCount_Set(T_pid, countRedisKey, cnt, tableCount)
|
|
|
+ }
|
|
|
+
|
|
|
+ if cnt == 0 {
|
|
|
+ return r, 0
|
|
|
+ }
|
|
|
+ if page_z == 9999 {
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ var tableWarning []Warning
|
|
|
+ tableDB := db.DB.Table(tableName)
|
|
|
+ tableDB.Scopes(gormReadWarningListScopes).
|
|
|
+ Order("t__ut desc").Offset(-1).Limit(-1).Find(&tableWarning)
|
|
|
+ warnings = append(warnings, tableWarning...)
|
|
|
+ }
|
|
|
+ for _, v := range warnings {
|
|
|
+ r = append(r, WarningToWarning_R(0, v))
|
|
|
+ }
|
|
|
+ return r, cnt
|
|
|
+ }
|
|
|
+ offset := (page - 1) * page_z
|
|
|
+ remaining := offset
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ var tableWarning []Warning
|
|
|
+ tableDB := db.DB.Table(tableName)
|
|
|
+ var count int64
|
|
|
+ if tableName == fmt.Sprintf("warning_%d_%02d", time.Now().Year(), time.Now().Month()) {
|
|
|
+ if saveTime, ok := tableCount["SaveTime"]; ok {
|
|
|
+ t1 := time.Unix(saveTime, 0)
|
|
|
+ if time.Now().Sub(t1) < 5*time.Minute {
|
|
|
+ if count, ok = tableCount[tableName]; !ok {
|
|
|
+ tableDB.Scopes(gormReadWarningListScopes).Count(&count)
|
|
|
+ tableCount[tableName] = count
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ tableDB.Scopes(gormReadWarningListScopes).Count(&count)
|
|
|
+ tableCount[tableName] = count
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ var ok bool
|
|
|
+ if count, ok = tableCount[tableName]; !ok {
|
|
|
+ tableDB.Scopes(gormReadWarningListScopes).Count(&count)
|
|
|
+ tableCount[tableName] = count
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if remaining >= int(count) {
|
|
|
+ remaining -= int(count)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ tableDB.Scopes(gormReadWarningListScopes).
|
|
|
+ Order("t__ut desc").Offset(remaining).Limit(page_z - len(warnings)).Find(&tableWarning)
|
|
|
+
|
|
|
+ warnings = append(warnings, tableWarning...)
|
|
|
+ if len(warnings) >= page_z {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ remaining = 0
|
|
|
+ }
|
|
|
+ for _, v := range warnings {
|
|
|
+ r = append(r, WarningToWarning_R(0, v))
|
|
|
+ }
|
|
|
+ return r, cnt
|
|
|
+}
|
|
|
+
|
|
|
+func Gorm_Read_Warning_List_Excel(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
|
|
|
+ countRedisKey string) (r []Warning_R, cnt int64) {
|
|
|
+ tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
|
|
|
+ gormReadWarningListScopes := GormReadWarningListScopes(T_pid, bindSN, tpList, T_name, T_handle, Time_start_, Time_end_)
|
|
|
+
|
|
|
+ var warnings []Warning
|
|
|
+ var wg sync.WaitGroup
|
|
|
+
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ wg.Add(1)
|
|
|
+ go func(table string) {
|
|
|
+ defer wg.Done()
|
|
|
+ offset := 0
|
|
|
+ limit := 10000
|
|
|
+ for {
|
|
|
+ var tableWarning []Warning
|
|
|
+ tableDB := db.DB.Table(table)
|
|
|
+ tableDB.Scopes(gormReadWarningListScopes).
|
|
|
+ Order("t__ut desc").Offset(offset).Limit(limit).Find(&tableWarning)
|
|
|
+ if len(tableWarning) == 0 {
|
|
|
+ break // 如果没有数据则结束循环
|
|
|
+ }
|
|
|
+ offset += limit // 更新偏移量
|
|
|
+ warnings = append(warnings, tableWarning...)
|
|
|
+ }
|
|
|
+ }(tableName)
|
|
|
+ }
|
|
|
+ // 等待所有协程完成
|
|
|
+ wg.Wait()
|
|
|
+ // 倒序
|
|
|
+ sort.Slice(warnings, func(i, j int) bool {
|
|
|
+ if warnings[i].T_Ut.After(warnings[j].T_Ut) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ })
|
|
|
+ for _, v := range warnings {
|
|
|
+ r = append(r, WarningToWarning_R(0, v))
|
|
|
+ }
|
|
|
+
|
|
|
+ return r, cnt
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func GormReadAdminWarningListScopes(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string) func(db *gorm.DB) *gorm.DB {
|
|
|
+ return func(db *gorm.DB) *gorm.DB {
|
|
|
+ if admin.T_pids != "*" && len(admin.T_pids) > 0 {
|
|
|
+ list := lib.SplitStringIds(admin.T_pids, "P")
|
|
|
+ db.Where("t_pid in (?)", list)
|
|
|
+ }
|
|
|
+ if len(T_tp) > 0 {
|
|
|
+ min, max, is := checkStringListContinuous(T_tp)
|
|
|
+ if is {
|
|
|
+ db.Where("t_tp between ? and ?", min, max)
|
|
|
+ } else {
|
|
|
+ db.Where("t_tp in (?)", T_tp)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(T_name) > 0 {
|
|
|
+ if len(T_name) == 16 {
|
|
|
+ db.Where("t_sn = ?", T_name)
|
|
|
+ } else {
|
|
|
+ likeName := "%" + T_name + "%"
|
|
|
+ db.Where("t_sn like ? or t__d_name like ? or t_id like ? or t__d_s_name like ? ", likeName, likeName, likeName, likeName)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(Time_start_) > 0 {
|
|
|
+ db.Where("t__ut >= ?", Time_start_)
|
|
|
+ }
|
|
|
+ if len(Time_end_) > 0 {
|
|
|
+ db.Where("t__ut <= ?", Time_end_)
|
|
|
+ }
|
|
|
+ // T_State 0 删除 1 不处理 2 已处理 3 未处理
|
|
|
+ // T_handle 1:默认 2:全部记录 3 未处理
|
|
|
+ if T_handle == 1 {
|
|
|
+ db.Where("t__state > ?", 1)
|
|
|
+ } else if T_handle == 3 {
|
|
|
+ db.Where("t__state = ?", 3)
|
|
|
+ } else {
|
|
|
+ db.Where("t__state > ?", 0)
|
|
|
+ }
|
|
|
+ return db
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 管理员报警列表
|
|
|
+func Gorm_Read_Admin_Warning_List(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
|
|
|
+ countRedisKey string) (r []Warning_R, cnt int64) {
|
|
|
+
|
|
|
+ if len(admin.T_pids) == 0 {
|
|
|
+ return r, cnt
|
|
|
+ }
|
|
|
+ gormReadAdminWarningListScopes := GormReadAdminWarningListScopes(admin, T_tp, T_name, T_handle, Time_start_, Time_end_)
|
|
|
+ tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
|
|
|
+ var err error
|
|
|
+ var warnings []Warning
|
|
|
+ tableCount := make(map[string]int64)
|
|
|
+ // 从redis获取报警统计数量
|
|
|
+ cnt, tableCount, err = Redis_WarningCount_GetOne(admin.Id, countRedisKey)
|
|
|
+ if err != nil {
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ tableDB := db.DB.Table(tableName)
|
|
|
+ var count int64
|
|
|
+
|
|
|
+ tableDB.Scopes(gormReadAdminWarningListScopes).Count(&count)
|
|
|
+ tableCount[tableName] = count
|
|
|
+ cnt += count
|
|
|
+ }
|
|
|
+ Redis_WarningCount_Set(admin.Id, countRedisKey, cnt, tableCount)
|
|
|
+ }
|
|
|
+
|
|
|
+ if cnt == 0 {
|
|
|
+ return r, 0
|
|
|
+ }
|
|
|
+ // page_z == 9999 获取全部
|
|
|
+ if page_z == 9999 {
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ var tableWarning []Warning
|
|
|
+ tableDB := db.DB.Table(tableName)
|
|
|
+ tableDB.Scopes(gormReadAdminWarningListScopes).
|
|
|
+ Order("t__ut desc").Offset(-1).Limit(-1).Find(&tableWarning)
|
|
|
+ warnings = append(warnings, tableWarning...)
|
|
|
+ }
|
|
|
+ for _, v := range warnings {
|
|
|
+ r = append(r, WarningToWarning_R(0, v))
|
|
|
+ }
|
|
|
+ return r, cnt
|
|
|
+ }
|
|
|
+
|
|
|
+ offset := (page - 1) * page_z
|
|
|
+ remaining := offset
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ var tableWarning []Warning
|
|
|
+ tableDB := db.DB.Table(tableName)
|
|
|
+
|
|
|
+ count := tableCount[tableName]
|
|
|
+
|
|
|
+ if remaining >= int(count) {
|
|
|
+ remaining -= int(count)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ tableDB.Scopes(gormReadAdminWarningListScopes).
|
|
|
+ Order("t__ut desc").Offset(remaining).Limit(page_z - len(warnings)).Find(&tableWarning)
|
|
|
+ warnings = append(warnings, tableWarning...)
|
|
|
+ if len(warnings) >= page_z {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ remaining = 0
|
|
|
+ }
|
|
|
+ for _, v := range warnings {
|
|
|
+ r = append(r, WarningToWarning_R(0, v))
|
|
|
+ }
|
|
|
+ return r, cnt
|
|
|
+}
|
|
|
+func Gorm_Read_Admin_Warning_List_Excel(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
|
|
|
+ countRedisKey string) (r []Warning_R, cnt int64) {
|
|
|
+
|
|
|
+ if len(admin.T_pids) == 0 {
|
|
|
+ return r, cnt
|
|
|
+ }
|
|
|
+ gormReadAdminWarningListScopes := GormReadAdminWarningListScopes(admin, T_tp, T_name, T_handle, Time_start_, Time_end_)
|
|
|
+ tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
|
|
|
+ var warnings []Warning
|
|
|
+ var wg sync.WaitGroup
|
|
|
+
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ wg.Add(1)
|
|
|
+ go func(table string) {
|
|
|
+ defer wg.Done()
|
|
|
+ offset := 0
|
|
|
+ limit := 10000
|
|
|
+ for {
|
|
|
+ var tableWarning []Warning
|
|
|
+ tableDB := db.DB.Table(table)
|
|
|
+ tableDB.Scopes(gormReadAdminWarningListScopes).
|
|
|
+ Order("t__ut desc").Offset(offset).Limit(limit).Find(&tableWarning)
|
|
|
+ if len(tableWarning) == 0 {
|
|
|
+ break // 如果没有数据则结束循环
|
|
|
+ }
|
|
|
+ offset += limit // 更新偏移量
|
|
|
+ warnings = append(warnings, tableWarning...)
|
|
|
+ }
|
|
|
+ }(tableName)
|
|
|
+ }
|
|
|
+ // 等待所有协程完成
|
|
|
+ wg.Wait()
|
|
|
+ // 倒序
|
|
|
+ sort.Slice(warnings, func(i, j int) bool {
|
|
|
+ if warnings[i].T_Ut.After(warnings[j].T_Ut) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ })
|
|
|
+ for _, v := range warnings {
|
|
|
+ r = append(r, WarningToWarning_R(0, v))
|
|
|
+ }
|
|
|
+
|
|
|
+ return r, cnt
|
|
|
+
|
|
|
+}
|
|
|
+func GormReadCompanyWarningListScopes(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string) func(db *gorm.DB) *gorm.DB {
|
|
|
+ return func(db *gorm.DB) *gorm.DB {
|
|
|
+ if len(T_pids) > 0 {
|
|
|
+ db.Where("t_pid in (?)", T_pids)
|
|
|
+ }
|
|
|
+ if len(T_tp) > 0 {
|
|
|
+ min, max, is := checkStringListContinuous(T_tp)
|
|
|
+ if is {
|
|
|
+ db.Where("t_tp between ? and ?", min, max)
|
|
|
+ } else {
|
|
|
+ db.Where("t_tp in (?)", T_tp)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(T_name) > 0 {
|
|
|
+ if len(T_name) == 16 {
|
|
|
+ db.Where("t_sn = ?", T_name)
|
|
|
+ } else {
|
|
|
+ likeName := "%" + T_name + "%"
|
|
|
+ db.Where("t_sn like ? or t__d_name like ? or t_id like ? or t__d_s_name like ?", likeName, likeName, likeName, likeName)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(Time_start_) > 0 {
|
|
|
+ db.Where("t__ut >= ?", Time_start_)
|
|
|
+ }
|
|
|
+ if len(Time_end_) > 0 {
|
|
|
+ db.Where("t__ut <= ?", Time_end_)
|
|
|
+ }
|
|
|
+ // T_State 0 删除 1 不处理 2 已处理 3 未处理
|
|
|
+ // T_handle 1:默认 2:全部记录 3 未处理
|
|
|
+ if T_handle == 1 {
|
|
|
+ db.Where("t__state > ?", 1)
|
|
|
+ } else if T_handle == 3 {
|
|
|
+ db.Where("t__state = ?", 3)
|
|
|
+ } else {
|
|
|
+ db.Where("t__state > ?", 0)
|
|
|
+ }
|
|
|
+ return db
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 公司管理报警列表
|
|
|
+func Gorm_Read_Company_Warning_List(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
|
|
|
+ T_pid int, countRedisKey string) (r []CompanyWarning_R, cnt int64) {
|
|
|
+
|
|
|
+ gormReadCompanyWarningListScopes := GormReadCompanyWarningListScopes(T_pids, T_tp, T_name, T_handle, Time_start_, Time_end_)
|
|
|
+ tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
|
|
|
+ var err error
|
|
|
+ var warnings []Warning
|
|
|
+ tableCount := make(map[string]int64)
|
|
|
+ // 从redis获取报警统计数量
|
|
|
+ cnt, tableCount, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
|
|
|
+ if err != nil {
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ tableDB := db.DB.Table(tableName)
|
|
|
+ var count int64
|
|
|
+
|
|
|
+ tableDB.Scopes(gormReadCompanyWarningListScopes).Count(&count)
|
|
|
+ tableCount[tableName] = count
|
|
|
+ cnt += count
|
|
|
+ }
|
|
|
+ Redis_WarningCount_Set(T_pid, countRedisKey, cnt, tableCount)
|
|
|
+ }
|
|
|
+
|
|
|
+ if cnt == 0 {
|
|
|
+ return r, 0
|
|
|
+ }
|
|
|
+
|
|
|
+ offset := (page - 1) * page_z
|
|
|
+ remaining := offset
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ var tableWarning []Warning
|
|
|
+ tableDB := db.DB.Table(tableName)
|
|
|
+
|
|
|
+ count := tableCount[tableName]
|
|
|
+ if remaining >= int(count) {
|
|
|
+ remaining -= int(count)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ tableDB.Scopes(gormReadCompanyWarningListScopes).
|
|
|
+ Order("t__ut desc").Offset(remaining).Limit(page_z - len(warnings)).Find(&tableWarning)
|
|
|
+ warnings = append(warnings, tableWarning...)
|
|
|
+ if len(warnings) >= page_z {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ remaining = 0
|
|
|
+ }
|
|
|
+ for _, v := range warnings {
|
|
|
+ r = append(r, WarningToCompanyWarning_R(0, v))
|
|
|
+ }
|
|
|
+ return r, cnt
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func Gorm_Read_Company_Warning_List_Excel(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
|
|
|
+ T_pid int, countRedisKey string) (r []CompanyWarning_R, cnt int64) {
|
|
|
+
|
|
|
+ gormReadCompanyWarningListScopes := GormReadCompanyWarningListScopes(T_pids, T_tp, T_name, T_handle, Time_start_, Time_end_)
|
|
|
+ tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
|
|
|
+ var warnings []Warning
|
|
|
+ var wg sync.WaitGroup
|
|
|
+
|
|
|
+ for _, tableName := range tableNames {
|
|
|
+ wg.Add(1)
|
|
|
+ go func(table string) {
|
|
|
+ defer wg.Done()
|
|
|
+ offset := 0
|
|
|
+ limit := 10000
|
|
|
+ for {
|
|
|
+ var tableWarning []Warning
|
|
|
+ tableDB := db.DB.Table(table)
|
|
|
+ tableDB.Scopes(gormReadCompanyWarningListScopes).
|
|
|
+ Order("t__ut desc").Offset(offset).Limit(limit).Find(&tableWarning)
|
|
|
+ if len(tableWarning) == 0 {
|
|
|
+ break // 如果没有数据则结束循环
|
|
|
+ }
|
|
|
+ offset += limit // 更新偏移量
|
|
|
+ warnings = append(warnings, tableWarning...)
|
|
|
+ }
|
|
|
+ }(tableName)
|
|
|
+ }
|
|
|
+ // 等待所有协程完成
|
|
|
+ wg.Wait()
|
|
|
+ // 倒序
|
|
|
+ sort.Slice(warnings, func(i, j int) bool {
|
|
|
+ if warnings[i].T_Ut.After(warnings[j].T_Ut) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ })
|
|
|
+ for _, v := range warnings {
|
|
|
+ r = append(r, WarningToCompanyWarning_R(0, v))
|
|
|
+ }
|
|
|
+ return r, cnt
|
|
|
+}
|
|
|
+
|
|
|
+func GenerateWarningTableNames(startDateStr string, endDateStr string) []string {
|
|
|
+ startDate, _ := lib.TimeStrToTime(startDateStr)
|
|
|
+
|
|
|
+ endDate, _ := lib.TimeStrToTime(endDateStr)
|
|
|
+
|
|
|
+ monthsArray := []string{}
|
|
|
+ for startDate.Before(endDate) || startDate.Equal(endDate) {
|
|
|
+ monthsArray = append(monthsArray, fmt.Sprintf("warning_%d_%02d", startDate.Year(), startDate.Month()))
|
|
|
+ startDate = time.Date(startDate.Year(), startDate.Month()+1, 1, 0, 0, 0, 0, time.Local)
|
|
|
+ }
|
|
|
+ sort.Stable(sort.Reverse(sort.StringSlice(monthsArray)))
|
|
|
+ return monthsArray
|
|
|
+}
|
|
|
+
|
|
|
+// 检查数据列表是否连续 返回最小值 最大值
|
|
|
+func checkStringListContinuous(nums []string) (int, int, bool) {
|
|
|
+ intNums := make([]int, len(nums))
|
|
|
+ for i, num := range nums {
|
|
|
+ parsedNum, err := strconv.Atoi(num)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error converting string to int:", err)
|
|
|
+ return 0, 0, false
|
|
|
+ }
|
|
|
+ intNums[i] = parsedNum
|
|
|
+ }
|
|
|
+
|
|
|
+ sort.Ints(intNums)
|
|
|
+
|
|
|
+ for i := 1; i < len(intNums); i++ {
|
|
|
+ if intNums[i] != intNums[i-1]+1 {
|
|
|
+ return 0, 0, false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return intNums[0], intNums[len(intNums)-1], true
|
|
|
+}
|