Device.go 11 KB

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