DeviceSensor.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/models/Account"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. _ "github.com/astaxie/beego/cache/redis"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. orm2 "github.com/beego/beego/v2/client/orm"
  12. _ "github.com/go-sql-driver/mysql"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // 模板
  18. type DeviceSensor struct {
  19. Id int `orm:"column(ID);size(11);auto;pk"`
  20. T_sn string `orm:"index;size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  21. T_id int `orm:"index;size(11);null"` // 传感器编号
  22. T_name string `orm:"size(256);null"` // 标题
  23. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  24. T_Class string `orm:"size(256);null"` // Device.DeviceClass.Id 设备分类 C1|C2|
  25. T_Notice string `orm:"size(256);null"` // Device.DeviceNotice.Id 设备通知策略 N1|N2|
  26. T_datashow int `orm:"index;size(2);default(1)"` // 0 屏蔽数据展示 1 正常数据展示
  27. T_sort int `orm:"index;size(200);1"` // 排序
  28. T_3dview string `orm:"size(256);null"` // 3D 视图ID
  29. // 设备同步参数
  30. T_Dattery int `orm:"size(4);null"` // 电量
  31. T_Site string `orm:"size(200);null"` // GPS
  32. T_type int `orm:"index;size(4);null"` // Device.DeviceType 1库房 2移动
  33. T_give int `orm:"index;size(2);default(1)"` // 屏蔽状态 0 屏蔽 1 正常
  34. T_monitor int `orm:"index;size(2);null"` // 监控状态 0 未监控 1 监控
  35. T_online int `orm:"index;size(2);default(1)"` // 在线状态 0 未启用 1 在线 2 离线
  36. T_online_s int `orm:"index;size(2);default(0)"` // 在线状态-备用 0 未启用 1 在线 2 离线
  37. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  38. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  39. }
  40. type DeviceSensor_Del struct {
  41. T_sn string
  42. T_id int
  43. }
  44. func (t *DeviceSensor) TableName() string {
  45. return "device_sensor" // 数据库名称 // ************** 替换 FormulaList **************
  46. }
  47. // 模板
  48. type DeviceSensor_R struct {
  49. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  50. T_id int // 传感器编号
  51. T_name string // 标题
  52. T_3dview string // 3D 视图ID
  53. T_sort int // 排序
  54. T_Dattery int // 电量
  55. T_Site string // GPS
  56. T_monitor int // 记录状态
  57. T_online int // 在线状态 1 在线 0 离线
  58. T_datashow int // 0 屏蔽数据展示 1 正常数据展示
  59. T_DeviceSensorData DeviceData_R // 传感器最新数据
  60. T_DeviceSensorParameter DeviceSensorParameter_R // 设备参数
  61. }
  62. var redisCache_DeviceSensor cache.Cache
  63. func init() {
  64. //注册模型
  65. orm.RegisterModel(new(DeviceSensor))
  66. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  67. "redis_DeviceSensor", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  68. fmt.Println(config)
  69. var err error
  70. redisCache_DeviceSensor, err = cache.NewCache("redis", config)
  71. if err != nil || redisCache_DeviceSensor == nil {
  72. errMsg := "failed to init redis"
  73. fmt.Println(errMsg, err)
  74. }
  75. }
  76. // ---------------- Redis -------------------
  77. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  78. func Redis_DeviceSensor_Set(r DeviceSensor) (err error) {
  79. key := r.T_sn + "|" + strconv.Itoa(r.T_id)
  80. //json序列化
  81. str, err := json.Marshal(r)
  82. if err != nil {
  83. fmt.Print(err)
  84. return
  85. }
  86. err = redisCache_DeviceSensor.Put(key, str, 24*time.Hour)
  87. if err != nil {
  88. fmt.Println("set key:", key, ",value:", str, err)
  89. }
  90. return
  91. }
  92. func Redis_DeviceSensor_Get(key string) (r DeviceSensor, is bool) {
  93. if redisCache_DeviceSensor.IsExist(key) {
  94. //println("找到key:",key)
  95. v := redisCache_DeviceSensor.Get(key)
  96. json.Unmarshal(v.([]byte), &r)
  97. return r, true
  98. }
  99. //println("没有 找到key:",key)
  100. return DeviceSensor{}, false
  101. }
  102. func Redis_DeviceSensor_DelK(r DeviceSensor) (err error) {
  103. key := r.T_sn + "|" + strconv.Itoa(r.T_id)
  104. err = redisCache_DeviceSensor.Delete(key)
  105. return
  106. }
  107. // ---------------- 特殊方法 -------------------
  108. func DeviceSensorToDeviceSensor_R(DeviceSensor_ DeviceSensor) (DeviceSensor_r DeviceSensor_R) {
  109. lib.DeviceRealSnMap[DeviceSensor_r.T_sn] = 3 // 连续请求 实时数据
  110. DeviceSensor_r.T_sn = DeviceSensor_.T_sn
  111. DeviceSensor_r.T_id = DeviceSensor_.T_id
  112. DeviceSensor_r.T_name = DeviceSensor_.T_name
  113. DeviceSensor_r.T_Site = DeviceSensor_.T_Site
  114. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  115. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  116. DeviceSensor_r.T_3dview = DeviceSensor_.T_3dview
  117. DeviceSensor_r.T_sort = DeviceSensor_.T_sort
  118. DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow
  119. // 最新系统参数
  120. DeviceSensor_r.T_DeviceSensorParameter, _ = Read_DeviceSensorParameter(DeviceSensor_.T_sn, DeviceSensor_.T_id)
  121. // 最新数据
  122. key_data := DeviceSensor_.T_sn + "|" + strconv.Itoa(DeviceSensor_.T_id)
  123. DeviceSensor_r.T_DeviceSensorData, _ = RedisDeviceData_Get(key_data)
  124. return
  125. }
  126. // ---------------- 特殊方法 -------------------
  127. // ----------------------------------- v2 -------------------------------------------------------------
  128. // 获取列表 - 总数
  129. func Read_V2_DeviceSensor_ByTsn_num(T_sn string) int {
  130. o := orm.NewOrm()
  131. // 也可以直接使用 Model 结构体作为表名
  132. qs := o.QueryTable(new(DeviceSensor))
  133. cnt, _ := qs.Filter("T_sn", T_sn).Count()
  134. return int(cnt)
  135. }
  136. // 获取列表
  137. func Read_DeviceSensorList(admin_r Account.Admin, T_pid int, T_sn string, T_name string, T_Class_id, T_Notice_id int, T_give int, T_datashow int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  138. o := orm.NewOrm()
  139. // 也可以直接使用 Model 结构体作为表名
  140. qs := o.QueryTable(new(DeviceSensor))
  141. var offset int64
  142. if page_z == 0 {
  143. page_z = conf.Page_size
  144. }
  145. if page <= 1 {
  146. offset = 0
  147. } else {
  148. offset = int64((page - 1) * page_z)
  149. }
  150. var r []DeviceSensor
  151. cond := orm.NewCondition()
  152. cond1 := cond.And("T_pid", T_pid)
  153. if T_Class_id > 0 {
  154. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  155. fmt.Println("T_Class:", T_Class)
  156. cond1 = cond1.And("T_Class__icontains", T_Class)
  157. }
  158. if T_Notice_id > 0 {
  159. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  160. fmt.Println("T_Class:", T_Notice)
  161. cond1 = cond1.And("T_Notice__icontains", T_Notice)
  162. }
  163. if len(T_sn) > 0 {
  164. cond1 = cond1.And("T_sn__icontains", T_sn)
  165. }
  166. if len(T_name) > 0 {
  167. cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name))
  168. }
  169. // 不是内部权限(T_pid>0),只显示T_give=1 0 屏蔽 1 正常
  170. if admin_r.T_pid > 0 {
  171. cond1 = cond1.And("T_give", 1)
  172. } else if T_give != -1 {
  173. cond1 = cond1.And("T_give", T_give)
  174. }
  175. if T_datashow != -1 { // 空:正常显示 1:强制显示 屏蔽数据展示
  176. cond1 = cond1.And("T_datashow", 1)
  177. }
  178. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  179. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  180. //
  181. //qs.Limit(page_z, offset).Filter("T_Bind__icontains", T_Bind).Filter("T_Class__icontains", T_Class).Filter("T_sn__icontains", SN_type).Filter("T_sn__icontains", T_sn).Filter("T_name__icontains", T_name).OrderBy("T_sort").All(&r)
  182. //cnt, _ = qs.Filter("T_Bind__icontains", T_Bind).Filter("T_Class__icontains", T_Class).Filter("T_sn__icontains", T_sn).Filter("T_sn__icontains", SN_type).Filter("T_name__icontains", T_name).Count()
  183. //for i, v := range r {
  184. //
  185. // // 提前最新数据
  186. // DeviceSensorData, is := Read_DeviceSensorData_ById_New(v.T_sn, v.T_id)
  187. // if !is {
  188. // continue
  189. // }
  190. // if DeviceSensorData.T_time.After(v.T_time) {
  191. // //fmt.Println(v.T_sn, v.T_id, DeviceSensorData.T_time)
  192. // r[i].T_t = DeviceSensorData.T_t
  193. // r[i].T_rh = DeviceSensorData.T_rh
  194. // r[i].T_time = DeviceSensorData.T_time
  195. // }
  196. //
  197. //}
  198. for _, v := range r {
  199. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  200. }
  201. return DeviceSensor_r, cnt
  202. //return r, cnt
  203. }
  204. // ----------------------------------- v1 -------------------------------------------------------------
  205. // 获取
  206. func Read_DeviceSensor_ByT_sn(T_sn string, T_id int) (r DeviceSensor, is bool) {
  207. key := r.T_sn + "|" + strconv.Itoa(r.T_id)
  208. if r, is := Redis_DeviceSensor_Get(key); is {
  209. return r, true
  210. }
  211. o := orm.NewOrm()
  212. r = DeviceSensor{T_sn: T_sn, T_id: T_id}
  213. err := o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  214. if err != nil {
  215. fmt.Println("Delete_DeviceSensor_ById", err)
  216. return r, false
  217. }
  218. Redis_DeviceSensor_Set(r)
  219. return r, true
  220. }
  221. // 删除
  222. func Delete_DeviceSensor_ById(T_sn string, T_id int) (err error) {
  223. fmt.Println("Delete_DeviceSensor : T_sn", T_sn, "T_id", T_id)
  224. o := orm.NewOrm()
  225. r := DeviceSensor{T_sn: T_sn, T_id: T_id}
  226. err = o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  227. if err != nil {
  228. fmt.Println("Delete_DeviceSensor_ById", err)
  229. return err
  230. }
  231. // ascertain id exists in the database
  232. var num int64
  233. if num, err = o.Delete(&DeviceSensor{Id: r.Id}); err == nil {
  234. fmt.Println("Number of records deleted in database:", num)
  235. }
  236. Redis_DeviceSensor_DelK(r)
  237. return
  238. }
  239. // 修改
  240. func Update_DeviceSensor(r DeviceSensor, cols ...string) bool {
  241. o := orm.NewOrm()
  242. if num, err := o.Update(&r, cols...); err == nil {
  243. fmt.Println("Number of records updated in database:", num)
  244. Redis_DeviceSensor_Set(r)
  245. return true
  246. }
  247. return false
  248. }
  249. // 修改T_Class,替换为""
  250. func DeviceSensor_Bind_T_Class_Del(T_sn string, T_id int, T_Class_id int) (err error) {
  251. o := orm.NewOrm()
  252. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  253. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  254. if err = o.Read(&v, "T_sn", "T_id"); err == nil {
  255. v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
  256. o.Update(&v, "T_Class")
  257. }
  258. return err
  259. }
  260. // 修改T_Class,追加
  261. func DeviceSensor_T_Class_Add(T_sn string, T_id int, T_Class_id int) (err error) {
  262. o := orm.NewOrm()
  263. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  264. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  265. if err = o.Read(&v, "T_sn", "T_id"); err == nil {
  266. v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
  267. v.T_Class = v.T_Class + T_Class
  268. o.Update(&v, "T_Class")
  269. }
  270. return err
  271. }
  272. // 修改
  273. func DeviceSensor_T_Class_ALL_Del(T_Class_id int) {
  274. o := orm.NewOrm()
  275. qs := o.QueryTable(new(DeviceSensor))
  276. var r []DeviceSensor
  277. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  278. qs.Filter("T_Class__icontains", T_Class).All(&r)
  279. for _, v := range r {
  280. v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
  281. o.Update(&v, "T_Class")
  282. }
  283. }
  284. //
  285. //// 修改
  286. //func Update_DeviceSensor_ByTsn_All_T_Bind(T_sn string, T_Bind string) (err error) {
  287. // o := orm.NewOrm()
  288. // var r_l []DeviceSensor
  289. // qs := o.QueryTable(new(DeviceSensor))
  290. //
  291. // qs.Filter("T_sn", T_sn).OrderBy("T_id").All(&r_l)
  292. // for _, v := range r_l {
  293. // v.T_Bind = T_Bind
  294. //
  295. // o.Update(&v, "T_Bind")
  296. // }
  297. //
  298. // return err
  299. //}
  300. // 获取最新数据
  301. func DeviceSensor_T_Bind_ALL(T_sn string, T_Bind string) {
  302. o := orm.NewOrm()
  303. res, err := o.Raw("UPDATE DeviceSensor SET `t__bind` = '" + T_Bind + "' WHERE `t_sn` = '" + T_sn + "' ").Exec()
  304. if err == nil {
  305. num, _ := res.RowsAffected()
  306. fmt.Println("mysql row affected nums: ", num)
  307. }
  308. }
  309. // 获取最新数据
  310. func DeviceSensor_T_type_ALL(T_sn string, T_type int) {
  311. o := orm.NewOrm()
  312. res, err := o.Raw("UPDATE DeviceSensor SET `t_type` = " + strconv.Itoa(T_type) + " WHERE `t_sn` = '" + T_sn + "' ").Exec()
  313. if err == nil {
  314. num, _ := res.RowsAffected()
  315. fmt.Println("mysql row affected nums: ", num)
  316. }
  317. }
  318. // 获取最新数据
  319. func DeviceSensor_t_give_ALL(T_sn string, t_give int) {
  320. o := orm.NewOrm()
  321. res, err := o.Raw("UPDATE DeviceSensor SET `t_give` = " + strconv.Itoa(t_give) + " WHERE `t_sn` = '" + T_sn + "' ").Exec()
  322. if err == nil {
  323. num, _ := res.RowsAffected()
  324. fmt.Println("mysql row affected nums: ", num)
  325. }
  326. }
  327. // 获取 ById
  328. func Read_DeviceSensor_ByTsn_Tid(T_sn string, T_id int) (r DeviceSensor, err error) {
  329. o := orm.NewOrm()
  330. r = DeviceSensor{T_sn: T_sn, T_id: T_id}
  331. err = o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  332. return r, err
  333. }
  334. // 获取列表
  335. func Read_DeviceSensor_ByTsn(T_sn string) ([]DeviceSensor, int) {
  336. o := orm.NewOrm()
  337. // 也可以直接使用 Model 结构体作为表名
  338. qs := o.QueryTable(new(DeviceSensor))
  339. var r []DeviceSensor
  340. qs.Filter("T_sn", T_sn).OrderBy("T_id").All(&r)
  341. cnt, _ := qs.Filter("T_sn", T_sn).Count()
  342. //var DeviceSensor_r []DeviceSensor_R
  343. //for _, v := range r {
  344. // DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  345. //}
  346. //
  347. //return DeviceSensor_r, int(cnt)
  348. return r, int(cnt)
  349. }
  350. // 获取列表
  351. func Read_DeviceSensor_ALL_T_sn(T_sn string, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  352. o := orm.NewOrm()
  353. // 也可以直接使用 Model 结构体作为表名
  354. qs := o.QueryTable(new(DeviceSensor))
  355. var offset int64
  356. if page <= 1 {
  357. offset = 0
  358. } else {
  359. offset = int64((page - 1) * page_z)
  360. }
  361. var r []DeviceSensor
  362. qs.Limit(page_z, offset).Filter("T_sn", T_sn).OrderBy("T_id").All(&r)
  363. cnt, _ = qs.Filter("T_sn", T_sn).Count()
  364. for _, v := range r {
  365. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  366. }
  367. return DeviceSensor_r, cnt
  368. }
  369. // 获取列表
  370. func Read_DeviceSensor_class_ALL_1(admin Account.Admin, T_pid int, T_Class_id int, page int, page_z int, T_sn string, T_name string, SN_type string) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  371. o := orm.NewOrm()
  372. // 也可以直接使用 Model 结构体作为表名
  373. qs := o.QueryTable(new(DeviceSensor))
  374. var offset int64
  375. if page_z == 0 {
  376. page_z = conf.Page_size
  377. }
  378. if page <= 1 {
  379. offset = 0
  380. } else {
  381. offset = int64((page - 1) * page_z)
  382. }
  383. T_Class := ""
  384. if T_Class_id != 0 {
  385. T_Class = "C" + strconv.Itoa(T_Class_id) + "|"
  386. }
  387. fmt.Println("T_Class:", T_Class)
  388. var r []DeviceSensor
  389. cond := orm.NewCondition()
  390. cond1 := cond.And("T_pid", T_pid).And("T_Class__icontains", T_Class).And("T_sn__icontains", SN_type).And("T_sn__icontains", T_sn).And("T_name__icontains", T_name).And("T_datashow", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  391. if len(T_sn) < 6 {
  392. cond1 = cond1.AndCond(cond.And("T_give", 1))
  393. }
  394. if admin.T_pid > 0 {
  395. cond1 = cond1.AndCond(cond.And("T_give", 1))
  396. }
  397. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  398. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  399. //
  400. //qs.Limit(page_z, offset).Filter("T_Bind__icontains", T_Bind).Filter("T_Class__icontains", T_Class).Filter("T_sn__icontains", SN_type).Filter("T_sn__icontains", T_sn).Filter("T_name__icontains", T_name).OrderBy("T_sort").All(&r)
  401. //cnt, _ = qs.Filter("T_Bind__icontains", T_Bind).Filter("T_Class__icontains", T_Class).Filter("T_sn__icontains", T_sn).Filter("T_sn__icontains", SN_type).Filter("T_name__icontains", T_name).Count()
  402. //for i, v := range r {
  403. //
  404. // // 提前最新数据
  405. // DeviceSensorData, is := Read_DeviceSensorData_ById_New(v.T_sn, v.T_id)
  406. // if !is {
  407. // continue
  408. // }
  409. // if DeviceSensorData.T_time.After(v.T_time) {
  410. // //fmt.Println(v.T_sn, v.T_id, DeviceSensorData.T_time)
  411. // r[i].T_t = DeviceSensorData.T_t
  412. // r[i].T_rh = DeviceSensorData.T_rh
  413. // r[i].T_time = DeviceSensorData.T_time
  414. // }
  415. //
  416. //}
  417. for _, v := range r {
  418. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  419. }
  420. return DeviceSensor_r, cnt
  421. //return r, cnt
  422. }
  423. // 获取列表
  424. func Read_DeviceSensor_Map_ALL_1(T_pid int, T_Class_id int, T_sn string, T_name string, SN_type string) (r []DeviceSensor) {
  425. o := orm.NewOrm()
  426. // 也可以直接使用 Model 结构体作为表名
  427. qs := o.QueryTable(new(DeviceSensor))
  428. //now := time.Now()
  429. //// 一天前
  430. //d, _ := time.ParseDuration("-1h")
  431. //now = now.Add(d)
  432. T_Class := ""
  433. if T_Class_id != 0 {
  434. T_Class = "C" + strconv.Itoa(T_Class_id) + "|"
  435. }
  436. fmt.Println("T_Class:", T_Class)
  437. qs.Filter("T_pid", T_pid).Filter("T_Class__icontains", T_Class).Filter("T_sn__icontains", SN_type).Filter("T_sn__icontains", T_sn).Filter("T_name__icontains", T_name).OrderBy("-UpdateTime").All(&r) //.Filter("UpdateTime__gte", now)
  438. return r
  439. }
  440. // 获取列表
  441. func Read_DeviceSensor_ALL_T_sn_T_id_class_1(T_sn string, T_id int, T_Class_id int) (r []DeviceSensor) {
  442. o := orm.NewOrm()
  443. // 也可以直接使用 Model 结构体作为表名
  444. qs := o.QueryTable(new(DeviceSensor))
  445. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  446. qs.Filter("T_Class__icontains", T_Class).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r)
  447. return r
  448. }
  449. // 获取列表
  450. func Read_DeviceSensor_ALL_List_T_sn(T_sn string) (r []DeviceSensor) {
  451. o := orm.NewOrm()
  452. // 也可以直接使用 Model 结构体作为表名
  453. qs := o.QueryTable(new(DeviceSensor))
  454. qs.Filter("T_sn", T_sn).All(&r)
  455. return r
  456. }
  457. // 获取列表
  458. func Read_DeviceSensor_ALL_Class_Id(T_Class_id int) (r []DeviceSensor) {
  459. o := orm.NewOrm()
  460. // 也可以直接使用 Model 结构体作为表名
  461. qs := o.QueryTable(new(DeviceSensor))
  462. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  463. qs.Filter("T_Class__icontains", T_Class).All(&r)
  464. return r
  465. }
  466. func DELETE_DeviceSensor(SN string) bool {
  467. sql := "DELETE FROM `cold`.`device_sensor` WHERE `t_sn` = '" + SN + "' "
  468. o := orm.NewOrm()
  469. _, err := o.Raw(sql).Exec()
  470. if err != nil {
  471. return false
  472. }
  473. return true
  474. }
  475. // 通知 -------------------
  476. // 通过T_sn T_id T_Notice_id 获取全部列表
  477. func Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn string, T_id int, T_Notice_id int) (r []DeviceSensor) {
  478. o := orm.NewOrm()
  479. // 也可以直接使用 Model 结构体作为表名
  480. qs := o.QueryTable(new(DeviceSensor))
  481. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  482. qs.Filter("T_Notice__icontains", T_Notice).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r)
  483. return r
  484. }
  485. // 修改T_Notice,替换为""
  486. func DeviceSensor_Bind_T_Notice_Del(T_sn string, T_id int, T_Notice_id int) (err error) {
  487. o := orm.NewOrm()
  488. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  489. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  490. if err = o.Read(&v, "T_sn", "T_id"); err == nil {
  491. v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1)
  492. o.Update(&v, "T_Notice")
  493. }
  494. return err
  495. }
  496. // 修改T_Notice,追加
  497. func DeviceSensor_T_Notice_Add(T_sn string, T_id int, T_Notice_id int) (err error) {
  498. o := orm.NewOrm()
  499. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  500. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  501. if err = o.Read(&v, "T_sn", "T_id"); err == nil {
  502. v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1)
  503. v.T_Notice = v.T_Notice + T_Notice
  504. o.Update(&v, "T_Notice")
  505. }
  506. return err
  507. }
  508. // 获取列表
  509. func Read_DeviceSensor_ALL_Notice_Id(T_Notice_id int) (r []DeviceSensor) {
  510. o := orm.NewOrm()
  511. // 也可以直接使用 Model 结构体作为表名
  512. qs := o.QueryTable(new(DeviceSensor))
  513. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  514. qs.Filter("T_Notice__icontains", T_Notice).All(&r)
  515. return r
  516. }
  517. // 获取列表
  518. func Read_DeviceSensor_Notice_ALL_1(T_pid int, T_Notice_id int, page int, page_z int, T_sn string, T_name string, SN_type string) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  519. o := orm.NewOrm()
  520. // 也可以直接使用 Model 结构体作为表名
  521. qs := o.QueryTable(new(DeviceSensor))
  522. var offset int64
  523. if page_z == 0 {
  524. page_z = conf.Page_size
  525. }
  526. if page <= 1 {
  527. offset = 0
  528. } else {
  529. offset = int64((page - 1) * page_z)
  530. }
  531. T_Notice := ""
  532. if T_Notice_id != 0 {
  533. T_Notice = "N" + strconv.Itoa(T_Notice_id) + "|"
  534. }
  535. fmt.Println("T_Notice:", T_Notice)
  536. var r []DeviceSensor
  537. cond := orm.NewCondition()
  538. cond1 := cond.And("T_pid", T_pid).And("T_Notice__icontains", T_Notice).And("T_sn__icontains", SN_type).And("T_sn__icontains", T_sn).And("T_name__icontains", T_name).And("T_datashow", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  539. if len(T_sn) < 6 {
  540. cond1 = cond1.AndCond(cond.And("T_give", 1))
  541. }
  542. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  543. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  544. for _, v := range r {
  545. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  546. }
  547. return DeviceSensor_r, cnt
  548. }
  549. // 设备同步参数
  550. func Update_Device_To_DeviceSensor(r_Device Device) bool {
  551. DeviceSensor_list, _ := Read_DeviceSensor_ByTsn(r_Device.T_sn)
  552. for _, v := range DeviceSensor_list {
  553. // 设备同步参数
  554. v.T_Dattery = r_Device.T_Dattery // 电量
  555. v.T_Site = r_Device.T_Site // GPS
  556. v.T_type = r_Device.T_type // 1库房 2移动
  557. v.T_give = r_Device.T_give // 屏蔽状态 0 屏蔽 1 正常
  558. v.T_monitor = r_Device.T_monitor // 监控状态 0 未监控 1 监控
  559. v.T_online = r_Device.T_online // 在线状态 0 未启用 1 在线 2 离线
  560. v.T_online_s = r_Device.T_online_s // 在线状态-备用 0 未启用 1 在线 2 离线
  561. Update_DeviceSensor(v, "T_Dattery", "T_Site", "T_type", "T_give", "T_monitor", "T_online", "T_online_s")
  562. }
  563. return true
  564. }