Device.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package controllers
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/models/Device"
  6. "ColdVerify_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: 201, 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: 201, 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: 202, 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: 201, 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: 202, 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: 202, 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: 201, Msg: "请重新登录!"}
  119. c.ServeJSON()
  120. return
  121. }
  122. Id, err := c.GetInt("Id")
  123. if err != nil {
  124. c.Data["json"] = lib.JSONS{Code: 202, 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. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  136. c.ServeJSON()
  137. return
  138. }
  139. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  140. c.ServeJSON()
  141. return
  142. }
  143. // 列表 - 分类设备
  144. func (c *DeviceController) Device_Class() {
  145. // 验证登录 User_is, User_r
  146. User_r, User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  147. if !User_is {
  148. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  149. c.ServeJSON()
  150. return
  151. }
  152. var r_jsons lib.R_JSONS_s
  153. page, _ := c.GetInt("page")
  154. if page < 1 {
  155. page = 1
  156. }
  157. page_z, _ := c.GetInt("page_z")
  158. if page_z < 1 {
  159. page_z = conf.Page_size
  160. }
  161. T_class, _ := c.GetInt("T_class")
  162. T_sn := c.GetString("T_sn")
  163. r, is := Device.Read_DeviceClass_ById(T_class)
  164. if !is {
  165. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  166. c.ServeJSON()
  167. return
  168. }
  169. if User_r.T_uuid != r.T_uuid {
  170. c.Data["json"] = lib.JSONS{Code: 203, Msg: "没有权限!"}
  171. c.ServeJSON()
  172. return
  173. }
  174. var cnt int64
  175. DeviceList, cnt := Device.Read_DeviceClassList_List(T_class, T_sn, page, page_z)
  176. for _, v := range DeviceList {
  177. Device_r, _ := Device.Read_Device(v.T_sn)
  178. Device_r.T_note_file_num = Device.Read_DeviceSensorData_List_z(v.T_sn, r.CreateTime.Format("2006-01-02 15:04:05"))
  179. r_jsons.List = append(r_jsons.List, Device_r)
  180. }
  181. page_size := math.Ceil(float64(cnt) / float64(page_z))
  182. r_jsons.Page = page
  183. r_jsons.Page_size = int(page_size)
  184. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  185. r_jsons.Num = int(cnt)
  186. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  187. c.ServeJSON()
  188. return
  189. }
  190. // 列表 - 分类设备
  191. func (c *DeviceController) Device_Data() {
  192. // 验证登录 User_is, User_r
  193. _, User_is := lib.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  194. if !User_is {
  195. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  196. c.ServeJSON()
  197. return
  198. }
  199. type R_JSONS_s struct {
  200. //必须的大写开头
  201. List []Device.DeviceData_
  202. Num int
  203. Page int
  204. Page_size int
  205. Pages []lib.Page_T
  206. }
  207. var r_jsons R_JSONS_s
  208. page, _ := c.GetInt("page")
  209. if page < 1 {
  210. page = 1
  211. }
  212. page_z, _ := c.GetInt("page_z")
  213. if page_z < 1 {
  214. page_z = conf.Page_size
  215. }
  216. T_sn := c.GetString("T_sn")
  217. Time_start := c.GetString("Time_start")
  218. Time_and := c.GetString("Time_end")
  219. var cnt int
  220. r_jsons.List, cnt = Device.Read_DeviceSensorData_List(T_sn, Time_start, Time_and, page, page_z)
  221. page_size := math.Ceil(float64(cnt) / float64(page_z))
  222. r_jsons.Page = page
  223. r_jsons.Page_size = int(page_size)
  224. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  225. r_jsons.Num = int(cnt)
  226. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  227. c.ServeJSON()
  228. return
  229. }