Device.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package controllers
  2. import (
  3. "bzd_server/conf"
  4. "bzd_server/lib"
  5. "bzd_server/models/Device"
  6. "bzd_server/models/System"
  7. beego "github.com/beego/beego/v2/server/web"
  8. "math"
  9. )
  10. type DeviceController struct {
  11. beego.Controller
  12. }
  13. // 列表 -
  14. func (c *DeviceController) List() {
  15. // 验证登录 User_is, User_r
  16. _,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  17. if !User_is {
  18. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  19. c.ServeJSON()
  20. return
  21. }
  22. var r_jsons lib.R_JSONS
  23. page, _ := c.GetInt("page")
  24. if page < 1 {
  25. page = 1
  26. }
  27. page_z, _ := c.GetInt("page_z")
  28. if page_z < 1 {
  29. page_z = conf.Page_size
  30. }
  31. T_sn := c.GetString("T_sn")
  32. T_MSISDN := c.GetString("T_MSISDN")
  33. var cnt int64
  34. List, cnt := Device.Read_Device_List(T_sn, T_MSISDN, page, page_z)
  35. page_size := math.Ceil(float64(cnt) / float64(page_z))
  36. r_jsons.List = List
  37. r_jsons.Page = page
  38. r_jsons.Page_size = int(page_size)
  39. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  40. r_jsons.Num = int(cnt)
  41. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  42. c.ServeJSON()
  43. return
  44. }
  45. // 添加-
  46. func (c *DeviceController) Add() {
  47. // 验证登录 User_is, User_r
  48. user_r,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  49. if !User_is {
  50. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  51. c.ServeJSON()
  52. return
  53. }
  54. T_sn := c.GetString("T_sn")
  55. T_MSISDN := c.GetString("T_MSISDN")
  56. var_ := Device.Device{
  57. T_sn: T_sn,
  58. T_MSISDN: T_MSISDN,
  59. T_State: 1,
  60. }
  61. if _, is := Device.Read_Device(T_sn);is{
  62. c.Data["json"] = lib.JSONS{Code: 201, Msg: "重复添加!"}
  63. c.ServeJSON()
  64. return
  65. }
  66. Id, is := Device.Add_Device(var_)
  67. if !is {
  68. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  69. c.ServeJSON()
  70. return
  71. }
  72. System.Add_UserLogs_T(user_r.T_uuid,"设备管理", "添加", var_)
  73. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!",Data: Id}
  74. c.ServeJSON()
  75. return
  76. }
  77. // 修改-
  78. func (c *DeviceController) Up() {
  79. // 验证登录 User_is, User_r
  80. user_r,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  81. if !User_is {
  82. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  83. c.ServeJSON()
  84. return
  85. }
  86. T_sn := c.GetString("T_sn")
  87. T_MSISDN := c.GetString("T_MSISDN")
  88. Id, err := c.GetInt("Id")
  89. if err != nil {
  90. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"}
  91. c.ServeJSON()
  92. return
  93. }
  94. r, is := Device.Read_Device_ById(Id);
  95. if !is {
  96. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"}
  97. c.ServeJSON()
  98. return
  99. }
  100. r.T_sn = T_sn
  101. r.T_MSISDN = T_MSISDN
  102. // .......
  103. if !Device.Update_Device(r,"T_sn","T_MSISDN") {
  104. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  105. c.ServeJSON()
  106. return
  107. }
  108. System.Add_UserLogs_T(user_r.T_uuid,"设备管理", "修改", r)
  109. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  110. c.ServeJSON()
  111. return
  112. }
  113. // 删除-
  114. func (c *DeviceController) Del() {
  115. // 验证登录 User_is, User_r
  116. user_r,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  117. if !User_is {
  118. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  119. c.ServeJSON()
  120. return
  121. }
  122. Id, err := c.GetInt("Id")
  123. if err != nil {
  124. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"}
  125. c.ServeJSON()
  126. return
  127. }
  128. if r, is := Device.Read_Device_ById(Id); is {
  129. if !Device.Delete_Device(r) {
  130. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  131. c.ServeJSON()
  132. return
  133. }
  134. System.Add_UserLogs_T(user_r.T_uuid,"设备管理", "删除", r)
  135. }
  136. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  137. c.ServeJSON()
  138. return
  139. }
  140. // 列表 - 分类设备
  141. func (c *DeviceController) Device_Class() {
  142. // 验证登录 User_is, User_r
  143. User_r,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  144. if !User_is {
  145. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  146. c.ServeJSON()
  147. return
  148. }
  149. var r_jsons lib.R_JSONS_s
  150. page, _ := c.GetInt("page")
  151. if page < 1 {
  152. page = 1
  153. }
  154. page_z, _ := c.GetInt("page_z")
  155. if page_z < 1 {
  156. page_z = conf.Page_size
  157. }
  158. T_class,_ := c.GetInt("T_class")
  159. T_sn := c.GetString("T_sn")
  160. r, is := Device.Read_DeviceClass_ById(T_class);
  161. if !is {
  162. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  163. c.ServeJSON()
  164. return
  165. }
  166. if User_r.T_uuid != r.T_uuid {
  167. c.Data["json"] = lib.JSONS{Code: 203, Msg: "没有权限!"}
  168. c.ServeJSON()
  169. return
  170. }
  171. var cnt int64
  172. DeviceList, cnt := Device.Read_DeviceClassList_List(T_class,T_sn, page, page_z)
  173. for _,v := range DeviceList{
  174. Device_r,_ := Device.Read_Device(v.T_sn)
  175. Device_r.T_note_file_num = Device.Read_DeviceSensorData_List_z(v.T_sn,r.CreateTime.Format("2006-01-02 15:04:05"))
  176. r_jsons.List = append(r_jsons.List, Device_r)
  177. }
  178. page_size := math.Ceil(float64(cnt) / float64(page_z))
  179. r_jsons.Page = page
  180. r_jsons.Page_size = int(page_size)
  181. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  182. r_jsons.Num = int(cnt)
  183. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  184. c.ServeJSON()
  185. return
  186. }
  187. // 列表 - 分类设备
  188. func (c *DeviceController) Device_Data() {
  189. // 验证登录 User_is, User_r
  190. _,User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  191. if !User_is {
  192. c.Data["json"] = lib.JSONS{Code: 302, Msg: "请重新登录!"}
  193. c.ServeJSON()
  194. return
  195. }
  196. type R_JSONS_s struct {
  197. //必须的大写开头
  198. List []Device.DeviceData_
  199. Num int
  200. Page int
  201. Page_size int
  202. Pages []lib.Page_T
  203. }
  204. var r_jsons R_JSONS_s
  205. page, _ := c.GetInt("page")
  206. if page < 1 {
  207. page = 1
  208. }
  209. page_z, _ := c.GetInt("page_z")
  210. if page_z < 1 {
  211. page_z = conf.Page_size
  212. }
  213. T_sn := c.GetString("T_sn")
  214. Time_start := c.GetString("Time_start")
  215. Time_and := c.GetString("Time_end")
  216. var cnt int
  217. r_jsons.List, cnt = Device.Read_DeviceSensorData_List(T_sn,Time_start,Time_and, page, page_z)
  218. page_size := math.Ceil(float64(cnt) / float64(page_z))
  219. r_jsons.Page = page
  220. r_jsons.Page_size = int(page_size)
  221. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  222. r_jsons.Num = int(cnt)
  223. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  224. c.ServeJSON()
  225. return
  226. }