Warning.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  1. package Warning
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. db "Cold_Api/initialize"
  6. "Cold_Api/models/Account"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/astaxie/beego/cache"
  10. _ "github.com/astaxie/beego/cache/redis"
  11. "github.com/beego/beego/v2/adapter/orm"
  12. orm2 "github.com/beego/beego/v2/client/orm"
  13. "github.com/beego/beego/v2/core/logs"
  14. _ "github.com/go-sql-driver/mysql"
  15. "github.com/gomodule/redigo/redis"
  16. "gorm.io/gorm"
  17. "sort"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "time"
  22. )
  23. var (
  24. CompanyCallthepolice = "companyCallthepolice" // 公司管理-设备报警
  25. Callthepolice = "callthepolice" // 公司管理-设备报警
  26. )
  27. // 模板
  28. type Warning struct {
  29. Id int64 `orm:"column(ID);size(11);auto;pk" gorm:"column:ID"`
  30. T_pid int `orm:"size(256);null" gorm:"column:t_pid"` // Account.Company 绑定公司
  31. T_tp int `orm:"size(200);null" gorm:"column:t_tp"` // 报警类型 ->WarningList
  32. T_sn string `orm:"index;size(256);null" gorm:"column:t_sn"` // 设备序列号
  33. T_D_name string `orm:"size(256);null" gorm:"column:t__d_name"` // 设备名称
  34. T_id int `orm:"size(200);null" gorm:"column:t_id"` // 传感器 ID
  35. T_DS_name string `orm:"size(256);null" gorm:"column:t__d_s_name"` // 传感器名称
  36. T_Remark string `orm:"type(text);null" gorm:"column:t__remark"` // 采集内容
  37. T_Ut time.Time `orm:"type(timestamp);null;" gorm:"column:t__ut"` // 采集时间
  38. T_fUt time.Time `orm:"type(timestamp);null;" gorm:"column:t_f_ut"` // 首次采集时间
  39. T_Text string `orm:"type(text);null" gorm:"column:t__text"` // 处理备注
  40. T_Log string `orm:"type(text);null" gorm:"column:t__log"` // 通知日志
  41. T_Msid int64 `orm:"size(256);null" gorm:"column:t__msid"` // 消息ID
  42. T_State int `orm:"size(2);default(2)" gorm:"column:t__state"` // 0 删除 1 不处理 2 已处理 3 未处理
  43. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  44. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  45. }
  46. // 多字段索引
  47. func (u *Warning) TableIndex() [][]string {
  48. return [][]string{
  49. []string{"T_pid", "T_tp", "T_State"},
  50. }
  51. }
  52. /*
  53. 分表机制
  54. |--------30天--------|
  55. |-----8月---||--------7月---------||--------6月---------||--------5月---------|
  56. 每天备份 到对应 月份
  57. */
  58. // 模板
  59. type Warning_R struct {
  60. Id int64
  61. T_pid int // Account.Company 绑定公司
  62. T_tp int // 报警类型 ->WarningList
  63. T_tp_name string // 报警类型名称
  64. T_sn string // 设备序列号
  65. T_D_name string // 设备名称
  66. T_id int // 传感器 ID
  67. T_DS_name string // 传感器名称
  68. T_Remark string // 采集内容
  69. T_Ut string // 采集时间
  70. T_fUt string // 首次采集时间
  71. T_Text string // 处理备注
  72. T_Log []string // 处理日志
  73. T_Msid int64 // 消息ID
  74. T_State int // 0 删除 1 未处理 2 已处理
  75. T_history int // 0 40天 1 历史数据
  76. CreateTime string // 创建时间
  77. UpdateTime string // 更新时间
  78. }
  79. type CompanyWarning_R struct {
  80. Id int64
  81. T_pid int // Account.Company 绑定公司
  82. T_pid_name string // Account.Company 公司名称
  83. T_tp int // 报警类型 ->WarningList
  84. T_tp_name string // 报警类型名称
  85. T_sn string // 设备序列号
  86. T_D_name string // 设备名称
  87. T_id int // 传感器 ID
  88. T_DS_name string // 传感器名称
  89. T_Remark string // 采集内容
  90. T_Ut string // 采集时间
  91. T_fUt string // 首次采集时间
  92. T_Text string // 处理备注
  93. T_Log []string // 处理日志
  94. T_Msid int64 // 消息ID
  95. T_State int // 0 删除 1 未处理 2 已处理
  96. T_history int // 0 40天 1 历史数据
  97. CreateTime string // 创建时间
  98. UpdateTime string // 更新时间
  99. }
  100. type Warning_Applet struct {
  101. Id int64
  102. T_pid int // Account.Company 绑定公司
  103. T_company_name string // 公司名称
  104. T_tp int // 报警类型 ->WarningList
  105. T_tp_name string // 报警类型名称
  106. T_sn string // 设备序列号
  107. T_D_name string // 设备名称
  108. T_id int // 传感器 ID
  109. T_DS_name string // 传感器名称
  110. T_Remark string // 采集内容
  111. T_Ut string // 采集时间
  112. T_fUt string // 首次采集时间
  113. T_Text string // 处理备注
  114. T_Log []string // 处理日志
  115. T_Msid int64 // 消息ID
  116. T_State int // 0 删除 1 未处理 2 已处理
  117. CreateTime string // 创建时间
  118. }
  119. type Warning_Count struct {
  120. Key string
  121. Count int64
  122. TableCount map[string]int64
  123. }
  124. func (t *Warning) TableName() string {
  125. return "warning" // 数据库名称 // ************** 替换 FormulaList **************
  126. }
  127. var redis_Warning_Count cache.Cache
  128. func init() {
  129. //注册模型
  130. orm.RegisterModel(new(Warning))
  131. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  132. "redis_Warning_Count", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  133. fmt.Println(config)
  134. var err error
  135. redis_Warning_Count, err = cache.NewCache("redis", config)
  136. if err != nil || redis_Warning_Count == nil {
  137. errMsg := "failed to init redis"
  138. logs.Error(errMsg, err)
  139. panic(errMsg)
  140. }
  141. }
  142. func Redis_WarningCount_Set(id int, key string, count int64, TableCount map[string]int64) (err error) {
  143. //json序列化
  144. idKey := strconv.Itoa(id)
  145. TableCount["SaveTime"] = time.Now().Unix()
  146. warningCount, err := Redis_WarningCount_Get(idKey)
  147. warningCount = append(warningCount, Warning_Count{
  148. Key: key,
  149. Count: count,
  150. TableCount: TableCount,
  151. })
  152. str, err := json.Marshal(warningCount)
  153. if err != nil {
  154. logs.Error(lib.FuncName(), err)
  155. return
  156. }
  157. err = redis_Warning_Count.Put(idKey, str, 30*time.Minute)
  158. if err != nil {
  159. logs.Error("Redis_Admin_Set", "set key:", idKey, ",value:", str, err)
  160. }
  161. return
  162. }
  163. // if r,is :=Redis_Get(T_sn);is{
  164. // return r,nil
  165. // }
  166. func Redis_WarningCount_Get(userId string) (count []Warning_Count, err error) {
  167. if redis_Warning_Count.IsExist(userId) {
  168. //println("找到key:",key)
  169. v := redis_Warning_Count.Get(userId)
  170. err = json.Unmarshal(v.([]byte), &count)
  171. if err != nil {
  172. logs.Error(lib.FuncName(), err)
  173. return count, err
  174. }
  175. return count, nil
  176. }
  177. return count, redis.ErrNil
  178. }
  179. func Redis_WarningCount_GetOne(id int, key string) (count int64, tableCount map[string]int64, err error) {
  180. countList := []Warning_Count{}
  181. tableCount = make(map[string]int64)
  182. idKey := strconv.Itoa(id)
  183. if !redis_Warning_Count.IsExist(idKey) {
  184. return count, tableCount, redis.ErrNil
  185. }
  186. //println("找到key:",key)
  187. v := redis_Warning_Count.Get(idKey)
  188. err = json.Unmarshal(v.([]byte), &countList)
  189. if err != nil {
  190. logs.Error(lib.FuncName(), err)
  191. return count, tableCount, err
  192. }
  193. for _, c := range countList {
  194. if c.Key == key {
  195. return c.Count, c.TableCount, nil
  196. }
  197. }
  198. return count, tableCount, redis.ErrNil
  199. }
  200. func Redis_WarningCount_DelK(id int) (err error) {
  201. idKey := strconv.Itoa(id)
  202. err = redis_Warning_Count.Delete(idKey)
  203. if err != nil {
  204. logs.Error(lib.FuncName(), err)
  205. }
  206. return
  207. }
  208. // ---------------- 特殊方法 -------------------
  209. // T_history 0 40天 1 历史数据
  210. func WarningToWarning_R(T_history int, t Warning) (r Warning_R) {
  211. r.Id = t.Id
  212. r.T_pid = t.T_pid
  213. r.T_tp = t.T_tp
  214. r.T_tp_name = Read_WarningType_Get(t.T_tp)
  215. r.T_sn = t.T_sn
  216. r.T_D_name = t.T_D_name
  217. r.T_id = t.T_id
  218. r.T_DS_name = t.T_DS_name
  219. r.T_Remark = t.T_Remark
  220. r.T_Ut = t.T_Ut.Format("2006-01-02 15:04:05")
  221. if !t.T_fUt.IsZero() {
  222. r.T_fUt = t.T_fUt.Format("2006-01-02 15:04:05")
  223. }
  224. r.T_Text = t.T_Text
  225. if len(t.T_Log) > 0 {
  226. r.T_Log = strings.Split(strings.TrimRight(t.T_Log, "\n"), "\n")
  227. }
  228. r.T_Msid = t.T_Msid
  229. r.T_State = t.T_State
  230. r.T_history = T_history
  231. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  232. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  233. return r
  234. }
  235. func WarningToCompanyWarning_R(T_history int, t Warning) (r CompanyWarning_R) {
  236. r.Id = t.Id
  237. r.T_pid = t.T_pid
  238. r.T_pid_name = Account.Read_Company_Get(t.T_pid)
  239. r.T_tp = t.T_tp
  240. r.T_tp_name = Read_WarningType_Get(t.T_tp)
  241. r.T_sn = t.T_sn
  242. r.T_D_name = t.T_D_name
  243. r.T_id = t.T_id
  244. r.T_DS_name = t.T_DS_name
  245. r.T_Remark = t.T_Remark
  246. r.T_Ut = t.T_Ut.Format("2006-01-02 15:04:05")
  247. if !t.T_fUt.IsZero() {
  248. r.T_fUt = t.T_fUt.Format("2006-01-02 15:04:05")
  249. }
  250. r.T_Text = t.T_Text
  251. if len(t.T_Log) > 0 {
  252. r.T_Log = strings.Split(strings.TrimRight(t.T_Log, "\n"), "\n")
  253. }
  254. r.T_Msid = t.T_Msid
  255. r.T_State = t.T_State
  256. r.T_history = T_history
  257. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  258. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  259. return r
  260. }
  261. func WarningToWarning_Applet(t Warning) (r Warning_Applet) {
  262. r.Id = t.Id
  263. r.T_pid = t.T_pid
  264. company, _ := Account.Read_Company_ById(t.T_pid)
  265. r.T_company_name = company.T_name
  266. r.T_tp = t.T_tp
  267. r.T_tp_name = Read_WarningType_Get(t.T_tp)
  268. r.T_sn = t.T_sn
  269. r.T_D_name = t.T_D_name
  270. r.T_id = t.T_id
  271. r.T_DS_name = t.T_DS_name
  272. r.T_Remark = t.T_Remark
  273. r.T_Ut = t.T_Ut.Format("2006-01-02 15:04:05")
  274. if !t.T_fUt.IsZero() {
  275. r.T_fUt = t.T_fUt.Format("2006-01-02 15:04:05")
  276. }
  277. r.T_Text = t.T_Text
  278. if len(t.T_Log) > 0 {
  279. r.T_Log = strings.Split(strings.TrimRight(t.T_Log, "\n"), "\n")
  280. }
  281. r.T_Msid = t.T_Msid
  282. r.T_State = t.T_State
  283. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  284. return r
  285. }
  286. // 获取 ById
  287. func Read_Warning_ById(id int64) (r Warning) {
  288. o := orm.NewOrm()
  289. r = Warning{Id: id}
  290. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  291. if err != nil {
  292. logs.Error(lib.FuncName(), err)
  293. }
  294. return r
  295. }
  296. // 获取 ById
  297. func Read_Warning_ById_Backups(id int, T_year string, T_month string) (r Warning, err error) {
  298. o := orm.NewOrm()
  299. var maps []Warning
  300. // Warning_2022_07
  301. Wtab := "warning_" + T_year + "_" + T_month
  302. sql_WHERE := fmt.Sprintf(" t__state > 0 AND `ID`= %d", id)
  303. sql := "SELECT ID,t_pid,t_tp,t_sn,t__d_name,t_id,t__d_s_name,t__remark,t__ut,t__text,t__log,t__msid,t__state,create_time,update_time " +
  304. "FROM " + Wtab + " WHERE" + sql_WHERE
  305. fmt.Println(sql)
  306. num, err := o.Raw(sql).QueryRows(&maps)
  307. if err != nil {
  308. logs.Error(lib.FuncName(), err)
  309. return
  310. }
  311. if num == 0 {
  312. return r, orm.ErrNoRows
  313. }
  314. return maps[0], nil
  315. }
  316. // 添加
  317. func Add_Warning(m Warning) (id int64, err error) {
  318. o := orm.NewOrm()
  319. id, err = o.Insert(&m)
  320. if err != nil {
  321. logs.Error(lib.FuncName(), err)
  322. }
  323. return id, err
  324. }
  325. // 修改
  326. func Update_Warning(r Warning, cols ...string) bool {
  327. o := orm.NewOrm()
  328. num, err := o.Update(&r, cols...)
  329. if err != nil {
  330. logs.Error(lib.FuncName(), err)
  331. return false
  332. }
  333. fmt.Println("Number of records updated in database:", num)
  334. return true
  335. }
  336. // 修改
  337. func Update_Warning_Backups(r Warning, T_year string, T_month string) bool {
  338. o := orm.NewOrm()
  339. Wtab := "warning_" + T_year + "_" + T_month
  340. num, err := o.Raw("UPDATE "+Wtab+" SET `t__text` = ?,`t__state` = ?, `update_time` = ? WHERE `ID` = ?", r.T_Text, r.T_State, r.UpdateTime, r.Id).Exec()
  341. if err != nil {
  342. logs.Error(lib.FuncName(), err)
  343. return false
  344. }
  345. fmt.Println("Number of records updated in database:", num)
  346. return true
  347. }
  348. // 获取列表
  349. func Read_Warning_List(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
  350. countRedisKey string) (r []Warning_R, cnt int64) {
  351. o := orm.NewOrm()
  352. // 也可以直接使用 Model 结构体作为表名
  353. var map_r []Warning
  354. qs := o.QueryTable(new(Warning))
  355. var offset int64
  356. if page <= 1 {
  357. offset = 0
  358. } else {
  359. offset = int64((page - 1) * page_z)
  360. }
  361. cond := orm.NewCondition()
  362. cond1 := orm.NewCondition()
  363. if len(bindSN) > 0 {
  364. cond1 = cond1.And("T_sn__in", bindSN)
  365. }
  366. if T_pid > 0 {
  367. cond1 = cond1.And("T_pid", T_pid)
  368. }
  369. if len(tpList) > 0 {
  370. cond1 = cond1.And("T_tp__in", tpList)
  371. }
  372. if len(T_name) > 0 {
  373. if len(T_name) == 16 {
  374. cond1 = cond1.And("T_sn", T_name)
  375. } else {
  376. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).
  377. Or("T_D_name__icontains", T_name).
  378. Or("T_id__icontains", T_name).
  379. Or("T_DS_name__icontains", T_name))
  380. }
  381. }
  382. if len(Time_start_) > 0 {
  383. cond1 = cond1.And("T_Ut__gte", Time_start_)
  384. }
  385. if len(Time_end_) > 0 {
  386. cond1 = cond1.And("T_Ut__lte", Time_end_)
  387. }
  388. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  389. // T_handle 1:默认 2:全部记录 3 未处理
  390. if T_handle == 1 {
  391. cond1 = cond1.And("T_State__gt", 1)
  392. } else if T_handle == 3 {
  393. cond1 = cond1.And("T_State", 3)
  394. } else {
  395. cond1 = cond1.And("T_State__gt", 0)
  396. }
  397. var err error
  398. // 从redis获取报警统计数量
  399. cnt, _, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
  400. if err != nil {
  401. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  402. if err != nil {
  403. logs.Error(lib.FuncName(), err)
  404. return
  405. }
  406. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
  407. }
  408. if cnt == 0 {
  409. return r, 0
  410. }
  411. if page_z == 9999 {
  412. // 获取全部
  413. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-T_Ut").All(&map_r)
  414. } else {
  415. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_Ut").All(&map_r)
  416. }
  417. if err != nil {
  418. logs.Error(lib.FuncName(), err)
  419. return
  420. }
  421. for _, v := range map_r {
  422. r = append(r, WarningToWarning_R(0, v))
  423. }
  424. return r, cnt
  425. }
  426. // 管理员报警列表
  427. func Read_Admin_Warning_List(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
  428. countRedisKey string) (r []Warning_R, cnt int64) {
  429. if len(admin.T_pids) == 0 {
  430. return r, cnt
  431. }
  432. o := orm.NewOrm()
  433. // 也可以直接使用 Model 结构体作为表名
  434. var map_r []Warning
  435. qs := o.QueryTable(new(Warning))
  436. var offset int64
  437. if page_z == 0 {
  438. page_z = conf.Page_size
  439. }
  440. if page <= 1 {
  441. offset = 0
  442. } else {
  443. offset = int64((page - 1) * page_z)
  444. }
  445. cond := orm.NewCondition()
  446. cond1 := orm.NewCondition()
  447. if admin.T_pids != "*" && len(admin.T_pids) > 0 {
  448. list := lib.SplitStringIds(admin.T_pids, "P")
  449. cond1 = cond1.And("T_pid__in", list)
  450. }
  451. if len(T_tp) > 0 {
  452. cond1 = cond1.And("T_tp__in", T_tp)
  453. }
  454. if len(T_name) > 0 {
  455. if len(T_name) == 16 {
  456. cond1 = cond1.And("T_sn", T_name)
  457. } else {
  458. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).
  459. Or("T_D_name__icontains", T_name).
  460. Or("T_id__icontains", T_name).
  461. Or("T_DS_name__icontains", T_name))
  462. }
  463. }
  464. if len(Time_start_) > 0 {
  465. cond1 = cond1.And("T_Ut__gte", Time_start_)
  466. }
  467. if len(Time_end_) > 0 {
  468. cond1 = cond1.And("T_Ut__lte", Time_end_)
  469. }
  470. //1:默认 2:全部记录 3 未处理
  471. if T_handle == 1 {
  472. cond1 = cond1.And("T_State__gt", 1)
  473. } else if T_handle == 3 {
  474. cond1 = cond1.And("T_State", 3)
  475. } else {
  476. cond1 = cond1.And("T_State__gt", 0)
  477. }
  478. var err error
  479. // 从redis获取报警统计数量
  480. cnt, _, err = Redis_WarningCount_GetOne(admin.Id, countRedisKey)
  481. if err != nil {
  482. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  483. if err != nil {
  484. logs.Error(lib.FuncName(), err)
  485. return
  486. }
  487. Redis_WarningCount_Set(admin.Id, countRedisKey, cnt, map[string]int64{})
  488. }
  489. if cnt == 0 {
  490. return r, 0
  491. }
  492. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_Ut").All(&map_r)
  493. if err != nil {
  494. logs.Error(lib.FuncName(), err)
  495. }
  496. for _, v := range map_r {
  497. r = append(r, WarningToWarning_R(0, v))
  498. }
  499. return r, cnt
  500. }
  501. // 获取列表备份
  502. func Read_Warning_Backups(T_pid int, bindSN []string, T_year string, T_month string, tpList []string, T_name string, T_handle int, Time_start_ string,
  503. Time_end_ string, page int, page_z int, countRedisKey string) (r []Warning_R, cnt int64) {
  504. o := orm.NewOrm()
  505. var maps []Warning
  506. var maps_z []orm2.ParamsList
  507. // 也可以直接使用 Model 结构体作为表名
  508. // Warning_2022_07
  509. Wtab := "warning_" + T_year + "_" + T_month
  510. var offset int
  511. if page <= 1 {
  512. offset = 0
  513. } else {
  514. offset = (page - 1) * page_z
  515. }
  516. sql_WHERE := ""
  517. if len(bindSN) > 0 {
  518. sql_WHERE += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
  519. }
  520. if T_pid > 0 {
  521. sql_WHERE += " AND t_pid = " + strconv.Itoa(T_pid)
  522. }
  523. if len(tpList) > 0 {
  524. tp := lib.StringListToDotStr(tpList)
  525. sql_WHERE += fmt.Sprintf(" AND t_tp in (%s)", tp)
  526. }
  527. if len(T_name) > 0 {
  528. sql_WHERE += " AND (t_sn like '%" + T_name + "%' OR t__d_name like '%" + T_name + "%' OR t_id like '%" + T_name + "%' OR t__d_s_name like '%" + T_name + "%')"
  529. }
  530. if len(Time_start_) > 0 {
  531. //cond1 = cond1.And("T_Ut__gte", Time_start_)
  532. Time_start_ = lib.ReplaceSQL(Time_start_)
  533. sql_WHERE += fmt.Sprintf(" AND t__ut >= '%s'", Time_start_)
  534. }
  535. if len(Time_end_) > 0 {
  536. //cond1 = cond1.And("T_Ut__lte", Time_end_)
  537. Time_end_ = lib.ReplaceSQL(Time_end_)
  538. sql_WHERE += fmt.Sprintf(" AND t__ut <= '%s'", Time_end_)
  539. }
  540. //1:默认 2:全部记录 3 未处理
  541. if T_handle == 1 {
  542. sql_WHERE += " AND t__state > 1"
  543. } else if T_handle == 3 {
  544. sql_WHERE += " AND t__state = 3"
  545. } else {
  546. sql_WHERE += " AND t__state > 0"
  547. }
  548. // -------------
  549. sql_WHERE = strings.TrimLeft(sql_WHERE, " AND")
  550. // 从redis获取报警统计数量
  551. cnt, _, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
  552. if err != nil {
  553. sql := "SELECT COUNT(*) FROM " + Wtab + " WHERE " + sql_WHERE
  554. fmt.Println(sql)
  555. _, err = o.Raw(sql).ValuesList(&maps_z)
  556. if err != nil {
  557. logs.Error(lib.FuncName(), err)
  558. return r, 0
  559. }
  560. if len(maps_z) == 0 {
  561. return r, 0
  562. }
  563. cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  564. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
  565. }
  566. if cnt == 0 {
  567. return r, 0
  568. }
  569. //fmt.Println("maps_z;",maps_z[0][0])
  570. sql := "SELECT ID,t_pid,t_tp,t_sn,t__d_name,t_id,t__d_s_name,t__remark,t__ut,t__text,t__log,t__msid,t__state,create_time,update_time " +
  571. "FROM " + Wtab + " WHERE " + sql_WHERE + " ORDER BY t__ut DESC"
  572. if page_z != 9999 {
  573. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  574. }
  575. fmt.Println(sql)
  576. _, err = o.Raw(sql).QueryRows(&maps)
  577. if err != nil {
  578. logs.Error(lib.FuncName(), err)
  579. return
  580. }
  581. for _, v := range maps {
  582. r = append(r, WarningToWarning_R(1, v))
  583. }
  584. return r, cnt
  585. }
  586. // 获取管理员列表备份
  587. func Read_Admin_Warning_Backups(admin Account.Admin, T_year string, T_month string, T_tp []string, T_name string, T_handle int,
  588. Time_start_ string, Time_end_ string, page int, page_z int, countRedisKey string) (r []Warning_R, cnt int64) {
  589. if len(admin.T_pids) == 0 {
  590. return r, cnt
  591. }
  592. o := orm.NewOrm()
  593. var maps []Warning
  594. var maps_z []orm2.ParamsList
  595. // 也可以直接使用 Model 结构体作为表名
  596. // Warning_2022_07
  597. Wtab := "warning_" + T_year + "_" + T_month
  598. var offset int
  599. if page_z == 0 {
  600. page_z = conf.Page_size
  601. }
  602. if page <= 1 {
  603. offset = 0
  604. } else {
  605. offset = (page - 1) * page_z
  606. }
  607. sql_WHERE := ""
  608. if admin.T_pids != "*" {
  609. list := lib.SplitStringToDotStr(admin.T_pids, "P")
  610. sql_WHERE += fmt.Sprintf(" AND t_pid in (%s)", list)
  611. }
  612. if len(T_tp) > 0 {
  613. tp := lib.StringListToDotStr(T_tp)
  614. sql_WHERE += fmt.Sprintf(" AND t_tp in (%s)", tp)
  615. }
  616. if len(T_name) > 0 {
  617. sql_WHERE += " AND (t_sn like '%" + T_name + "%' OR t__d_name like '%" + T_name + "%' OR t_id like '%" + T_name + "%' OR t__d_s_name like '%" + T_name + "%')"
  618. }
  619. if len(Time_start_) > 0 {
  620. //cond1 = cond1.And("T_Ut__gte", Time_start_)
  621. Time_start_ = lib.ReplaceSQL(Time_start_)
  622. sql_WHERE += fmt.Sprintf(" AND t__ut >= '%s'", Time_start_)
  623. }
  624. if len(Time_end_) > 0 {
  625. //cond1 = cond1.And("T_Ut__lte", Time_end_)
  626. Time_end_ = lib.ReplaceSQL(Time_end_)
  627. sql_WHERE += fmt.Sprintf(" AND t__ut <= '%s'", Time_end_)
  628. }
  629. //1:默认 2:全部记录 3 未处理
  630. if T_handle == 1 {
  631. sql_WHERE += " AND t__state > 1"
  632. } else if T_handle == 3 {
  633. sql_WHERE += " AND t__state = 3"
  634. } else {
  635. sql_WHERE += " AND t__state > 0"
  636. }
  637. sql_WHERE = strings.TrimLeft(sql_WHERE, " AND")
  638. // 从redis获取报警统计数量
  639. cnt, _, err := Redis_WarningCount_GetOne(admin.Id, countRedisKey)
  640. if err != nil {
  641. sql := "SELECT COUNT(*) FROM " + Wtab + " WHERE " + sql_WHERE
  642. fmt.Println(sql)
  643. _, err = o.Raw(sql).ValuesList(&maps_z)
  644. if err != nil {
  645. logs.Error(lib.FuncName(), err)
  646. return r, 0
  647. }
  648. if len(maps_z) == 0 {
  649. return r, 0
  650. }
  651. cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  652. Redis_WarningCount_Set(admin.Id, countRedisKey, cnt, map[string]int64{})
  653. }
  654. if cnt == 0 {
  655. return r, 0
  656. }
  657. //fmt.Println("maps_z;",maps_z[0][0])
  658. sql := "SELECT ID,t_pid,t_tp,t_sn,t__d_name,t_id,t__d_s_name,t__remark,t__ut,t__text,t__log,t__msid,t__state,create_time,update_time " +
  659. "FROM " + Wtab + " WHERE " + sql_WHERE + " ORDER BY t__ut DESC"
  660. if page_z != 9999 {
  661. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  662. }
  663. fmt.Println(sql)
  664. _, err = o.Raw(sql).QueryRows(&maps)
  665. if err != nil {
  666. logs.Error(lib.FuncName(), err)
  667. return
  668. }
  669. key, _ := strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  670. for _, v := range maps {
  671. r = append(r, WarningToWarning_R(1, v))
  672. }
  673. return r, key
  674. }
  675. // 用户首页统计
  676. func Read_Warning_ALL_T_State_Count2(T_pid int, T_pids []int, T_handle int, bindSN []string, T_Warning string, Is_Today bool) (cnt int64) {
  677. o := orm.NewOrm()
  678. qs := o.QueryTable(new(Warning))
  679. cond := orm.NewCondition()
  680. cond1 := cond.And("T_State__gt", 0)
  681. if T_pid > 0 {
  682. cond1 = cond1.And("T_pid", T_pid)
  683. }
  684. if T_pid == 0 {
  685. cond1 = cond1.And("T_pid__in", T_pids)
  686. }
  687. if len(bindSN) > 0 {
  688. cond1 = cond1.And("T_sn__in", bindSN)
  689. }
  690. if Is_Today {
  691. startTime := time.Now().Format("2006-01-02") + " 00:00:00"
  692. endTime := time.Now().Format("2006-01-02") + " 23:59:59"
  693. cond1 = cond1.And("T_Ut__gte", startTime).And("T_Ut__lte", endTime)
  694. }
  695. if len(T_Warning) > 0 && T_Warning != "*" {
  696. list := lib.SplitStringIds(T_Warning, "W")
  697. cond1 = cond1.And("T_tp__in", list)
  698. }
  699. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  700. // T_handle 1:默认 2:全部记录 3 未处理
  701. if T_handle == 1 {
  702. cond1 = cond1.And("T_State__gt", 1)
  703. }
  704. if T_handle == 3 {
  705. cond1 = cond1.And("T_State", 3)
  706. }
  707. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
  708. if err != nil {
  709. logs.Error(lib.FuncName(), err)
  710. }
  711. return cnt
  712. }
  713. // 用户首页统计,使用gorm查询
  714. func Read_Warning_ALL_T_State_Count(T_pid int, T_pids []int, T_handle int, bindSN []string, T_Warning string, Is_Today bool) (cnt int64) {
  715. tableName := "warning"
  716. if conf.UseNewWarningQuery {
  717. tableName = fmt.Sprintf("warning_%s_%s", time.Now().Format("2006"), time.Now().Format("01"))
  718. }
  719. gormDB := db.DB.Table(tableName)
  720. daysAgo40 := time.Now().AddDate(0, 0, -40).Format("2006-01-02")
  721. gormDB.Where("t__state > 0 ").Where("t__ut > ?", daysAgo40+" 00:00:00")
  722. if T_pid > 0 {
  723. gormDB.Where("t_pid = ?", T_pid)
  724. }
  725. if T_pid == 0 {
  726. gormDB.Where("t_pid in (?)", T_pids)
  727. }
  728. if len(bindSN) > 0 {
  729. gormDB.Where("t_sn in (?)", bindSN)
  730. }
  731. if Is_Today {
  732. startTime := time.Now().Format("2006-01-02") + " 00:00:00"
  733. endTime := time.Now().Format("2006-01-02") + " 23:59:59"
  734. gormDB.Where("t__ut between ? and ?", startTime, endTime)
  735. }
  736. if len(T_Warning) > 0 && T_Warning != "*" {
  737. list := lib.SplitStringIds(T_Warning, "W")
  738. gormDB.Where("t_tp in (?)", list)
  739. }
  740. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  741. // T_handle 1:默认 2:全部记录 3 未处理
  742. if T_handle == 1 {
  743. gormDB.Where("t__state > ?", 1)
  744. }
  745. if T_handle == 3 {
  746. gormDB.Where("t__state = ?", 3)
  747. }
  748. err := gormDB.Count(&cnt).Error
  749. if err != nil {
  750. logs.Error(lib.FuncName(), err)
  751. }
  752. return cnt
  753. }
  754. // 设备日志
  755. type DeviceLogs struct {
  756. Id int64
  757. T_sn string //
  758. Logs_Txt string // 详情
  759. CreateTime string
  760. }
  761. func WarningToDeviceLogs(t Warning) (r DeviceLogs) {
  762. r.Id = t.Id
  763. r.T_sn = t.T_sn
  764. r.Logs_Txt = t.T_Remark
  765. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  766. return r
  767. }
  768. // 获取列表
  769. func Read_DeviceLogs_List(T_sn string, page, page_z int) (r []DeviceLogs, cnt int64) {
  770. o := orm.NewOrm()
  771. // 也可以直接使用 Model 结构体作为表名
  772. qs := o.QueryTable(new(Warning))
  773. var maps []Warning
  774. var offset int64
  775. if page <= 1 {
  776. offset = 0
  777. } else {
  778. offset = int64((page - 1) * page_z)
  779. }
  780. _, err := qs.Limit(page_z, offset).Filter("T_sn", T_sn).Filter("T_tp__in", DeviceLogType).OrderBy("-Id").All(&maps)
  781. if err != nil {
  782. logs.Error(lib.FuncName(), err)
  783. return
  784. }
  785. cnt, err = qs.Filter("T_sn", T_sn).Filter("T_tp__in", DeviceLogType).Count()
  786. if err != nil {
  787. logs.Error(lib.FuncName(), err)
  788. return
  789. }
  790. for _, v := range maps {
  791. r = append(r, WarningToDeviceLogs(v))
  792. }
  793. return r, cnt
  794. }
  795. // 通过传感器类型获取报警列表
  796. func Read_Warning_List_By_DS_T_type(admin *Account.Admin, T_pid int, bindSN []string, T_pids []int, DS_T_type int, T_name string, T_tp []string,
  797. page int, page_z int, countRedisKey string) (r []Warning_Applet, cnt int64) {
  798. o := orm.NewOrm()
  799. // 也可以直接使用 Model 结构体作为表名
  800. var maps []Warning
  801. var maps_z []orm2.ParamsList
  802. var offset int
  803. if page <= 1 {
  804. offset = 0
  805. } else {
  806. offset = (page - 1) * page_z
  807. }
  808. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  809. sql_WHERE := "1 < t__state"
  810. if admin.T_pid > 0 {
  811. if len(T_pids) > 0 {
  812. sql_WHERE += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids))
  813. }
  814. } else {
  815. sql_WHERE += " AND t_pid = " + strconv.Itoa(T_pid)
  816. }
  817. if len(T_tp) > 0 {
  818. tp := lib.StringListToDotStr(T_tp)
  819. sql_WHERE += fmt.Sprintf(" AND w.t_tp in (%s)", tp)
  820. }
  821. if len(bindSN) > 0 {
  822. sql_WHERE += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
  823. }
  824. if DS_T_type > 0 {
  825. sql_WHERE += " AND ds.t_type = " + strconv.Itoa(DS_T_type)
  826. }
  827. if len(T_name) > 0 {
  828. sql_WHERE += " AND (t_sn like '%" + T_name + "%' OR t__d_name like '%" + T_name + "%' OR t_id like '%" + T_name + "%' OR t__d_s_name like '%" + T_name + "%')"
  829. }
  830. sql := "SELECT COUNT(w.ID) FROM warning w " +
  831. "LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_type FROM device_sensor) AS ds " +
  832. "ON w.t_sn = ds.tsn AND w.t_id = ds.tid " +
  833. "WHERE " + sql_WHERE
  834. cnt, _, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
  835. if err != nil {
  836. fmt.Println(sql)
  837. _, err = o.Raw(sql).ValuesList(&maps_z)
  838. if err != nil {
  839. logs.Error(lib.FuncName(), err)
  840. return r, 0
  841. }
  842. if len(maps_z) == 0 {
  843. return r, 0
  844. }
  845. cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  846. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
  847. }
  848. if cnt == 0 {
  849. return r, 0
  850. }
  851. sql = "SELECT w.*, ds.tsn, ds.tid, ds.t_type FROM warning w " +
  852. "LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_type FROM device_sensor) AS ds " +
  853. "ON w.t_sn = ds.tsn AND w.t_id = ds.tid " +
  854. "WHERE " + sql_WHERE + " ORDER BY t__ut DESC"
  855. if page_z != 9999 {
  856. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  857. }
  858. fmt.Println(sql)
  859. _, err = o.Raw(sql).QueryRows(&maps)
  860. if err != nil {
  861. logs.Error(lib.FuncName(), err)
  862. return
  863. }
  864. for _, v := range maps {
  865. r = append(r, WarningToWarning_Applet(v))
  866. }
  867. return r, cnt
  868. }
  869. // 获取当天设备报警总数
  870. func Read_WarningCount_byDay(T_pids []int) (cnt int64) {
  871. o := orm.NewOrm()
  872. qs := o.QueryTable(new(Warning))
  873. cond := orm.NewCondition()
  874. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  875. cond = cond.And("T_State__gt", 0).And("T_tp__in", RateType).And("CreateTime__gte", yesterday+" 00:00:00").And("CreateTime__lte", yesterday+" 23:59:59")
  876. if len(T_pids) == 1 {
  877. cond = cond.And("T_pid", T_pids[0])
  878. } else if len(T_pids) > 1 {
  879. cond = cond.And("T_pid__in", T_pids)
  880. }
  881. cnt, err := qs.SetCond((*orm2.Condition)(cond)).Count()
  882. if err != nil {
  883. logs.Error(lib.FuncName(), err)
  884. }
  885. return cnt
  886. }
  887. // 公司管理报警列表
  888. func Read_Company_Warning_List(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
  889. T_pid int, countRedisKey string) (r []CompanyWarning_R, cnt int64) {
  890. o := orm.NewOrm()
  891. // 也可以直接使用 Model 结构体作为表名
  892. var map_r []Warning
  893. qs := o.QueryTable(new(Warning))
  894. var offset int64
  895. if page_z == 0 {
  896. page_z = conf.Page_size
  897. }
  898. if page <= 1 {
  899. offset = 0
  900. } else {
  901. offset = int64((page - 1) * page_z)
  902. }
  903. cond := orm.NewCondition()
  904. cond1 := orm.NewCondition()
  905. if len(T_pids) == 1 {
  906. cond1 = cond1.And("T_pid", T_pids[0])
  907. } else if len(T_pids) > 1 {
  908. cond1 = cond1.And("T_pid__in", T_pids)
  909. }
  910. if len(T_tp) > 0 {
  911. cond1 = cond1.And("T_tp__in", T_tp)
  912. }
  913. if len(T_name) > 0 {
  914. if len(T_name) == 16 {
  915. cond1 = cond1.And("T_sn", T_name)
  916. } else {
  917. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).
  918. Or("T_D_name__icontains", T_name).
  919. Or("T_id__icontains", T_name).
  920. Or("T_DS_name__icontains", T_name))
  921. }
  922. }
  923. if len(Time_start_) > 0 {
  924. cond1 = cond1.And("T_Ut__gte", Time_start_)
  925. }
  926. if len(Time_end_) > 0 {
  927. cond1 = cond1.And("T_Ut__lte", Time_end_)
  928. }
  929. //1:默认 2:全部记录 3 未处理
  930. if T_handle == 1 {
  931. cond1 = cond1.And("T_State__gt", 1)
  932. } else if T_handle == 3 {
  933. cond1 = cond1.And("T_State", 3)
  934. } else {
  935. cond1 = cond1.And("T_State__gt", 0)
  936. }
  937. var err error
  938. // 从redis获取报警统计数量
  939. cnt, _, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
  940. if err != nil {
  941. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  942. if err != nil {
  943. logs.Error(lib.FuncName(), err)
  944. return
  945. }
  946. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
  947. }
  948. if cnt == 0 {
  949. return r, 0
  950. }
  951. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_Ut").All(&map_r)
  952. if err != nil {
  953. logs.Error(lib.FuncName(), err)
  954. }
  955. for _, v := range map_r {
  956. r = append(r, WarningToCompanyWarning_R(0, v))
  957. }
  958. return r, cnt
  959. }
  960. // 获取公司管理列表备份
  961. func Read_Company_Warning_Backups(T_pids []int, T_year string, T_month string, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string,
  962. page int, page_z int, T_pid int, countRedisKey string) (r []CompanyWarning_R, cnt int64) {
  963. if len(T_pids) == 0 {
  964. return r, cnt
  965. }
  966. o := orm.NewOrm()
  967. var maps []Warning
  968. var maps_z []orm2.ParamsList
  969. // 也可以直接使用 Model 结构体作为表名
  970. // Warning_2022_07
  971. Wtab := "warning_" + T_year + "_" + T_month
  972. var offset int
  973. if page_z == 0 {
  974. page_z = conf.Page_size
  975. }
  976. if page <= 1 {
  977. offset = 0
  978. } else {
  979. offset = (page - 1) * page_z
  980. }
  981. sql_WHERE := ""
  982. if len(T_pids) == 1 {
  983. sql_WHERE += fmt.Sprintf(" AND t_pid = %d", T_pids[0])
  984. } else if len(T_pids) > 1 {
  985. sql_WHERE += fmt.Sprintf(" AND t_pid in (%s)", lib.IntListToDotStr(T_pids))
  986. }
  987. if len(T_tp) > 0 {
  988. tp := lib.StringListToDotStr(T_tp)
  989. sql_WHERE += fmt.Sprintf(" AND t_tp in (%s)", tp)
  990. }
  991. if len(T_name) > 0 {
  992. sql_WHERE += " AND (t_sn like '%" + T_name + "%' OR t__d_name like '%" + T_name + "%' OR t_id like '%" + T_name + "%' OR t__d_s_name like '%" + T_name + "%')"
  993. }
  994. if len(Time_start_) > 0 {
  995. //cond1 = cond1.And("T_Ut__gte", Time_start_)
  996. Time_start_ = lib.ReplaceSQL(Time_start_)
  997. sql_WHERE += fmt.Sprintf(" AND t__ut >= '%s'", Time_start_)
  998. }
  999. if len(Time_end_) > 0 {
  1000. //cond1 = cond1.And("T_Ut__lte", Time_end_)
  1001. Time_end_ = lib.ReplaceSQL(Time_end_)
  1002. sql_WHERE += fmt.Sprintf(" AND t__ut <= '%s'", Time_end_)
  1003. }
  1004. //1:默认 2:全部记录 3 未处理
  1005. if T_handle == 1 {
  1006. sql_WHERE += " AND t__state > 1"
  1007. } else if T_handle == 3 {
  1008. sql_WHERE += " AND t__state = 3"
  1009. } else {
  1010. sql_WHERE += " AND t__state > 0"
  1011. }
  1012. sql_WHERE = strings.TrimLeft(sql_WHERE, " AND")
  1013. // 从redis获取报警统计数量
  1014. cnt, _, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
  1015. if err != nil {
  1016. sql := "SELECT COUNT(*) FROM " + Wtab + " WHERE " + sql_WHERE
  1017. fmt.Println(sql)
  1018. _, err = o.Raw(sql).ValuesList(&maps_z)
  1019. if err != nil {
  1020. logs.Error(lib.FuncName(), err)
  1021. return r, 0
  1022. }
  1023. if len(maps_z) == 0 {
  1024. return r, 0
  1025. }
  1026. cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  1027. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
  1028. }
  1029. if cnt == 0 {
  1030. return r, 0
  1031. }
  1032. sql := "SELECT ID,t_pid,t_tp,t_sn,t__d_name,t_id,t__d_s_name,t__remark,t__ut,t__text,t__log,t__msid,t__state,create_time,update_time " +
  1033. "FROM " + Wtab + " WHERE " + sql_WHERE + " ORDER BY t__ut DESC"
  1034. if page_z != 9999 {
  1035. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  1036. }
  1037. fmt.Println(sql)
  1038. _, err = o.Raw(sql).QueryRows(&maps)
  1039. if err != nil {
  1040. logs.Error(lib.FuncName(), err)
  1041. return
  1042. }
  1043. for _, v := range maps {
  1044. r = append(r, WarningToCompanyWarning_R(1, v))
  1045. }
  1046. return r, cnt
  1047. }
  1048. func GormReadWarningListScopes(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string) func(db *gorm.DB) *gorm.DB {
  1049. return func(db *gorm.DB) *gorm.DB {
  1050. if T_pid > 0 {
  1051. db.Where("t_pid = ?", T_pid)
  1052. }
  1053. if T_abandon == 1 {
  1054. // 弃用
  1055. if len(abandonSN) > 0 {
  1056. db.Where("t_sn in (?)", abandonSN)
  1057. } else {
  1058. db.Where("1=0")
  1059. return db
  1060. }
  1061. } else {
  1062. if len(bindSN) > 0 {
  1063. // 将绑定sn内非弃用sn筛选出来
  1064. bindSNWithOutAbandon := lib.FindUniqueElements(bindSN, abandonSN)
  1065. if len(bindSNWithOutAbandon) > 0 {
  1066. db.Where("t_sn in (?)", bindSNWithOutAbandon)
  1067. } else {
  1068. db.Where("1=0")
  1069. return db
  1070. }
  1071. } else {
  1072. if len(abandonSN) > 0 {
  1073. db.Where("t_sn not in (?)", abandonSN)
  1074. }
  1075. }
  1076. }
  1077. if len(tpList) > 0 {
  1078. min, max, is := checkStringListContinuous(tpList)
  1079. if is {
  1080. db.Where("t_tp between ? and ?", min, max)
  1081. } else {
  1082. db.Where("t_tp in (?)", tpList)
  1083. }
  1084. }
  1085. if len(T_name) > 0 {
  1086. if len(T_name) == 16 {
  1087. db.Where("t_sn = ?", T_name)
  1088. } else {
  1089. likeName := "%" + T_name + "%"
  1090. db.Where("t_sn like ? or t__d_name like ? or t_id like ? or t__d_s_name like ? ", likeName, likeName, likeName, likeName)
  1091. }
  1092. }
  1093. if len(Time_start_) > 0 {
  1094. db.Where("t__ut >= ?", Time_start_)
  1095. }
  1096. if len(Time_end_) > 0 {
  1097. db.Where("t__ut <= ?", Time_end_)
  1098. }
  1099. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  1100. // T_handle 1:默认 2:全部记录 3 未处理
  1101. if T_handle == 1 {
  1102. db.Where("t__state > ?", 1)
  1103. } else if T_handle == 3 {
  1104. db.Where("t__state = ?", 3)
  1105. } else {
  1106. db.Where("t__state > ?", 0)
  1107. }
  1108. return db
  1109. }
  1110. }
  1111. // 获取列表
  1112. func Gorm_Read_Warning_List(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string, page int, page_z int,
  1113. countRedisKey string) (r []Warning_R, cnt int64) {
  1114. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1115. var err error
  1116. var warnings []Warning
  1117. tableCount := make(map[string]int64)
  1118. gormReadWarningListScopes := GormReadWarningListScopes(T_pid, bindSN, tpList, T_name, T_handle, Time_start_, Time_end_, T_abandon, abandonSN)
  1119. // 从redis获取报警统计数量
  1120. cnt, tableCount, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
  1121. if err != nil {
  1122. for _, tableName := range tableNames {
  1123. tableDB := db.DB.Table(tableName)
  1124. var count int64
  1125. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  1126. tableCount[tableName] = count
  1127. cnt += count
  1128. }
  1129. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, tableCount)
  1130. }
  1131. //if cnt == 0 {
  1132. // return r, 0
  1133. //}
  1134. if page_z == 9999 {
  1135. for _, tableName := range tableNames {
  1136. var tableWarning []Warning
  1137. tableDB := db.DB.Table(tableName)
  1138. tableDB.Scopes(gormReadWarningListScopes).
  1139. Order("t__ut desc").Offset(-1).Limit(-1).Find(&tableWarning)
  1140. warnings = append(warnings, tableWarning...)
  1141. }
  1142. for _, v := range warnings {
  1143. r = append(r, WarningToWarning_R(0, v))
  1144. }
  1145. return r, cnt
  1146. }
  1147. offset := (page - 1) * page_z
  1148. remaining := offset
  1149. for _, tableName := range tableNames {
  1150. var tableWarning []Warning
  1151. tableDB := db.DB.Table(tableName)
  1152. var count int64
  1153. if tableName == fmt.Sprintf("warning_%d_%02d", time.Now().Year(), time.Now().Month()) {
  1154. if saveTime, ok := tableCount["SaveTime"]; ok {
  1155. t1 := time.Unix(saveTime, 0)
  1156. if time.Now().Sub(t1) < 5*time.Minute {
  1157. if count, ok = tableCount[tableName]; !ok {
  1158. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  1159. tableCount[tableName] = count
  1160. }
  1161. } else {
  1162. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  1163. tableCount[tableName] = count
  1164. }
  1165. }
  1166. } else {
  1167. var ok bool
  1168. if count, ok = tableCount[tableName]; !ok {
  1169. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  1170. tableCount[tableName] = count
  1171. }
  1172. }
  1173. if remaining >= int(count) {
  1174. remaining -= int(count)
  1175. continue
  1176. }
  1177. tableDB2 := db.DB.Table(tableName)
  1178. tableDB2.Scopes(gormReadWarningListScopes).Order("t__ut desc").Offset(remaining).Limit(page_z - len(warnings)).Find(&tableWarning)
  1179. warnings = append(warnings, tableWarning...)
  1180. if len(warnings) >= page_z {
  1181. break
  1182. }
  1183. remaining = 0
  1184. }
  1185. for _, v := range warnings {
  1186. r = append(r, WarningToWarning_R(0, v))
  1187. }
  1188. return r, cnt
  1189. }
  1190. func Gorm_Read_Warning_List_Excel(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string, page int, page_z int,
  1191. countRedisKey string) (r []Warning_R, cnt int64) {
  1192. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1193. gormReadWarningListScopes := GormReadWarningListScopes(T_pid, bindSN, tpList, T_name, T_handle, Time_start_, Time_end_, T_abandon, abandonSN)
  1194. var warnings []Warning
  1195. var wg sync.WaitGroup
  1196. for _, tableName := range tableNames {
  1197. wg.Add(1)
  1198. go func(table string) {
  1199. defer wg.Done()
  1200. offset := 0
  1201. limit := 10000
  1202. for {
  1203. var tableWarning []Warning
  1204. tableDB := db.DB.Table(table)
  1205. tableDB.Scopes(gormReadWarningListScopes).
  1206. Order("t__ut desc").Offset(offset).Limit(limit).Find(&tableWarning)
  1207. if len(tableWarning) == 0 {
  1208. break // 如果没有数据则结束循环
  1209. }
  1210. offset += limit // 更新偏移量
  1211. warnings = append(warnings, tableWarning...)
  1212. }
  1213. }(tableName)
  1214. }
  1215. // 等待所有协程完成
  1216. wg.Wait()
  1217. // 倒序
  1218. sort.Slice(warnings, func(i, j int) bool {
  1219. if warnings[i].T_Ut.After(warnings[j].T_Ut) {
  1220. return true
  1221. }
  1222. return false
  1223. })
  1224. for _, v := range warnings {
  1225. r = append(r, WarningToWarning_R(0, v))
  1226. }
  1227. return r, cnt
  1228. }
  1229. func GormReadAdminWarningListScopes(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string) func(db *gorm.DB) *gorm.DB {
  1230. return func(db *gorm.DB) *gorm.DB {
  1231. if admin.T_pids != "*" && len(admin.T_pids) > 0 {
  1232. list := lib.SplitStringIds(admin.T_pids, "P")
  1233. db.Where("t_pid in (?)", list)
  1234. }
  1235. if T_abandon == 1 {
  1236. // 查询弃用设备
  1237. if len(abandonSN) > 0 {
  1238. db.Where("t_sn in (?)", abandonSN)
  1239. } else {
  1240. db.Where("1=0")
  1241. return db
  1242. }
  1243. } else {
  1244. if len(abandonSN) > 0 {
  1245. db.Where("t_sn not in (?)", abandonSN)
  1246. }
  1247. }
  1248. if len(T_tp) > 0 {
  1249. min, max, is := checkStringListContinuous(T_tp)
  1250. if is {
  1251. db.Where("t_tp between ? and ?", min, max)
  1252. } else {
  1253. db.Where("t_tp in (?)", T_tp)
  1254. }
  1255. }
  1256. if len(T_name) > 0 {
  1257. if len(T_name) == 16 {
  1258. db.Where("t_sn = ?", T_name)
  1259. } else {
  1260. likeName := "%" + T_name + "%"
  1261. db.Where("t_sn like ? or t__d_name like ? or t_id like ? or t__d_s_name like ? ", likeName, likeName, likeName, likeName)
  1262. }
  1263. }
  1264. if len(Time_start_) > 0 {
  1265. db.Where("t__ut >= ?", Time_start_)
  1266. }
  1267. if len(Time_end_) > 0 {
  1268. db.Where("t__ut <= ?", Time_end_)
  1269. }
  1270. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  1271. // T_handle 1:默认 2:全部记录 3 未处理
  1272. if T_handle == 1 {
  1273. db.Where("t__state > ?", 1)
  1274. } else if T_handle == 3 {
  1275. db.Where("t__state = ?", 3)
  1276. } else {
  1277. db.Where("t__state > ?", 0)
  1278. }
  1279. return db
  1280. }
  1281. }
  1282. // 管理员报警列表
  1283. func Gorm_Read_Admin_Warning_List(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string, page int, page_z int,
  1284. countRedisKey string) (r []Warning_R, cnt int64) {
  1285. if len(admin.T_pids) == 0 {
  1286. return r, cnt
  1287. }
  1288. gormReadAdminWarningListScopes := GormReadAdminWarningListScopes(admin, T_tp, T_name, T_handle, Time_start_, Time_end_, T_abandon, abandonSN)
  1289. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1290. var err error
  1291. var warnings []Warning
  1292. tableCount := make(map[string]int64)
  1293. // 从redis获取报警统计数量
  1294. cnt, tableCount, err = Redis_WarningCount_GetOne(admin.Id, countRedisKey)
  1295. if err != nil {
  1296. for _, tableName := range tableNames {
  1297. tableDB := db.DB.Table(tableName)
  1298. var count int64
  1299. tableDB.Scopes(gormReadAdminWarningListScopes).Count(&count)
  1300. tableCount[tableName] = count
  1301. cnt += count
  1302. }
  1303. Redis_WarningCount_Set(admin.Id, countRedisKey, cnt, tableCount)
  1304. }
  1305. if cnt == 0 {
  1306. return r, 0
  1307. }
  1308. // page_z == 9999 获取全部
  1309. if page_z == 9999 {
  1310. for _, tableName := range tableNames {
  1311. var tableWarning []Warning
  1312. tableDB := db.DB.Table(tableName)
  1313. tableDB.Scopes(gormReadAdminWarningListScopes).
  1314. Order("t__ut desc").Offset(-1).Limit(-1).Find(&tableWarning)
  1315. warnings = append(warnings, tableWarning...)
  1316. }
  1317. for _, v := range warnings {
  1318. r = append(r, WarningToWarning_R(0, v))
  1319. }
  1320. return r, cnt
  1321. }
  1322. offset := (page - 1) * page_z
  1323. remaining := offset
  1324. for _, tableName := range tableNames {
  1325. var tableWarning []Warning
  1326. tableDB := db.DB.Table(tableName)
  1327. count := tableCount[tableName]
  1328. if remaining >= int(count) {
  1329. remaining -= int(count)
  1330. continue
  1331. }
  1332. tableDB.Scopes(gormReadAdminWarningListScopes).
  1333. Order("t__ut desc").Offset(remaining).Limit(page_z - len(warnings)).Find(&tableWarning)
  1334. warnings = append(warnings, tableWarning...)
  1335. if len(warnings) >= page_z {
  1336. break
  1337. }
  1338. remaining = 0
  1339. }
  1340. for _, v := range warnings {
  1341. r = append(r, WarningToWarning_R(0, v))
  1342. }
  1343. return r, cnt
  1344. }
  1345. func Gorm_Read_Admin_Warning_List_Excel(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string, page int, page_z int,
  1346. countRedisKey string) (r []Warning_R, cnt int64) {
  1347. if len(admin.T_pids) == 0 {
  1348. return r, cnt
  1349. }
  1350. gormReadAdminWarningListScopes := GormReadAdminWarningListScopes(admin, T_tp, T_name, T_handle, Time_start_, Time_end_, T_abandon, abandonSN)
  1351. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1352. var warnings []Warning
  1353. var wg sync.WaitGroup
  1354. for _, tableName := range tableNames {
  1355. wg.Add(1)
  1356. go func(table string) {
  1357. defer wg.Done()
  1358. offset := 0
  1359. limit := 10000
  1360. for {
  1361. var tableWarning []Warning
  1362. tableDB := db.DB.Table(table)
  1363. tableDB.Scopes(gormReadAdminWarningListScopes).
  1364. Order("t__ut desc").Offset(offset).Limit(limit).Find(&tableWarning)
  1365. if len(tableWarning) == 0 {
  1366. break // 如果没有数据则结束循环
  1367. }
  1368. offset += limit // 更新偏移量
  1369. warnings = append(warnings, tableWarning...)
  1370. }
  1371. }(tableName)
  1372. }
  1373. // 等待所有协程完成
  1374. wg.Wait()
  1375. // 倒序
  1376. sort.Slice(warnings, func(i, j int) bool {
  1377. if warnings[i].T_Ut.After(warnings[j].T_Ut) {
  1378. return true
  1379. }
  1380. return false
  1381. })
  1382. for _, v := range warnings {
  1383. r = append(r, WarningToWarning_R(0, v))
  1384. }
  1385. return r, cnt
  1386. }
  1387. func GormReadCompanyWarningListScopes(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string) func(db *gorm.DB) *gorm.DB {
  1388. return func(db *gorm.DB) *gorm.DB {
  1389. if len(T_pids) > 0 {
  1390. db.Where("t_pid in (?)", T_pids)
  1391. }
  1392. if len(T_tp) > 0 {
  1393. min, max, is := checkStringListContinuous(T_tp)
  1394. if is {
  1395. db.Where("t_tp between ? and ?", min, max)
  1396. } else {
  1397. db.Where("t_tp in (?)", T_tp)
  1398. }
  1399. }
  1400. if len(T_name) > 0 {
  1401. if len(T_name) == 16 {
  1402. db.Where("t_sn = ?", T_name)
  1403. } else {
  1404. likeName := "%" + T_name + "%"
  1405. db.Where("t_sn like ? or t__d_name like ? or t_id like ? or t__d_s_name like ?", likeName, likeName, likeName, likeName)
  1406. }
  1407. }
  1408. if len(Time_start_) > 0 {
  1409. db.Where("t__ut >= ?", Time_start_)
  1410. }
  1411. if len(Time_end_) > 0 {
  1412. db.Where("t__ut <= ?", Time_end_)
  1413. }
  1414. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  1415. // T_handle 1:默认 2:全部记录 3 未处理
  1416. if T_handle == 1 {
  1417. db.Where("t__state > ?", 1)
  1418. } else if T_handle == 3 {
  1419. db.Where("t__state = ?", 3)
  1420. } else {
  1421. db.Where("t__state > ?", 0)
  1422. }
  1423. return db
  1424. }
  1425. }
  1426. // 公司管理报警列表
  1427. func Gorm_Read_Company_Warning_List(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
  1428. T_pid int, countRedisKey string) (r []CompanyWarning_R, cnt int64) {
  1429. gormReadCompanyWarningListScopes := GormReadCompanyWarningListScopes(T_pids, T_tp, T_name, T_handle, Time_start_, Time_end_)
  1430. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1431. var err error
  1432. var warnings []Warning
  1433. tableCount := make(map[string]int64)
  1434. // 从redis获取报警统计数量
  1435. cnt, tableCount, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
  1436. if err != nil {
  1437. for _, tableName := range tableNames {
  1438. tableDB := db.DB.Table(tableName)
  1439. var count int64
  1440. tableDB.Scopes(gormReadCompanyWarningListScopes).Count(&count)
  1441. tableCount[tableName] = count
  1442. cnt += count
  1443. }
  1444. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, tableCount)
  1445. }
  1446. if cnt == 0 {
  1447. return r, 0
  1448. }
  1449. offset := (page - 1) * page_z
  1450. remaining := offset
  1451. for _, tableName := range tableNames {
  1452. var tableWarning []Warning
  1453. tableDB := db.DB.Table(tableName)
  1454. count := tableCount[tableName]
  1455. if remaining >= int(count) {
  1456. remaining -= int(count)
  1457. continue
  1458. }
  1459. tableDB.Scopes(gormReadCompanyWarningListScopes).
  1460. Order("t__ut desc").Offset(remaining).Limit(page_z - len(warnings)).Find(&tableWarning)
  1461. warnings = append(warnings, tableWarning...)
  1462. if len(warnings) >= page_z {
  1463. break
  1464. }
  1465. remaining = 0
  1466. }
  1467. for _, v := range warnings {
  1468. r = append(r, WarningToCompanyWarning_R(0, v))
  1469. }
  1470. return r, cnt
  1471. }
  1472. func Gorm_Read_Company_Warning_List_Excel(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
  1473. T_pid int, countRedisKey string) (r []CompanyWarning_R, cnt int64) {
  1474. gormReadCompanyWarningListScopes := GormReadCompanyWarningListScopes(T_pids, T_tp, T_name, T_handle, Time_start_, Time_end_)
  1475. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1476. var warnings []Warning
  1477. var wg sync.WaitGroup
  1478. for _, tableName := range tableNames {
  1479. wg.Add(1)
  1480. go func(table string) {
  1481. defer wg.Done()
  1482. offset := 0
  1483. limit := 10000
  1484. for {
  1485. var tableWarning []Warning
  1486. tableDB := db.DB.Table(table)
  1487. tableDB.Scopes(gormReadCompanyWarningListScopes).
  1488. Order("t__ut desc").Offset(offset).Limit(limit).Find(&tableWarning)
  1489. if len(tableWarning) == 0 {
  1490. break // 如果没有数据则结束循环
  1491. }
  1492. offset += limit // 更新偏移量
  1493. warnings = append(warnings, tableWarning...)
  1494. }
  1495. }(tableName)
  1496. }
  1497. // 等待所有协程完成
  1498. wg.Wait()
  1499. // 倒序
  1500. sort.Slice(warnings, func(i, j int) bool {
  1501. if warnings[i].T_Ut.After(warnings[j].T_Ut) {
  1502. return true
  1503. }
  1504. return false
  1505. })
  1506. for _, v := range warnings {
  1507. r = append(r, WarningToCompanyWarning_R(0, v))
  1508. }
  1509. return r, cnt
  1510. }
  1511. func GenerateWarningTableNames(startDateStr string, endDateStr string) []string {
  1512. if len(startDateStr) == 0 || len(endDateStr) == 0 {
  1513. return []string{}
  1514. }
  1515. startDate, _ := lib.TimeStrToTime(startDateStr)
  1516. endDate, _ := lib.TimeStrToTime(endDateStr)
  1517. monthsArray := []string{}
  1518. for startDate.Before(endDate) || startDate.Equal(endDate) {
  1519. monthsArray = append(monthsArray, fmt.Sprintf("warning_%d_%02d", startDate.Year(), startDate.Month()))
  1520. startDate = time.Date(startDate.Year(), startDate.Month()+1, 1, 0, 0, 0, 0, time.Local)
  1521. }
  1522. sort.Stable(sort.Reverse(sort.StringSlice(monthsArray)))
  1523. return monthsArray
  1524. }
  1525. // 检查数据列表是否连续 返回最小值 最大值
  1526. func checkStringListContinuous(nums []string) (int, int, bool) {
  1527. intNums := make([]int, len(nums))
  1528. for i, num := range nums {
  1529. parsedNum, err := strconv.Atoi(num)
  1530. if err != nil {
  1531. fmt.Println("Error converting string to int:", err)
  1532. return 0, 0, false
  1533. }
  1534. intNums[i] = parsedNum
  1535. }
  1536. sort.Ints(intNums)
  1537. for i := 1; i < len(intNums); i++ {
  1538. if intNums[i] != intNums[i-1]+1 {
  1539. return 0, 0, false
  1540. }
  1541. }
  1542. return intNums[0], intNums[len(intNums)-1], true
  1543. }