DeviceClass.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. errList := []string{}
  231. var successNum int // 成功数量
  232. for _, v := range list {
  233. if strings.Contains(v, "!") {
  234. excludeId[fmt.Sprintf("%03d", lib.To_int(strings.TrimLeft(strings.Trim(v, "!"), "0")))] = struct{}{}
  235. continue
  236. }
  237. // 001~005
  238. if strings.Contains(v, "~") {
  239. vlist := strings.Split(v, "~")
  240. if len(vlist) != 2 {
  241. c.Data["json"] = lib.JSONS{Code: 202, Msg: "布局编号格式错误!"}
  242. c.ServeJSON()
  243. return
  244. }
  245. vstart, vend := lib.To_int(strings.TrimLeft(vlist[0], "0")), lib.To_int(strings.TrimLeft(vlist[1], "0"))
  246. for i := vstart; i < vend+1; i++ {
  247. T_id := fmt.Sprintf("%03d", i)
  248. cert, is := Certificate.Read_Certificate_ByT_layout_no(T_id)
  249. if !is {
  250. //c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s布局编号不存在!", T_id)}
  251. //c.ServeJSON()
  252. //return
  253. errList = append(errList, fmt.Sprintf("%s布局编号不存在!", T_id))
  254. continue
  255. }
  256. snMap[T_id] = cert.T_sn
  257. }
  258. } else {
  259. // 001,002
  260. cert, is := Certificate.Read_Certificate_ByT_layout_no(v)
  261. if !is {
  262. //c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s布局编号不存在!", v)}
  263. //c.ServeJSON()
  264. //return
  265. errList = append(errList, v)
  266. continue
  267. }
  268. snMap[v] = cert.T_sn
  269. }
  270. }
  271. errList2 := []string{}
  272. succesId := []string{}
  273. for T_id, T_sn := range snMap {
  274. if _, ok := excludeId[T_id]; ok {
  275. continue
  276. }
  277. //if _, is = Device.Read_Device(T_sn); !is {
  278. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 不存在!"}
  279. // c.ServeJSON()
  280. // return
  281. //}
  282. if dc, is := Device.Read_DeviceClassList_T_class_T_sn(T_class, T_sn); is && dc.Id > 0 {
  283. successNum += 1
  284. continue
  285. }
  286. var pdf Certificate.CertificatePdf
  287. pdfList, _ := Certificate.Read_CertificatePdf_Newest(T_sn)
  288. if len(pdfList) > 0 {
  289. pdf = pdfList[0]
  290. }
  291. var_ := Device.DeviceClassList{
  292. T_class: T_class,
  293. T_id: T_id,
  294. T_sn: T_sn,
  295. T_failure_time: pdf.T_failure_time,
  296. T_pdf: pdf.T_pdf,
  297. T_remark: T_remark,
  298. T_State: 1,
  299. }
  300. _, is = Device.Add_DeviceClassList(var_)
  301. if !is {
  302. //c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  303. //c.ServeJSON()
  304. //return
  305. errList2 = append(errList2, T_id)
  306. continue
  307. }
  308. successNum += 1
  309. succesId = append(succesId, T_id)
  310. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "添加", var_)
  311. }
  312. if len(errList) == 0 && len(errList2) == 0 {
  313. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: successNum}
  314. c.ServeJSON()
  315. return
  316. }
  317. var errStr string
  318. if len(errList) > 0 {
  319. errStr += strings.Join(errList, ",") + "编号不存在"
  320. }
  321. if len(errList2) > 0 {
  322. if len(errStr) > 0 {
  323. errStr += ","
  324. }
  325. errStr += strings.Join(errList2, ",") + "添加失败"
  326. }
  327. //if len(succesId) > 0 {
  328. // if len(errStr) > 0 {
  329. // errStr += ","
  330. // }
  331. // errStr += strings.Join(succesId, ",") + "添加成功"
  332. //}
  333. c.Data["json"] = lib.JSONS{Code: 210, Msg: errStr, Data: successNum}
  334. c.ServeJSON()
  335. return
  336. }
  337. // 修改-
  338. func (c *DeviceClassController) List_Up() {
  339. // 验证登录 User_is, User_r
  340. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  341. if !User_is {
  342. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  343. c.ServeJSON()
  344. return
  345. }
  346. T_id := c.GetString("T_id")
  347. T_remark := c.GetString("T_remark")
  348. Id, err := c.GetInt("Id")
  349. if err != nil {
  350. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  351. c.ServeJSON()
  352. return
  353. }
  354. r, is := Device.Read_DeviceClassList_ById(Id)
  355. if !is {
  356. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  357. c.ServeJSON()
  358. return
  359. }
  360. _, is = Device.Read_DeviceClass_ById(r.T_class)
  361. if !is {
  362. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  363. c.ServeJSON()
  364. return
  365. }
  366. if len(T_id) > 0 {
  367. r.T_id = T_id
  368. }
  369. if len(T_remark) > 0 {
  370. r.T_remark = T_remark
  371. }
  372. if !Device.Update_DeviceClassList(r, "T_id", "T_remark") {
  373. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  374. c.ServeJSON()
  375. return
  376. }
  377. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "修改", r)
  378. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  379. c.ServeJSON()
  380. return
  381. }
  382. // 删除-
  383. func (c *DeviceClassController) List_Del() {
  384. // 验证登录 User_is, User_r
  385. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  386. if !User_is {
  387. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  388. c.ServeJSON()
  389. return
  390. }
  391. Id, err := c.GetInt("Id")
  392. if err != nil {
  393. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  394. c.ServeJSON()
  395. return
  396. }
  397. if r, is := Device.Read_DeviceClassList_ById(Id); is {
  398. _, is = Device.Read_DeviceClass_ById(r.T_class)
  399. if !is {
  400. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  401. c.ServeJSON()
  402. return
  403. }
  404. if !Device.Delete_DeviceClassList_(r) {
  405. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  406. c.ServeJSON()
  407. return
  408. }
  409. System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "删除", r)
  410. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  411. c.ServeJSON()
  412. return
  413. }
  414. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  415. c.ServeJSON()
  416. return
  417. }