Certificate.go 11 KB

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