Certificate.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. "ColdVerify_server/models/Task"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "math"
  12. )
  13. type CertificateController struct {
  14. beego.Controller
  15. }
  16. // 列表-
  17. func (c *CertificateController) List() {
  18. // 验证登录 User_is, User_r
  19. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  20. if !User_is {
  21. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  22. c.ServeJSON()
  23. return
  24. }
  25. var r_jsons lib.R_JSONS
  26. page, _ := c.GetInt("page")
  27. if page < 1 {
  28. page = 1
  29. }
  30. page_z, _ := c.GetInt("page_z")
  31. if page_z < 1 {
  32. page_z = conf.Page_size
  33. }
  34. //T_sn := c.GetString("T_sn")
  35. T_Certificate_sn := c.GetString("T_Certificate_sn")
  36. T_layout_no := c.GetString("T_layout_no")
  37. Time_start := c.GetString("Time_start")
  38. Time_end := c.GetString("Time_end")
  39. T_release_time_sort, _ := c.GetInt("T_release_time_sort") // 1 升序 2 降序
  40. T_failure_time_sort, _ := c.GetInt("T_failure_time_sort") // 1 升序 2 降序
  41. T_layout_no_sort, _ := c.GetInt("T_layout_no_sort") // 1 升序 2 降序
  42. if len(Time_start) > 0 && !lib.IsDateStr(Time_start) {
  43. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  44. c.ServeJSON()
  45. return
  46. }
  47. if len(Time_end) > 0 && !lib.IsDateStr(Time_end) {
  48. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  49. c.ServeJSON()
  50. return
  51. }
  52. var cnt int
  53. List, cnt := Certificate.Read_Certificate_List(T_Certificate_sn, T_layout_no, Time_start, Time_end, T_release_time_sort, T_failure_time_sort, T_layout_no_sort, page, page_z)
  54. page_size := math.Ceil(float64(cnt) / float64(page_z))
  55. r_jsons.List = List
  56. r_jsons.Page = page
  57. r_jsons.Page_size = int(page_size)
  58. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  59. r_jsons.Num = cnt
  60. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  61. c.ServeJSON()
  62. return
  63. }
  64. func (c *CertificateController) ListWithoutDeviceList() {
  65. // 验证登录 User_is, User_r
  66. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  67. if !User_is {
  68. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  69. c.ServeJSON()
  70. return
  71. }
  72. T_task_id := c.GetString("T_task_id")
  73. task, is := Task.Read_Task(T_task_id)
  74. if !is {
  75. c.Data["json"] = lib.JSONS{Code: 202, Msg: "任务id错误!"}
  76. c.ServeJSON()
  77. return
  78. }
  79. var r_jsons lib.R_JSONS
  80. var list []Certificate.Certificate_
  81. var cnt int
  82. certificateList, _ := Certificate.Read_Certificate_List("", "", "", "", 0, 0, 0, 0, 9999)
  83. DeviceList, _ := Device.Read_DeviceClassList_OrderList(task.T_class, "", "", "", 0, 9999)
  84. var deviceMap = make(map[string]struct{})
  85. for _, v := range DeviceList {
  86. deviceMap[v.T_id] = struct{}{}
  87. }
  88. for _, certificate := range certificateList {
  89. if _, ok := deviceMap[certificate.T_layout_no]; ok {
  90. continue
  91. }
  92. list = append(list, certificate)
  93. cnt += 1
  94. }
  95. r_jsons.List = list
  96. r_jsons.Num = cnt
  97. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  98. c.ServeJSON()
  99. return
  100. }
  101. // 删除-
  102. func (c *CertificateController) Get() {
  103. // 验证登录 User_is, User_r
  104. _, 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_layout_no := c.GetString("T_layout_no")
  111. if _, is := Certificate.Read_Certificate(T_layout_no); !is {
  112. c.Data["json"] = lib.JSONS{Code: 202, Msg: "布局编号不存在!"}
  113. c.ServeJSON()
  114. return
  115. }
  116. r, err := Certificate.Read_CertificatePdf_Newest(T_layout_no)
  117. if err != nil {
  118. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  119. c.ServeJSON()
  120. return
  121. }
  122. if len(r) == 0 {
  123. c.Data["json"] = lib.JSONS{Code: 202, Msg: "证书不存在!"}
  124. c.ServeJSON()
  125. return
  126. }
  127. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Certificate.CertificatePdfToCertificatePdf_R(r[0])}
  128. c.ServeJSON()
  129. return
  130. }
  131. // 添加-
  132. func (c *CertificateController) Add() {
  133. // 验证登录 User_is, User_r
  134. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  135. if !User_is {
  136. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  137. c.ServeJSON()
  138. return
  139. }
  140. T_layout_no := c.GetString("T_layout_no")
  141. var_ := Certificate.Certificate{
  142. T_layout_no: T_layout_no,
  143. T_State: 1,
  144. }
  145. if _, is := Certificate.Read_Certificate(T_layout_no); is {
  146. c.Data["json"] = lib.JSONS{Code: 202, Msg: "设备编号重复!"}
  147. c.ServeJSON()
  148. return
  149. }
  150. Id, is := Certificate.Add_Certificate(var_)
  151. if !is {
  152. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  153. c.ServeJSON()
  154. return
  155. }
  156. System.Add_UserLogs_T(user_r.T_uuid, "校准证书管理", "添加", var_)
  157. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  158. c.ServeJSON()
  159. return
  160. }
  161. // 添加-
  162. func (c *CertificateController) Edit() {
  163. // 验证登录 User_is, User_r
  164. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  165. if !User_is {
  166. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  167. c.ServeJSON()
  168. return
  169. }
  170. Id, err := c.GetInt("Id")
  171. if err != nil {
  172. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  173. c.ServeJSON()
  174. return
  175. }
  176. r, is := Certificate.Read_Certificate_ById(Id)
  177. if !is {
  178. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  179. c.ServeJSON()
  180. return
  181. }
  182. old_T_layout_no := r.T_layout_no
  183. T_layout_no := c.GetString("T_layout_no")
  184. if _, is = Certificate.Read_Certificate(T_layout_no); is && r.T_layout_no != T_layout_no {
  185. c.Data["json"] = lib.JSONS{Code: 202, Msg: "设备编号重复!"}
  186. c.ServeJSON()
  187. return
  188. }
  189. r.T_layout_no = T_layout_no
  190. is = Certificate.Update_Certificate(r, "T_layout_no")
  191. if !is {
  192. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  193. c.ServeJSON()
  194. return
  195. }
  196. pdfList, _ := Certificate.Read_CertificatePdf_List(old_T_layout_no, 0, 9999)
  197. for _, pdf := range pdfList {
  198. // 修改pdf
  199. var_ := Certificate.CertificatePdf{
  200. Id: pdf.Id,
  201. T_layout_no: T_layout_no,
  202. }
  203. if !Certificate.Update_CertificatePdf(var_, "T_layout_no") {
  204. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  205. c.ServeJSON()
  206. return
  207. }
  208. }
  209. System.Add_UserLogs_T(user_r.T_uuid, "校准证书管理", "编辑", r)
  210. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  211. c.ServeJSON()
  212. return
  213. }
  214. // 删除-
  215. func (c *CertificateController) Del() {
  216. // 验证登录 User_is, User_r
  217. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  218. if !User_is {
  219. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  220. c.ServeJSON()
  221. return
  222. }
  223. Id, err := c.GetInt("Id")
  224. if err != nil {
  225. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  226. c.ServeJSON()
  227. return
  228. }
  229. if r, is := Certificate.Read_Certificate_ById(Id); is {
  230. if !Certificate.Delete_Certificate_(r) {
  231. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  232. c.ServeJSON()
  233. return
  234. }
  235. System.Add_UserLogs_T(user_r.T_uuid, "校准证书管理", "删除", r)
  236. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  237. c.ServeJSON()
  238. return
  239. }
  240. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  241. c.ServeJSON()
  242. return
  243. }
  244. // 列表-
  245. func (c *CertificateController) Pdf_List() {
  246. // 验证登录 User_is, User_r
  247. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  248. if !User_is {
  249. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  250. c.ServeJSON()
  251. return
  252. }
  253. var r_jsons lib.R_JSONS
  254. page, _ := c.GetInt("page")
  255. if page < 1 {
  256. page = 1
  257. }
  258. page_z, _ := c.GetInt("page_z")
  259. if page_z < 1 {
  260. page_z = conf.Page_size
  261. }
  262. //T_Certificate_sn := c.GetString("T_Certificate_sn")
  263. T_layout_no := c.GetString("T_layout_no")
  264. var cnt int64
  265. List, cnt := Certificate.Read_CertificatePdf_List(T_layout_no, page, page_z)
  266. page_size := math.Ceil(float64(cnt) / float64(page_z))
  267. r_jsons.List = List
  268. r_jsons.Page = page
  269. r_jsons.Page_size = int(page_size)
  270. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  271. r_jsons.Num = int(cnt)
  272. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  273. c.ServeJSON()
  274. return
  275. }
  276. // 添加-
  277. func (c *CertificateController) Pdf_Add() {
  278. // 验证登录 User_is, User_r
  279. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  280. if !User_is {
  281. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  282. c.ServeJSON()
  283. return
  284. }
  285. T_layout_no := c.GetString("T_layout_no") // 布局编号
  286. T_Certificate_sn := c.GetString("T_Certificate_sn") // 证书编号
  287. T_release_time := c.GetString("T_release_time")
  288. T_failure_time := c.GetString("T_failure_time")
  289. T_pdf := c.GetString("T_pdf")
  290. if len(T_layout_no) == 0 {
  291. c.Data["json"] = lib.JSONS{Code: 202, Msg: "布局编号不能为空"}
  292. c.ServeJSON()
  293. return
  294. }
  295. if !lib.IsDateStr(T_release_time) {
  296. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  297. c.ServeJSON()
  298. return
  299. }
  300. if !lib.IsDateStr(T_failure_time) {
  301. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  302. c.ServeJSON()
  303. return
  304. }
  305. var_ := Certificate.CertificatePdf{
  306. T_layout_no: T_layout_no,
  307. T_Certificate_sn: T_Certificate_sn,
  308. T_release_time: T_release_time,
  309. T_failure_time: T_failure_time,
  310. T_pdf: T_pdf,
  311. T_State: 1,
  312. }
  313. if _, err := Certificate.Read_CertificatePdf(T_Certificate_sn, T_release_time, T_failure_time); err == nil {
  314. c.Data["json"] = lib.JSONS{Code: 202, Msg: "证书编号重复!"}
  315. c.ServeJSON()
  316. return
  317. }
  318. Id, is := Certificate.Add_CertificatePdf(var_)
  319. if !is {
  320. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  321. c.ServeJSON()
  322. return
  323. }
  324. System.Add_UserLogs_T(user_r.T_uuid, "校准证书Pdf", "添加", var_)
  325. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  326. c.ServeJSON()
  327. return
  328. }
  329. // 修改-
  330. func (c *CertificateController) Pdf_Up() {
  331. // 验证登录 User_is, User_r
  332. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  333. if !User_is {
  334. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  335. c.ServeJSON()
  336. return
  337. }
  338. Id, err := c.GetInt("Id")
  339. if err != nil {
  340. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  341. c.ServeJSON()
  342. return
  343. }
  344. T_Certificate_sn := c.GetString("T_Certificate_sn")
  345. T_release_time := c.GetString("T_release_time")
  346. T_failure_time := c.GetString("T_failure_time")
  347. T_pdf := c.GetString("T_pdf")
  348. if !lib.IsDateStr(T_release_time) {
  349. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  350. c.ServeJSON()
  351. return
  352. }
  353. if !lib.IsDateStr(T_failure_time) {
  354. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  355. c.ServeJSON()
  356. return
  357. }
  358. r, is := Certificate.Read_CertificatePdf_ById(Id)
  359. if !is {
  360. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  361. c.ServeJSON()
  362. return
  363. }
  364. _, err = Certificate.Read_CertificatePdf(T_Certificate_sn, T_release_time, T_failure_time)
  365. if err == nil && r.T_failure_time != T_failure_time && r.T_release_time != T_release_time {
  366. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据已存在!"}
  367. c.ServeJSON()
  368. return
  369. }
  370. if len(T_Certificate_sn) > 0 {
  371. r.T_Certificate_sn = T_Certificate_sn
  372. }
  373. if len(T_release_time) > 0 {
  374. r.T_release_time = T_release_time
  375. }
  376. if len(T_failure_time) > 0 {
  377. r.T_failure_time = T_failure_time
  378. }
  379. if len(T_pdf) > 0 {
  380. r.T_pdf = T_pdf
  381. }
  382. if !Certificate.Update_CertificatePdf(r, "T_Certificate_sn", "T_release_time", "T_failure_time", "T_pdf") {
  383. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  384. c.ServeJSON()
  385. return
  386. }
  387. System.Add_UserLogs_T(user_r.T_uuid, "校准证书Pdf", "修改", r)
  388. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  389. c.ServeJSON()
  390. return
  391. }
  392. // 删除-
  393. func (c *CertificateController) Pdf_Del() {
  394. // 验证登录 User_is, User_r
  395. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  396. if !User_is {
  397. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  398. c.ServeJSON()
  399. return
  400. }
  401. Id, err := c.GetInt("Id")
  402. if err != nil {
  403. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  404. c.ServeJSON()
  405. return
  406. }
  407. if r, is := Certificate.Read_CertificatePdf_ById(Id); is {
  408. if !Certificate.Delete_CertificatePdf_(r) {
  409. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  410. c.ServeJSON()
  411. return
  412. }
  413. System.Add_UserLogs_T(user_r.T_uuid, "校准证书Pdf", "删除", r)
  414. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  415. c.ServeJSON()
  416. return
  417. }
  418. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  419. c.ServeJSON()
  420. return
  421. }