Product.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Account"
  7. "Cold_Api/models/Device"
  8. "Cold_Api/models/Product"
  9. "Cold_Api/models/System"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "math"
  12. "strconv"
  13. )
  14. type ProductController struct {
  15. beego.Controller
  16. Admin_r Account.Admin // 登陆的用户
  17. }
  18. func (c *ProductController) Prepare() {
  19. GetCookie := c.Ctx.GetCookie("User_tokey")
  20. GetString := c.GetString("User_tokey")
  21. User_tokey := GetCookie
  22. if len(User_tokey) == 0 {
  23. User_tokey = GetString
  24. }
  25. c.Admin_r = *Account.Admin_r
  26. }
  27. func (c *ProductController) ProductUpgrade_List() {
  28. type R_JSONS struct {
  29. //必须的大写开头
  30. Data []Product.ProductUpgrade_R
  31. Num int64
  32. Page int
  33. Page_size int
  34. }
  35. var r_jsons R_JSONS
  36. page, _ := c.GetInt("page")
  37. if page < 1 {
  38. page = 1
  39. }
  40. page_z, _ := c.GetInt("page_z")
  41. if page_z < 1 {
  42. page_z = conf.Page_size
  43. }
  44. T_model := c.GetString("T_model")
  45. r_jsons.Data, r_jsons.Num = Product.Read_ProductUpgrade_List(T_model, page, page_z)
  46. r_jsons.Page = page
  47. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  48. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  49. c.ServeJSON()
  50. return
  51. }
  52. func (c *ProductController) ProductUpgrade_T_model_List() {
  53. var list []string
  54. for _, v := range Product.Read_ProductType_All() {
  55. list = append(list, v.T_model)
  56. }
  57. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: list}
  58. c.ServeJSON()
  59. return
  60. }
  61. func (c *ProductController) ProductUpgrade_Add() {
  62. T_model := c.GetString("T_model")
  63. T_version := c.GetString("T_version")
  64. T_file := c.GetString("T_file")
  65. T_remarks := c.GetString("T_remarks")
  66. T_carryout, _ := c.GetInt("T_carryout")
  67. var_ := Product.ProductUpgrade{
  68. T_model: T_model,
  69. T_version: T_version,
  70. T_file: T_file,
  71. T_remarks: T_remarks,
  72. T_carryout: T_carryout,
  73. }
  74. Id, err := Product.Add_ProductUpgrade(var_)
  75. if err != nil {
  76. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  77. c.ServeJSON()
  78. return
  79. }
  80. if T_carryout == 1 {
  81. deviceList := Device.Read_Device_List_ByT_model(T_model)
  82. for _, v := range deviceList {
  83. device, _ := Device.Read_Device_ByT_sn(v.T_sn)
  84. NatsServer.Up_ProductUpgrade(v.T_sn, T_version, T_file, device.T_mqttid)
  85. }
  86. }
  87. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备版本升级管理", "添加", var_)
  88. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  89. c.ServeJSON()
  90. return
  91. }
  92. func (c *ProductController) ProductUpgrade_Del() {
  93. id, _ := c.GetInt("T_id")
  94. _, err := Product.Read_ProductUpgrade_ById(id)
  95. if err != nil {
  96. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  97. c.ServeJSON()
  98. return
  99. }
  100. if is := Product.Delete_ProductUpgrade_ById(id); !is {
  101. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  102. c.ServeJSON()
  103. return
  104. }
  105. System.Add_UserLogs(c.Admin_r.T_uuid, "设备版本升级管理", "删除", strconv.Itoa(id))
  106. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  107. c.ServeJSON()
  108. return
  109. }