Device.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/models/Admin"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. _ "github.com/astaxie/beego/cache/redis"
  9. orm2 "github.com/beego/beego/v2/client/orm"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. _ "github.com/go-sql-driver/mysql"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // 模板
  17. type Device struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_sn string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  20. T_MSISDN string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  21. T_devName string `orm:"size(256);null"` //设备名称
  22. //T_class int `orm:"size(200);null"` // 分类
  23. //T_img string `orm:"size(256);null"` // 图片
  24. T_sensor int `orm:"size(22);null"` // 传感器数量
  25. T_l_p int `orm:"size(22);null"` // 1物流端 2药店端
  26. T_give int `orm:"size(2);1"` // 0 丢弃 1 正常
  27. T_Putime_Parameter time.Time `orm:"type(timestamp);null;"` // 参数时间
  28. T_Bind string `orm:"size(256);null"` //设备绑定 Uid
  29. T_protocol int `orm:"size(2);1"` // 1 1.0协议 2 2.0协议
  30. T_monitor int `orm:"size(22);null"` // 监控状态 1 监控 0 未监控
  31. T_online int `orm:"size(22);1"` // 在线状态 1 在线 0 离线
  32. T_online_4g int `orm:"size(22);0"` // 在线状态4G 0 包含(2 离线 3 异常断开) 1 在线 2 离线 3 异常断开
  33. T_Dattery int `orm:"size(256);null"` //电量
  34. T_err int `orm:"size(2);1"` // 0 正常 1 异常
  35. T_State int `orm:"size(2);1"` // 0 删除 1 正常
  36. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  37. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  38. }
  39. type R_Device struct {
  40. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  41. T_devName string //设备名称
  42. T_sensor int // 传感器数量
  43. T_monitor int // 监控状态 1 监控 0 未监控
  44. T_give int // 0 丢弃 1 正常
  45. T_online int // 在线状态 1 在线 0 离线
  46. T_online_4g int // 在线状态4G 1 在线 0 离线
  47. T_Dattery int //电量
  48. T_protocol int //协议
  49. T_sensor_list []DeviceSensor_R // 传感器List
  50. T_MSISDN string //MSISDN
  51. UpdateTime time.Time //auto_now 每次 model 保存时都会对时间自动更新
  52. }
  53. type R2_Device struct {
  54. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  55. T_MSISDN string //MSISDN
  56. T_devName string //设备名称
  57. T_sensor int // 传感器数量
  58. T_monitor int // 监控状态 1 监控 0 未监控
  59. T_give int // 0 丢弃 1 正常
  60. T_online int // 在线状态 1 在线 0 离线
  61. T_online_4g int // 在线状态4G 1 在线 0 离线
  62. T_Dattery int //电量
  63. T_protocol int //协议
  64. UpdateTime time.Time //auto_now 每次 model 保存时都会对时间自动更新
  65. }
  66. type Device_task struct {
  67. T_sn string
  68. T_task string
  69. }
  70. func (t *Device) TableName() string {
  71. return "Device" // 数据库名称 // ************** 替换 FormulaList **************
  72. }
  73. var redisCache_Device cache.Cache
  74. func init() {
  75. //注册模型
  76. orm.RegisterModel(new(Device))
  77. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  78. "redis_Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  79. fmt.Println(config)
  80. var err error
  81. redisCache_Device, err = cache.NewCache("redis", config)
  82. if err != nil || redisCache_Device == nil {
  83. errMsg := "failed to init redis"
  84. fmt.Println(errMsg, err)
  85. }
  86. }
  87. func DeviceToR2_Device(r Device) (t R2_Device) {
  88. t.T_sn = r.T_sn
  89. t.T_MSISDN = r.T_MSISDN
  90. t.T_devName = r.T_devName
  91. t.T_sensor = r.T_sensor
  92. t.T_monitor = r.T_monitor
  93. t.T_give = r.T_give
  94. t.T_online = r.T_online
  95. t.T_online_4g = r.T_online_4g
  96. t.T_Dattery = r.T_Dattery
  97. t.T_protocol = r.T_protocol
  98. t.UpdateTime = r.UpdateTime
  99. //
  100. return
  101. }
  102. // ---------------- Redis -------------------
  103. //Redis_Set(m.T_sn,m) // Redis 更新缓存
  104. func Redis_Set(r Device) (err error) {
  105. //json序列化
  106. str, err := json.Marshal(r)
  107. if err != nil {
  108. fmt.Print(err)
  109. return
  110. }
  111. err = redisCache_Device.Put(r.T_sn, str, 24*time.Hour)
  112. if err != nil {
  113. fmt.Println("set key:", r.T_sn, ",value:", str, err)
  114. }
  115. return
  116. }
  117. //if r,is :=Redis_Get(T_sn);is{
  118. //return r,nil
  119. //}
  120. func Redis_Get(key string) (r Device, is bool) {
  121. if redisCache_Device.IsExist(key) {
  122. //println("找到key:",key)
  123. v := redisCache_Device.Get(key)
  124. json.Unmarshal(v.([]byte), &r)
  125. return r, true
  126. }
  127. //println("没有 找到key:",key)
  128. return Device{}, false
  129. }
  130. func Redis_DelK(key string) (err error) {
  131. err = redisCache_Device.Delete(key)
  132. return
  133. }
  134. // ---------------- 特殊方法 -------------------
  135. // 获取 ById
  136. func Read_Device_ById(id int) (r Device) {
  137. o := orm.NewOrm()
  138. r = Device{Id: id}
  139. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  140. if err != nil {
  141. fmt.Println(err)
  142. }
  143. return r
  144. }
  145. // 获取 ById
  146. func Read_Device_ByT_sn(T_sn string) (r Device, err error) {
  147. if r, is := Redis_Get(T_sn); is {
  148. //println("Redis_Get OK")
  149. return r, nil
  150. }
  151. //println("没有 Redis_Get SN")
  152. o := orm.NewOrm()
  153. r = Device{T_sn: T_sn, T_State: 1}
  154. err = o.Read(&r, "T_sn", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  155. if err != nil {
  156. fmt.Println(err)
  157. } else {
  158. Redis_Set(r) // Redis 更新缓存
  159. }
  160. return r, err
  161. }
  162. // 添加
  163. func Add_Device(m Device) (id int64, err error) {
  164. o := orm.NewOrm()
  165. id, err = o.Insert(&m)
  166. if err != nil {
  167. fmt.Println(err)
  168. }
  169. m.Id = int(id)
  170. Redis_Set(m) // Redis 更新缓存
  171. return id, err
  172. }
  173. // 删除
  174. func Delete_Device(m Device) (err error) {
  175. o := orm.NewOrm()
  176. if num, err := o.Delete(&m); err == nil {
  177. fmt.Println("Number of records deleted in database:", num)
  178. }
  179. Redis_DelK(m.T_sn)
  180. return
  181. }
  182. // 修改
  183. func Update_Device(r Device, cols ...string) bool {
  184. o := orm.NewOrm()
  185. if num, err := o.Update(&r, cols...); err == nil {
  186. fmt.Println("Number of records updated in database:", num)
  187. Redis_Set(r) // Redis 更新缓存
  188. return true
  189. }
  190. return false
  191. }
  192. // 修改
  193. //func Update_Device_T_devName(T_sn string, T_devName string) (err error) {
  194. // o := orm.NewOrm()
  195. // v := Device{T_sn: T_sn}
  196. // // ascertain id exists in the database
  197. // if err = o.Read(&v, "T_sn"); err == nil {
  198. // v.T_devName = T_devName
  199. // o.Update(&v, "T_devName")
  200. // }
  201. // Redis_Set( v) // Redis 更新缓存
  202. // return err
  203. //}
  204. // 修改
  205. //func Update_Device_T_l_p(T_sn string, T_l_p int) (err error) {
  206. // o := orm.NewOrm()
  207. // v := Device{T_sn: T_sn}
  208. // // ascertain id exists in the database
  209. // if err = o.Read(&v, "T_sn"); err == nil {
  210. // v.T_l_p = T_l_p
  211. // o.Update(&v, "T_l_p")
  212. // }
  213. // Redis_Set( v) // Redis 更新缓存
  214. // return err
  215. //}
  216. //// 获取列表
  217. //func Read_Device_ALL_1(user_ Admin.Admin, page int, T_sn string, T_devName string, class string) (r []Device, cnt int64) {
  218. //
  219. // o := orm.NewOrm()
  220. // // 也可以直接使用 Model 结构体作为表名
  221. //
  222. // qs := o.QueryTable(new(Device))
  223. // var offset int64
  224. // if page <= 1 {
  225. // offset = 0
  226. // } else {
  227. // offset = int64((page - 1) * conf.Page_size)
  228. // }
  229. // T_Bind := "U" + strconv.Itoa(user_.Id)
  230. // if user_.Admin_master <= 1 {
  231. // T_Bind = ""
  232. // }
  233. // fmt.Println("T_Bind:", T_Bind)
  234. // qs.Limit(conf.Page_size, offset).Filter("T_Bind__icontains", T_Bind).Filter("T_sn__icontains", T_sn).Filter("T_devName__icontains", T_devName).OrderBy("-UpdateTime").Filter("T_State", 1).All(&r)
  235. // cnt, _ = qs.Filter("T_Bind__icontains", T_Bind).Filter("T_sn__icontains", T_sn).Filter("T_devName__icontains", T_devName).Filter("T_State", 1).Count()
  236. //
  237. // return r, cnt
  238. //}
  239. // 获取列表
  240. func Read_Device_ALL_bind_1(user_ Admin.Admin, page int, page_z int, T_sn string, T_devName string, class string, T_monitor string, T_online string) (r []Device, cnt int64) {
  241. o := orm.NewOrm()
  242. // 也可以直接使用 Model 结构体作为表名
  243. qs := o.QueryTable(new(Device))
  244. var offset int64
  245. if page_z == 0 {
  246. page_z = conf.Page_size
  247. }
  248. if page <= 1 {
  249. offset = 0
  250. } else {
  251. offset = int64((page - 1) * page_z)
  252. }
  253. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  254. if user_.Admin_master <= 1 {
  255. T_Bind = ""
  256. }
  257. fmt.Println("T_Bind:", T_Bind)
  258. cond := orm.NewCondition()
  259. cond1 := cond.And("T_Bind__icontains", T_Bind).And("T_sn__icontains", T_sn).And("T_devName__icontains", T_devName).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  260. if T_monitor == "1" {
  261. //cond1.AndCond(cond.And("T_class", class))
  262. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_monitor", 1))
  263. } else if T_monitor == "0" {
  264. //cond1.AndCond(cond.And("T_class", class))
  265. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_monitor", 0))
  266. }
  267. if T_online == "1" {
  268. //cond1.AndCond(cond.And("T_class", class))
  269. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_online", 1))
  270. } else if T_online == "2" {
  271. //cond1.AndCond(cond.And("T_class", class))
  272. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_online", 2))
  273. } else if T_online == "3" {
  274. //cond1.AndCond(cond.And("T_class", class))
  275. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_online", 3))
  276. } else if T_online == "0" {
  277. //cond1.AndCond(cond.And("T_class", class))
  278. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_online", 0).Or("T_online", 2).Or("T_online", 3))
  279. }
  280. // 非内部权限
  281. //println("user_.Admin_power:", user_.Admin_power)
  282. if user_.Admin_power > 6 {
  283. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_give", 1))
  284. }
  285. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").OrderBy("-T_give").All(&r)
  286. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  287. return r, cnt
  288. }
  289. // 获取列表
  290. func Read_Device_v2_List(user_ Admin.Admin, T_name string, T_monitor string, T_online string, page int, page_z int) (r []R2_Device, cnt int64) {
  291. o := orm.NewOrm()
  292. // 也可以直接使用 Model 结构体作为表名
  293. qs := o.QueryTable(new(Device))
  294. var offset int64
  295. if page_z == 0 {
  296. page_z = conf.Page_size
  297. }
  298. if page <= 1 {
  299. offset = 0
  300. } else {
  301. offset = int64((page - 1) * page_z)
  302. }
  303. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  304. //最高管理、一级账户、没有任何限制
  305. if user_.Admin_master <= 1 {
  306. T_Bind = ""
  307. }
  308. cond := orm.NewCondition()
  309. cond1 := cond.And("T_Bind__icontains", T_Bind).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  310. if len(T_name) > 0 {
  311. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).Or("T_devName__icontains", T_name).Or("T_MSISDN__icontains", T_name))
  312. }
  313. if len(T_monitor) > 0 {
  314. T_monitor_int, err := strconv.Atoi(T_monitor)
  315. if err == nil {
  316. cond1 = cond1.AndCond(cond.And("T_monitor", T_monitor_int))
  317. }
  318. }
  319. if T_online == "1" {
  320. cond1 = cond1.AndCond(cond.And("T_online", 1))
  321. } else if T_online == "2" {
  322. cond1 = cond1.AndCond(cond.And("T_online", 2))
  323. } else if T_online == "3" {
  324. cond1 = cond1.AndCond(cond.And("T_online", 3))
  325. } else if T_online == "0" {
  326. cond1 = cond1.AndCond(cond.And("T_online", 0).Or("T_online", 2).Or("T_online", 3))
  327. }
  328. // 非内部权限 - 丢弃
  329. if user_.Admin_power > 6 {
  330. cond1 = cond1.AndCond(cond.And("T_give", 1))
  331. }
  332. var rx []Device
  333. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").OrderBy("-T_give").All(&rx)
  334. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  335. for _, v := range rx {
  336. r = append(r, DeviceToR2_Device(v))
  337. }
  338. return r, cnt
  339. }
  340. // 获取列表
  341. func Read_Device_ALL_bind(user_ Admin.Admin) (r []Device) {
  342. o := orm.NewOrm()
  343. // 也可以直接使用 Model 结构体作为表名
  344. qs := o.QueryTable(new(Device))
  345. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  346. if user_.Admin_master <= 1 {
  347. T_Bind = ""
  348. }
  349. fmt.Println("T_Bind:", T_Bind)
  350. qs.Filter("T_Bind__icontains", T_Bind).All(&r)
  351. return r
  352. }
  353. // 获取列表
  354. func Read_Device_ALL_bind_T_l_p(user_ Admin.Admin, T_l_p int) (r []Device) {
  355. o := orm.NewOrm()
  356. // 也可以直接使用 Model 结构体作为表名
  357. qs := o.QueryTable(new(Device))
  358. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  359. if user_.Admin_master <= 1 {
  360. T_Bind = ""
  361. }
  362. fmt.Println("T_Bind:", T_Bind)
  363. qs.Filter("T_Bind__icontains", T_Bind).Filter("T_l_p", T_l_p).All(&r)
  364. return r
  365. }
  366. // 获取列表
  367. func Read_Device_ALL_T_sn_T_devName_bind_1(T_sn string, T_devName string, user_ Admin.Admin) (r []Device) {
  368. o := orm.NewOrm()
  369. // 也可以直接使用 Model 结构体作为表名
  370. qs := o.QueryTable(new(Device))
  371. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  372. if user_.Admin_master <= 1 {
  373. T_Bind = ""
  374. }
  375. fmt.Println("T_Bind:", T_Bind)
  376. qs.Filter("T_Bind__icontains", T_Bind).Filter("T_sn__icontains", T_sn).Filter("T_devName__icontains", T_devName).Filter("T_State", 1).All(&r)
  377. return r
  378. }
  379. // 获取列表
  380. func Read_Device_ALL_T_sn_bind_1(T_sn string, user_ Admin.Admin) (r []Device) {
  381. o := orm.NewOrm()
  382. // 也可以直接使用 Model 结构体作为表名
  383. qs := o.QueryTable(new(Device))
  384. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  385. qs.Filter("T_Bind__icontains", T_Bind).Filter("T_sn__icontains", T_sn).Filter("T_State", 1).All(&r)
  386. return r
  387. }
  388. // 修改
  389. func Device_Bind_Add(T_sn string, user_ Admin.Admin) string {
  390. o := orm.NewOrm()
  391. v := Device{T_sn: T_sn}
  392. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  393. if err := o.Read(&v, "T_sn"); err == nil {
  394. v.T_Bind = strings.Replace(v.T_Bind, T_Bind, "", -1)
  395. v.T_Bind = v.T_Bind + T_Bind
  396. o.Update(&v, "T_Bind")
  397. }
  398. Redis_Set(v) // Redis 更新缓存
  399. return v.T_Bind
  400. }
  401. // 修改
  402. func Device_Bind_Del(T_sn string, user_ Admin.Admin) string {
  403. o := orm.NewOrm()
  404. v := Device{T_sn: T_sn}
  405. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  406. if err := o.Read(&v, "T_sn"); err == nil {
  407. v.T_Bind = strings.Replace(v.T_Bind, T_Bind, "", -1)
  408. o.Update(&v, "T_Bind")
  409. }
  410. Redis_Set(v) // Redis 更新缓存
  411. return v.T_Bind
  412. }
  413. // 修改
  414. func Device_Bind_ALL_Del(admin_r Admin.Admin) {
  415. o := orm.NewOrm()
  416. qs := o.QueryTable(new(Device))
  417. var r []Device
  418. T_Bind := "U" + strconv.Itoa(admin_r.Id) + "|"
  419. qs.Filter("T_Bind__icontains", T_Bind).All(&r)
  420. for _, v := range r {
  421. v.T_Bind = strings.Replace(v.T_Bind, T_Bind, "", -1)
  422. o.Update(&v, "T_Bind")
  423. Redis_Set(v) // Redis 更新缓存
  424. }
  425. }
  426. //
  427. func Read_Device_ALL_T_Bind_Count(user_ Admin.Admin, T_sn string, T_l_p int) int {
  428. o := orm.NewOrm()
  429. // 也可以直接使用 Model 结构体作为表名
  430. qs := o.QueryTable(new(Device))
  431. T_Bind := "U" + strconv.Itoa(user_.Id)
  432. if user_.Admin_master <= 1 {
  433. T_Bind = ""
  434. }
  435. fmt.Println("T_Bind:", T_Bind)
  436. var cnt int64
  437. if T_l_p > 0 {
  438. cnt, _ = qs.Filter("T_sn__icontains", T_sn).Filter("T_Bind__icontains", T_Bind).Filter("T_l_p", T_l_p).Count()
  439. return int(cnt)
  440. }
  441. cnt, _ = qs.Filter("T_sn__icontains", T_sn).Filter("T_Bind__icontains", T_Bind).Count()
  442. return int(cnt)
  443. }