DeviceClass.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. "fmt"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "math"
  12. "strings"
  13. )
  14. type DeviceClassController struct {
  15. beego.Controller
  16. }
  17. // 列表 -
  18. func (c *DeviceClassController) List() {
  19. // 验证登录 User_is, User_r
  20. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  21. if !User_is {
  22. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  23. c.ServeJSON()
  24. return
  25. }
  26. var r_jsons lib.R_JSONS
  27. page, _ := c.GetInt("page")
  28. if page < 1 {
  29. page = 1
  30. }
  31. page_z, _ := c.GetInt("page_z")
  32. if page_z < 1 {
  33. page_z = conf.Page_size
  34. }
  35. T_name := c.GetString("T_name")
  36. var cnt int64
  37. List, cnt := Device.Read_DeviceClass_List(User_r.T_uuid, T_name, page, page_z)
  38. page_size := math.Ceil(float64(cnt) / float64(page_z))
  39. r_jsons.List = List
  40. r_jsons.Page = page
  41. r_jsons.Page_size = int(page_size)
  42. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  43. r_jsons.Num = int(cnt)
  44. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  45. c.ServeJSON()
  46. return
  47. }
  48. // 获取-
  49. func (c *DeviceClassController) Get() {
  50. // 验证登录 User_is, User_r
  51. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  52. if !User_is {
  53. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  54. c.ServeJSON()
  55. return
  56. }
  57. Id, err := c.GetInt("Id")
  58. if err != nil {
  59. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  60. c.ServeJSON()
  61. return
  62. }
  63. r, is := Device.Read_DeviceClass_ById(Id)
  64. if !is {
  65. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  66. c.ServeJSON()
  67. return
  68. }
  69. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r}
  70. c.ServeJSON()
  71. return
  72. }
  73. // 添加-
  74. func (c *DeviceClassController) Add() {
  75. // 验证登录 User_is, User_r
  76. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  77. if !User_is {
  78. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  79. c.ServeJSON()
  80. return
  81. }
  82. T_name := c.GetString("T_name")
  83. var_ := Device.DeviceClass{
  84. T_uuid: User_r.T_uuid,
  85. T_name: T_name,
  86. T_State: 1,
  87. }
  88. Id, is := Device.Add_DeviceClass(var_)
  89. if !is {
  90. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  91. c.ServeJSON()
  92. return
  93. }
  94. System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "添加", var_)
  95. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  96. c.ServeJSON()
  97. return
  98. }
  99. // 修改-
  100. func (c *DeviceClassController) Up() {
  101. // 验证登录 User_is, User_r
  102. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  103. if !User_is {
  104. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  105. c.ServeJSON()
  106. return
  107. }
  108. T_name := c.GetString("T_name")
  109. Id, err := c.GetInt("Id")
  110. if err != nil {
  111. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  112. c.ServeJSON()
  113. return
  114. }
  115. r, is := Device.Read_DeviceClass_ById(Id)
  116. if !is {
  117. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  118. c.ServeJSON()
  119. return
  120. }
  121. // .......
  122. if len(T_name) > 0 {
  123. r.T_name = T_name
  124. }
  125. // .......
  126. if !Device.Update_DeviceClass(r, "T_name") {
  127. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  128. c.ServeJSON()
  129. return
  130. }
  131. System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "修改", r)
  132. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  133. c.ServeJSON()
  134. return
  135. }
  136. // 删除-
  137. func (c *DeviceClassController) Del() {
  138. // 验证登录 User_is, User_r
  139. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  140. if !User_is {
  141. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  142. c.ServeJSON()
  143. return
  144. }
  145. Id, err := c.GetInt("Id")
  146. if err != nil {
  147. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  148. c.ServeJSON()
  149. return
  150. }
  151. if r, is := Device.Read_DeviceClass_ById(Id); is {
  152. if !Device.Delete_DeviceClass_(r) {
  153. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  154. c.ServeJSON()
  155. return
  156. }
  157. System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "删除", r)
  158. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  159. c.ServeJSON()
  160. return
  161. }
  162. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  163. c.ServeJSON()
  164. return
  165. }
  166. //--------------------------
  167. // 列表 -
  168. func (c *DeviceClassController) List_List() {
  169. // 验证登录 User_is, User_r
  170. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  171. if !User_is {
  172. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  173. c.ServeJSON()
  174. return
  175. }
  176. var r_jsons lib.R_JSONS
  177. page, _ := c.GetInt("page")
  178. if page < 1 {
  179. page = 1
  180. }
  181. page_z, _ := c.GetInt("page_z")
  182. if page_z < 1 {
  183. page_z = conf.Page_size
  184. }
  185. T_class, _ := c.GetInt("T_class")
  186. T_sn := c.GetString("T_sn")
  187. _, is := Device.Read_DeviceClass_ById(T_class)
  188. if !is {
  189. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  190. c.ServeJSON()
  191. return
  192. }
  193. var cnt int64
  194. //List, cnt := Device.Read_DeviceClassList_List(T_class, T_sn, page, 9999)
  195. List, cnt := Device.Read_DeviceClassList_OrderList(T_class, T_sn, page, 9999)
  196. page_size := math.Ceil(float64(cnt) / float64(page_z))
  197. r_jsons.List = List
  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 *DeviceClassController) List_Add() {
  208. // 验证登录 User_is, User_r
  209. User_r, 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. T_class, _ := c.GetInt("T_class")
  216. //方式1:001,002,003,004,005,008
  217. //方式2:001~005,008
  218. T_layout_no_list := c.GetString("T_layout_no_list")
  219. //T_sn := c.GetString("T_sn")
  220. T_remark := c.GetString("T_remark")
  221. _, is := Device.Read_DeviceClass_ById(T_class)
  222. if !is {
  223. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  224. c.ServeJSON()
  225. return
  226. }
  227. list := strings.Split(strings.TrimRight(T_layout_no_list, ","), ",")
  228. snMap := make(map[string]string)
  229. excludeId := make(map[string]struct{})
  230. for _, v := range list {
  231. if strings.Contains(v, "!") {
  232. excludeId[fmt.Sprintf("%03d", lib.To_int(strings.TrimLeft(strings.Trim(v, "!"), "0")))] = struct{}{}
  233. continue
  234. }
  235. // 001~005
  236. if strings.Contains(v, "~") {
  237. vlist := strings.Split(v, "~")
  238. if len(vlist) != 2 {
  239. c.Data["json"] = lib.JSONS{Code: 202, Msg: "布局编号格式错误!"}
  240. c.ServeJSON()
  241. return
  242. }
  243. vstart, vend := lib.To_int(strings.TrimLeft(vlist[0], "0")), lib.To_int(strings.TrimLeft(vlist[1], "0"))
  244. for i := vstart; i < vend+1; i++ {
  245. T_id := fmt.Sprintf("%03d", i)
  246. cert, is := Certificate.Read_Certificate_ByT_layout_no(T_id)
  247. if !is {
  248. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s布局编号不存在!", T_id)}
  249. c.ServeJSON()
  250. return
  251. }
  252. snMap[T_id] = cert.T_sn
  253. }
  254. } else {
  255. // 001,002
  256. cert, is := Certificate.Read_Certificate_ByT_layout_no(v)
  257. if !is {
  258. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s布局编号不存在!", v)}
  259. c.ServeJSON()
  260. return
  261. }
  262. snMap[v] = cert.T_sn
  263. }
  264. }
  265. for T_id, T_sn := range snMap {
  266. if _, ok := excludeId[T_id]; ok {
  267. continue
  268. }
  269. if _, is = Device.Read_Device(T_sn); !is {
  270. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 不存在!"}
  271. c.ServeJSON()
  272. return
  273. }
  274. if dc, is := Device.Read_DeviceClassList_T_class_T_sn(T_class, T_sn); is && dc.Id > 0 {
  275. continue
  276. }
  277. var pdf Certificate.CertificatePdf
  278. pdfList, _ := Certificate.Read_CertificatePdf_Newest(T_sn)
  279. if len(pdfList) > 0 {
  280. pdf = pdfList[0]
  281. }
  282. var_ := Device.DeviceClassList{
  283. T_class: T_class,
  284. T_id: T_id,
  285. T_sn: T_sn,
  286. T_failure_time: pdf.T_failure_time,
  287. T_pdf: pdf.T_pdf,
  288. T_remark: T_remark,
  289. T_State: 1,
  290. }
  291. _, is = Device.Add_DeviceClassList(var_)
  292. if !is {
  293. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  294. c.ServeJSON()
  295. return
  296. }
  297. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "添加", var_)
  298. }
  299. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  300. c.ServeJSON()
  301. return
  302. }
  303. // 修改-
  304. func (c *DeviceClassController) List_Up() {
  305. // 验证登录 User_is, User_r
  306. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  307. if !User_is {
  308. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  309. c.ServeJSON()
  310. return
  311. }
  312. T_id := c.GetString("T_id")
  313. T_remark := c.GetString("T_remark")
  314. Id, err := c.GetInt("Id")
  315. if err != nil {
  316. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  317. c.ServeJSON()
  318. return
  319. }
  320. r, is := Device.Read_DeviceClassList_ById(Id)
  321. if !is {
  322. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  323. c.ServeJSON()
  324. return
  325. }
  326. _, is = Device.Read_DeviceClass_ById(r.T_class)
  327. if !is {
  328. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  329. c.ServeJSON()
  330. return
  331. }
  332. if len(T_id) > 0 {
  333. r.T_id = T_id
  334. }
  335. if len(T_remark) > 0 {
  336. r.T_remark = T_remark
  337. }
  338. if !Device.Update_DeviceClassList(r, "T_id", "T_remark") {
  339. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  340. c.ServeJSON()
  341. return
  342. }
  343. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "修改", r)
  344. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  345. c.ServeJSON()
  346. return
  347. }
  348. // 删除-
  349. func (c *DeviceClassController) List_Del() {
  350. // 验证登录 User_is, User_r
  351. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  352. if !User_is {
  353. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  354. c.ServeJSON()
  355. return
  356. }
  357. Id, err := c.GetInt("Id")
  358. if err != nil {
  359. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  360. c.ServeJSON()
  361. return
  362. }
  363. if r, is := Device.Read_DeviceClassList_ById(Id); is {
  364. _, is = Device.Read_DeviceClass_ById(r.T_class)
  365. if !is {
  366. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  367. c.ServeJSON()
  368. return
  369. }
  370. if !Device.Delete_DeviceClassList_(r) {
  371. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  372. c.ServeJSON()
  373. return
  374. }
  375. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "删除", r)
  376. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  377. c.ServeJSON()
  378. return
  379. }
  380. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  381. c.ServeJSON()
  382. return
  383. }