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