Device.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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_monitor int `orm:"size(22);null"` // 监控状态 1 监控 0 未监控
  30. T_online int `orm:"size(22);1"` // 在线状态 1 在线 0 离线
  31. T_online_4g int `orm:"size(22);0"` // 在线状态4G 0 包含(2 离线 3 异常断开) 1 在线 2 离线 3 异常断开
  32. T_Dattery int `orm:"size(256);null"` //电量
  33. T_err int `orm:"size(2);1"` // 0 正常 1 异常
  34. T_State int `orm:"size(2);1"` // 0 删除 1 正常
  35. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  36. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  37. }
  38. type R_Device struct {
  39. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  40. T_devName string //设备名称
  41. T_sensor int // 传感器数量
  42. T_monitor int // 监控状态 1 监控 0 未监控
  43. T_give int // 0 丢弃 1 正常
  44. T_online int // 在线状态 1 在线 0 离线
  45. T_online_4g int // 在线状态4G 1 在线 0 离线
  46. T_Dattery int //电量
  47. T_sensor_list []DeviceSensor_R // 传感器List
  48. T_MSISDN string //MSISDN
  49. UpdateTime time.Time //auto_now 每次 model 保存时都会对时间自动更新
  50. }
  51. type Device_task struct {
  52. T_sn string
  53. T_task string
  54. }
  55. func (t *Device) TableName() string {
  56. return "Device" // 数据库名称 // ************** 替换 FormulaList **************
  57. }
  58. var redisCache_Device cache.Cache
  59. func init() {
  60. //注册模型
  61. orm.RegisterModel(new(Device))
  62. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  63. "redis_Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  64. fmt.Println(config)
  65. var err error
  66. redisCache_Device, err = cache.NewCache("redis", config)
  67. if err != nil || redisCache_Device == nil {
  68. errMsg := "failed to init redis"
  69. fmt.Println(errMsg, err)
  70. }
  71. }
  72. // ---------------- Redis -------------------
  73. //Redis_Set(m.T_sn,m) // Redis 更新缓存
  74. func Redis_Set(r Device) (err error) {
  75. //json序列化
  76. str, err := json.Marshal(r)
  77. if err != nil {
  78. fmt.Print(err)
  79. return
  80. }
  81. err = redisCache_Device.Put(r.T_sn, str, 24*time.Hour)
  82. if err != nil {
  83. fmt.Println("set key:", r.T_sn, ",value:", str, err)
  84. }
  85. return
  86. }
  87. //if r,is :=Redis_Get(T_sn);is{
  88. //return r,nil
  89. //}
  90. func Redis_Get(key string) (r Device, is bool) {
  91. if redisCache_Device.IsExist(key) {
  92. //println("找到key:",key)
  93. v := redisCache_Device.Get(key)
  94. json.Unmarshal(v.([]byte), &r)
  95. return r, true
  96. }
  97. //println("没有 找到key:",key)
  98. return Device{}, false
  99. }
  100. func Redis_DelK(key string) (err error) {
  101. err = redisCache_Device.Delete(key)
  102. return
  103. }
  104. // ---------------- 特殊方法 -------------------
  105. // 获取 ById
  106. func Read_Device_ById(id int) (r Device) {
  107. o := orm.NewOrm()
  108. r = Device{Id: id}
  109. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  110. if err != nil {
  111. fmt.Println(err)
  112. }
  113. return r
  114. }
  115. // 获取 ById
  116. func Read_Device_ByT_sn(T_sn string) (r Device, err error) {
  117. if r, is := Redis_Get(T_sn); is {
  118. //println("Redis_Get OK")
  119. return r, nil
  120. }
  121. //println("没有 Redis_Get SN")
  122. o := orm.NewOrm()
  123. r = Device{T_sn: T_sn, T_State: 1}
  124. err = o.Read(&r, "T_sn", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  125. if err != nil {
  126. fmt.Println(err)
  127. } else {
  128. Redis_Set(r) // Redis 更新缓存
  129. }
  130. return r, err
  131. }
  132. // 添加
  133. func Add_Device(m Device) (id int64, err error) {
  134. o := orm.NewOrm()
  135. id, err = o.Insert(&m)
  136. if err != nil {
  137. fmt.Println(err)
  138. }
  139. m.Id = int(id)
  140. Redis_Set(m) // Redis 更新缓存
  141. return id, err
  142. }
  143. // 删除
  144. func Delete_Device(m Device) (err error) {
  145. o := orm.NewOrm()
  146. if num, err := o.Delete(&m); err == nil {
  147. fmt.Println("Number of records deleted in database:", num)
  148. }
  149. Redis_DelK(m.T_sn)
  150. return
  151. }
  152. // 修改
  153. func Update_Device(r Device, cols ...string) bool {
  154. o := orm.NewOrm()
  155. if num, err := o.Update(&r, cols...); err == nil {
  156. fmt.Println("Number of records updated in database:", num)
  157. Redis_Set(r) // Redis 更新缓存
  158. return true
  159. }
  160. return false
  161. }
  162. // 修改
  163. //func Update_Device_T_devName(T_sn string, T_devName string) (err error) {
  164. // o := orm.NewOrm()
  165. // v := Device{T_sn: T_sn}
  166. // // ascertain id exists in the database
  167. // if err = o.Read(&v, "T_sn"); err == nil {
  168. // v.T_devName = T_devName
  169. // o.Update(&v, "T_devName")
  170. // }
  171. // Redis_Set( v) // Redis 更新缓存
  172. // return err
  173. //}
  174. // 修改
  175. //func Update_Device_T_l_p(T_sn string, T_l_p int) (err error) {
  176. // o := orm.NewOrm()
  177. // v := Device{T_sn: T_sn}
  178. // // ascertain id exists in the database
  179. // if err = o.Read(&v, "T_sn"); err == nil {
  180. // v.T_l_p = T_l_p
  181. // o.Update(&v, "T_l_p")
  182. // }
  183. // Redis_Set( v) // Redis 更新缓存
  184. // return err
  185. //}
  186. //// 获取列表
  187. //func Read_Device_ALL_1(user_ Admin.Admin, page int, T_sn string, T_devName string, class string) (r []Device, cnt int64) {
  188. //
  189. // o := orm.NewOrm()
  190. // // 也可以直接使用 Model 结构体作为表名
  191. //
  192. // qs := o.QueryTable(new(Device))
  193. // var offset int64
  194. // if page <= 1 {
  195. // offset = 0
  196. // } else {
  197. // offset = int64((page - 1) * conf.Page_size)
  198. // }
  199. // T_Bind := "U" + strconv.Itoa(user_.Id)
  200. // if user_.Admin_master <= 1 {
  201. // T_Bind = ""
  202. // }
  203. // fmt.Println("T_Bind:", T_Bind)
  204. // 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)
  205. // 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()
  206. //
  207. // return r, cnt
  208. //}
  209. // 获取列表
  210. 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) {
  211. o := orm.NewOrm()
  212. // 也可以直接使用 Model 结构体作为表名
  213. qs := o.QueryTable(new(Device))
  214. var offset int64
  215. if page_z == 0 {
  216. page_z = conf.Page_size
  217. }
  218. if page <= 1 {
  219. offset = 0
  220. } else {
  221. offset = int64((page - 1) * page_z)
  222. }
  223. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  224. if user_.Admin_master <= 1 {
  225. T_Bind = ""
  226. }
  227. fmt.Println("T_Bind:", T_Bind)
  228. cond := orm.NewCondition()
  229. 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)
  230. if T_monitor == "1" {
  231. //cond1.AndCond(cond.And("T_class", class))
  232. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_monitor", 1))
  233. } else if T_monitor == "0" {
  234. //cond1.AndCond(cond.And("T_class", class))
  235. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_monitor", 0))
  236. }
  237. if T_online == "1" {
  238. //cond1.AndCond(cond.And("T_class", class))
  239. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_online", 1))
  240. } else if T_online == "2" {
  241. //cond1.AndCond(cond.And("T_class", class))
  242. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_online", 2))
  243. } else if T_online == "3" {
  244. //cond1.AndCond(cond.And("T_class", class))
  245. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_online", 3))
  246. } else if T_online == "0" {
  247. //cond1.AndCond(cond.And("T_class", class))
  248. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_online", 0).Or("T_online", 2).Or("T_online", 3))
  249. }
  250. // 非内部权限
  251. //println("user_.Admin_power:", user_.Admin_power)
  252. if user_.Admin_power > 6 {
  253. cond1 = cond.AndCond(cond1).AndCond(cond.And("T_give", 1))
  254. }
  255. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").OrderBy("-T_give").All(&r)
  256. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  257. return r, cnt
  258. }
  259. // 获取列表
  260. func Read_Device_ALL_bind(user_ Admin.Admin) (r []Device) {
  261. o := orm.NewOrm()
  262. // 也可以直接使用 Model 结构体作为表名
  263. qs := o.QueryTable(new(Device))
  264. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  265. if user_.Admin_master <= 1 {
  266. T_Bind = ""
  267. }
  268. fmt.Println("T_Bind:", T_Bind)
  269. qs.Filter("T_Bind__icontains", T_Bind).All(&r)
  270. return r
  271. }
  272. // 获取列表
  273. func Read_Device_ALL_bind_T_l_p(user_ Admin.Admin, T_l_p int) (r []Device) {
  274. o := orm.NewOrm()
  275. // 也可以直接使用 Model 结构体作为表名
  276. qs := o.QueryTable(new(Device))
  277. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  278. if user_.Admin_master <= 1 {
  279. T_Bind = ""
  280. }
  281. fmt.Println("T_Bind:", T_Bind)
  282. qs.Filter("T_Bind__icontains", T_Bind).Filter("T_l_p", T_l_p).All(&r)
  283. return r
  284. }
  285. // 获取列表
  286. func Read_Device_ALL_T_sn_T_devName_bind_1(T_sn string, T_devName string, user_ Admin.Admin) (r []Device) {
  287. o := orm.NewOrm()
  288. // 也可以直接使用 Model 结构体作为表名
  289. qs := o.QueryTable(new(Device))
  290. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  291. if user_.Admin_master <= 1 {
  292. T_Bind = ""
  293. }
  294. fmt.Println("T_Bind:", T_Bind)
  295. 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)
  296. return r
  297. }
  298. // 获取列表
  299. func Read_Device_ALL_T_sn_bind_1(T_sn string, user_ Admin.Admin) (r []Device) {
  300. o := orm.NewOrm()
  301. // 也可以直接使用 Model 结构体作为表名
  302. qs := o.QueryTable(new(Device))
  303. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  304. qs.Filter("T_Bind__icontains", T_Bind).Filter("T_sn__icontains", T_sn).Filter("T_State", 1).All(&r)
  305. return r
  306. }
  307. // 修改
  308. func Device_Bind_Add(T_sn string, user_ Admin.Admin) string {
  309. o := orm.NewOrm()
  310. v := Device{T_sn: T_sn}
  311. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  312. if err := o.Read(&v, "T_sn"); err == nil {
  313. v.T_Bind = strings.Replace(v.T_Bind, T_Bind, "", -1)
  314. v.T_Bind = v.T_Bind + T_Bind
  315. o.Update(&v, "T_Bind")
  316. }
  317. Redis_Set(v) // Redis 更新缓存
  318. return v.T_Bind
  319. }
  320. // 修改
  321. func Device_Bind_Del(T_sn string, user_ Admin.Admin) string {
  322. o := orm.NewOrm()
  323. v := Device{T_sn: T_sn}
  324. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  325. if err := o.Read(&v, "T_sn"); err == nil {
  326. v.T_Bind = strings.Replace(v.T_Bind, T_Bind, "", -1)
  327. o.Update(&v, "T_Bind")
  328. }
  329. Redis_Set(v) // Redis 更新缓存
  330. return v.T_Bind
  331. }
  332. // 修改
  333. func Device_Bind_ALL_Del(admin_r Admin.Admin) {
  334. o := orm.NewOrm()
  335. qs := o.QueryTable(new(Device))
  336. var r []Device
  337. T_Bind := "U" + strconv.Itoa(admin_r.Id) + "|"
  338. qs.Filter("T_Bind__icontains", T_Bind).All(&r)
  339. for _, v := range r {
  340. v.T_Bind = strings.Replace(v.T_Bind, T_Bind, "", -1)
  341. o.Update(&v, "T_Bind")
  342. Redis_Set(v) // Redis 更新缓存
  343. }
  344. }
  345. //
  346. func Read_Device_ALL_T_Bind_Count(user_ Admin.Admin, T_sn string, T_l_p int) int {
  347. o := orm.NewOrm()
  348. // 也可以直接使用 Model 结构体作为表名
  349. qs := o.QueryTable(new(Device))
  350. T_Bind := "U" + strconv.Itoa(user_.Id)
  351. if user_.Admin_master <= 1 {
  352. T_Bind = ""
  353. }
  354. fmt.Println("T_Bind:", T_Bind)
  355. var cnt int64
  356. if T_l_p > 0 {
  357. cnt, _ = qs.Filter("T_sn__icontains", T_sn).Filter("T_Bind__icontains", T_Bind).Filter("T_l_p", T_l_p).Count()
  358. return int(cnt)
  359. }
  360. cnt, _ = qs.Filter("T_sn__icontains", T_sn).Filter("T_Bind__icontains", T_Bind).Count()
  361. return int(cnt)
  362. }