DeviceClass.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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_r, 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. if User_r.T_uuid != r.T_uuid {
  67. c.Data["json"] = lib.JSONS{Code: 203, Msg: "没有权限!"}
  68. c.ServeJSON()
  69. return
  70. }
  71. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r}
  72. c.ServeJSON()
  73. return
  74. }
  75. // 添加-
  76. func (c *DeviceClassController) Add() {
  77. // 验证登录 User_is, User_r
  78. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  79. if !User_is {
  80. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  81. c.ServeJSON()
  82. return
  83. }
  84. T_name := c.GetString("T_name")
  85. var_ := Device.DeviceClass{
  86. T_uuid: User_r.T_uuid,
  87. T_name: T_name,
  88. T_State: 1,
  89. }
  90. Id, is := Device.Add_DeviceClass(var_)
  91. if !is {
  92. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  93. c.ServeJSON()
  94. return
  95. }
  96. System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "添加", var_)
  97. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  98. c.ServeJSON()
  99. return
  100. }
  101. // 修改-
  102. func (c *DeviceClassController) Up() {
  103. // 验证登录 User_is, User_r
  104. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  105. if !User_is {
  106. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  107. c.ServeJSON()
  108. return
  109. }
  110. T_name := c.GetString("T_name")
  111. Id, err := c.GetInt("Id")
  112. if err != nil {
  113. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  114. c.ServeJSON()
  115. return
  116. }
  117. r, is := Device.Read_DeviceClass_ById(Id)
  118. if !is {
  119. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  120. c.ServeJSON()
  121. return
  122. }
  123. if User_r.T_uuid != r.T_uuid {
  124. c.Data["json"] = lib.JSONS{Code: 203, Msg: "没有权限!"}
  125. c.ServeJSON()
  126. return
  127. }
  128. // .......
  129. if len(T_name) > 0 {
  130. r.T_name = T_name
  131. }
  132. // .......
  133. if !Device.Update_DeviceClass(r, "T_name") {
  134. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  135. c.ServeJSON()
  136. return
  137. }
  138. System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "修改", r)
  139. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  140. c.ServeJSON()
  141. return
  142. }
  143. // 删除-
  144. func (c *DeviceClassController) Del() {
  145. // 验证登录 User_is, User_r
  146. User_r, User_is := Account.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. Id, err := c.GetInt("Id")
  153. if err != nil {
  154. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  155. c.ServeJSON()
  156. return
  157. }
  158. if r, is := Device.Read_DeviceClass_ById(Id); is {
  159. if User_r.T_uuid != r.T_uuid {
  160. c.Data["json"] = lib.JSONS{Code: 203, Msg: "没有权限!"}
  161. c.ServeJSON()
  162. return
  163. }
  164. if !Device.Delete_DeviceClass_(r) {
  165. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  166. c.ServeJSON()
  167. return
  168. }
  169. System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "删除", r)
  170. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  171. c.ServeJSON()
  172. return
  173. }
  174. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  175. c.ServeJSON()
  176. return
  177. }
  178. //--------------------------
  179. // 列表 -
  180. func (c *DeviceClassController) List_List() {
  181. // 验证登录 User_is, User_r
  182. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  183. if !User_is {
  184. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  185. c.ServeJSON()
  186. return
  187. }
  188. var r_jsons lib.R_JSONS
  189. page, _ := c.GetInt("page")
  190. if page < 1 {
  191. page = 1
  192. }
  193. page_z, _ := c.GetInt("page_z")
  194. if page_z < 1 {
  195. page_z = conf.Page_size
  196. }
  197. T_class, _ := c.GetInt("T_class")
  198. T_sn := c.GetString("T_sn")
  199. r, is := Device.Read_DeviceClass_ById(T_class)
  200. if !is {
  201. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  202. c.ServeJSON()
  203. return
  204. }
  205. if User_r.T_uuid != r.T_uuid {
  206. c.Data["json"] = lib.JSONS{Code: 203, Msg: "没有权限!"}
  207. c.ServeJSON()
  208. return
  209. }
  210. var cnt int64
  211. List, cnt := Device.Read_DeviceClassList_List(T_class, T_sn, page, page_z)
  212. page_size := math.Ceil(float64(cnt) / float64(page_z))
  213. r_jsons.List = List
  214. r_jsons.Page = page
  215. r_jsons.Page_size = int(page_size)
  216. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  217. r_jsons.Num = int(cnt)
  218. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  219. c.ServeJSON()
  220. return
  221. }
  222. // 添加-
  223. func (c *DeviceClassController) List_Add() {
  224. // 验证登录 User_is, User_r
  225. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  226. if !User_is {
  227. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  228. c.ServeJSON()
  229. return
  230. }
  231. T_class, _ := c.GetInt("T_class")
  232. T_id, _ := c.GetInt("T_id")
  233. T_sn := c.GetString("T_sn")
  234. r, is := Device.Read_DeviceClass_ById(T_class)
  235. if !is {
  236. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  237. c.ServeJSON()
  238. return
  239. }
  240. if User_r.T_uuid != r.T_uuid {
  241. c.Data["json"] = lib.JSONS{Code: 203, Msg: "没有权限!"}
  242. c.ServeJSON()
  243. return
  244. }
  245. if _, is = Device.Read_Device(T_sn); !is {
  246. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 不存在!"}
  247. c.ServeJSON()
  248. return
  249. }
  250. if dc, is := Device.Read_DeviceClassList_T_class_T_sn(T_class, T_sn); is && dc.Id > 0 {
  251. c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复添加"}
  252. c.ServeJSON()
  253. return
  254. }
  255. var_ := Device.DeviceClassList{
  256. T_class: T_class,
  257. T_id: T_id,
  258. T_sn: T_sn,
  259. T_State: 1,
  260. }
  261. Id, is := Device.Add_DeviceClassList(var_)
  262. if !is {
  263. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  264. c.ServeJSON()
  265. return
  266. }
  267. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "添加", var_)
  268. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  269. c.ServeJSON()
  270. return
  271. }
  272. // 修改-
  273. func (c *DeviceClassController) List_Up() {
  274. // 验证登录 User_is, User_r
  275. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  276. if !User_is {
  277. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  278. c.ServeJSON()
  279. return
  280. }
  281. T_id, _ := c.GetInt("T_id")
  282. Id, err := c.GetInt("Id")
  283. if err != nil {
  284. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  285. c.ServeJSON()
  286. return
  287. }
  288. r, is := Device.Read_DeviceClassList_ById(Id)
  289. if !is {
  290. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  291. c.ServeJSON()
  292. return
  293. }
  294. Cr, is := Device.Read_DeviceClass_ById(r.T_class)
  295. if !is {
  296. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  297. c.ServeJSON()
  298. return
  299. }
  300. if User_r.T_uuid != Cr.T_uuid {
  301. c.Data["json"] = lib.JSONS{Code: 203, Msg: "没有权限!"}
  302. c.ServeJSON()
  303. return
  304. }
  305. // .......
  306. r.T_id = T_id
  307. // .......
  308. if !Device.Update_DeviceClassList(r, "T_id") {
  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. // 删除-
  319. func (c *DeviceClassController) List_Del() {
  320. // 验证登录 User_is, User_r
  321. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  322. if !User_is {
  323. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  324. c.ServeJSON()
  325. return
  326. }
  327. Id, err := c.GetInt("Id")
  328. if err != nil {
  329. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  330. c.ServeJSON()
  331. return
  332. }
  333. if r, is := Device.Read_DeviceClassList_ById(Id); is {
  334. Cr, is := Device.Read_DeviceClass_ById(r.T_class)
  335. if !is {
  336. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  337. c.ServeJSON()
  338. return
  339. }
  340. if User_r.T_uuid != Cr.T_uuid {
  341. c.Data["json"] = lib.JSONS{Code: 203, Msg: "没有权限!"}
  342. c.ServeJSON()
  343. return
  344. }
  345. if !Device.Delete_DeviceClassList_(r) {
  346. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  347. c.ServeJSON()
  348. return
  349. }
  350. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "删除", r)
  351. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  352. c.ServeJSON()
  353. return
  354. }
  355. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  356. c.ServeJSON()
  357. return
  358. }