Device.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package controllers
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/models/Account"
  6. "ColdVerify_server/models/Device"
  7. "ColdVerify_server/models/System"
  8. beego "github.com/beego/beego/v2/server/web"
  9. "math"
  10. )
  11. type DeviceController struct {
  12. beego.Controller
  13. }
  14. // 列表 -
  15. func (c *DeviceController) List() {
  16. // 验证登录 User_is, User_r
  17. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  18. if !User_is {
  19. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  20. c.ServeJSON()
  21. return
  22. }
  23. var r_jsons lib.R_JSONS
  24. page, _ := c.GetInt("page")
  25. if page < 1 {
  26. page = 1
  27. }
  28. page_z, _ := c.GetInt("page_z")
  29. if page_z < 1 {
  30. page_z = conf.Page_size
  31. }
  32. T_sn := c.GetString("T_sn")
  33. T_MSISDN := c.GetString("T_MSISDN")
  34. var cnt int64
  35. List, cnt := Device.Read_Device_List(T_sn, T_MSISDN, page, page_z)
  36. page_size := math.Ceil(float64(cnt) / float64(page_z))
  37. r_jsons.List = List
  38. r_jsons.Page = page
  39. r_jsons.Page_size = int(page_size)
  40. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  41. r_jsons.Num = int(cnt)
  42. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  43. c.ServeJSON()
  44. return
  45. }
  46. // 添加-
  47. func (c *DeviceController) Add() {
  48. // 验证登录 User_is, User_r
  49. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  50. if !User_is {
  51. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  52. c.ServeJSON()
  53. return
  54. }
  55. T_sn := c.GetString("T_sn")
  56. T_MSISDN := c.GetString("T_MSISDN")
  57. var_ := Device.Device{
  58. T_sn: T_sn,
  59. T_MSISDN: T_MSISDN,
  60. T_State: 1,
  61. }
  62. if _, is := Device.Read_Device(T_sn); is {
  63. c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复添加!"}
  64. c.ServeJSON()
  65. return
  66. }
  67. Id, is := Device.Add_Device(var_)
  68. if !is {
  69. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  70. c.ServeJSON()
  71. return
  72. }
  73. System.Add_UserLogs_T(user_r.T_uuid, "设备管理", "添加", var_)
  74. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  75. c.ServeJSON()
  76. return
  77. }
  78. // 修改-
  79. func (c *DeviceController) Up() {
  80. // 验证登录 User_is, User_r
  81. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  82. if !User_is {
  83. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  84. c.ServeJSON()
  85. return
  86. }
  87. T_sn := c.GetString("T_sn")
  88. T_MSISDN := c.GetString("T_MSISDN")
  89. Id, err := c.GetInt("Id")
  90. if err != nil {
  91. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  92. c.ServeJSON()
  93. return
  94. }
  95. r, is := Device.Read_Device_ById(Id)
  96. if !is {
  97. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  98. c.ServeJSON()
  99. return
  100. }
  101. r.T_sn = T_sn
  102. r.T_MSISDN = T_MSISDN
  103. // .......
  104. if !Device.Update_Device(r, "T_sn", "T_MSISDN") {
  105. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  106. c.ServeJSON()
  107. return
  108. }
  109. System.Add_UserLogs_T(user_r.T_uuid, "设备管理", "修改", r)
  110. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  111. c.ServeJSON()
  112. return
  113. }
  114. // 删除-
  115. func (c *DeviceController) Del() {
  116. // 验证登录 User_is, User_r
  117. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  118. if !User_is {
  119. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  120. c.ServeJSON()
  121. return
  122. }
  123. Id, err := c.GetInt("Id")
  124. if err != nil {
  125. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  126. c.ServeJSON()
  127. return
  128. }
  129. if r, is := Device.Read_Device_ById(Id); is {
  130. if !Device.Delete_Device(r) {
  131. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  132. c.ServeJSON()
  133. return
  134. }
  135. System.Add_UserLogs_T(user_r.T_uuid, "设备管理", "删除", r)
  136. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  137. c.ServeJSON()
  138. return
  139. }
  140. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  141. c.ServeJSON()
  142. return
  143. }
  144. // 列表 - 分类设备
  145. func (c *DeviceController) Device_Class() {
  146. // 验证登录 User_is, User_r
  147. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  148. if !User_is {
  149. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  150. c.ServeJSON()
  151. return
  152. }
  153. var r_jsons lib.R_JSONS_s
  154. page, _ := c.GetInt("page")
  155. if page < 1 {
  156. page = 1
  157. }
  158. page_z, _ := c.GetInt("page_z")
  159. if page_z < 1 {
  160. page_z = conf.Page_size
  161. }
  162. T_class, _ := c.GetInt("T_class")
  163. T_sn := c.GetString("T_sn")
  164. r, is := Device.Read_DeviceClass_ById(T_class)
  165. if !is {
  166. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  167. c.ServeJSON()
  168. return
  169. }
  170. var cnt int64
  171. //DeviceList, cnt := Device.Read_DeviceClassList_List(T_class, T_sn, page, 9999)
  172. DeviceList, cnt := Device.Read_DeviceClassList_OrderList(T_class, T_sn, "", "", page, 9999)
  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.DeviceToDevice_R(Device_r, v.T_id))
  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 := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  191. if !User_is {
  192. c.Data["json"] = lib.JSONS{Code: 201, 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 = cnt
  223. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  224. c.ServeJSON()
  225. return
  226. }