Warning.go 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. db "Cold_Api/initialize"
  7. "Cold_Api/models/Account"
  8. "Cold_Api/models/Device"
  9. "Cold_Api/models/System"
  10. "Cold_Api/models/Warning"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/beego/beego/v2/core/logs"
  14. "github.com/robfig/cron/v3"
  15. "github.com/xuri/excelize/v2"
  16. "math"
  17. "net/http"
  18. "os"
  19. "strconv"
  20. "time"
  21. )
  22. // 设备告警 ------------------------------------------
  23. // 告警列表
  24. func (c *DeviceController) DeviceWarning_List() {
  25. type R_JSONS struct {
  26. //必须的大写开头
  27. Data []Warning.Warning_R
  28. Num int64
  29. Page int
  30. Page_size int
  31. }
  32. var r_jsons R_JSONS
  33. page, _ := c.GetInt("page")
  34. if page < 1 {
  35. page = 1
  36. }
  37. page_z, _ := c.GetInt("page_z")
  38. if page_z < 1 {
  39. page_z = conf.Page_size
  40. }
  41. T_name := c.GetString("T_name")
  42. T_tp := c.GetString("T_tp")
  43. Time_start := c.GetString("Time_start")
  44. Time_end := c.GetString("Time_end")
  45. T_handle, _ := c.GetInt("T_handle")
  46. T_admin, _ := c.GetInt("T_admin")
  47. T_history, _ := c.GetInt("T_history")
  48. tpCount := Warning.Read_WarningType_Count()
  49. var tpList []string
  50. if len(T_tp) > 0 {
  51. tpList = lib.SplitStringIds(T_tp, "T")
  52. if len(tpList) == int(tpCount) {
  53. // 类型全选,替换为空,不进行查询筛选(查询全部)
  54. tpList = []string{}
  55. }
  56. } else {
  57. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  58. if power.T_warning != "*" {
  59. tpList = lib.SplitStringIds(power.T_warning, "W")
  60. }
  61. }
  62. 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)
  63. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  64. if err != nil {
  65. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  66. c.ServeJSON()
  67. return
  68. }
  69. var T_year, T_month string
  70. if T_history == 1 {
  71. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  72. if err != nil {
  73. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  74. c.ServeJSON()
  75. return
  76. }
  77. T_year, T_month = date.Format("2006"), date.Format("01")
  78. }
  79. // 管理员界面
  80. if T_admin == 1 {
  81. if T_history == 1 {
  82. // 获取备份
  83. 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)
  84. } else {
  85. // 获取最新
  86. 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)
  87. }
  88. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  89. c.ServeJSON()
  90. return
  91. }
  92. // 公司报警界面
  93. if T_history == 1 {
  94. 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)
  95. } else {
  96. // 获取最新
  97. 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)
  98. }
  99. r_jsons.Page = page
  100. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  101. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  102. c.ServeJSON()
  103. return
  104. }
  105. func (c *DeviceController) DeviceWarning_List2() {
  106. type R_JSONS struct {
  107. //必须的大写开头
  108. Data []Warning.Warning_R
  109. Num int64
  110. Page int
  111. Page_size int
  112. }
  113. var r_jsons R_JSONS
  114. page, _ := c.GetInt("page")
  115. if page < 1 {
  116. page = 1
  117. }
  118. page_z, _ := c.GetInt("page_z")
  119. if page_z < 1 {
  120. page_z = conf.Page_size
  121. }
  122. T_name := c.GetString("T_name")
  123. T_tp := c.GetString("T_tp")
  124. Time_start := c.GetString("Time_start")
  125. Time_end := c.GetString("Time_end")
  126. T_handle, _ := c.GetInt("T_handle")
  127. T_admin, _ := c.GetInt("T_admin")
  128. T_history, _ := c.GetInt("T_history")
  129. T_abandon, _ := c.GetInt("T_abandon") // 1 弃用设备
  130. if len(Time_start) == 0 {
  131. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"}
  132. c.ServeJSON()
  133. return
  134. }
  135. if len(Time_end) == 0 {
  136. c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"}
  137. c.ServeJSON()
  138. return
  139. }
  140. tpCount := Warning.Read_WarningType_Count()
  141. var tpList []string
  142. if len(T_tp) > 0 {
  143. tpList = lib.SplitStringIds(T_tp, "T")
  144. if len(tpList) == int(tpCount) {
  145. // 类型全选,替换为空,不进行查询筛选(查询全部)
  146. tpList = []string{}
  147. }
  148. } else {
  149. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  150. if power.T_warning != "*" {
  151. tpList = lib.SplitStringIds(power.T_warning, "W")
  152. }
  153. }
  154. 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)
  155. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  156. if err != nil {
  157. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  158. c.ServeJSON()
  159. return
  160. }
  161. // 查询弃用设备
  162. abandonDeviceList, _ := Device.Read_Device_List_ByT_State(bindSN, c.T_pid, 0)
  163. abandonSN := make([]string, 0)
  164. for _, r := range abandonDeviceList {
  165. abandonSN = append(abandonSN, r.T_sn)
  166. }
  167. // 管理员界面
  168. if T_admin == 1 {
  169. // 获取最新 获取绑定的sn 如果未绑定则获取全部
  170. 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)
  171. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  172. c.ServeJSON()
  173. return
  174. }
  175. 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)
  176. r_jsons.Page = page
  177. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  178. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  179. c.ServeJSON()
  180. return
  181. }
  182. func (c *DeviceController) DeviceWarning_List_Sse() {
  183. page, _ := c.GetInt("page")
  184. if page < 1 {
  185. page = 1
  186. }
  187. page_z, _ := c.GetInt("page_z")
  188. if page_z < 1 {
  189. page_z = conf.Page_size
  190. }
  191. T_name := c.GetString("T_name")
  192. T_tp := c.GetString("T_tp")
  193. Time_start := c.GetString("Time_start")
  194. Time_end := c.GetString("Time_end")
  195. T_handle, _ := c.GetInt("T_handle")
  196. T_admin, _ := c.GetInt("T_admin")
  197. T_abandon, _ := c.GetInt("T_abandon") // 1 弃用设备
  198. tpCount := Warning.Read_WarningType_Count()
  199. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  200. if err != nil {
  201. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  202. c.ServeJSON()
  203. return
  204. }
  205. // 查询弃用设备
  206. abandonDeviceList, _ := Device.Read_Device_List_ByT_State(bindSN, c.T_pid, 0)
  207. abandonSN := make([]string, 0)
  208. for _, r := range abandonDeviceList {
  209. abandonSN = append(abandonSN, r.T_sn)
  210. }
  211. var tpList []string
  212. if len(T_tp) > 0 {
  213. tpList = lib.SplitStringIds(T_tp, "T")
  214. if len(tpList) == int(tpCount) {
  215. // 类型全选,替换为空,不进行查询筛选(查询全部)
  216. tpList = []string{}
  217. }
  218. } else {
  219. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  220. if power.T_warning != "*" {
  221. tpList = lib.SplitStringIds(power.T_warning, "W")
  222. }
  223. }
  224. 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)
  225. tableNames := Warning.GenerateWarningTableNames(Time_start, Time_end)
  226. // 设置响应头
  227. c.Ctx.ResponseWriter.Header().Set("Content-Type", "text/event-stream")
  228. c.Ctx.ResponseWriter.Header().Set("Cache-Control", "no-cache")
  229. c.Ctx.ResponseWriter.Header().Set("Connection", "keep-alive")
  230. c.Ctx.ResponseWriter.WriteHeader(http.StatusOK)
  231. var cnt int64
  232. tableCount := make(map[string]int64)
  233. // 管理员界面
  234. if T_admin == 1 {
  235. gormReadWarningListScopes := Warning.GormReadAdminWarningListScopes(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN)
  236. cnt, tableCount, err = Warning.Redis_WarningCount_GetOne(c.Admin_r.Id, countRedisKey)
  237. offset := (page - 1) * page_z
  238. remaining := offset
  239. var warningsCount int
  240. for _, tableName := range tableNames {
  241. tableDB := db.DB.Table(tableName)
  242. var count int64
  243. if tableName == fmt.Sprintf("warning_%d_%02d", time.Now().Year(), time.Now().Month()) {
  244. if saveTime, ok := tableCount["SaveTime"]; ok {
  245. t1 := time.Unix(saveTime, 0)
  246. if time.Now().Sub(t1) < 5*time.Minute {
  247. if count, ok = tableCount[tableName]; !ok {
  248. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  249. tableCount[tableName] = count
  250. }
  251. } else {
  252. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  253. tableCount[tableName] = count
  254. }
  255. }
  256. } else {
  257. var ok bool
  258. if count, ok = tableCount[tableName]; !ok {
  259. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  260. tableCount[tableName] = count
  261. }
  262. }
  263. cnt += count
  264. if remaining >= int(count) {
  265. remaining -= int(count)
  266. continue
  267. }
  268. var tableWarning []Warning.Warning
  269. if warningsCount < page_z {
  270. tableDB.Scopes(gormReadWarningListScopes).
  271. Order("t__ut desc").Offset(remaining).Limit(page_z - warningsCount).Find(&tableWarning)
  272. }
  273. warningsCount += len(tableWarning)
  274. remaining = 0
  275. var r_jsons lib.R_JSONS
  276. r_jsons.Num = cnt
  277. r_jsons.Data = tableWarning
  278. json_b, _ := json.Marshal(r_jsons)
  279. c.Ctx.ResponseWriter.Write(json_b)
  280. c.Ctx.ResponseWriter.Flush()
  281. // 可以根据需要调整间隔时间
  282. time.Sleep(1 * time.Second)
  283. }
  284. Warning.Redis_WarningCount_Set(c.Admin_r.Id, countRedisKey, cnt, tableCount)
  285. return
  286. }
  287. //从redis获取报警统计数量
  288. gormReadWarningListScopes := Warning.GormReadWarningListScopes(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN)
  289. cnt, tableCount, err = Warning.Redis_WarningCount_GetOne(c.T_pid, countRedisKey)
  290. offset := (page - 1) * page_z
  291. remaining := offset
  292. var warningsCount int
  293. for _, tableName := range tableNames {
  294. tableDB := db.DB.Table(tableName)
  295. var count int64
  296. if tableName == fmt.Sprintf("warning_%d_%02d", time.Now().Year(), time.Now().Month()) {
  297. if saveTime, ok := tableCount["SaveTime"]; ok {
  298. t1 := time.Unix(saveTime, 0)
  299. if time.Now().Sub(t1) < 5*time.Minute {
  300. if count, ok = tableCount[tableName]; !ok {
  301. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  302. tableCount[tableName] = count
  303. }
  304. } else {
  305. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  306. tableCount[tableName] = count
  307. }
  308. }
  309. } else {
  310. var ok bool
  311. if count, ok = tableCount[tableName]; !ok {
  312. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  313. tableCount[tableName] = count
  314. }
  315. }
  316. cnt += count
  317. if remaining >= int(count) {
  318. remaining -= int(count)
  319. continue
  320. }
  321. var tableWarning []Warning.Warning
  322. if warningsCount < page_z {
  323. tableDB.Scopes(gormReadWarningListScopes).
  324. Order("t__ut desc").Offset(remaining).Limit(page_z - warningsCount).Find(&tableWarning)
  325. }
  326. warningsCount += len(tableWarning)
  327. remaining = 0
  328. var r_jsons lib.R_JSONS
  329. r_jsons.Num = cnt
  330. r_jsons.Data = tableWarning
  331. json_b, _ := json.Marshal(r_jsons)
  332. c.Ctx.ResponseWriter.Write(json_b)
  333. c.Ctx.ResponseWriter.Flush()
  334. // 可以根据需要调整间隔时间
  335. time.Sleep(1 * time.Second)
  336. }
  337. Warning.Redis_WarningCount_Set(c.T_pid, countRedisKey, cnt, tableCount)
  338. }
  339. func (c *DeviceController) CompanyWarning_List() {
  340. var r_jsons lib.R_JSONS
  341. page, _ := c.GetInt("page")
  342. if page < 1 {
  343. page = 1
  344. }
  345. page_z, _ := c.GetInt("page_z")
  346. if page_z < 1 {
  347. page_z = conf.Page_size
  348. }
  349. T_name := c.GetString("T_name")
  350. T_tp := c.GetString("T_tp")
  351. Time_start := c.GetString("Time_start")
  352. Time_end := c.GetString("Time_end")
  353. T_handle, _ := c.GetInt("T_handle")
  354. T_history, _ := c.GetInt("T_history")
  355. tpCount := Warning.Read_WarningType_Count()
  356. var tpList []string
  357. if len(T_tp) > 0 {
  358. tpList = lib.SplitStringIds(T_tp, "T")
  359. if len(tpList) == int(tpCount) {
  360. // 类型全选,替换为空,不进行查询筛选(查询全部)
  361. tpList = []string{}
  362. }
  363. } else {
  364. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  365. if power.T_warning != "*" {
  366. tpList = lib.SplitStringIds(power.T_warning, "W")
  367. }
  368. }
  369. 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)
  370. Company_r, err := Account.Read_Company_ById(c.T_pid)
  371. if err != nil {
  372. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  373. c.ServeJSON()
  374. return
  375. }
  376. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  377. var T_year, T_month string
  378. if T_history == 1 {
  379. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  380. if err != nil {
  381. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  382. c.ServeJSON()
  383. return
  384. }
  385. T_year, T_month = date.Format("2006"), date.Format("01")
  386. }
  387. Account.Read_Company_All_Maps()
  388. if T_history == 1 {
  389. 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)
  390. } else {
  391. // 获取最新
  392. 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)
  393. }
  394. r_jsons.Page = page
  395. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  396. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  397. c.ServeJSON()
  398. return
  399. }
  400. func (c *DeviceController) CompanyWarning_List2() {
  401. var r_jsons lib.R_JSONS
  402. page, _ := c.GetInt("page")
  403. if page < 1 {
  404. page = 1
  405. }
  406. page_z, _ := c.GetInt("page_z")
  407. if page_z < 1 {
  408. page_z = conf.Page_size
  409. }
  410. T_name := c.GetString("T_name")
  411. T_tp := c.GetString("T_tp")
  412. Time_start := c.GetString("Time_start")
  413. Time_end := c.GetString("Time_end")
  414. T_handle, _ := c.GetInt("T_handle")
  415. if len(Time_start) == 0 {
  416. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"}
  417. c.ServeJSON()
  418. return
  419. }
  420. if len(Time_end) == 0 {
  421. c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"}
  422. c.ServeJSON()
  423. return
  424. }
  425. tpCount := Warning.Read_WarningType_Count()
  426. var tpList []string
  427. if len(T_tp) > 0 {
  428. tpList = lib.SplitStringIds(T_tp, "T")
  429. if len(tpList) == int(tpCount) {
  430. // 类型全选,替换为空,不进行查询筛选(查询全部)
  431. tpList = []string{}
  432. }
  433. } else {
  434. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  435. if power.T_warning != "*" {
  436. tpList = lib.SplitStringIds(power.T_warning, "W")
  437. }
  438. }
  439. 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)
  440. Company_r, err := Account.Read_Company_ById(c.T_pid)
  441. if err != nil {
  442. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  443. c.ServeJSON()
  444. return
  445. }
  446. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  447. Account.Read_Company_All_Maps()
  448. // 获取最新
  449. 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)
  450. r_jsons.Page = page
  451. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  452. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  453. c.ServeJSON()
  454. return
  455. }
  456. // 查询告警 暂时没有用到
  457. func (c *DeviceController) DeviceWarning_Get() {
  458. id, _ := c.GetInt("T_id")
  459. T_time := c.GetString("T_time")
  460. if len(T_time) == 0 {
  461. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  462. c.ServeJSON()
  463. return
  464. }
  465. var T_year, T_month string
  466. date, err := time.Parse("2006-01-02 15:04:05", T_time)
  467. if err != nil {
  468. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  469. c.ServeJSON()
  470. return
  471. }
  472. T_year, T_month = date.Format("2006"), date.Format("01")
  473. Wtab := "warning"
  474. Wtab += "_" + T_year + "_" + T_month
  475. T, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  476. if err != nil {
  477. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  478. c.ServeJSON()
  479. return
  480. }
  481. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.WarningToWarning_R(0, T)}
  482. c.ServeJSON()
  483. return
  484. }
  485. // 编辑告警(处理告警)
  486. func (c *DeviceController) DeviceWarning_Post() {
  487. id, _ := c.GetInt("T_id")
  488. T_Text := c.GetString("T_Text")
  489. T_time := c.GetString("T_time")
  490. T_history, _ := c.GetInt("T_history") // TODO 后期废弃
  491. if len(T_time) == 0 {
  492. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  493. c.ServeJSON()
  494. return
  495. }
  496. var T_year, T_month string
  497. var warning Warning.Warning
  498. Wtab := "warning"
  499. if T_history == 1 {
  500. date, err := time.Parse("2006-01-02 15:04:05", T_time)
  501. if err != nil {
  502. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  503. c.ServeJSON()
  504. return
  505. }
  506. T_year, T_month = date.Format("2006"), date.Format("01")
  507. Wtab += "_" + T_year + "_" + T_month
  508. warning, err = Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  509. if err != nil && err.Error() != "<QuerySeter> no row found" {
  510. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  511. c.ServeJSON()
  512. return
  513. }
  514. warning.T_Text = T_Text
  515. warning.T_State = 2
  516. warning.UpdateTime = time.Now()
  517. if is := Warning.Update_Warning_Backups(warning, T_year, T_month); !is {
  518. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  519. c.ServeJSON()
  520. return
  521. }
  522. warning = Warning.Read_Warning_ById(int64(id))
  523. if warning.Id > 0 {
  524. warning.T_Text = T_Text
  525. warning.T_State = 2
  526. warning.UpdateTime = time.Now()
  527. if is := Warning.Update_Warning(warning, "T_Text", "T_State", "UpdateTime"); !is {
  528. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  529. c.ServeJSON()
  530. return
  531. }
  532. }
  533. } else {
  534. // 使用新的查询方式后 warning表不做更新
  535. warning = Warning.Read_Warning_ById(int64(id))
  536. if warning.Id > 0 {
  537. warning.T_Text = T_Text
  538. warning.T_State = 2
  539. warning.UpdateTime = time.Now()
  540. if is := Warning.Update_Warning(warning, "T_Text", "T_State", "UpdateTime"); !is {
  541. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  542. c.ServeJSON()
  543. return
  544. }
  545. }
  546. // 同步处理历史数据
  547. date, err := time.Parse("2006-01-02 15:04:05", T_time)
  548. T_year, T_month = date.Format("2006"), date.Format("01")
  549. Wtab += "_" + T_year + "_" + T_month
  550. warningBackups, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  551. if err != nil && err.Error() != "<QuerySeter> no row found" {
  552. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  553. c.ServeJSON()
  554. return
  555. }
  556. if warningBackups.Id > 0 {
  557. warningBackups.T_Text = T_Text
  558. warningBackups.T_State = 2
  559. warningBackups.UpdateTime = time.Now()
  560. if is := Warning.Update_Warning_Backups(warningBackups, T_year, T_month); !is {
  561. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  562. c.ServeJSON()
  563. return
  564. }
  565. }
  566. }
  567. // 删除报警列表统计的缓存
  568. Warning.Redis_WarningCount_DelK(c.T_pid)
  569. Warning.Redis_WarningCount_DelK(c.Admin_r.Id)
  570. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警处理操作", Wtab+":"+strconv.Itoa(id)+"->"+T_Text)
  571. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  572. c.ServeJSON()
  573. return
  574. }
  575. // 删除告警
  576. func (c *DeviceController) DeviceWarning_Del() {
  577. id, _ := c.GetInt("T_id")
  578. T_time := c.GetString("T_time")
  579. T_history, _ := c.GetInt("T_history") // TODO 后期废弃
  580. if len(T_time) == 0 {
  581. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  582. c.ServeJSON()
  583. return
  584. }
  585. var T_year, T_month string
  586. var warning Warning.Warning
  587. Wtab := "warning"
  588. if T_history == 1 {
  589. date, err := time.Parse("2006-01-02 15:04:05", T_time)
  590. if err != nil {
  591. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  592. c.ServeJSON()
  593. return
  594. }
  595. T_year, T_month = date.Format("2006"), date.Format("01")
  596. Wtab += "_" + T_year + "_" + T_month
  597. warningBackups, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  598. if err != nil && err.Error() != "<QuerySeter> no row found" {
  599. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  600. c.ServeJSON()
  601. return
  602. }
  603. if warningBackups.Id > 0 {
  604. warning.T_State = 0
  605. warning.UpdateTime = time.Now()
  606. if is := Warning.Update_Warning_Backups(warning, T_year, T_month); !is {
  607. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  608. c.ServeJSON()
  609. return
  610. }
  611. }
  612. warning = Warning.Read_Warning_ById(int64(id))
  613. if warning.Id > 0 {
  614. warning.T_State = 0
  615. if is := Warning.Update_Warning(warning, "T_State"); !is {
  616. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  617. c.ServeJSON()
  618. return
  619. }
  620. }
  621. } else {
  622. // 删除主表
  623. warning = Warning.Read_Warning_ById(int64(id))
  624. if warning.Id > 0 {
  625. warning.T_State = 0
  626. if is := Warning.Update_Warning(warning, "T_State"); !is {
  627. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  628. c.ServeJSON()
  629. return
  630. }
  631. }
  632. // 删除分表
  633. date, err := time.Parse("2006-01-02 15:04:05", T_time)
  634. T_year, T_month = date.Format("2006"), date.Format("01")
  635. Wtab += "_" + T_year + "_" + T_month
  636. warningBackups, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  637. if err != nil && err.Error() != "<QuerySeter> no row found" {
  638. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  639. c.ServeJSON()
  640. return
  641. }
  642. if warningBackups.Id > 0 {
  643. warningBackups.T_State = 0
  644. warningBackups.UpdateTime = time.Now()
  645. if is := Warning.Update_Warning_Backups(warningBackups, T_year, T_month); !is {
  646. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  647. c.ServeJSON()
  648. return
  649. }
  650. }
  651. }
  652. // 删除报警列表统计的缓存
  653. Warning.Redis_WarningCount_DelK(c.T_pid)
  654. Warning.Redis_WarningCount_DelK(c.Admin_r.Id)
  655. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警删除操作", Wtab+":"+strconv.Itoa(id))
  656. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  657. c.ServeJSON()
  658. return
  659. }
  660. // 导出告警
  661. func (c *DeviceController) DeviceWarning_Data_Excel() {
  662. T_name := c.GetString("T_name")
  663. T_tp := c.GetString("T_tp")
  664. Time_start := c.GetString("Time_start")
  665. Time_end := c.GetString("Time_end")
  666. T_handle, _ := c.GetInt("T_handle")
  667. T_admin, _ := c.GetInt("T_admin")
  668. T_history, _ := c.GetInt("T_history")
  669. tpCount := Warning.Read_WarningType_Count()
  670. var tpList []string
  671. if len(T_tp) > 0 {
  672. tpList = lib.SplitStringIds(T_tp, "T")
  673. if len(tpList) == int(tpCount) {
  674. // 类型全选,替换为空,不进行查询筛选(查询全部)
  675. tpList = []string{}
  676. }
  677. } else {
  678. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  679. if power.T_warning != "*" {
  680. tpList = lib.SplitStringIds(power.T_warning, "W")
  681. }
  682. }
  683. 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)
  684. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  685. if err != nil {
  686. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  687. c.ServeJSON()
  688. return
  689. }
  690. var T_year, T_month string
  691. if T_history == 1 {
  692. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  693. if err != nil {
  694. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  695. c.ServeJSON()
  696. return
  697. }
  698. T_year, T_month = date.Format("2006"), date.Format("01")
  699. }
  700. var Device_data []Warning.Warning_R
  701. // 管理员界面
  702. if T_admin == 1 {
  703. if T_history == 1 {
  704. // 获取备份
  705. 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)
  706. } else {
  707. // 获取最新
  708. Device_data, _ = Warning.Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  709. }
  710. }
  711. if T_history == 1 {
  712. // 获取备份
  713. 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)
  714. } else {
  715. // 获取最新
  716. Device_data, _ = Warning.Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  717. }
  718. f := excelize.NewFile() // 设置单元格的值
  719. // 这里设置表头ÒÒ
  720. f.SetCellValue("Sheet1", "A1", "报警类型")
  721. f.SetCellValue("Sheet1", "B1", "Sn")
  722. f.SetCellValue("Sheet1", "C1", "设备名称")
  723. f.SetCellValue("Sheet1", "D1", "传感器")
  724. f.SetCellValue("Sheet1", "E1", "报警内容")
  725. f.SetCellValue("Sheet1", "F1", "记录时间")
  726. f.SetCellValue("Sheet1", "G1", "处理")
  727. f.SetCellValue("Sheet1", "H1", "处理时间")
  728. // 设置列宽
  729. f.SetColWidth("Sheet1", "A", "A", 20)
  730. f.SetColWidth("Sheet1", "B", "B", 25)
  731. f.SetColWidth("Sheet1", "C", "C", 30)
  732. f.SetColWidth("Sheet1", "D", "D", 30)
  733. f.SetColWidth("Sheet1", "G", "E", 30)
  734. f.SetColWidth("Sheet1", "H", "F", 15)
  735. f.SetColWidth("Sheet1", "I", "G", 30)
  736. f.SetColWidth("Sheet1", "J", "H", 15)
  737. line := 1
  738. // 循环写入数据
  739. for _, v := range Device_data {
  740. line++
  741. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  742. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  743. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_D_name)
  744. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_DS_name)
  745. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_Remark)
  746. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Ut)
  747. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Text)
  748. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.UpdateTime)
  749. }
  750. timeStr := time.Now().Format("20060102150405")
  751. // 保存文件
  752. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  753. logs.Error(err)
  754. }
  755. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  756. if !is {
  757. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  758. c.ServeJSON()
  759. return
  760. }
  761. // 删除文件
  762. err = os.Remove("ofile/" + timeStr + ".xlsx")
  763. if err != nil {
  764. logs.Error(err)
  765. }
  766. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  767. c.ServeJSON()
  768. return
  769. }
  770. func (c *DeviceController) DeviceWarning_Data_Excel2() {
  771. T_name := c.GetString("T_name")
  772. T_tp := c.GetString("T_tp")
  773. Time_start := c.GetString("Time_start")
  774. Time_end := c.GetString("Time_end")
  775. T_handle, _ := c.GetInt("T_handle")
  776. if len(Time_start) == 0 {
  777. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"}
  778. c.ServeJSON()
  779. return
  780. }
  781. if len(Time_end) == 0 {
  782. c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"}
  783. c.ServeJSON()
  784. return
  785. }
  786. T_admin, _ := c.GetInt("T_admin")
  787. T_abandon, _ := c.GetInt("T_abandon") // 1 弃用设备
  788. tpCount := Warning.Read_WarningType_Count()
  789. var tpList []string
  790. if len(T_tp) > 0 {
  791. tpList = lib.SplitStringIds(T_tp, "T")
  792. if len(tpList) == int(tpCount) {
  793. // 类型全选,替换为空,不进行查询筛选(查询全部)
  794. tpList = []string{}
  795. }
  796. } else {
  797. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  798. if power.T_warning != "*" {
  799. tpList = lib.SplitStringIds(power.T_warning, "W")
  800. }
  801. }
  802. 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)
  803. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  804. if err != nil {
  805. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  806. c.ServeJSON()
  807. return
  808. }
  809. // 查询弃用设备
  810. abandonDeviceList, _ := Device.Read_Device_List_ByT_State(bindSN, c.T_pid, 0)
  811. abandonSN := make([]string, 0)
  812. for _, r := range abandonDeviceList {
  813. abandonSN = append(abandonSN, r.T_sn)
  814. }
  815. var Device_data []Warning.Warning_R
  816. // 管理员界面
  817. if T_admin == 1 {
  818. // 获取最新
  819. //Device_data, _ = Warning.Gorm_Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  820. 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)
  821. } else {
  822. // 获取最新
  823. //Device_data, _ = Warning.Gorm_Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  824. 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)
  825. }
  826. f := excelize.NewFile() // 设置单元格的值
  827. // 这里设置表头ÒÒ
  828. f.SetCellValue("Sheet1", "A1", "报警类型")
  829. f.SetCellValue("Sheet1", "B1", "Sn")
  830. f.SetCellValue("Sheet1", "C1", "设备名称")
  831. f.SetCellValue("Sheet1", "D1", "传感器")
  832. f.SetCellValue("Sheet1", "E1", "报警内容")
  833. f.SetCellValue("Sheet1", "F1", "记录时间")
  834. f.SetCellValue("Sheet1", "G1", "处理")
  835. f.SetCellValue("Sheet1", "H1", "处理时间")
  836. // 设置列宽
  837. f.SetColWidth("Sheet1", "A", "A", 20)
  838. f.SetColWidth("Sheet1", "B", "B", 25)
  839. f.SetColWidth("Sheet1", "C", "C", 30)
  840. f.SetColWidth("Sheet1", "D", "D", 30)
  841. f.SetColWidth("Sheet1", "G", "E", 30)
  842. f.SetColWidth("Sheet1", "H", "F", 15)
  843. f.SetColWidth("Sheet1", "I", "G", 30)
  844. f.SetColWidth("Sheet1", "J", "H", 15)
  845. line := 1
  846. // 循环写入数据
  847. for _, v := range Device_data {
  848. line++
  849. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  850. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  851. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_D_name)
  852. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_DS_name)
  853. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_Remark)
  854. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Ut)
  855. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Text)
  856. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.UpdateTime)
  857. }
  858. timeStr := time.Now().Format("20060102150405")
  859. // 保存文件
  860. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  861. logs.Error(err)
  862. }
  863. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  864. if !is {
  865. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  866. c.ServeJSON()
  867. return
  868. }
  869. // 删除文件
  870. err = os.Remove("ofile/" + timeStr + ".xlsx")
  871. if err != nil {
  872. logs.Error(err)
  873. }
  874. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  875. c.ServeJSON()
  876. return
  877. }
  878. // 公司管理 - 导出告警
  879. func (c *DeviceController) CompanyWarning_Data_Excel() {
  880. T_name := c.GetString("T_name")
  881. T_tp := c.GetString("T_tp")
  882. Time_start := c.GetString("Time_start")
  883. Time_end := c.GetString("Time_end")
  884. T_handle, _ := c.GetInt("T_handle")
  885. T_history, _ := c.GetInt("T_history")
  886. tpCount := Warning.Read_WarningType_Count()
  887. var tpList []string
  888. if len(T_tp) > 0 {
  889. tpList = lib.SplitStringIds(T_tp, "T")
  890. if len(tpList) == int(tpCount) {
  891. // 类型全选,替换为空,不进行查询筛选(查询全部)
  892. tpList = []string{}
  893. }
  894. } else {
  895. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  896. if power.T_warning != "*" {
  897. tpList = lib.SplitStringIds(power.T_warning, "W")
  898. }
  899. }
  900. 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)
  901. Account.Read_Company_All_Maps()
  902. Company_r, err := Account.Read_Company_ById(c.T_pid)
  903. if err != nil {
  904. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  905. c.ServeJSON()
  906. return
  907. }
  908. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  909. var T_year, T_month string
  910. if T_history == 1 {
  911. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  912. if err != nil {
  913. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  914. c.ServeJSON()
  915. return
  916. }
  917. T_year, T_month = date.Format("2006"), date.Format("01")
  918. }
  919. var Device_data []Warning.CompanyWarning_R
  920. if T_history == 1 {
  921. // 获取备份
  922. 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)
  923. } else {
  924. // 获取最新
  925. Device_data, _ = Warning.Read_Company_Warning_List(T_pids, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, c.T_pid, countRedisKey)
  926. }
  927. f := excelize.NewFile() // 设置单元格的值
  928. // 这里设置表头
  929. f.SetCellValue("Sheet1", "A1", "报警类型")
  930. f.SetCellValue("Sheet1", "A1", "公司名称")
  931. f.SetCellValue("Sheet1", "B1", "Sn")
  932. f.SetCellValue("Sheet1", "C1", "设备名称")
  933. f.SetCellValue("Sheet1", "D1", "传感器")
  934. f.SetCellValue("Sheet1", "E1", "报警内容")
  935. f.SetCellValue("Sheet1", "F1", "记录时间")
  936. f.SetCellValue("Sheet1", "G1", "处理")
  937. f.SetCellValue("Sheet1", "H1", "处理时间")
  938. // 设置列宽
  939. f.SetColWidth("Sheet1", "A", "A", 20)
  940. f.SetColWidth("Sheet1", "B", "B", 40)
  941. f.SetColWidth("Sheet1", "C", "C", 25)
  942. f.SetColWidth("Sheet1", "D", "D", 30)
  943. f.SetColWidth("Sheet1", "E", "E", 30)
  944. f.SetColWidth("Sheet1", "F", "F", 30)
  945. f.SetColWidth("Sheet1", "J", "J", 15)
  946. f.SetColWidth("Sheet1", "H", "H", 30)
  947. f.SetColWidth("Sheet1", "I", "I", 15)
  948. line := 1
  949. // 循环写入数据
  950. for _, v := range Device_data {
  951. line++
  952. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  953. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s", v.T_pid_name))
  954. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  955. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_D_name)
  956. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_DS_name)
  957. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Remark)
  958. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.T_Ut)
  959. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_Text)
  960. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.UpdateTime)
  961. }
  962. timeStr := time.Now().Format("20060102150405")
  963. // 保存文件
  964. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  965. logs.Error(err)
  966. }
  967. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  968. if !is {
  969. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  970. c.ServeJSON()
  971. return
  972. }
  973. // 删除文件
  974. err = os.Remove("ofile/" + timeStr + ".xlsx")
  975. if err != nil {
  976. logs.Error(err)
  977. }
  978. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  979. c.ServeJSON()
  980. return
  981. }
  982. func (c *DeviceController) CompanyWarning_Data_Excel2() {
  983. T_name := c.GetString("T_name")
  984. T_tp := c.GetString("T_tp")
  985. Time_start := c.GetString("Time_start")
  986. Time_end := c.GetString("Time_end")
  987. T_handle, _ := c.GetInt("T_handle")
  988. if len(Time_start) == 0 {
  989. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"}
  990. c.ServeJSON()
  991. return
  992. }
  993. if len(Time_end) == 0 {
  994. c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"}
  995. c.ServeJSON()
  996. return
  997. }
  998. tpCount := Warning.Read_WarningType_Count()
  999. var tpList []string
  1000. if len(T_tp) > 0 {
  1001. tpList = lib.SplitStringIds(T_tp, "T")
  1002. if len(tpList) == int(tpCount) {
  1003. // 类型全选,替换为空,不进行查询筛选(查询全部)
  1004. tpList = []string{}
  1005. }
  1006. } else {
  1007. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  1008. if power.T_warning != "*" {
  1009. tpList = lib.SplitStringIds(power.T_warning, "W")
  1010. }
  1011. }
  1012. 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)
  1013. Account.Read_Company_All_Maps()
  1014. Company_r, err := Account.Read_Company_ById(c.T_pid)
  1015. if err != nil {
  1016. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1017. c.ServeJSON()
  1018. return
  1019. }
  1020. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1021. var Device_data []Warning.CompanyWarning_R
  1022. // 获取最新
  1023. 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)
  1024. f := excelize.NewFile() // 设置单元格的值
  1025. // 这里设置表头
  1026. f.SetCellValue("Sheet1", "A1", "报警类型")
  1027. f.SetCellValue("Sheet1", "B1", "公司名称")
  1028. f.SetCellValue("Sheet1", "C1", "Sn")
  1029. f.SetCellValue("Sheet1", "D1", "设备名称")
  1030. f.SetCellValue("Sheet1", "E1", "传感器")
  1031. f.SetCellValue("Sheet1", "F1", "报警内容")
  1032. f.SetCellValue("Sheet1", "G1", "记录时间")
  1033. f.SetCellValue("Sheet1", "H1", "处理")
  1034. f.SetCellValue("Sheet1", "I1", "处理时间")
  1035. // 设置列宽
  1036. f.SetColWidth("Sheet1", "A", "A", 20)
  1037. f.SetColWidth("Sheet1", "B", "B", 40)
  1038. f.SetColWidth("Sheet1", "C", "C", 25)
  1039. f.SetColWidth("Sheet1", "D", "D", 30)
  1040. f.SetColWidth("Sheet1", "E", "E", 30)
  1041. f.SetColWidth("Sheet1", "F", "F", 30)
  1042. f.SetColWidth("Sheet1", "J", "J", 15)
  1043. f.SetColWidth("Sheet1", "H", "H", 30)
  1044. f.SetColWidth("Sheet1", "I", "I", 15)
  1045. line := 1
  1046. // 循环写入数据
  1047. for _, v := range Device_data {
  1048. line++
  1049. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  1050. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s", v.T_pid_name))
  1051. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  1052. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_D_name)
  1053. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_DS_name)
  1054. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Remark)
  1055. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Ut)
  1056. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_Text)
  1057. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.UpdateTime)
  1058. }
  1059. timeStr := time.Now().Format("20060102150405")
  1060. // 保存文件
  1061. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1062. logs.Error(err)
  1063. }
  1064. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1065. if !is {
  1066. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1067. c.ServeJSON()
  1068. return
  1069. }
  1070. // 删除文件
  1071. err = os.Remove("ofile/" + timeStr + ".xlsx")
  1072. if err != nil {
  1073. logs.Error(err)
  1074. }
  1075. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1076. c.ServeJSON()
  1077. return
  1078. }
  1079. // 告警提示列表
  1080. func (c *DeviceController) DeviceWarningList_T_Tips() {
  1081. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All_T_Notice_mechanism()}
  1082. c.ServeJSON()
  1083. return
  1084. }
  1085. // 告警类型列表
  1086. func (c *DeviceController) WarningType_List_All() {
  1087. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All()}
  1088. c.ServeJSON()
  1089. return
  1090. }
  1091. // 告警类型列表 - 权限关联列表
  1092. func (c *DeviceController) WarningType_Power_List_All() {
  1093. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1094. if err != nil {
  1095. c.Data["json"] = lib.JSONS{Code: 200, Msg: "获取菜单失败"}
  1096. c.ServeJSON()
  1097. return
  1098. }
  1099. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_Power_All(power.T_warning)}
  1100. c.ServeJSON()
  1101. return
  1102. }
  1103. // 通过传感器类型获取报警列表
  1104. func (c *DeviceController) Read_Warning_List_By_DS_T_type() {
  1105. var r_jsons lib.R_JSONS
  1106. page, _ := c.GetInt("page")
  1107. if page < 1 {
  1108. page = 1
  1109. }
  1110. page_z, _ := c.GetInt("page_z")
  1111. if page_z < 1 {
  1112. page_z = conf.Page_size
  1113. }
  1114. T_type, _ := c.GetInt("T_type")
  1115. T_name := c.GetString("T_name")
  1116. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  1117. if err != nil {
  1118. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1119. c.ServeJSON()
  1120. return
  1121. }
  1122. var T_pids []int
  1123. if c.Admin_r.T_pid > 0 {
  1124. // 查询公司
  1125. Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
  1126. if err != nil {
  1127. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1128. c.ServeJSON()
  1129. return
  1130. }
  1131. // 查询公司下面所有子公司id
  1132. T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
  1133. }
  1134. var tpList []string
  1135. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  1136. if power.T_warning != "*" {
  1137. tpList = lib.SplitStringIds(power.T_warning, "W")
  1138. }
  1139. 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)
  1140. 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)
  1141. r_jsons.Page = page
  1142. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1143. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1144. c.ServeJSON()
  1145. return
  1146. }
  1147. func (c *DeviceController) Read_WarningSend_List() {
  1148. var r_jsons lib.R_JSONS
  1149. page, _ := c.GetInt("page")
  1150. if page < 1 {
  1151. page = 1
  1152. }
  1153. page_z, _ := c.GetInt("page_z")
  1154. if page_z < 1 {
  1155. page_z = conf.Page_size
  1156. }
  1157. T_ntype, _ := c.GetInt("T_ntype")
  1158. r_jsons.Data, r_jsons.Num = Warning.Read_WarningSand_List(c.T_pid, T_ntype, page, page_z)
  1159. r_jsons.Page = page
  1160. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1161. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1162. c.ServeJSON()
  1163. return
  1164. }
  1165. func (c *DeviceController) Read_WarningRate_List() {
  1166. page, _ := c.GetInt("page")
  1167. if page < 1 {
  1168. page = 1
  1169. }
  1170. page_z, _ := c.GetInt("page_z")
  1171. if page_z < 1 {
  1172. page_z = conf.Page_size
  1173. }
  1174. T_uuid := c.GetString("T_uuid")
  1175. T_year, _ := c.GetInt("T_year")
  1176. T_month, _ := c.GetInt("T_month")
  1177. var startTime, endTime string
  1178. now := time.Now()
  1179. var month time.Month
  1180. // 默认本月
  1181. if T_year == 0 {
  1182. T_year = now.Year()
  1183. }
  1184. if T_month == 0 {
  1185. month = now.Month()
  1186. } else {
  1187. month = time.Month(T_month)
  1188. }
  1189. T_type := Warning.WarningRateDay
  1190. if T_month == 0 {
  1191. T_type = Warning.WarningRateMonth
  1192. }
  1193. if T_type == Warning.WarningRateDay {
  1194. startTime = time.Date(T_year, month, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  1195. endTime = time.Date(T_year, month+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  1196. }
  1197. // 默认本年
  1198. if T_type == Warning.WarningRateMonth {
  1199. startTime = time.Date(T_year, 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  1200. endTime = time.Date(T_year, 12, 31, 0, 0, 0, 0, time.Local).Format("2006-01")
  1201. }
  1202. if len(T_uuid) == 0 {
  1203. T_uuid = c.Admin_r.T_uuid
  1204. }
  1205. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningRate_List(T_uuid, T_type, startTime, endTime)}
  1206. c.ServeJSON()
  1207. return
  1208. }
  1209. func (c *DeviceController) Read_WarningHandle_List() {
  1210. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningHandle_List()}
  1211. c.ServeJSON()
  1212. return
  1213. }
  1214. func Cron_WarningRate() {
  1215. // 创建一个定时任务对象
  1216. c := cron.New(cron.WithSeconds())
  1217. // 给对象增加定时任务
  1218. // @daily 每日运行一次
  1219. // c.AddFunc("0 */1 * * * ?", Cron_WarningRateDay_Add)
  1220. // c.AddFunc("0 */1 * * * ?", Cron_WarningRateMonth_Add)
  1221. c.AddFunc("@daily", Cron_WarningRateDay_Add)
  1222. c.AddFunc("@monthly", Cron_WarningRateMonth_Add)
  1223. // 启动定时任务
  1224. c.Start()
  1225. defer c.Stop()
  1226. // 查询语句,阻塞,让main函数不退出,保持程序运行
  1227. select {}
  1228. }
  1229. // 保存每天的设备报警率
  1230. func Cron_WarningRateDay_Add() {
  1231. T_date := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  1232. logs.Info("开始统计" + T_date + "设备报警率")
  1233. // 获取管理员用户列表
  1234. adminList := Account.Read_Admin_List_All()
  1235. // 获取内部用户关联的pid
  1236. for _, admin := range adminList {
  1237. // 内部用户已绑定公司,* 绑定所有公司
  1238. if len(admin.T_pids) == 0 || admin.T_pids == "*" {
  1239. continue
  1240. }
  1241. // 获取排除后的pid
  1242. T_pids := lib.SplitStringToIntIds(admin.T_pids, "P")
  1243. if len(T_pids) == 0 {
  1244. continue
  1245. }
  1246. pids := Account.ReadCompanyWarningIds_T_pids(T_pids)
  1247. if len(pids) == 0 {
  1248. continue
  1249. }
  1250. // 获取探头数量
  1251. dsCnt := Device.Read_DeviceSensorCount_ByT_pids(pids)
  1252. // 获取当天报警数量
  1253. warningCount := Warning.Read_WarningCount_byDay(pids)
  1254. T_rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(warningCount)/float64(dsCnt)), 64) // 保留2位小数
  1255. // 保存设备报警率
  1256. warningRate := Warning.WarningRate{
  1257. T_uuid: admin.T_uuid,
  1258. T_date: T_date,
  1259. T_type: Warning.WarningRateDay,
  1260. T_device_num: dsCnt,
  1261. T_warning_num: warningCount,
  1262. T_rate: float32(T_rate),
  1263. }
  1264. Warning.Add_WarningRate(warningRate)
  1265. }
  1266. }
  1267. // 保存每月的设备报警率
  1268. func Cron_WarningRateMonth_Add() {
  1269. now := time.Now()
  1270. // fixme 测试
  1271. //now := time.Now().AddDate(0, 1, 0)
  1272. T_date := now.AddDate(0, -1, 0).Format("2006-01")
  1273. firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)
  1274. startTime := firstOfMonth.AddDate(0, -1, 0).Format("2006-01-02") // 上月第一天
  1275. endTime := firstOfMonth.AddDate(0, 0, -1).Format("2006-01-02") // 上月最后一天
  1276. logs.Info("开始统计" + T_date + "设备报警率")
  1277. logs.Info("开始时间" + startTime)
  1278. logs.Info("结束时间" + endTime)
  1279. rateAvgList := Warning.Read_WarningRateAvg_Month(startTime, endTime)
  1280. // 获取内部用户关联的pid
  1281. for _, rate := range rateAvgList {
  1282. T_rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(rate.T_warning_num)/float64(rate.T_device_num)), 64) // 保留2位小数
  1283. // 保存设备报警率
  1284. warningRate := Warning.WarningRate{
  1285. T_uuid: rate.T_uuid,
  1286. T_date: T_date,
  1287. T_type: Warning.WarningRateMonth,
  1288. T_device_num: rate.T_device_num,
  1289. T_warning_num: rate.T_warning_num,
  1290. T_rate: float32(T_rate),
  1291. }
  1292. Warning.Add_WarningRate(warningRate)
  1293. }
  1294. }