DeviceClass.go 9.2 KB

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