DeviceClass.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 DeviceClassController struct {
  12. beego.Controller
  13. }
  14. // 列表 -
  15. func (c *DeviceClassController) List() {
  16. // 验证登录 User_is, User_r
  17. User_r, 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_name := c.GetString("T_name")
  33. var cnt int64
  34. List, cnt := Device.Read_DeviceClass_List(User_r.T_uuid, T_name, 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 *DeviceClassController) Get() {
  47. // 验证登录 User_is, User_r
  48. _, User_is := Account.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. Id, err := c.GetInt("Id")
  55. if err != nil {
  56. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  57. c.ServeJSON()
  58. return
  59. }
  60. r, is := Device.Read_DeviceClass_ById(Id)
  61. if !is {
  62. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  63. c.ServeJSON()
  64. return
  65. }
  66. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r}
  67. c.ServeJSON()
  68. return
  69. }
  70. // 添加-
  71. func (c *DeviceClassController) Add() {
  72. // 验证登录 User_is, User_r
  73. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  74. if !User_is {
  75. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  76. c.ServeJSON()
  77. return
  78. }
  79. T_name := c.GetString("T_name")
  80. var_ := Device.DeviceClass{
  81. T_uuid: User_r.T_uuid,
  82. T_name: T_name,
  83. T_State: 1,
  84. }
  85. Id, is := Device.Add_DeviceClass(var_)
  86. if !is {
  87. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  88. c.ServeJSON()
  89. return
  90. }
  91. System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "添加", var_)
  92. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  93. c.ServeJSON()
  94. return
  95. }
  96. // 修改-
  97. func (c *DeviceClassController) Up() {
  98. // 验证登录 User_is, User_r
  99. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  100. if !User_is {
  101. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  102. c.ServeJSON()
  103. return
  104. }
  105. T_name := c.GetString("T_name")
  106. Id, err := c.GetInt("Id")
  107. if err != nil {
  108. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  109. c.ServeJSON()
  110. return
  111. }
  112. r, is := Device.Read_DeviceClass_ById(Id)
  113. if !is {
  114. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  115. c.ServeJSON()
  116. return
  117. }
  118. // .......
  119. if len(T_name) > 0 {
  120. r.T_name = T_name
  121. }
  122. // .......
  123. if !Device.Update_DeviceClass(r, "T_name") {
  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 *DeviceClassController) 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_DeviceClass_ById(Id); is {
  149. if !Device.Delete_DeviceClass_(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. // 列表 -
  165. func (c *DeviceClassController) List_List() {
  166. // 验证登录 User_is, User_r
  167. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  168. if !User_is {
  169. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  170. c.ServeJSON()
  171. return
  172. }
  173. var r_jsons lib.R_JSONS
  174. page, _ := c.GetInt("page")
  175. if page < 1 {
  176. page = 1
  177. }
  178. page_z, _ := c.GetInt("page_z")
  179. if page_z < 1 {
  180. page_z = conf.Page_size
  181. }
  182. T_class, _ := c.GetInt("T_class")
  183. T_sn := c.GetString("T_sn")
  184. _, is := Device.Read_DeviceClass_ById(T_class)
  185. if !is {
  186. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  187. c.ServeJSON()
  188. return
  189. }
  190. var cnt int64
  191. List, cnt := Device.Read_DeviceClassList_List(T_class, T_sn, page, page_z)
  192. page_size := math.Ceil(float64(cnt) / float64(page_z))
  193. r_jsons.List = List
  194. r_jsons.Page = page
  195. r_jsons.Page_size = int(page_size)
  196. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  197. r_jsons.Num = int(cnt)
  198. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  199. c.ServeJSON()
  200. return
  201. }
  202. // 添加-
  203. func (c *DeviceClassController) List_Add() {
  204. // 验证登录 User_is, User_r
  205. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  206. if !User_is {
  207. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  208. c.ServeJSON()
  209. return
  210. }
  211. T_class, _ := c.GetInt("T_class")
  212. T_id := c.GetString("T_id")
  213. T_sn := c.GetString("T_sn")
  214. _, is := Device.Read_DeviceClass_ById(T_class)
  215. if !is {
  216. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  217. c.ServeJSON()
  218. return
  219. }
  220. if _, is = Device.Read_Device(T_sn); !is {
  221. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 不存在!"}
  222. c.ServeJSON()
  223. return
  224. }
  225. if dc, is := Device.Read_DeviceClassList_T_class_T_sn(T_class, T_sn); is && dc.Id > 0 {
  226. c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复添加"}
  227. c.ServeJSON()
  228. return
  229. }
  230. var_ := Device.DeviceClassList{
  231. T_class: T_class,
  232. T_id: T_id,
  233. T_sn: T_sn,
  234. T_State: 1,
  235. }
  236. Id, is := Device.Add_DeviceClassList(var_)
  237. if !is {
  238. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  239. c.ServeJSON()
  240. return
  241. }
  242. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "添加", var_)
  243. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  244. c.ServeJSON()
  245. return
  246. }
  247. // 修改-
  248. func (c *DeviceClassController) List_Up() {
  249. // 验证登录 User_is, User_r
  250. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  251. if !User_is {
  252. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  253. c.ServeJSON()
  254. return
  255. }
  256. T_id := c.GetString("T_id")
  257. Id, err := c.GetInt("Id")
  258. if err != nil {
  259. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  260. c.ServeJSON()
  261. return
  262. }
  263. r, is := Device.Read_DeviceClassList_ById(Id)
  264. if !is {
  265. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  266. c.ServeJSON()
  267. return
  268. }
  269. _, is = Device.Read_DeviceClass_ById(r.T_class)
  270. if !is {
  271. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  272. c.ServeJSON()
  273. return
  274. }
  275. r.T_id = T_id
  276. if !Device.Update_DeviceClassList(r, "T_id") {
  277. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  278. c.ServeJSON()
  279. return
  280. }
  281. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "修改", r)
  282. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  283. c.ServeJSON()
  284. return
  285. }
  286. // 删除-
  287. func (c *DeviceClassController) List_Del() {
  288. // 验证登录 User_is, User_r
  289. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  290. if !User_is {
  291. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  292. c.ServeJSON()
  293. return
  294. }
  295. Id, err := c.GetInt("Id")
  296. if err != nil {
  297. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  298. c.ServeJSON()
  299. return
  300. }
  301. if r, is := Device.Read_DeviceClassList_ById(Id); is {
  302. Cr, is := Device.Read_DeviceClass_ById(r.T_class)
  303. if !is {
  304. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  305. c.ServeJSON()
  306. return
  307. }
  308. if !Device.Delete_DeviceClassList_(r) {
  309. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  310. c.ServeJSON()
  311. return
  312. }
  313. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "删除", r)
  314. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  315. c.ServeJSON()
  316. return
  317. }
  318. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  319. c.ServeJSON()
  320. return
  321. }