DeviceClass.go 9.1 KB

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