DeviceSensor.go 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  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/beego/beego/v2/core/logs"
  13. _ "github.com/go-sql-driver/mysql"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // 模板
  19. type DeviceSensor struct {
  20. Id int `orm:"column(ID);size(11);auto;pk"`
  21. T_sn string `orm:"index;size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  22. T_id int `orm:"index;size(11);null"` // 传感器编号
  23. T_sp int `orm:"size(11);null"` // 传感器参数id
  24. T_name string `orm:"size(256);null"` // 标题
  25. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  26. T_Class string `orm:"size(256);null"` // Device.DeviceClass.Id 设备分类 C1|C2|
  27. T_Notice string `orm:"size(256);null"` // 通知绑定
  28. T_datashow int `orm:"size(2);default(1)"` // 0 屏蔽数据展示 1 正常数据展示 (屏蔽后 数据展示无法看到,设备管理中 不受影响)
  29. T_sort int `orm:"size(200);default(1)"` // 排序
  30. T_3dview string `orm:"size(256);null"` // 3D 视图ID
  31. T_type int `orm:"size(4);null"` // Device.DeviceSensorType 1库房 2移动
  32. T_link int `orm:"size(4);default(1)"` // 0:断开/故障 1连接 实时数据
  33. T_State int `orm:"index;size(2);default(1)"` // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
  34. // 设备同步参数
  35. T_Dattery int `orm:"size(4);null"` // 电量
  36. T_Site string `orm:"size(200);null"` // GPS
  37. T_monitor int `orm:"size(2);null"` // 监控状态 0 未监控 1 监控
  38. T_online int `orm:"size(2);default(1)"` // 在线状态 0 未启用 1 在线 2 离线
  39. T_online_s int `orm:"size(2);default(0)"` // 在线状态-备用 0 未启用 1 在线 2 离线
  40. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  41. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  42. }
  43. type DeviceSensor_Del struct {
  44. T_sn string
  45. T_id int
  46. }
  47. func (t *DeviceSensor) TableName() string {
  48. return "device_sensor" // 数据库名称 // ************** 替换 FormulaList **************
  49. }
  50. // 模板
  51. type DeviceSensor_R struct {
  52. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  53. T_id int // 传感器编号
  54. T_name string // 标题
  55. T_pid int // 公司id
  56. T_pid_name string // 公司名称
  57. T_3dview string // 3D 视图ID
  58. T_sort int // 排序
  59. T_Dattery int // 电量
  60. T_Site string // GPS
  61. T_monitor int // 记录状态
  62. T_online int // 在线状态 1 在线 0 离线
  63. T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线
  64. T_datashow int // 0 屏蔽数据展示 1 正常数据展示
  65. T_type int // 类型
  66. T_link int // 0:断开/故障 1连接 实时数据
  67. T_State int // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
  68. T_DeviceSensorData DeviceData_R // 传感器最新数据
  69. T_DeviceSensorParameter DeviceSensorParameter_R // 设备参数
  70. T_DeviceSensorType DeviceSensorType_R // 传感器类型
  71. }
  72. type DeviceSensor_Applet struct {
  73. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  74. T_id int // 传感器编号
  75. T_name string // 标题
  76. T_pid int // 公司id
  77. T_pid_name string // 公司名称
  78. T_3dview string // 3D 视图ID
  79. T_sort int // 排序
  80. T_Dattery int // 电量
  81. T_Site string // GPS
  82. T_monitor int // 记录状态
  83. T_online int // 在线状态 1 在线 0 离线
  84. T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线
  85. T_datashow int // 0 屏蔽数据展示 1 正常数据展示
  86. T_type int // 1库房 2移动
  87. T_link int // 0:断开/故障 1连接 实时数据
  88. T_State int // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
  89. T_DeviceSensorData DeviceData_R // 传感器最新数据
  90. T_DeviceSensorParameter DeviceSensorParameter_R // 设备参数
  91. T_DeviceSensorType DeviceSensorType_R // 传感器类型
  92. T_Device Device_R // 传感器类型
  93. }
  94. type DeviceSensor_RealTime struct {
  95. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  96. T_id int // 传感器编号
  97. T_name string // 标题
  98. T_pid int // 公司id
  99. T_pid_name string // 公司名称
  100. T_3dview string // 3D 视图ID
  101. T_sort int // 排序
  102. T_Dattery int // 电量
  103. T_Site string // GPS
  104. T_monitor int // 记录状态
  105. T_online int // 在线状态 1 在线 0 离线
  106. T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线
  107. T_datashow int // 0 屏蔽数据展示 1 正常数据展示
  108. T_type int // 类型
  109. T_link int // 0:断开/故障 1连接 实时数据
  110. T_State int // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
  111. // 传感器参数
  112. // 报警
  113. T_Tlower float32 // 温度下限
  114. T_Tupper float32 // 温度上限
  115. T_RHlower float32 // 湿度下限
  116. T_RHupper float32 // 湿度上限
  117. T_en int // en:是否启用传感器,
  118. T_free int // free:监测点是否为闲置状态(空库,只监测不报警)
  119. }
  120. // 传感器管理
  121. type DeviceSensor_P struct {
  122. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  123. T_id int // 传感器编号
  124. T_name string // 标题
  125. T_type int // 类型
  126. T_type_name string // 类型
  127. T_sort int // 排序
  128. T_datashow int // 0 屏蔽数据展示 1 正常数据展示
  129. T_State int // 0 屏蔽 1 正常
  130. // DeviceSensorParameter
  131. T_Tlower *float32 // 温度下限
  132. T_Tupper *float32 // 温度上限
  133. T_RHlower *float32 // 湿度下限
  134. T_RHupper *float32 // 湿度上限
  135. T_enprel *int // 是否启用预警 1开启 0 关闭
  136. T_tprel *float32 // 温度预警下限
  137. T_tpreu *float32 // 温度预警上限
  138. T_hprel *float32 // 湿度预警下限
  139. T_hpreu *float32 // 温度预警上限
  140. T_en *int // en:是否启用传感器,
  141. T_free *int // free:监测点是否为闲置状态(空库,只监测不报警)
  142. }
  143. type DeviceSensor_ struct {
  144. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  145. T_id int // 传感器编号
  146. T_name string // 标题
  147. }
  148. var redisCache_DeviceSensor cache.Cache
  149. func init() {
  150. //注册模型
  151. orm.RegisterModel(new(DeviceSensor))
  152. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  153. "redis_DeviceSensor", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  154. fmt.Println(config)
  155. var err error
  156. redisCache_DeviceSensor, err = cache.NewCache("redis", config)
  157. if err != nil || redisCache_DeviceSensor == nil {
  158. errMsg := "failed to init redis"
  159. logs.Error(errMsg, err)
  160. panic(errMsg)
  161. }
  162. }
  163. // ---------------- Redis -------------------
  164. // Redis_Device_Set(m.T_sn,m) // Redis 更新缓存
  165. func Redis_DeviceSensor_Set(r DeviceSensor) (err error) {
  166. key := r.T_sn + "|" + strconv.Itoa(r.T_id)
  167. //json序列化
  168. str, err := json.Marshal(r)
  169. if err != nil {
  170. logs.Error(lib.FuncName(), err)
  171. return
  172. }
  173. err = redisCache_DeviceSensor.Put(key, str, 24*time.Hour)
  174. if err != nil {
  175. logs.Error("set key:", key, ",value:", str, err)
  176. }
  177. return
  178. }
  179. func Redis_DeviceSensor_Get(key string) (r DeviceSensor, is bool) {
  180. if redisCache_DeviceSensor.IsExist(key) {
  181. //println("找到key:",key)
  182. v := redisCache_DeviceSensor.Get(key)
  183. err := json.Unmarshal(v.([]byte), &r)
  184. if err != nil {
  185. logs.Error(lib.FuncName(), err)
  186. return DeviceSensor{}, false
  187. }
  188. return r, true
  189. }
  190. return DeviceSensor{}, false
  191. }
  192. func Redis_DeviceSensor_DelK(r DeviceSensor) (err error) {
  193. key := r.T_sn + "|" + strconv.Itoa(r.T_id)
  194. err = redisCache_DeviceSensor.Delete(key)
  195. if err != nil {
  196. logs.Error(lib.FuncName(), err)
  197. }
  198. return
  199. }
  200. // ---------------- 特殊方法 -------------------
  201. func DeviceSensorToDeviceSensor_R(DeviceSensor_ DeviceSensor) (DeviceSensor_r DeviceSensor_R) {
  202. device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn)
  203. //lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据
  204. lib.DeviceRealSnMap.Store(DeviceSensor_.T_sn+"|"+device.T_mqttid, 0) // 连续请求 实时数据
  205. DeviceSensor_r.T_sn = DeviceSensor_.T_sn
  206. DeviceSensor_r.T_id = DeviceSensor_.T_id
  207. DeviceSensor_r.T_name = DeviceSensor_.T_name
  208. DeviceSensor_r.T_pid = DeviceSensor_.T_pid
  209. DeviceSensor_r.T_pid_name = Account.Read_Company_Get(DeviceSensor_.T_pid)
  210. DeviceSensor_r.T_Site = DeviceSensor_.T_Site
  211. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  212. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  213. DeviceSensor_r.T_3dview = DeviceSensor_.T_3dview
  214. DeviceSensor_r.T_type = DeviceSensor_.T_type
  215. DeviceSensor_r.T_sort = DeviceSensor_.T_sort
  216. DeviceSensor_r.T_link = DeviceSensor_.T_link
  217. DeviceSensor_r.T_State = DeviceSensor_.T_State
  218. DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow
  219. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  220. DeviceSensor_r.T_online = DeviceSensor_.T_online
  221. DeviceSensor_r.T_online_s = DeviceSensor_.T_online_s
  222. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  223. //if DeviceSensor_.T_online == 2 && (DeviceSensor_.T_online_s == 0 || DeviceSensor_.T_online_s == 2) && DeviceSensor_.T_monitor == 1 {
  224. // DeviceSensor_r.T_monitor = 2
  225. //}
  226. // 最新系统参数
  227. DeviceSensor_r.T_DeviceSensorParameter, _ = Read_DeviceSensorParameter(DeviceSensor_.T_sn, DeviceSensor_.T_id)
  228. // 最新数据
  229. //key_data := DeviceSensor_.T_sn + "|" + strconv.Itoa(DeviceSensor_.T_id)
  230. //DeviceSensor_r.T_DeviceSensorData, _ = RedisDeviceData_Get(key_data)
  231. DeviceData := Read_DeviceData(DeviceSensor_.T_sn, DeviceSensor_.T_id)
  232. // 获取传感器数据
  233. DeviceSensor_r.T_DeviceSensorData = DeviceDataToDeviceData_R2(device, DeviceSensor_r.T_DeviceSensorParameter, DeviceData)
  234. // 获取传感器类型数据
  235. DeviceSensor_r.T_DeviceSensorType = DeviceSensorTypeToDeviceSensorType_R(DeviceSensor_.T_type)
  236. return
  237. }
  238. func DeviceSensorRealTimeToDeviceSensor_R(DeviceSensor_ DeviceSensor_RealTime) (DeviceSensor_r DeviceSensor_R) {
  239. device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn)
  240. //lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据
  241. lib.DeviceRealSnMap.Store(DeviceSensor_.T_sn+"|"+device.T_mqttid, 0) // 连续请求 实时数据
  242. DeviceSensor_r.T_sn = DeviceSensor_.T_sn
  243. DeviceSensor_r.T_id = DeviceSensor_.T_id
  244. DeviceSensor_r.T_name = DeviceSensor_.T_name
  245. DeviceSensor_r.T_pid = DeviceSensor_.T_pid
  246. DeviceSensor_r.T_pid_name = Account.Read_Company_Get(DeviceSensor_.T_pid)
  247. DeviceSensor_r.T_Site = DeviceSensor_.T_Site
  248. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  249. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  250. DeviceSensor_r.T_3dview = DeviceSensor_.T_3dview
  251. DeviceSensor_r.T_type = DeviceSensor_.T_type
  252. DeviceSensor_r.T_sort = DeviceSensor_.T_sort
  253. DeviceSensor_r.T_link = DeviceSensor_.T_link
  254. DeviceSensor_r.T_State = DeviceSensor_.T_State
  255. DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow
  256. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  257. DeviceSensor_r.T_online = DeviceSensor_.T_online
  258. DeviceSensor_r.T_online_s = DeviceSensor_.T_online_s
  259. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  260. // 传感器参数
  261. DeviceSensor_r.T_DeviceSensorParameter.T_Tlower = DeviceSensor_.T_Tlower
  262. DeviceSensor_r.T_DeviceSensorParameter.T_Tupper = DeviceSensor_.T_Tupper
  263. DeviceSensor_r.T_DeviceSensorParameter.T_RHlower = DeviceSensor_.T_RHlower
  264. DeviceSensor_r.T_DeviceSensorParameter.T_RHupper = DeviceSensor_.T_RHupper
  265. DeviceSensor_r.T_DeviceSensorParameter.T_en = DeviceSensor_.T_en
  266. DeviceSensor_r.T_DeviceSensorParameter.T_free = DeviceSensor_.T_free
  267. DeviceData := Read_DeviceData(DeviceSensor_.T_sn, DeviceSensor_.T_id)
  268. DeviceSensor_r.T_DeviceSensorData = DeviceDataToDeviceData_R2(device, DeviceSensor_r.T_DeviceSensorParameter, DeviceData)
  269. DeviceSensor_r.T_DeviceSensorType = DeviceSensorTypeToDeviceSensorType_R(DeviceSensor_.T_type)
  270. return
  271. }
  272. func DeviceSensorToDeviceSensor_Applet(DeviceSensor_ DeviceSensor) (DeviceSensor_r DeviceSensor_Applet) {
  273. device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn)
  274. //lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据
  275. lib.DeviceRealSnMap.Store(DeviceSensor_.T_sn+"|"+device.T_mqttid, 0) // 连续请求 实时数据
  276. DeviceSensor_r.T_sn = DeviceSensor_.T_sn
  277. DeviceSensor_r.T_id = DeviceSensor_.T_id
  278. DeviceSensor_r.T_name = DeviceSensor_.T_name
  279. DeviceSensor_r.T_pid = DeviceSensor_.T_pid
  280. DeviceSensor_r.T_pid_name = Account.Read_Company_Get(DeviceSensor_.T_pid)
  281. DeviceSensor_r.T_Site = DeviceSensor_.T_Site
  282. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  283. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  284. DeviceSensor_r.T_3dview = DeviceSensor_.T_3dview
  285. DeviceSensor_r.T_type = DeviceSensor_.T_type
  286. DeviceSensor_r.T_sort = DeviceSensor_.T_sort
  287. DeviceSensor_r.T_link = DeviceSensor_.T_link
  288. DeviceSensor_r.T_State = DeviceSensor_.T_State
  289. DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow
  290. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  291. DeviceSensor_r.T_online = DeviceSensor_.T_online
  292. DeviceSensor_r.T_online_s = DeviceSensor_.T_online_s
  293. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  294. //if DeviceSensor_.T_online == 2 && (DeviceSensor_.T_online_s == 0 || DeviceSensor_.T_online_s == 2) && DeviceSensor_.T_monitor == 1 {
  295. // DeviceSensor_r.T_monitor = 2
  296. //}
  297. // 最新系统参数
  298. DeviceSensor_r.T_DeviceSensorParameter, _ = Read_DeviceSensorParameter(DeviceSensor_.T_sn, DeviceSensor_.T_id)
  299. // 最新数据
  300. DeviceData := Read_DeviceData(DeviceSensor_.T_sn, DeviceSensor_.T_id)
  301. DeviceSensor_r.T_DeviceSensorData = DeviceDataToDeviceData_R2(device, DeviceSensor_r.T_DeviceSensorParameter, DeviceData)
  302. DeviceSensor_r.T_DeviceSensorType = DeviceSensorTypeToDeviceSensorType_R(DeviceSensor_.T_type)
  303. DeviceSensor_r.T_Device = DeviceToDevice_R(device)
  304. return
  305. }
  306. func DeviceSensorToDeviceSensor_(r DeviceSensor) (t DeviceSensor_) {
  307. t.T_sn = r.T_sn
  308. t.T_id = r.T_id
  309. t.T_name = r.T_name
  310. return
  311. }
  312. func DeviceSensorToDeviceSensor_P(r DeviceSensor_P) (t DeviceSensor_P) {
  313. t = r
  314. if r.T_type == 0 {
  315. t.T_type_name = "默认"
  316. }
  317. if r.T_type > 0 {
  318. deviceSensorType := Read_DeviceSensorType_Get(r.T_type)
  319. t.T_type_name = deviceSensorType.T_name
  320. }
  321. return t
  322. }
  323. // ---------------- 特殊方法 -------------------
  324. // 获取列表 - 总数
  325. func Read_DeviceSensor_Num_ByT_sn(T_sn string) int {
  326. o := orm.NewOrm()
  327. // 也可以直接使用 Model 结构体作为表名
  328. qs := o.QueryTable(new(DeviceSensor))
  329. cnt, err := qs.Filter("T_sn", T_sn).Count()
  330. if err != nil {
  331. logs.Error(lib.FuncName(), err)
  332. return 0
  333. }
  334. return int(cnt)
  335. }
  336. // 获取列表
  337. func Read_DeviceSensorList(admin_r *Account.Admin, bindSN []string, T_pid int, T_sn string, T_name string, T_Class_id, T_datashow int,
  338. T_type string, T_State int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  339. o := orm.NewOrm()
  340. // 也可以直接使用 Model 结构体作为表名
  341. qs := o.QueryTable(new(DeviceSensor))
  342. var offset int64
  343. if page <= 1 {
  344. offset = 0
  345. } else {
  346. offset = int64((page - 1) * page_z)
  347. }
  348. var r []DeviceSensor
  349. cond := orm.NewCondition()
  350. cond1 := cond.And("T_pid", T_pid)
  351. if T_Class_id > 0 {
  352. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  353. cond1 = cond1.And("T_Class__icontains", T_Class)
  354. }
  355. if len(bindSN) > 0 {
  356. cond1 = cond1.And("T_sn__in", bindSN)
  357. }
  358. if len(T_type) > 0 {
  359. list := strings.Split(T_type, ",")
  360. cond1 = cond1.And("T_type__in", list)
  361. }
  362. if len(T_sn) > 0 {
  363. cond1 = cond1.And("T_sn__icontains", T_sn)
  364. } else {
  365. // 不是内部权限(T_pid>0),T_State=1 0 屏蔽 1 正常
  366. if admin_r.T_pid > 0 || T_State == 1 {
  367. cond1 = cond1.And("T_State", 1)
  368. }
  369. }
  370. if len(T_name) > 0 {
  371. cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name))
  372. }
  373. if T_datashow == 1 { // 0 屏蔽数据展示 1 正常数据展示
  374. cond1 = cond1.And("T_datashow", 1)
  375. }
  376. var err error
  377. if page_z == 9999 {
  378. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  379. } else {
  380. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  381. }
  382. if err != nil {
  383. logs.Error(lib.FuncName(), err)
  384. return
  385. }
  386. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  387. if err != nil {
  388. logs.Error(lib.FuncName(), err)
  389. return
  390. }
  391. for _, v := range r {
  392. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  393. }
  394. return DeviceSensor_r, cnt
  395. }
  396. // 获取
  397. func Read_DeviceSensor_ByT_sn(T_sn string, T_id int) (r DeviceSensor, is bool) {
  398. key := r.T_sn + "|" + strconv.Itoa(r.T_id)
  399. if r, is := Redis_DeviceSensor_Get(key); is {
  400. return r, true
  401. }
  402. o := orm.NewOrm()
  403. r = DeviceSensor{T_sn: T_sn, T_id: T_id}
  404. err := o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  405. if err != nil {
  406. logs.Error(lib.FuncName(), err)
  407. return r, false
  408. }
  409. Redis_DeviceSensor_Set(r)
  410. return r, true
  411. }
  412. // 删除
  413. func Delete_DeviceSensor_ById(T_sn string, T_id int) (err error) {
  414. o := orm.NewOrm()
  415. r := DeviceSensor{T_sn: T_sn, T_id: T_id}
  416. err = o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  417. if err != nil {
  418. logs.Error("Delete_DeviceSensor_ById", err)
  419. return err
  420. }
  421. _, err = o.Delete(&DeviceSensor{Id: r.Id})
  422. if err != nil {
  423. logs.Error(lib.FuncName(), err)
  424. return err
  425. }
  426. Redis_DeviceSensor_DelK(r)
  427. return
  428. }
  429. // 修改
  430. func Update_DeviceSensor(r DeviceSensor, cols ...string) bool {
  431. o := orm.NewOrm()
  432. num, err := o.Update(&r, cols...)
  433. if err != nil {
  434. logs.Error(lib.FuncName(), err)
  435. return false
  436. }
  437. logs.Info("Number of records updated in database:", num)
  438. Redis_DeviceSensor_Set(r)
  439. return true
  440. }
  441. // 修改T_Class,替换为""
  442. func DeviceSensor_Bind_T_Class_Del(T_sn string, T_id int, T_Class_id int) (err error) {
  443. o := orm.NewOrm()
  444. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  445. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  446. err = o.Read(&v, "T_sn", "T_id")
  447. if err != nil {
  448. logs.Error(lib.FuncName(), err)
  449. return err
  450. }
  451. v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
  452. _, err = o.Update(&v, "T_Class")
  453. if err != nil {
  454. logs.Error(lib.FuncName(), err)
  455. return err
  456. }
  457. Redis_DeviceSensor_Set(v)
  458. return nil
  459. }
  460. // 修改T_Class,追加
  461. func DeviceSensor_Bind_T_Class_Add(T_sn string, T_id int, T_Class_id int) (err error) {
  462. o := orm.NewOrm()
  463. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  464. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  465. err = o.Read(&v, "T_sn", "T_id")
  466. if err != nil {
  467. logs.Error(lib.FuncName(), err)
  468. return err
  469. }
  470. v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
  471. v.T_Class = v.T_Class + T_Class
  472. _, err = o.Update(&v, "T_Class")
  473. if err != nil {
  474. logs.Error(lib.FuncName(), err)
  475. return err
  476. }
  477. Redis_DeviceSensor_Set(v)
  478. return nil
  479. }
  480. // 获取 ById
  481. func Read_DeviceSensor_ByTsn_Tid(T_sn string, T_id int) (r DeviceSensor, err error) {
  482. o := orm.NewOrm()
  483. r = DeviceSensor{T_sn: T_sn, T_id: T_id}
  484. err = o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  485. if err != nil {
  486. logs.Error(lib.FuncName(), err)
  487. }
  488. return r, err
  489. }
  490. // 获取列表
  491. func Read_DeviceSensor_ByTsn(T_sn string) ([]DeviceSensor, int) {
  492. o := orm.NewOrm()
  493. // 也可以直接使用 Model 结构体作为表名
  494. qs := o.QueryTable(new(DeviceSensor))
  495. var r []DeviceSensor
  496. _, err := qs.Filter("T_sn", T_sn).OrderBy("T_id").All(&r)
  497. if err != nil {
  498. logs.Error(lib.FuncName(), err)
  499. return r, 0
  500. }
  501. cnt, err := qs.Filter("T_sn", T_sn).Count()
  502. if err != nil {
  503. logs.Error(lib.FuncName(), err)
  504. return r, 0
  505. }
  506. return r, int(cnt)
  507. }
  508. // 获取列表
  509. func Read_DeviceSensor_List_T_Class(T_pid int, T_Class_id int, T_sn string, T_name string, T_type int, page int, page_z int) (DeviceSensor_r []DeviceSensor_, cnt int64) {
  510. o := orm.NewOrm()
  511. // 也可以直接使用 Model 结构体作为表名
  512. qs := o.QueryTable(new(DeviceSensor))
  513. var offset int64
  514. if page <= 1 {
  515. offset = 0
  516. } else {
  517. offset = int64((page - 1) * page_z)
  518. }
  519. T_Class := ""
  520. if T_Class_id != 0 {
  521. T_Class = "C" + strconv.Itoa(T_Class_id) + "|"
  522. }
  523. var r []DeviceSensor
  524. cond := orm.NewCondition()
  525. cond1 := cond.And("T_pid", T_pid).And("T_State", 1).And("T_Class__icontains", T_Class).And("T_sn__icontains", T_sn).And("T_name__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  526. if T_type > 0 {
  527. cond1 = cond1.AndCond(cond.And("T_type", T_type))
  528. }
  529. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  530. if err != nil {
  531. logs.Error(lib.FuncName(), err)
  532. return
  533. }
  534. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  535. if err != nil {
  536. logs.Error(lib.FuncName(), err)
  537. return
  538. }
  539. for _, v := range r {
  540. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_(v))
  541. }
  542. return DeviceSensor_r, cnt
  543. }
  544. // 获取列表
  545. func Read_DeviceSensor_ALL_T_sn_T_id_T_Class(T_sn string, T_id int, T_Class_id int) (r []DeviceSensor) {
  546. o := orm.NewOrm()
  547. // 也可以直接使用 Model 结构体作为表名
  548. qs := o.QueryTable(new(DeviceSensor))
  549. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  550. _, err := qs.Filter("T_Class__icontains", T_Class).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r)
  551. if err != nil {
  552. logs.Error(lib.FuncName(), err)
  553. return
  554. }
  555. return r
  556. }
  557. // 获取列表
  558. func Read_DeviceSensor_ALL_List_T_sn(T_sn string) (r []DeviceSensor) {
  559. o := orm.NewOrm()
  560. // 也可以直接使用 Model 结构体作为表名
  561. qs := o.QueryTable(new(DeviceSensor))
  562. _, err := qs.Filter("T_sn", T_sn).All(&r)
  563. if err != nil {
  564. logs.Error(lib.FuncName(), err)
  565. return
  566. }
  567. return r
  568. }
  569. func Read_DeviceSensor_List_T_sn_T_datashow(T_sn string) (r []DeviceSensor) {
  570. o := orm.NewOrm()
  571. // 也可以直接使用 Model 结构体作为表名
  572. qs := o.QueryTable(new(DeviceSensor))
  573. _, err := qs.Filter("T_sn", T_sn).Filter("T_State", 1).Filter("T_datashow", 1).All(&r)
  574. if err != nil {
  575. logs.Error(lib.FuncName(), err)
  576. return
  577. }
  578. return r
  579. }
  580. // 获取列表
  581. func Read_DeviceSensor_ALL_Class_Id(T_Class_id int) (r []DeviceSensor) {
  582. o := orm.NewOrm()
  583. // 也可以直接使用 Model 结构体作为表名
  584. qs := o.QueryTable(new(DeviceSensor))
  585. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  586. _, err := qs.Filter("T_Class__icontains", T_Class).All(&r)
  587. if err != nil {
  588. logs.Error(lib.FuncName(), err)
  589. return
  590. }
  591. return r
  592. }
  593. func DELETE_DeviceSensor(SN string) bool {
  594. sql := "DELETE FROM `cold`.`device_sensor` WHERE `t_sn` = '" + SN + "' "
  595. o := orm.NewOrm()
  596. _, err := o.Raw(sql).Exec()
  597. if err != nil {
  598. logs.Error(lib.FuncName(), err)
  599. return false
  600. }
  601. return true
  602. }
  603. // 报警策略 -------------------
  604. // 通过T_sn T_id T_Notice_id 获取全部列表
  605. func Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn string, T_id int, T_Notice_id int) (r []DeviceSensor) {
  606. o := orm.NewOrm()
  607. // 也可以直接使用 Model 结构体作为表名
  608. qs := o.QueryTable(new(DeviceSensor))
  609. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  610. _, err := qs.Filter("T_Notice__icontains", T_Notice).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r)
  611. if err != nil {
  612. logs.Error(lib.FuncName(), err)
  613. return
  614. }
  615. return r
  616. }
  617. // 修改T_Notice,替换为""
  618. func DeviceSensor_Bind_T_Notice_Del(T_sn string, T_id int, T_Notice_id int) (err error) {
  619. o := orm.NewOrm()
  620. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  621. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  622. err = o.Read(&v, "T_sn", "T_id")
  623. if err != nil {
  624. logs.Error(lib.FuncName(), err)
  625. return
  626. }
  627. v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1)
  628. _, err = o.Update(&v, "T_Notice")
  629. if err != nil {
  630. logs.Error(lib.FuncName(), err)
  631. return
  632. }
  633. Redis_DeviceSensor_Set(v)
  634. return nil
  635. }
  636. // 修改T_Notice,追加
  637. func DeviceSensor_Bind_T_Notice_Add(T_sn string, T_id int, T_Notice_id int) (err error) {
  638. o := orm.NewOrm()
  639. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  640. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  641. err = o.Read(&v, "T_sn", "T_id")
  642. if err != nil {
  643. logs.Error(lib.FuncName(), err)
  644. return
  645. }
  646. v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1)
  647. v.T_Notice = v.T_Notice + T_Notice
  648. _, err = o.Update(&v, "T_Notice")
  649. if err != nil {
  650. logs.Error(lib.FuncName(), err)
  651. return
  652. }
  653. Redis_DeviceSensor_Set(v)
  654. return nil
  655. }
  656. // 获取列表
  657. func Read_DeviceSensor_ALL_Notice_Id(T_Notice_id int) (r []DeviceSensor) {
  658. o := orm.NewOrm()
  659. // 也可以直接使用 Model 结构体作为表名
  660. qs := o.QueryTable(new(DeviceSensor))
  661. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  662. _, err := qs.Filter("T_Notice__icontains", T_Notice).All(&r)
  663. if err != nil {
  664. logs.Error(lib.FuncName(), err)
  665. return
  666. }
  667. return r
  668. }
  669. // 获取列表
  670. func Read_DeviceSensor_List_T_Notice(T_pid int, T_Notice_id int, page int, page_z int, T_sn string, T_name string, T_type int) (DeviceSensor_r []DeviceSensor_, cnt int64) {
  671. o := orm.NewOrm()
  672. // 也可以直接使用 Model 结构体作为表名
  673. qs := o.QueryTable(new(DeviceSensor))
  674. var offset int64
  675. if page <= 1 {
  676. offset = 0
  677. } else {
  678. offset = int64((page - 1) * page_z)
  679. }
  680. T_Notice := ""
  681. if T_Notice_id != 0 {
  682. T_Notice = "N" + strconv.Itoa(T_Notice_id) + "|"
  683. }
  684. var r []DeviceSensor
  685. cond := orm.NewCondition()
  686. cond1 := cond.And("T_pid", T_pid).And("T_State", 1).And("T_Notice__icontains", T_Notice).And("T_sn__icontains", T_sn).And("T_name__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  687. if T_type > 0 {
  688. cond1 = cond1.AndCond(cond.And("T_type", T_type))
  689. }
  690. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  691. if err != nil {
  692. logs.Error(lib.FuncName(), err)
  693. return
  694. }
  695. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  696. if err != nil {
  697. logs.Error(lib.FuncName(), err)
  698. return
  699. }
  700. for _, v := range r {
  701. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_(v))
  702. }
  703. return DeviceSensor_r, cnt
  704. }
  705. // 设备同步参数
  706. func Update_Device_To_DeviceSensor(r_Device Device) bool {
  707. DeviceSensor_list, _ := Read_DeviceSensor_ByTsn(r_Device.T_sn)
  708. for _, v := range DeviceSensor_list {
  709. // 设备同步参数
  710. v.T_Dattery = r_Device.T_Dattery // 电量
  711. v.T_Site = r_Device.T_Site // GPS
  712. v.T_monitor = r_Device.T_monitor // 监控状态 0 未监控 1 监控
  713. v.T_online = r_Device.T_online // 在线状态 0 未启用 1 在线 2 离线
  714. v.T_online_s = r_Device.T_online_s // 在线状态-备用 0 未启用 1 在线 2 离线
  715. v.T_State = r_Device.T_State // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
  716. Update_DeviceSensor(v, "T_Dattery", "T_Site", "T_type", "T_State", "T_monitor", "T_online", "T_online_s")
  717. }
  718. return true
  719. }
  720. // 传感器管理列列表 -------------------
  721. // 与Read_DeviceSensorManageList功能一样(优化前)
  722. func Read_DeviceSensorManageList1(admin_r *Account.Admin, bindSN []string, T_pid int, T_name string, T_calss_id, T_en, T_free, T_datashow, T_sort, page, page_z int) (DeviceSensor_p []DeviceSensor_P, cnt int64) {
  723. o := orm.NewOrm()
  724. var maps []DeviceSensor_P
  725. var maps_z []orm2.ParamsList
  726. var offset int
  727. if page <= 1 {
  728. offset = 0
  729. } else {
  730. offset = (page - 1) * page_z
  731. }
  732. sql_WHERE := "t_pid = " + strconv.Itoa(T_pid)
  733. if len(bindSN) > 0 {
  734. sql_WHERE += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
  735. }
  736. if admin_r.T_pid > 0 {
  737. sql_WHERE += " AND t__state = 1"
  738. }
  739. if len(T_name) > 0 {
  740. sql_WHERE += " AND (ds.t_sn like '%" + T_name + "%' OR ds.t_name like '%" + T_name + "%')"
  741. }
  742. if T_calss_id > 0 {
  743. T_calss := "C" + strconv.Itoa(T_calss_id) + "|"
  744. sql_WHERE += " AND t__class like '%" + T_calss + "%'"
  745. }
  746. if T_en != -1 {
  747. sql_WHERE += " AND t_en = " + strconv.Itoa(T_en)
  748. }
  749. if T_free != -1 {
  750. sql_WHERE += " AND t_free = " + strconv.Itoa(T_free)
  751. }
  752. if T_datashow != -1 {
  753. sql_WHERE += " AND t_datashow =" + strconv.Itoa(T_datashow)
  754. }
  755. sql_ORDER := " ORDER BY t_sort ASC"
  756. if T_sort == 1 {
  757. sql_ORDER = " ORDER BY t_sort DESC"
  758. }
  759. // -------------
  760. sql := "SELECT COUNT(ds.ID) FROM " + "device_sensor ds WHERE " + sql_WHERE
  761. logs.Debug(sql)
  762. _, err := o.Raw(sql).ValuesList(&maps_z)
  763. if err != nil {
  764. logs.Error(lib.FuncName(), err)
  765. return DeviceSensor_p, 0
  766. }
  767. if len(maps_z) == 0 {
  768. return DeviceSensor_p, 0
  769. }
  770. sql = "SELECT * FROM device_sensor ds " +
  771. "LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_en,t_free,t__tlower,t__tupper,t__r_hlower,t__r_hupper,t_enprel,t_tprel,t_tpreu,t_hprel,t_hpreu " +
  772. "FROM device_sensor_parameter WHERE id IN (SELECT MAX(id) FROM device_sensor_parameter WHERE t__state=1 GROUP BY t_sn,t_id)) AS dsp " +
  773. "ON ds.t_sn = dsp.tsn AND ds.t_id = dsp.tid " +
  774. "WHERE " + sql_WHERE + sql_ORDER
  775. if page_z != 9999 {
  776. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  777. }
  778. logs.Debug(sql)
  779. _, err = o.Raw(sql).QueryRows(&maps)
  780. if err != nil {
  781. logs.Error(lib.FuncName(), err)
  782. return
  783. }
  784. key, _ := strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  785. for _, v := range maps {
  786. DeviceSensor_p = append(DeviceSensor_p, DeviceSensorToDeviceSensor_P(v))
  787. }
  788. return DeviceSensor_p, key
  789. }
  790. // 使用联表查询
  791. func Read_DeviceSensorManageList(admin_r *Account.Admin, bindSN []string, T_pid int, T_name string, T_calss_id, T_en, T_free, T_datashow, T_sort, T_abandon, page, page_z int) (DeviceSensor_p []DeviceSensor_P, cnt int64) {
  792. o := orm.NewOrm()
  793. var maps []DeviceSensor_P
  794. var maps_z []orm2.ParamsList
  795. var offset int
  796. if page <= 1 {
  797. offset = 0
  798. } else {
  799. offset = (page - 1) * page_z
  800. }
  801. sql_WHERE := "t_pid = " + strconv.Itoa(T_pid)
  802. if len(bindSN) > 0 {
  803. sql_WHERE += fmt.Sprintf(" AND ds.t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
  804. }
  805. //if admin_r.T_pid > 0 {
  806. // sql_WHERE += " AND ds.t__state = 1"
  807. //}
  808. if T_abandon == 1 {
  809. sql_WHERE += " AND ds.t__state = 0"
  810. } else {
  811. sql_WHERE += " AND ds.t__state = 1"
  812. }
  813. if len(T_name) > 0 {
  814. sql_WHERE += " AND (ds.t_sn like '%" + T_name + "%' OR ds.t_name like '%" + T_name + "%')"
  815. }
  816. if T_calss_id > 0 {
  817. T_calss := "C" + strconv.Itoa(T_calss_id) + "|"
  818. sql_WHERE += " AND t__class like '%" + T_calss + "%'"
  819. }
  820. if T_en != -1 {
  821. sql_WHERE += " AND t_en = " + strconv.Itoa(T_en)
  822. }
  823. if T_free != -1 {
  824. sql_WHERE += " AND t_free = " + strconv.Itoa(T_free)
  825. }
  826. if T_datashow != -1 {
  827. sql_WHERE += " AND t_datashow =" + strconv.Itoa(T_datashow)
  828. }
  829. sql_ORDER := " ORDER BY ds.t_sort ASC,ds.t_id,ds.id"
  830. if T_sort == 1 {
  831. sql_ORDER = " ORDER BY ds.t_sort DESC,ds.t_id,ds.id"
  832. }
  833. // -------------
  834. sql := "SELECT COUNT(ds.ID) FROM " + "device_sensor ds " +
  835. "LEFT JOIN device_sensor_parameter dsp " +
  836. "ON ds.t_sp=dsp.id " +
  837. "WHERE " + sql_WHERE + sql_ORDER
  838. logs.Debug(sql)
  839. _, err := o.Raw(sql).ValuesList(&maps_z)
  840. if err != nil {
  841. logs.Error(lib.FuncName(), err)
  842. return DeviceSensor_p, 0
  843. }
  844. if len(maps_z) == 0 {
  845. return DeviceSensor_p, 0
  846. }
  847. //"LEFT JOIN (SELECT id,t_en,t_free,t__tlower,t__tupper,t__r_hlower,t__r_hupper,t_enprel,t_tprel,t_tpreu,t_hprel,t_hpreu " +
  848. //"FROM device_sensor_parameter) dsp " +
  849. sql = "SELECT ds.t_sn,ds.t_id,ds.t_name,t_type,t_sort,t_datashow,ds.t__state,t_en,t_free,t__tlower,t__tupper,t__r_hlower,t__r_hupper,t_enprel,t_tprel,t_tpreu,t_hprel,t_hpreu FROM device_sensor ds " +
  850. "LEFT JOIN device_sensor_parameter dsp " +
  851. "ON ds.t_sp=dsp.id " +
  852. "WHERE " + sql_WHERE + sql_ORDER
  853. if page_z != 9999 {
  854. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  855. }
  856. logs.Debug(sql)
  857. _, err = o.Raw(sql).QueryRows(&maps)
  858. if err != nil {
  859. logs.Error(lib.FuncName(), err)
  860. return
  861. }
  862. key, _ := strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  863. for _, v := range maps {
  864. DeviceSensor_p = append(DeviceSensor_p, DeviceSensorToDeviceSensor_P(v))
  865. }
  866. return DeviceSensor_p, key
  867. }
  868. // 数据展示菜单下 传感器参数列表
  869. // 实时数据页面 温湿度异常数据往前排
  870. // 轨迹展示页面
  871. func Read_DeviceSensor_List_For_Data(bindSN []string, T_pid int, T_name string, T_Class_id, T_type, T_RealTime, T_MapShow, T_abandon int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  872. o := orm.NewOrm()
  873. // 也可以直接使用 Model 结构体作为表名
  874. qs := o.QueryTable(new(DeviceSensor))
  875. var offset int
  876. if page <= 1 {
  877. offset = 0
  878. } else {
  879. offset = (page - 1) * page_z
  880. }
  881. var r []DeviceSensor
  882. cond := orm.NewCondition()
  883. cond1 := cond.And("T_pid", T_pid)
  884. if len(bindSN) > 0 {
  885. cond1 = cond1.And("T_sn__in", bindSN)
  886. }
  887. if T_Class_id > 0 {
  888. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  889. cond1 = cond1.And("T_Class__icontains", T_Class)
  890. }
  891. if T_type > 0 {
  892. cond1 = cond1.And("T_type", T_type)
  893. }
  894. if T_abandon == 1 {
  895. cond1 = cond1.And("T_State", 0)
  896. } else {
  897. cond1 = cond1.And("T_State", 1)
  898. }
  899. //if len(T_name) >= 16 {
  900. // cond1 = cond1.And("T_sn", T_name)
  901. //} else if len(T_name) > 0 && len(T_name) < 16 {
  902. // cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_State", 1).And("T_datashow", 1)
  903. //} else {
  904. // cond1 = cond1.And("T_State", 1).And("T_datashow", 1)
  905. //}
  906. if len(T_name) >= 16 {
  907. cond1 = cond1.And("T_sn", T_name)
  908. } else if len(T_name) > 0 && len(T_name) < 16 {
  909. cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_datashow", 1)
  910. } else {
  911. cond1 = cond1.And("T_datashow", 1)
  912. }
  913. if T_MapShow == 1 {
  914. cond1 = cond1.AndNot("T_Site", "")
  915. }
  916. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
  917. if err != nil {
  918. logs.Error(lib.FuncName(), err)
  919. return
  920. }
  921. // 实时数据页面,温度或者湿度不在正常区间,排在最前面
  922. if T_RealTime == 1 {
  923. offset_z := offset + page_z
  924. if cnt < int64(offset_z) {
  925. offset_z = int(cnt)
  926. }
  927. if offset > offset_z {
  928. offset = offset_z
  929. }
  930. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id", "Id").All(&r)
  931. if err != nil {
  932. logs.Error(lib.FuncName(), err)
  933. return
  934. }
  935. var DeviceSensor_unusual []DeviceSensor_R
  936. var DeviceSensor_monitor []DeviceSensor_R
  937. for _, v := range r {
  938. v_r := DeviceSensorToDeviceSensor_R(v)
  939. data := v_r.T_DeviceSensorData
  940. // 状态在监控中,温度或者湿度不在正常区间,排在最前面
  941. if (data.T_t > data.T_tu || data.T_t < data.T_tl || data.T_rh > data.T_rhu || data.T_rh < data.T_rhl) && v.T_monitor == 1 && v.T_link == 1 && v_r.T_DeviceSensorParameter.T_free == 0 {
  942. DeviceSensor_unusual = append(DeviceSensor_unusual, v_r)
  943. } else if v.T_monitor == 1 && v.T_link == 1 && v_r.T_DeviceSensorParameter.T_free == 0 {
  944. DeviceSensor_monitor = append(DeviceSensor_monitor, v_r)
  945. } else {
  946. DeviceSensor_r = append(DeviceSensor_r, v_r)
  947. }
  948. }
  949. DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_monitor...)
  950. DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_r...)
  951. return DeviceSensor_unusual[offset:offset_z], cnt
  952. }
  953. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id", "Id").All(&r)
  954. if err != nil {
  955. logs.Error(lib.FuncName(), err)
  956. return
  957. }
  958. for _, v := range r {
  959. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  960. }
  961. return DeviceSensor_r, cnt
  962. }
  963. // 与Read_DeviceSensor_List_For_Data功能一样(优化前)
  964. func Read_DeviceSensor_List_For_Data1(bindSN []string, T_pid int, T_name string, T_Class_id, T_type, T_RealTime, T_MapShow int,
  965. page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  966. o := orm.NewOrm()
  967. var maps_z []orm2.ParamsList
  968. // 也可以直接使用 Model 结构体作为表名
  969. var offset int
  970. if page <= 1 {
  971. offset = 0
  972. } else {
  973. offset = (page - 1) * page_z
  974. }
  975. var r []DeviceSensor_RealTime
  976. sql_WHERE := "t_pid = " + strconv.Itoa(T_pid)
  977. if len(bindSN) > 0 {
  978. sql_WHERE += fmt.Sprintf(" AND t_sn in (%s) ", lib.StringListToDotStr(bindSN))
  979. }
  980. if T_Class_id > 0 {
  981. T_calss := "C" + strconv.Itoa(T_Class_id) + "|"
  982. sql_WHERE += " AND t__class like '%" + T_calss + "%'"
  983. }
  984. if T_type > 0 {
  985. sql_WHERE += " AND t_type = " + strconv.Itoa(T_type)
  986. }
  987. if len(T_name) >= 16 {
  988. sql_WHERE += " AND t_sn = '" + T_name + "'"
  989. } else if len(T_name) > 0 && len(T_name) < 16 {
  990. sql_WHERE += " AND (ds.t_sn like '%" + T_name + "%' OR ds.t_name like '%" + T_name + "%') AND ds.t__state = 1 AND t_datashow = 1"
  991. } else {
  992. sql_WHERE += " AND ds.t__state = 1 AND t_datashow = 1"
  993. }
  994. if T_MapShow == 1 {
  995. sql_WHERE += " AND t__site is not null"
  996. }
  997. // -------------
  998. sql := "SELECT COUNT(*) FROM " + "device_sensor ds WHERE " + sql_WHERE
  999. logs.Debug(sql)
  1000. _, err := o.Raw(sql).ValuesList(&maps_z)
  1001. if err != nil {
  1002. logs.Error(lib.FuncName(), err)
  1003. return DeviceSensor_r, 0
  1004. }
  1005. if len(maps_z) == 0 {
  1006. return DeviceSensor_r, 0
  1007. }
  1008. cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  1009. sql = "SELECT *,ds.t_sn as t_sn,ds.t_id as t_id,ds.t_name as t_name,ds.t__state as t__state FROM device_sensor ds " +
  1010. "LEFT JOIN device_sensor_parameter dsp " +
  1011. "ON ds.t_sp=dsp.id " +
  1012. "WHERE " + sql_WHERE + " ORDER BY ds.t_sort, ds.id ASC"
  1013. realTimeSql := sql
  1014. if page_z != 9999 {
  1015. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  1016. }
  1017. logs.Debug(sql)
  1018. _, err = o.Raw(sql).QueryRows(&r)
  1019. if err != nil {
  1020. logs.Error(lib.FuncName(), err)
  1021. return
  1022. }
  1023. if T_RealTime == 1 {
  1024. offset_z := offset + page_z
  1025. if cnt < int64(offset_z) {
  1026. offset_z = int(cnt)
  1027. }
  1028. if offset > offset_z {
  1029. offset = offset_z
  1030. }
  1031. _, err = o.Raw(realTimeSql).QueryRows(&r)
  1032. if err != nil {
  1033. logs.Error(lib.FuncName(), err)
  1034. return
  1035. }
  1036. var DeviceSensor_unusual []DeviceSensor_R
  1037. for _, v := range r {
  1038. v_r := DeviceSensorRealTimeToDeviceSensor_R(v)
  1039. data := v_r.T_DeviceSensorData
  1040. // 温度或者湿度不在正常区间,排在最前面
  1041. if (data.T_t > data.T_tu || data.T_t < data.T_tl || data.T_rh > data.T_rhu || data.T_rh < data.T_rhl) && v.T_monitor == 1 && v_r.T_DeviceSensorParameter.T_free == 0 {
  1042. DeviceSensor_unusual = append(DeviceSensor_unusual, v_r)
  1043. } else {
  1044. DeviceSensor_r = append(DeviceSensor_r, v_r)
  1045. }
  1046. }
  1047. DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_r...)
  1048. return DeviceSensor_unusual[offset:offset_z], cnt
  1049. }
  1050. for _, v := range r {
  1051. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorRealTimeToDeviceSensor_R(v))
  1052. }
  1053. return DeviceSensor_r, cnt
  1054. }
  1055. // 公司管理 实时数据
  1056. // 查询当前公司及其下面所有子公司的传感器列表
  1057. func Read_CompanyDeviceSensor_List_For_Data(T_pids []int, T_name string, T_Class_id, T_type, T_RealTime, T_MapShow int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  1058. o := orm.NewOrm()
  1059. // 也可以直接使用 Model 结构体作为表名
  1060. qs := o.QueryTable(new(DeviceSensor))
  1061. var offset int
  1062. if page <= 1 {
  1063. offset = 0
  1064. } else {
  1065. offset = (page - 1) * page_z
  1066. }
  1067. var r []DeviceSensor
  1068. cond := orm.NewCondition()
  1069. cond1 := cond.And("T_pid__in", T_pids)
  1070. if T_Class_id > 0 {
  1071. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  1072. cond1 = cond1.And("T_Class__icontains", T_Class)
  1073. }
  1074. if T_type > 0 {
  1075. cond1 = cond1.And("T_type", T_type)
  1076. }
  1077. if len(T_name) >= 16 {
  1078. cond1 = cond1.And("T_sn", T_name)
  1079. } else if len(T_name) > 0 && len(T_name) < 16 {
  1080. cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_State", 1).And("T_datashow", 1)
  1081. } else {
  1082. cond1 = cond1.And("T_State", 1).And("T_datashow", 1)
  1083. }
  1084. if T_MapShow == 1 {
  1085. cond1 = cond1.AndNot("T_Site", "")
  1086. }
  1087. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
  1088. if err != nil {
  1089. logs.Error(lib.FuncName(), err)
  1090. return
  1091. }
  1092. // 实时数据页面,温度或者湿度不在正常区间,排在最前面
  1093. if T_RealTime == 1 {
  1094. offset_z := offset + page_z
  1095. if cnt < int64(offset_z) {
  1096. offset_z = int(cnt)
  1097. }
  1098. if offset > offset_z {
  1099. offset = offset_z
  1100. }
  1101. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  1102. if err != nil {
  1103. logs.Error(lib.FuncName(), err)
  1104. return
  1105. }
  1106. var DeviceSensor_unusual []DeviceSensor_R
  1107. for _, v := range r {
  1108. v_r := DeviceSensorToDeviceSensor_R(v)
  1109. data := v_r.T_DeviceSensorData
  1110. // 温度或者湿度不在正常区间,排在最前面
  1111. if (data.T_t > data.T_tu || data.T_t < data.T_tl || data.T_rh > data.T_rhu || data.T_rh < data.T_rhl) && v.T_monitor == 1 && v_r.T_DeviceSensorParameter.T_free == 0 {
  1112. DeviceSensor_unusual = append(DeviceSensor_unusual, v_r)
  1113. } else {
  1114. DeviceSensor_r = append(DeviceSensor_r, v_r)
  1115. }
  1116. }
  1117. DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_r...)
  1118. return DeviceSensor_unusual[offset:offset_z], cnt
  1119. }
  1120. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  1121. if err != nil {
  1122. logs.Error(lib.FuncName(), err)
  1123. return
  1124. }
  1125. for _, v := range r {
  1126. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  1127. }
  1128. return DeviceSensor_r, cnt
  1129. }
  1130. func Read_CompanyDeviceSensor_List_For_Data1(T_pids []int, T_name string, T_Class_id, T_type, T_RealTime, T_MapShow int,
  1131. page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  1132. o := orm.NewOrm()
  1133. var maps_z []orm2.ParamsList
  1134. // 也可以直接使用 Model 结构体作为表名
  1135. var offset int
  1136. if page <= 1 {
  1137. offset = 0
  1138. } else {
  1139. offset = (page - 1) * page_z
  1140. }
  1141. var r []DeviceSensor_RealTime
  1142. sql_WHERE := ""
  1143. if len(T_pids) == 1 {
  1144. sql_WHERE += " AND t_pid = " + strconv.Itoa(T_pids[0])
  1145. } else if len(T_pids) > 1 {
  1146. sql_WHERE += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids))
  1147. }
  1148. if T_Class_id > 0 {
  1149. T_calss := "C" + strconv.Itoa(T_Class_id) + "|"
  1150. sql_WHERE += " AND t__class like '%" + T_calss + "%'"
  1151. }
  1152. if T_type > 0 {
  1153. sql_WHERE += " AND t_type = " + strconv.Itoa(T_type)
  1154. }
  1155. if len(T_name) >= 16 {
  1156. sql_WHERE += " AND t_sn = '" + T_name + "'"
  1157. } else if len(T_name) > 0 && len(T_name) < 16 {
  1158. sql_WHERE += " AND (ds.t_sn like '%" + T_name + "%' OR ds.t_name like '%" + T_name + "%') AND ds.t__state = 1 AND t_datashow = 1"
  1159. } else {
  1160. sql_WHERE += " AND ds.t__state = 1 AND t_datashow = 1"
  1161. }
  1162. if T_MapShow == 1 {
  1163. sql_WHERE += " AND t__site is not null"
  1164. }
  1165. sql_WHERE = strings.TrimLeft(sql_WHERE, " AND")
  1166. // -------------
  1167. sql := "SELECT COUNT(*) FROM " + "device_sensor ds WHERE " + sql_WHERE
  1168. logs.Debug(sql)
  1169. _, err := o.Raw(sql).ValuesList(&maps_z)
  1170. if err != nil {
  1171. logs.Error(lib.FuncName(), err)
  1172. return DeviceSensor_r, 0
  1173. }
  1174. if len(maps_z) == 0 {
  1175. return DeviceSensor_r, 0
  1176. }
  1177. cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  1178. sql = "SELECT *,ds.t_sn as t_sn,ds.t_id as t_id,ds.t_name as t_name,ds.t__state as t__state FROM device_sensor ds " +
  1179. "LEFT JOIN device_sensor_parameter dsp " +
  1180. "ON ds.t_sp=dsp.id " +
  1181. "WHERE " + sql_WHERE + " ORDER BY ds.t_sort, ds.id ASC"
  1182. realTimeSql := sql
  1183. if page_z != 9999 {
  1184. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  1185. }
  1186. logs.Debug(sql)
  1187. _, err = o.Raw(sql).QueryRows(&r)
  1188. if err != nil {
  1189. logs.Error(lib.FuncName(), err)
  1190. return
  1191. }
  1192. if T_RealTime == 1 {
  1193. offset_z := offset + page_z
  1194. if cnt < int64(offset_z) {
  1195. offset_z = int(cnt)
  1196. }
  1197. if offset > offset_z {
  1198. offset = offset_z
  1199. }
  1200. _, err = o.Raw(realTimeSql).QueryRows(&r)
  1201. if err != nil {
  1202. logs.Error(lib.FuncName(), err)
  1203. return
  1204. }
  1205. var DeviceSensor_unusual []DeviceSensor_R
  1206. for _, v := range r {
  1207. v_r := DeviceSensorRealTimeToDeviceSensor_R(v)
  1208. data := v_r.T_DeviceSensorData
  1209. // 温度或者湿度不在正常区间,排在最前面
  1210. if (data.T_t > data.T_tu || data.T_t < data.T_tl || data.T_rh > data.T_rhu || data.T_rh < data.T_rhl) && v.T_monitor == 1 && v_r.T_DeviceSensorParameter.T_free == 0 {
  1211. DeviceSensor_unusual = append(DeviceSensor_unusual, v_r)
  1212. } else {
  1213. DeviceSensor_r = append(DeviceSensor_r, v_r)
  1214. }
  1215. }
  1216. DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_r...)
  1217. return DeviceSensor_unusual[offset:offset_z], cnt
  1218. }
  1219. for _, v := range r {
  1220. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorRealTimeToDeviceSensor_R(v))
  1221. }
  1222. return DeviceSensor_r, cnt
  1223. }
  1224. func Read_CompanyDeviceSensor_List_For_Data_ByKey(T_pids []int, T_sn string) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  1225. o := orm.NewOrm()
  1226. // 也可以直接使用 Model 结构体作为表名
  1227. qs := o.QueryTable(new(DeviceSensor))
  1228. var r []DeviceSensor
  1229. cond := orm.NewCondition()
  1230. cond1 := cond.And("T_pid__in", T_pids)
  1231. if len(T_sn) > 0 {
  1232. cond1 = cond1.And("T_sn", T_sn)
  1233. }
  1234. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
  1235. if err != nil {
  1236. logs.Error(lib.FuncName(), err)
  1237. return
  1238. }
  1239. _, err = qs.SetCond((*orm2.Condition)(cond1)).All(&r)
  1240. if err != nil {
  1241. logs.Error(lib.FuncName(), err)
  1242. return
  1243. }
  1244. for _, v := range r {
  1245. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  1246. }
  1247. return DeviceSensor_r, cnt
  1248. }
  1249. // 小程序-获取传感器类型ID
  1250. func Read_DeviceSensor_T_type(admin *Account.Admin, T_pid int, bindSN []string, T_pids []int) (lists orm2.ParamsList, err error) {
  1251. o := orm.NewOrm()
  1252. var pl_lists orm2.ParamsList
  1253. sql := "SELECT DISTINCT t_type FROM device_sensor WHERE t_datashow=1 AND t__state=1"
  1254. if admin.T_pid > 0 {
  1255. // 客户访问自己及其子公司数据
  1256. if len(T_pids) > 0 {
  1257. sql += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids))
  1258. }
  1259. } else {
  1260. // 管理员访问当前公司数据
  1261. sql += " AND t_pid = " + strconv.Itoa(T_pid)
  1262. }
  1263. if len(bindSN) > 0 {
  1264. sql += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
  1265. }
  1266. sql += " LIMIT 0,1000"
  1267. _, err = o.Raw(sql).ValuesFlat(&pl_lists)
  1268. if err != nil {
  1269. logs.Error(lib.FuncName(), err)
  1270. return
  1271. }
  1272. return pl_lists, nil
  1273. }
  1274. // 数据展示菜单下 传感器参数列表
  1275. func Read_DeviceSensor_List_For_Applet(admin *Account.Admin, bindSN []string, T_pid int, T_pids []int, T_name, T_online string,
  1276. T_RealTime, T_type, page, page_z int) (DeviceSensor_r []DeviceSensor_Applet, cnt int64) {
  1277. o := orm.NewOrm()
  1278. // 也可以直接使用 Model 结构体作为表名
  1279. var offset int
  1280. if page <= 1 {
  1281. offset = 0
  1282. } else {
  1283. offset = (page - 1) * page_z
  1284. }
  1285. qs := o.QueryTable(new(DeviceSensor))
  1286. var r []DeviceSensor
  1287. cond := orm.NewCondition()
  1288. cond1 := orm.NewCondition()
  1289. if admin.T_pid > 0 {
  1290. cond1 = cond.And("T_pid__in", T_pids)
  1291. } else {
  1292. cond1 = cond.And("T_pid", T_pid)
  1293. }
  1294. if T_type > 0 {
  1295. cond1 = cond1.And("T_type", T_type)
  1296. }
  1297. if len(bindSN) > 0 {
  1298. cond1 = cond1.And("T_sn__in", bindSN)
  1299. }
  1300. if admin.T_pid > 0 {
  1301. if len(T_name) >= 16 {
  1302. cond1 = cond1.And("T_sn", T_name)
  1303. } else if len(T_name) > 0 && len(T_name) < 16 {
  1304. cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_State", 1).And("T_datashow", 1)
  1305. } else {
  1306. cond1 = cond1.And("T_State", 1).And("T_datashow", 1)
  1307. }
  1308. } else {
  1309. if len(T_name) >= 16 {
  1310. cond1 = cond1.And("T_sn", T_name)
  1311. } else if len(T_name) > 0 && len(T_name) < 16 {
  1312. cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_State", 1).And("T_datashow", 1)
  1313. } else {
  1314. cond1 = cond1.And("T_State", 1).And("T_datashow", 1)
  1315. }
  1316. }
  1317. // 0 未监控 1 监控中 2 无网络 3 全部
  1318. if T_online == "1" {
  1319. cond1 = cond1.And("T_monitor", 1)
  1320. } else if T_online == "2" {
  1321. cond1 = cond1.AndCond(cond.AndCond(cond.AndNot("T_online", 1)).
  1322. AndCond(cond.AndNot("T_online_s", 1)))
  1323. } else if T_online == "0" {
  1324. cond1 = cond1.And("T_monitor", 0)
  1325. }
  1326. var err error
  1327. if page_z == 9999 {
  1328. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  1329. } else {
  1330. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  1331. }
  1332. if err != nil {
  1333. logs.Error(lib.FuncName(), err)
  1334. return
  1335. }
  1336. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  1337. if err != nil {
  1338. logs.Error(lib.FuncName(), err)
  1339. return
  1340. }
  1341. for _, v := range r {
  1342. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_Applet(v))
  1343. }
  1344. return DeviceSensor_r, cnt
  1345. }
  1346. // 小程序-通过传感器类型获取sn
  1347. func Read_DeviceSensor_T_sn_ByT_type(admin *Account.Admin, T_pid int, bindSN []string, T_pids []int, T_type int) (lists []string, err error) {
  1348. o := orm.NewOrm()
  1349. sql := "t__state=1 AND t_datashow=1 AND t_type = " + strconv.Itoa(T_type)
  1350. if admin.T_pid > 0 {
  1351. // 客户访问自己及其子公司数据
  1352. if len(T_pids) > 0 {
  1353. sql += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids))
  1354. }
  1355. } else {
  1356. // 管理员访问当前公司数据
  1357. sql += " AND t_pid = " + strconv.Itoa(T_pid)
  1358. }
  1359. if len(bindSN) > 0 {
  1360. sql += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
  1361. }
  1362. _, err = o.Raw("SELECT DISTINCT T_sn FROM device_sensor WHERE " + sql + " LIMIT 0,1000").QueryRows(&lists)
  1363. if err != nil {
  1364. logs.Error(lib.FuncName(), err)
  1365. return
  1366. }
  1367. return lists, nil
  1368. }
  1369. func Read_DeviceSensorCount_ByT_pids(T_pids []int) (cnt int64) {
  1370. o := orm.NewOrm()
  1371. // 也可以直接使用 Model 结构体作为表名
  1372. qs := o.QueryTable(new(DeviceSensor))
  1373. cond := orm.NewCondition()
  1374. cond1 := cond.And("T_pid__in", T_pids).And("T_State", 1)
  1375. //// 不是内部权限(T_pid>0),T_State=1 0 屏蔽 1 正常
  1376. //cond1 = cond1.And("T_State", 1)
  1377. //// 0 屏蔽数据展示 1 正常数据展示
  1378. //cond1 = cond1.And("T_datashow", 1)
  1379. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
  1380. if err != nil {
  1381. logs.Error(lib.FuncName(), err)
  1382. }
  1383. return cnt
  1384. }
  1385. func Read_DeviceSensor_All() (DeviceSensor_r []DeviceSensor) {
  1386. o := orm.NewOrm()
  1387. // 也可以直接使用 Model 结构体作为表名
  1388. qs := o.QueryTable(new(DeviceSensor))
  1389. _, err := qs.All(&DeviceSensor_r)
  1390. if err != nil {
  1391. logs.Error(lib.FuncName(), err)
  1392. }
  1393. return DeviceSensor_r
  1394. }
  1395. // 获取对接电信的实时数据
  1396. func Read_Docking_Real_Data(T_pids []int) (DeviceData_r []DeviceData_Docking) {
  1397. o := orm.NewOrm()
  1398. // 也可以直接使用 Model 结构体作为表名
  1399. qs := o.QueryTable(new(DeviceSensor))
  1400. var r []DeviceSensor
  1401. cond := orm.NewCondition()
  1402. cond1 := cond.And("T_pid__in", T_pids)
  1403. _, err := qs.SetCond((*orm2.Condition)(cond1)).All(&r)
  1404. if err != nil {
  1405. logs.Error(lib.FuncName(), err)
  1406. return
  1407. }
  1408. for _, v := range r {
  1409. sp, _ := Read_DeviceSensorParameter(v.T_sn, v.T_id)
  1410. deviceDate := Read_DeviceData(v.T_sn, v.T_id)
  1411. data := DeviceData_Docking{
  1412. T_monitor: v.T_monitor,
  1413. T_online: v.T_online,
  1414. T_online_s: v.T_online_s,
  1415. T_sn: v.T_sn,
  1416. T_id: v.T_id,
  1417. T_t: deviceDate.T_t,
  1418. T_rh: deviceDate.T_rh,
  1419. T_site: deviceDate.T_site,
  1420. T_time: deviceDate.T_time.Format("2006-01-02 15:04:05"),
  1421. T_sp: deviceDate.T_sp,
  1422. T_name: sp.T_name,
  1423. T_tl: sp.T_Tlower,
  1424. T_tu: sp.T_Tupper,
  1425. T_rhl: sp.T_RHlower,
  1426. T_rhu: sp.T_RHupper,
  1427. }
  1428. DeviceData_r = append(DeviceData_r, data)
  1429. }
  1430. return DeviceData_r
  1431. }
  1432. // 获取列表
  1433. func Read_DeviceSensorList_BySN(T_sn string) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
  1434. o := orm.NewOrm()
  1435. // 也可以直接使用 Model 结构体作为表名
  1436. qs := o.QueryTable(new(DeviceSensor))
  1437. var r []DeviceSensor
  1438. cond := orm.NewCondition()
  1439. cond1 := cond.And("T_sn__icontains", T_sn)
  1440. _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  1441. if err != nil {
  1442. logs.Error(lib.FuncName(), err)
  1443. return
  1444. }
  1445. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  1446. if err != nil {
  1447. logs.Error(lib.FuncName(), err)
  1448. return
  1449. }
  1450. for _, v := range r {
  1451. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  1452. }
  1453. return DeviceSensor_r, cnt
  1454. }
  1455. func Read_DeviceSensorList_By_T_snid(T_snid string) []DeviceSensor {
  1456. T_snid_list := strings.Split(strings.Trim(T_snid, "|"), "|")
  1457. var maps []DeviceSensor
  1458. ids := []int{}
  1459. for _, v := range T_snid_list {
  1460. sn_id := strings.Split(v, ",")
  1461. if len(sn_id) == 2 {
  1462. ds, _ := Read_DeviceSensor_ByTsn_Tid(sn_id[0], lib.To_int(sn_id[1]))
  1463. ids = append(ids, ds.Id)
  1464. }
  1465. }
  1466. o := orm.NewOrm()
  1467. cond := orm.NewCondition()
  1468. cond1 := cond.And("id__in", ids)
  1469. qs := o.QueryTable(new(DeviceSensor))
  1470. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&maps)
  1471. return maps
  1472. }