Device.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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) Get() {
  80. // 验证登录 User_is, User_r
  81. _, 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. r, is := Device.Read_Device(T_sn)
  89. if !is {
  90. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  91. c.ServeJSON()
  92. return
  93. }
  94. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Device.DeviceToDevice_R(r, "0")}
  95. c.ServeJSON()
  96. return
  97. }
  98. func (c *DeviceController) Up() {
  99. // 验证登录 User_is, User_r
  100. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  101. if !User_is {
  102. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  103. c.ServeJSON()
  104. return
  105. }
  106. T_sn := c.GetString("T_sn")
  107. T_MSISDN := c.GetString("T_MSISDN")
  108. Id, err := c.GetInt("Id")
  109. if err != nil {
  110. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  111. c.ServeJSON()
  112. return
  113. }
  114. r, is := Device.Read_Device_ById(Id)
  115. if !is {
  116. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  117. c.ServeJSON()
  118. return
  119. }
  120. r.T_sn = T_sn
  121. r.T_MSISDN = T_MSISDN
  122. // .......
  123. if !Device.Update_Device(r, "T_sn", "T_MSISDN") {
  124. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  125. c.ServeJSON()
  126. return
  127. }
  128. System.Add_UserLogs_T(user_r.T_uuid, "设备管理", "修改", r)
  129. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  130. c.ServeJSON()
  131. return
  132. }
  133. // 删除-
  134. func (c *DeviceController) Del() {
  135. // 验证登录 User_is, User_r
  136. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  137. if !User_is {
  138. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  139. c.ServeJSON()
  140. return
  141. }
  142. Id, err := c.GetInt("Id")
  143. if err != nil {
  144. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  145. c.ServeJSON()
  146. return
  147. }
  148. if r, is := Device.Read_Device_ById(Id); is {
  149. if !Device.Delete_Device(r) {
  150. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  151. c.ServeJSON()
  152. return
  153. }
  154. System.Add_UserLogs_T(user_r.T_uuid, "设备管理", "删除", r)
  155. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  156. c.ServeJSON()
  157. return
  158. }
  159. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  160. c.ServeJSON()
  161. return
  162. }
  163. // 列表 - 分类设备
  164. func (c *DeviceController) Device_Class() {
  165. // 验证登录 User_is, User_r
  166. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  167. if !User_is {
  168. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  169. c.ServeJSON()
  170. return
  171. }
  172. var r_jsons lib.R_JSONS_s
  173. page, _ := c.GetInt("page")
  174. if page < 1 {
  175. page = 1
  176. }
  177. page_z, _ := c.GetInt("page_z")
  178. if page_z < 1 {
  179. page_z = conf.Page_size
  180. }
  181. T_class, _ := c.GetInt("T_class")
  182. T_sn := c.GetString("T_sn")
  183. r, is := Device.Read_DeviceClass_ById(T_class)
  184. if !is {
  185. c.Data["json"] = lib.JSONS{Code: 1202, Msg: "T_class 错误!"}
  186. c.ServeJSON()
  187. return
  188. }
  189. var cnt int64
  190. //DeviceList, cnt := Device.Read_DeviceClassList_List(T_class, T_sn, page, 9999)
  191. DeviceList, cnt := Device.Read_DeviceClassList_OrderList(T_class, T_sn, "", "", page, 9999)
  192. for _, v := range DeviceList {
  193. Device_r, _ := Device.Read_Device(v.T_sn)
  194. Device_r.T_note_file_num = Device.Read_DeviceSensorData_List_z(v.T_sn, r.CreateTime.Format("2006-01-02 15:04:05"))
  195. r_jsons.List = append(r_jsons.List, Device.DeviceToDevice_R(Device_r, v.T_id))
  196. }
  197. page_size := math.Ceil(float64(cnt) / float64(page_z))
  198. r_jsons.Page = page
  199. r_jsons.Page_size = int(page_size)
  200. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  201. r_jsons.Num = int(cnt)
  202. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  203. c.ServeJSON()
  204. return
  205. }
  206. // 列表 - 分类设备
  207. func (c *DeviceController) Device_Data() {
  208. // 验证登录 User_is, User_r
  209. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  210. if !User_is {
  211. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  212. c.ServeJSON()
  213. return
  214. }
  215. type R_JSONS_s struct {
  216. //必须的大写开头
  217. List []Device.DeviceData_
  218. Num int
  219. Page int
  220. Page_size int
  221. Pages []lib.Page_T
  222. }
  223. var r_jsons R_JSONS_s
  224. page, _ := c.GetInt("page")
  225. if page < 1 {
  226. page = 1
  227. }
  228. page_z, _ := c.GetInt("page_z")
  229. if page_z < 1 {
  230. page_z = conf.Page_size
  231. }
  232. T_sn := c.GetString("T_sn")
  233. Time_start := c.GetString("Time_start")
  234. Time_and := c.GetString("Time_end")
  235. var cnt int
  236. r_jsons.List, cnt = Device.Read_DeviceSensorData_List(T_sn, Time_start, Time_and, page, page_z)
  237. page_size := math.Ceil(float64(cnt) / float64(page_z))
  238. r_jsons.Page = page
  239. r_jsons.Page_size = int(page_size)
  240. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  241. r_jsons.Num = cnt
  242. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  243. c.ServeJSON()
  244. return
  245. }