1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453 |
- 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() != "<QuerySeter> 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() != "<QuerySeter> 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() != "<QuerySeter> 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() != "<QuerySeter> 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)
- }
- }
|