Device.go 11 KB

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