package controllers import ( "Cold_Api/Nats/NatsServer" "Cold_Api/conf" "Cold_Api/controllers/lib" db "Cold_Api/initialize" "Cold_Api/models/Account" "Cold_Api/models/Device" "Cold_Api/models/System" "Cold_Api/models/Warning" "encoding/json" "fmt" "github.com/beego/beego/v2/core/logs" "github.com/robfig/cron/v3" "github.com/xuri/excelize/v2" "math" "net/http" "os" "strconv" "time" ) // 设备告警 ------------------------------------------ // 告警列表 func (c *DeviceController) DeviceWarning_List() { type R_JSONS struct { //必须的大写开头 Data []Warning.Warning_R Num int64 Page int Page_size int } var r_jsons R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_name := c.GetString("T_name") T_tp := c.GetString("T_tp") Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_handle, _ := c.GetInt("T_handle") T_admin, _ := c.GetInt("T_admin") T_history, _ := c.GetInt("T_history") tpCount := Warning.Read_WarningType_Count() var tpList []string if len(T_tp) > 0 { tpList = lib.SplitStringIds(T_tp, "T") if len(tpList) == int(tpCount) { // 类型全选,替换为空,不进行查询筛选(查询全部) tpList = []string{} } } else { power, _ := Account.Read_Power_ById(c.Admin_r.T_power) if power.T_warning != "*" { tpList = lib.SplitStringIds(power.T_warning, "W") } } countRedisKey := fmt.Sprintf("Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, T_admin, T_history) bindSN, err := Account.Read_UserDevice_List(*c.Admin_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } var T_year, T_month string if T_history == 1 { date, err := time.Parse("2006-01-02 15:04:05", Time_start) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"} c.ServeJSON() return } T_year, T_month = date.Format("2006"), date.Format("01") } // 管理员界面 if T_admin == 1 { if T_history == 1 { // 获取备份 r_jsons.Data, r_jsons.Num = Warning.Read_Admin_Warning_Backups(*c.Admin_r, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, countRedisKey) } else { // 获取最新 r_jsons.Data, r_jsons.Num = Warning.Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, countRedisKey) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // 公司报警界面 if T_history == 1 { r_jsons.Data, r_jsons.Num = Warning.Read_Warning_Backups(c.T_pid, bindSN, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, countRedisKey) } else { // 获取最新 r_jsons.Data, r_jsons.Num = Warning.Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, countRedisKey) } r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *DeviceController) DeviceWarning_List2() { type R_JSONS struct { //必须的大写开头 Data []Warning.Warning_R Num int64 Page int Page_size int } var r_jsons R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_name := c.GetString("T_name") T_tp := c.GetString("T_tp") Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_handle, _ := c.GetInt("T_handle") T_admin, _ := c.GetInt("T_admin") T_history, _ := c.GetInt("T_history") T_abandon, _ := c.GetInt("T_abandon") // 1 弃用设备 if len(Time_start) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"} c.ServeJSON() return } if len(Time_end) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"} c.ServeJSON() return } tpCount := Warning.Read_WarningType_Count() var tpList []string if len(T_tp) > 0 { tpList = lib.SplitStringIds(T_tp, "T") if len(tpList) == int(tpCount) { // 类型全选,替换为空,不进行查询筛选(查询全部) tpList = []string{} } } else { power, _ := Account.Read_Power_ById(c.Admin_r.T_power) if power.T_warning != "*" { tpList = lib.SplitStringIds(power.T_warning, "W") } } countRedisKey := fmt.Sprintf("Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, T_admin, T_history, T_abandon) bindSN, err := Account.Read_UserDevice_List(*c.Admin_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } // 查询弃用设备 abandonDeviceList, _ := Device.Read_Device_List_ByT_State(bindSN, c.T_pid, 0) abandonSN := make([]string, 0) for _, r := range abandonDeviceList { abandonSN = append(abandonSN, r.T_sn) } // 管理员界面 if T_admin == 1 { // 获取最新 获取绑定的sn 如果未绑定则获取全部 r_jsons.Data, r_jsons.Num = Warning.Gorm_Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN, page, page_z, countRedisKey) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } r_jsons.Data, r_jsons.Num = Warning.Gorm_Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN, page, page_z, countRedisKey) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *DeviceController) DeviceWarning_List_Sse() { page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_name := c.GetString("T_name") T_tp := c.GetString("T_tp") Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_handle, _ := c.GetInt("T_handle") T_admin, _ := c.GetInt("T_admin") T_abandon, _ := c.GetInt("T_abandon") // 1 弃用设备 tpCount := Warning.Read_WarningType_Count() bindSN, err := Account.Read_UserDevice_List(*c.Admin_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } // 查询弃用设备 abandonDeviceList, _ := Device.Read_Device_List_ByT_State(bindSN, c.T_pid, 0) abandonSN := make([]string, 0) for _, r := range abandonDeviceList { abandonSN = append(abandonSN, r.T_sn) } var tpList []string if len(T_tp) > 0 { tpList = lib.SplitStringIds(T_tp, "T") if len(tpList) == int(tpCount) { // 类型全选,替换为空,不进行查询筛选(查询全部) tpList = []string{} } } else { power, _ := Account.Read_Power_ById(c.Admin_r.T_power) if power.T_warning != "*" { tpList = lib.SplitStringIds(power.T_warning, "W") } } countRedisKey := fmt.Sprintf("Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, T_admin, 0, T_abandon) tableNames := Warning.GenerateWarningTableNames(Time_start, Time_end) // 设置响应头 c.Ctx.ResponseWriter.Header().Set("Content-Type", "text/event-stream") c.Ctx.ResponseWriter.Header().Set("Cache-Control", "no-cache") c.Ctx.ResponseWriter.Header().Set("Connection", "keep-alive") c.Ctx.ResponseWriter.WriteHeader(http.StatusOK) var cnt int64 tableCount := make(map[string]int64) // 管理员界面 if T_admin == 1 { gormReadWarningListScopes := Warning.GormReadAdminWarningListScopes(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN) cnt, tableCount, err = Warning.Redis_WarningCount_GetOne(c.Admin_r.Id, countRedisKey) offset := (page - 1) * page_z remaining := offset var warningsCount int for _, tableName := range tableNames { 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 } } cnt += count if remaining >= int(count) { remaining -= int(count) continue } var tableWarning []Warning.Warning if warningsCount < page_z { tableDB.Scopes(gormReadWarningListScopes). Order("t__ut desc").Offset(remaining).Limit(page_z - warningsCount).Find(&tableWarning) } warningsCount += len(tableWarning) remaining = 0 var r_jsons lib.R_JSONS r_jsons.Num = cnt r_jsons.Data = tableWarning json_b, _ := json.Marshal(r_jsons) c.Ctx.ResponseWriter.Write(json_b) c.Ctx.ResponseWriter.Flush() // 可以根据需要调整间隔时间 time.Sleep(1 * time.Second) } Warning.Redis_WarningCount_Set(c.Admin_r.Id, countRedisKey, cnt, tableCount) return } //从redis获取报警统计数量 gormReadWarningListScopes := Warning.GormReadWarningListScopes(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN) cnt, tableCount, err = Warning.Redis_WarningCount_GetOne(c.T_pid, countRedisKey) offset := (page - 1) * page_z remaining := offset var warningsCount int for _, tableName := range tableNames { 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 } } cnt += count if remaining >= int(count) { remaining -= int(count) continue } var tableWarning []Warning.Warning if warningsCount < page_z { tableDB.Scopes(gormReadWarningListScopes). Order("t__ut desc").Offset(remaining).Limit(page_z - warningsCount).Find(&tableWarning) } warningsCount += len(tableWarning) remaining = 0 var r_jsons lib.R_JSONS r_jsons.Num = cnt r_jsons.Data = tableWarning json_b, _ := json.Marshal(r_jsons) c.Ctx.ResponseWriter.Write(json_b) c.Ctx.ResponseWriter.Flush() // 可以根据需要调整间隔时间 time.Sleep(1 * time.Second) } Warning.Redis_WarningCount_Set(c.T_pid, countRedisKey, cnt, tableCount) } func (c *DeviceController) CompanyWarning_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_name := c.GetString("T_name") T_tp := c.GetString("T_tp") Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_handle, _ := c.GetInt("T_handle") T_history, _ := c.GetInt("T_history") tpCount := Warning.Read_WarningType_Count() var tpList []string if len(T_tp) > 0 { tpList = lib.SplitStringIds(T_tp, "T") if len(tpList) == int(tpCount) { // 类型全选,替换为空,不进行查询筛选(查询全部) tpList = []string{} } } else { power, _ := Account.Read_Power_ById(c.Admin_r.T_power) if power.T_warning != "*" { tpList = lib.SplitStringIds(power.T_warning, "W") } } countRedisKey := fmt.Sprintf("Company_Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0, T_history) Company_r, err := Account.Read_Company_ById(c.T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path) var T_year, T_month string if T_history == 1 { date, err := time.Parse("2006-01-02 15:04:05", Time_start) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"} c.ServeJSON() return } T_year, T_month = date.Format("2006"), date.Format("01") } Account.Read_Company_All_Maps() if T_history == 1 { r_jsons.Data, r_jsons.Num = Warning.Read_Company_Warning_Backups(T_pids, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, c.T_pid, countRedisKey) } else { // 获取最新 r_jsons.Data, r_jsons.Num = Warning.Read_Company_Warning_List(T_pids, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, c.T_pid, countRedisKey) } r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *DeviceController) CompanyWarning_List2() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_name := c.GetString("T_name") T_tp := c.GetString("T_tp") Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_handle, _ := c.GetInt("T_handle") if len(Time_start) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"} c.ServeJSON() return } if len(Time_end) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"} c.ServeJSON() return } tpCount := Warning.Read_WarningType_Count() var tpList []string if len(T_tp) > 0 { tpList = lib.SplitStringIds(T_tp, "T") if len(tpList) == int(tpCount) { // 类型全选,替换为空,不进行查询筛选(查询全部) tpList = []string{} } } else { power, _ := Account.Read_Power_ById(c.Admin_r.T_power) if power.T_warning != "*" { tpList = lib.SplitStringIds(power.T_warning, "W") } } countRedisKey := fmt.Sprintf("Company_Warning_Count_%d_%s_%s_%s_%s_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0) Company_r, err := Account.Read_Company_ById(c.T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path) Account.Read_Company_All_Maps() // 获取最新 r_jsons.Data, r_jsons.Num = Warning.Gorm_Read_Company_Warning_List(T_pids, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, c.T_pid, countRedisKey) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // 查询告警 暂时没有用到 func (c *DeviceController) DeviceWarning_Get() { id, _ := c.GetInt("T_id") T_time := c.GetString("T_time") if len(T_time) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"} c.ServeJSON() return } var T_year, T_month string date, err := time.Parse("2006-01-02 15:04:05", T_time) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"} c.ServeJSON() return } T_year, T_month = date.Format("2006"), date.Format("01") Wtab := "warning" Wtab += "_" + T_year + "_" + T_month T, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.WarningToWarning_R(0, T)} c.ServeJSON() return } // 编辑告警(处理告警) func (c *DeviceController) DeviceWarning_Post() { id, _ := c.GetInt("T_id") T_Text := c.GetString("T_Text") T_time := c.GetString("T_time") T_history, _ := c.GetInt("T_history") // TODO 后期废弃 if len(T_time) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"} c.ServeJSON() return } var T_year, T_month string var warning Warning.Warning Wtab := "warning" if T_history == 1 { date, err := time.Parse("2006-01-02 15:04:05", T_time) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"} c.ServeJSON() return } T_year, T_month = date.Format("2006"), date.Format("01") Wtab += "_" + T_year + "_" + T_month warning, err = Warning.Read_Warning_ById_Backups(id, T_year, T_month) if err != nil && err.Error() != " no row found" { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } warning.T_Text = T_Text warning.T_State = 2 warning.UpdateTime = time.Now() if is := Warning.Update_Warning_Backups(warning, T_year, T_month); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } warning = Warning.Read_Warning_ById(int64(id)) if warning.Id > 0 { warning.T_Text = T_Text warning.T_State = 2 warning.UpdateTime = time.Now() if is := Warning.Update_Warning(warning, "T_Text", "T_State", "UpdateTime"); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } } } else { // 使用新的查询方式后 warning表不做更新 warning = Warning.Read_Warning_ById(int64(id)) if warning.Id > 0 { warning.T_Text = T_Text warning.T_State = 2 warning.UpdateTime = time.Now() if is := Warning.Update_Warning(warning, "T_Text", "T_State", "UpdateTime"); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } } // 同步处理历史数据 date, err := time.Parse("2006-01-02 15:04:05", T_time) T_year, T_month = date.Format("2006"), date.Format("01") Wtab += "_" + T_year + "_" + T_month warningBackups, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month) if err != nil && err.Error() != " no row found" { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } if warningBackups.Id > 0 { warningBackups.T_Text = T_Text warningBackups.T_State = 2 warningBackups.UpdateTime = time.Now() if is := Warning.Update_Warning_Backups(warningBackups, T_year, T_month); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } } } // 删除报警列表统计的缓存 Warning.Redis_WarningCount_DelK(c.T_pid) Warning.Redis_WarningCount_DelK(c.Admin_r.Id) System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警处理操作", Wtab+":"+strconv.Itoa(id)+"->"+T_Text) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 删除告警 func (c *DeviceController) DeviceWarning_Del() { id, _ := c.GetInt("T_id") T_time := c.GetString("T_time") T_history, _ := c.GetInt("T_history") // TODO 后期废弃 if len(T_time) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"} c.ServeJSON() return } var T_year, T_month string var warning Warning.Warning Wtab := "warning" if T_history == 1 { date, err := time.Parse("2006-01-02 15:04:05", T_time) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"} c.ServeJSON() return } T_year, T_month = date.Format("2006"), date.Format("01") Wtab += "_" + T_year + "_" + T_month warningBackups, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month) if err != nil && err.Error() != " no row found" { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } if warningBackups.Id > 0 { warning.T_State = 0 warning.UpdateTime = time.Now() if is := Warning.Update_Warning_Backups(warning, T_year, T_month); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } } warning = Warning.Read_Warning_ById(int64(id)) if warning.Id > 0 { warning.T_State = 0 if is := Warning.Update_Warning(warning, "T_State"); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } } } else { // 删除主表 warning = Warning.Read_Warning_ById(int64(id)) if warning.Id > 0 { warning.T_State = 0 if is := Warning.Update_Warning(warning, "T_State"); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } } // 删除分表 date, err := time.Parse("2006-01-02 15:04:05", T_time) T_year, T_month = date.Format("2006"), date.Format("01") Wtab += "_" + T_year + "_" + T_month warningBackups, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month) if err != nil && err.Error() != " no row found" { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } if warningBackups.Id > 0 { warningBackups.T_State = 0 warningBackups.UpdateTime = time.Now() if is := Warning.Update_Warning_Backups(warningBackups, T_year, T_month); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } } } // 删除报警列表统计的缓存 Warning.Redis_WarningCount_DelK(c.T_pid) Warning.Redis_WarningCount_DelK(c.Admin_r.Id) System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警删除操作", Wtab+":"+strconv.Itoa(id)) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 导出告警 func (c *DeviceController) DeviceWarning_Data_Excel() { T_name := c.GetString("T_name") T_tp := c.GetString("T_tp") Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_handle, _ := c.GetInt("T_handle") T_admin, _ := c.GetInt("T_admin") T_history, _ := c.GetInt("T_history") tpCount := Warning.Read_WarningType_Count() var tpList []string if len(T_tp) > 0 { tpList = lib.SplitStringIds(T_tp, "T") if len(tpList) == int(tpCount) { // 类型全选,替换为空,不进行查询筛选(查询全部) tpList = []string{} } } else { power, _ := Account.Read_Power_ById(c.Admin_r.T_power) if power.T_warning != "*" { tpList = lib.SplitStringIds(power.T_warning, "W") } } countRedisKey := fmt.Sprintf("Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0, T_history) bindSN, err := Account.Read_UserDevice_List(*c.Admin_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } var T_year, T_month string if T_history == 1 { date, err := time.Parse("2006-01-02 15:04:05", Time_start) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"} c.ServeJSON() return } T_year, T_month = date.Format("2006"), date.Format("01") } var Device_data []Warning.Warning_R // 管理员界面 if T_admin == 1 { if T_history == 1 { // 获取备份 Device_data, _ = Warning.Read_Admin_Warning_Backups(*c.Admin_r, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey) } else { // 获取最新 Device_data, _ = Warning.Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey) } } if T_history == 1 { // 获取备份 Device_data, _ = Warning.Read_Warning_Backups(c.T_pid, bindSN, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey) } else { // 获取最新 Device_data, _ = Warning.Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey) } f := excelize.NewFile() // 设置单元格的值 // 这里设置表头ÒÒ f.SetCellValue("Sheet1", "A1", "报警类型") f.SetCellValue("Sheet1", "B1", "Sn") f.SetCellValue("Sheet1", "C1", "设备名称") f.SetCellValue("Sheet1", "D1", "传感器") f.SetCellValue("Sheet1", "E1", "报警内容") f.SetCellValue("Sheet1", "F1", "记录时间") f.SetCellValue("Sheet1", "G1", "处理") f.SetCellValue("Sheet1", "H1", "处理时间") // 设置列宽 f.SetColWidth("Sheet1", "A", "A", 20) f.SetColWidth("Sheet1", "B", "B", 25) f.SetColWidth("Sheet1", "C", "C", 30) f.SetColWidth("Sheet1", "D", "D", 30) f.SetColWidth("Sheet1", "G", "E", 30) f.SetColWidth("Sheet1", "H", "F", 15) f.SetColWidth("Sheet1", "I", "G", 30) f.SetColWidth("Sheet1", "J", "H", 15) line := 1 // 循环写入数据 for _, v := range Device_data { line++ f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp)) f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id)) f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_D_name) f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_DS_name) f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_Remark) f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Ut) f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Text) f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.UpdateTime) } timeStr := time.Now().Format("20060102150405") // 保存文件 if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil { logs.Error(err) } url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"} c.ServeJSON() return } // 删除文件 err = os.Remove("ofile/" + timeStr + ".xlsx") if err != nil { logs.Error(err) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url} c.ServeJSON() return } func (c *DeviceController) DeviceWarning_Data_Excel2() { T_name := c.GetString("T_name") T_tp := c.GetString("T_tp") Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_handle, _ := c.GetInt("T_handle") if len(Time_start) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"} c.ServeJSON() return } if len(Time_end) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"} c.ServeJSON() return } T_admin, _ := c.GetInt("T_admin") T_abandon, _ := c.GetInt("T_abandon") // 1 弃用设备 tpCount := Warning.Read_WarningType_Count() var tpList []string if len(T_tp) > 0 { tpList = lib.SplitStringIds(T_tp, "T") if len(tpList) == int(tpCount) { // 类型全选,替换为空,不进行查询筛选(查询全部) tpList = []string{} } } else { power, _ := Account.Read_Power_ById(c.Admin_r.T_power) if power.T_warning != "*" { tpList = lib.SplitStringIds(power.T_warning, "W") } } countRedisKey := fmt.Sprintf("Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0, T_abandon) bindSN, err := Account.Read_UserDevice_List(*c.Admin_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } // 查询弃用设备 abandonDeviceList, _ := Device.Read_Device_List_ByT_State(bindSN, c.T_pid, 0) abandonSN := make([]string, 0) for _, r := range abandonDeviceList { abandonSN = append(abandonSN, r.T_sn) } var Device_data []Warning.Warning_R // 管理员界面 if T_admin == 1 { // 获取最新 //Device_data, _ = Warning.Gorm_Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey) Device_data, _ = Warning.Gorm_Read_Admin_Warning_List_Excel(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN, 0, 9999, countRedisKey) } else { // 获取最新 //Device_data, _ = Warning.Gorm_Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey) Device_data, _ = Warning.Gorm_Read_Warning_List_Excel(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN, 0, 9999, countRedisKey) } f := excelize.NewFile() // 设置单元格的值 // 这里设置表头ÒÒ f.SetCellValue("Sheet1", "A1", "报警类型") f.SetCellValue("Sheet1", "B1", "Sn") f.SetCellValue("Sheet1", "C1", "设备名称") f.SetCellValue("Sheet1", "D1", "传感器") f.SetCellValue("Sheet1", "E1", "报警内容") f.SetCellValue("Sheet1", "F1", "记录时间") f.SetCellValue("Sheet1", "G1", "处理") f.SetCellValue("Sheet1", "H1", "处理时间") // 设置列宽 f.SetColWidth("Sheet1", "A", "A", 20) f.SetColWidth("Sheet1", "B", "B", 25) f.SetColWidth("Sheet1", "C", "C", 30) f.SetColWidth("Sheet1", "D", "D", 30) f.SetColWidth("Sheet1", "G", "E", 30) f.SetColWidth("Sheet1", "H", "F", 15) f.SetColWidth("Sheet1", "I", "G", 30) f.SetColWidth("Sheet1", "J", "H", 15) line := 1 // 循环写入数据 for _, v := range Device_data { line++ f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp)) f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id)) f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_D_name) f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_DS_name) f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_Remark) f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Ut) f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Text) f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.UpdateTime) } timeStr := time.Now().Format("20060102150405") // 保存文件 if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil { logs.Error(err) } url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"} c.ServeJSON() return } // 删除文件 err = os.Remove("ofile/" + timeStr + ".xlsx") if err != nil { logs.Error(err) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url} c.ServeJSON() return } // 公司管理 - 导出告警 func (c *DeviceController) CompanyWarning_Data_Excel() { T_name := c.GetString("T_name") T_tp := c.GetString("T_tp") Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_handle, _ := c.GetInt("T_handle") T_history, _ := c.GetInt("T_history") tpCount := Warning.Read_WarningType_Count() var tpList []string if len(T_tp) > 0 { tpList = lib.SplitStringIds(T_tp, "T") if len(tpList) == int(tpCount) { // 类型全选,替换为空,不进行查询筛选(查询全部) tpList = []string{} } } else { power, _ := Account.Read_Power_ById(c.Admin_r.T_power) if power.T_warning != "*" { tpList = lib.SplitStringIds(power.T_warning, "W") } } countRedisKey := fmt.Sprintf("Company_Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0, T_history) Account.Read_Company_All_Maps() Company_r, err := Account.Read_Company_ById(c.T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path) var T_year, T_month string if T_history == 1 { date, err := time.Parse("2006-01-02 15:04:05", Time_start) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"} c.ServeJSON() return } T_year, T_month = date.Format("2006"), date.Format("01") } var Device_data []Warning.CompanyWarning_R if T_history == 1 { // 获取备份 Device_data, _ = Warning.Read_Company_Warning_Backups(T_pids, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, c.T_pid, countRedisKey) } else { // 获取最新 Device_data, _ = Warning.Read_Company_Warning_List(T_pids, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, c.T_pid, countRedisKey) } f := excelize.NewFile() // 设置单元格的值 // 这里设置表头 f.SetCellValue("Sheet1", "A1", "报警类型") f.SetCellValue("Sheet1", "A1", "公司名称") f.SetCellValue("Sheet1", "B1", "Sn") f.SetCellValue("Sheet1", "C1", "设备名称") f.SetCellValue("Sheet1", "D1", "传感器") f.SetCellValue("Sheet1", "E1", "报警内容") f.SetCellValue("Sheet1", "F1", "记录时间") f.SetCellValue("Sheet1", "G1", "处理") f.SetCellValue("Sheet1", "H1", "处理时间") // 设置列宽 f.SetColWidth("Sheet1", "A", "A", 20) f.SetColWidth("Sheet1", "B", "B", 40) f.SetColWidth("Sheet1", "C", "C", 25) f.SetColWidth("Sheet1", "D", "D", 30) f.SetColWidth("Sheet1", "E", "E", 30) f.SetColWidth("Sheet1", "F", "F", 30) f.SetColWidth("Sheet1", "J", "J", 15) f.SetColWidth("Sheet1", "H", "H", 30) f.SetColWidth("Sheet1", "I", "I", 15) line := 1 // 循环写入数据 for _, v := range Device_data { line++ f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp)) f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s", v.T_pid_name)) f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id)) f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_D_name) f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_DS_name) f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Remark) f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.T_Ut) f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_Text) f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.UpdateTime) } timeStr := time.Now().Format("20060102150405") // 保存文件 if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil { logs.Error(err) } url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"} c.ServeJSON() return } // 删除文件 err = os.Remove("ofile/" + timeStr + ".xlsx") if err != nil { logs.Error(err) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url} c.ServeJSON() return } func (c *DeviceController) CompanyWarning_Data_Excel2() { T_name := c.GetString("T_name") T_tp := c.GetString("T_tp") Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_handle, _ := c.GetInt("T_handle") if len(Time_start) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"} c.ServeJSON() return } if len(Time_end) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"} c.ServeJSON() return } tpCount := Warning.Read_WarningType_Count() var tpList []string if len(T_tp) > 0 { tpList = lib.SplitStringIds(T_tp, "T") if len(tpList) == int(tpCount) { // 类型全选,替换为空,不进行查询筛选(查询全部) tpList = []string{} } } else { power, _ := Account.Read_Power_ById(c.Admin_r.T_power) if power.T_warning != "*" { tpList = lib.SplitStringIds(power.T_warning, "W") } } countRedisKey := fmt.Sprintf("Company_Warning_Count_%d_%s_%s_%s_%s_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0) Account.Read_Company_All_Maps() Company_r, err := Account.Read_Company_ById(c.T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path) var Device_data []Warning.CompanyWarning_R // 获取最新 Device_data, _ = Warning.Gorm_Read_Company_Warning_List_Excel(T_pids, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, c.T_pid, countRedisKey) f := excelize.NewFile() // 设置单元格的值 // 这里设置表头 f.SetCellValue("Sheet1", "A1", "报警类型") f.SetCellValue("Sheet1", "B1", "公司名称") f.SetCellValue("Sheet1", "C1", "Sn") f.SetCellValue("Sheet1", "D1", "设备名称") f.SetCellValue("Sheet1", "E1", "传感器") f.SetCellValue("Sheet1", "F1", "报警内容") f.SetCellValue("Sheet1", "G1", "记录时间") f.SetCellValue("Sheet1", "H1", "处理") f.SetCellValue("Sheet1", "I1", "处理时间") // 设置列宽 f.SetColWidth("Sheet1", "A", "A", 20) f.SetColWidth("Sheet1", "B", "B", 40) f.SetColWidth("Sheet1", "C", "C", 25) f.SetColWidth("Sheet1", "D", "D", 30) f.SetColWidth("Sheet1", "E", "E", 30) f.SetColWidth("Sheet1", "F", "F", 30) f.SetColWidth("Sheet1", "J", "J", 15) f.SetColWidth("Sheet1", "H", "H", 30) f.SetColWidth("Sheet1", "I", "I", 15) line := 1 // 循环写入数据 for _, v := range Device_data { line++ f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp)) f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s", v.T_pid_name)) f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id)) f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_D_name) f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_DS_name) f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Remark) f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Ut) f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_Text) f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.UpdateTime) } timeStr := time.Now().Format("20060102150405") // 保存文件 if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil { logs.Error(err) } url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"} c.ServeJSON() return } // 删除文件 err = os.Remove("ofile/" + timeStr + ".xlsx") if err != nil { logs.Error(err) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url} c.ServeJSON() return } // 告警提示列表 func (c *DeviceController) DeviceWarningList_T_Tips() { c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All_T_Notice_mechanism()} c.ServeJSON() return } // 告警类型列表 func (c *DeviceController) WarningType_List_All() { c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All()} c.ServeJSON() return } // 告警类型列表 - 权限关联列表 func (c *DeviceController) WarningType_Power_List_All() { power, err := Account.Read_Power_ById(c.Admin_r.T_power) if err != nil { c.Data["json"] = lib.JSONS{Code: 200, Msg: "获取菜单失败"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_Power_All(power.T_warning)} c.ServeJSON() return } // 通过传感器类型获取报警列表 func (c *DeviceController) Read_Warning_List_By_DS_T_type() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_type, _ := c.GetInt("T_type") T_name := c.GetString("T_name") bindSN, err := Account.Read_UserDevice_List(*c.Admin_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } var T_pids []int if c.Admin_r.T_pid > 0 { // 查询公司 Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } // 查询公司下面所有子公司id T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path) } var tpList []string power, _ := Account.Read_Power_ById(c.Admin_r.T_power) if power.T_warning != "*" { tpList = lib.SplitStringIds(power.T_warning, "W") } countRedisKey := fmt.Sprintf("Company_Warning_Count_By_DS_T_type_%d_%s_%d_%d", c.T_pid, T_name, T_type, c.Admin_r.T_pid) r_jsons.Data, r_jsons.Num = Warning.Read_Warning_List_By_DS_T_type(c.Admin_r, c.T_pid, bindSN, T_pids, T_type, T_name, tpList, page, page_z, countRedisKey) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *DeviceController) Read_WarningSend_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_ntype, _ := c.GetInt("T_ntype") r_jsons.Data, r_jsons.Num = Warning.Read_WarningSand_List(c.T_pid, T_ntype, page, page_z) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *DeviceController) Read_WarningRate_List() { page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_uuid := c.GetString("T_uuid") T_year, _ := c.GetInt("T_year") T_month, _ := c.GetInt("T_month") var startTime, endTime string now := time.Now() var month time.Month // 默认本月 if T_year == 0 { T_year = now.Year() } if T_month == 0 { month = now.Month() } else { month = time.Month(T_month) } T_type := Warning.WarningRateDay if T_month == 0 { T_type = Warning.WarningRateMonth } if T_type == Warning.WarningRateDay { startTime = time.Date(T_year, month, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02") endTime = time.Date(T_year, month+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02") } // 默认本年 if T_type == Warning.WarningRateMonth { startTime = time.Date(T_year, 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01") endTime = time.Date(T_year, 12, 31, 0, 0, 0, 0, time.Local).Format("2006-01") } if len(T_uuid) == 0 { T_uuid = c.Admin_r.T_uuid } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningRate_List(T_uuid, T_type, startTime, endTime)} c.ServeJSON() return } func (c *DeviceController) Read_WarningHandle_List() { c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningHandle_List()} c.ServeJSON() return } func Cron_WarningRate() { // 创建一个定时任务对象 c := cron.New(cron.WithSeconds()) // 给对象增加定时任务 // @daily 每日运行一次 // c.AddFunc("0 */1 * * * ?", Cron_WarningRateDay_Add) // c.AddFunc("0 */1 * * * ?", Cron_WarningRateMonth_Add) c.AddFunc("@daily", Cron_WarningRateDay_Add) c.AddFunc("@monthly", Cron_WarningRateMonth_Add) // 启动定时任务 c.Start() defer c.Stop() // 查询语句,阻塞,让main函数不退出,保持程序运行 select {} } // 保存每天的设备报警率 func Cron_WarningRateDay_Add() { T_date := time.Now().AddDate(0, 0, -1).Format("2006-01-02") logs.Info("开始统计" + T_date + "设备报警率") // 获取管理员用户列表 adminList := Account.Read_Admin_List_All() // 获取内部用户关联的pid for _, admin := range adminList { // 内部用户已绑定公司,* 绑定所有公司 if len(admin.T_pids) == 0 || admin.T_pids == "*" { continue } // 获取排除后的pid T_pids := lib.SplitStringToIntIds(admin.T_pids, "P") if len(T_pids) == 0 { continue } pids := Account.ReadCompanyWarningIds_T_pids(T_pids) if len(pids) == 0 { continue } // 获取探头数量 dsCnt := Device.Read_DeviceSensorCount_ByT_pids(pids) // 获取当天报警数量 warningCount := Warning.Read_WarningCount_byDay(pids) T_rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(warningCount)/float64(dsCnt)), 64) // 保留2位小数 // 保存设备报警率 warningRate := Warning.WarningRate{ T_uuid: admin.T_uuid, T_date: T_date, T_type: Warning.WarningRateDay, T_device_num: dsCnt, T_warning_num: warningCount, T_rate: float32(T_rate), } Warning.Add_WarningRate(warningRate) } } // 保存每月的设备报警率 func Cron_WarningRateMonth_Add() { now := time.Now() // fixme 测试 //now := time.Now().AddDate(0, 1, 0) T_date := now.AddDate(0, -1, 0).Format("2006-01") firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local) startTime := firstOfMonth.AddDate(0, -1, 0).Format("2006-01-02") // 上月第一天 endTime := firstOfMonth.AddDate(0, 0, -1).Format("2006-01-02") // 上月最后一天 logs.Info("开始统计" + T_date + "设备报警率") logs.Info("开始时间" + startTime) logs.Info("结束时间" + endTime) rateAvgList := Warning.Read_WarningRateAvg_Month(startTime, endTime) // 获取内部用户关联的pid for _, rate := range rateAvgList { T_rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(rate.T_warning_num)/float64(rate.T_device_num)), 64) // 保留2位小数 // 保存设备报警率 warningRate := Warning.WarningRate{ T_uuid: rate.T_uuid, T_date: T_date, T_type: Warning.WarningRateMonth, T_device_num: rate.T_device_num, T_warning_num: rate.T_warning_num, T_rate: float32(T_rate), } Warning.Add_WarningRate(warningRate) } }