Device.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package controllers
  2. import (
  3. "Yunlot/lib"
  4. "Yunlot/models/Device"
  5. "Yunlot/models/Product"
  6. beego "github.com/beego/beego/v2/server/web"
  7. )
  8. type DeviceController struct {
  9. beego.Controller
  10. }
  11. func (c *DeviceController) List() {
  12. PageIndex, _ := c.GetInt("PageIndex", 0)
  13. PageSize, _ := c.GetInt("PageSize", 10)
  14. Devicer := Device.Device{}
  15. c.ParseForm(&Devicer)
  16. Device_r, Total := Devicer.Lists(PageIndex, PageSize)
  17. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(Device_r, PageIndex, PageSize, Total)}
  18. c.ServeJSON()
  19. return
  20. }
  21. func (c *DeviceController) Add() {
  22. // 验证秘钥
  23. ProductKey := c.GetString("ProductKey")
  24. ProductID := c.GetString("ProductID")
  25. ProductType_r := Product.ProductType{T_ProductID: ProductID}
  26. if !ProductType_r.Read() {
  27. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID!"}
  28. c.ServeJSON()
  29. return
  30. }
  31. if ProductType_r.T_akey != ProductKey {
  32. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductKey!"}
  33. c.ServeJSON()
  34. return
  35. }
  36. // 创建sn
  37. T_sn := c.GetString("T_sn")
  38. Device_r := Device.Device{T_ProductID: ProductID, T_sn: T_sn}
  39. //
  40. CreateNum, _ := c.GetInt("CreateNum", 1)
  41. if CreateNum == 1 || len(Device_r.T_sn) >= 10 {
  42. // 单个
  43. if !Device_r.CreateSn(1) {
  44. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_sn 重复!"}
  45. c.ServeJSON()
  46. return
  47. }
  48. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_r}
  49. c.ServeJSON()
  50. return
  51. } else {
  52. // 多个
  53. var Device_List []Device.Device
  54. for i := 1; i <= CreateNum; i++ {
  55. Device_rr := Device_r
  56. Device_rr.CreateSn(i)
  57. Device_List = append(Device_List, Device_rr)
  58. }
  59. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_List}
  60. c.ServeJSON()
  61. return
  62. }
  63. }
  64. func (c *DeviceController) Update() {
  65. Devicer := Device.Device{}
  66. c.ParseForm(&Devicer)
  67. if !Devicer.Update("T_name", "T_img", "T_prot") {
  68. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  69. c.ServeJSON()
  70. return
  71. }
  72. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  73. c.ServeJSON()
  74. return
  75. }
  76. func (c *DeviceController) Delete() {
  77. Devicer := Device.Device{}
  78. c.ParseForm(&Devicer)
  79. if !Devicer.Delete() {
  80. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  81. c.ServeJSON()
  82. return
  83. }
  84. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  85. c.ServeJSON()
  86. return
  87. }
  88. func (c *DeviceController) Get() {
  89. Devicer := Device.Device{}
  90. c.ParseForm(&Devicer)
  91. if !Devicer.Read_Tidy() {
  92. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "SN E!"}
  93. c.ServeJSON()
  94. return
  95. }
  96. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Devicer}
  97. c.ServeJSON()
  98. return
  99. }
  100. func (c *DeviceController) DataList() {
  101. DeviceData_Form := Device.DeviceData_Form{}
  102. c.ParseForm(&DeviceData_Form)
  103. if len(DeviceData_Form.T_sn) < 1 {
  104. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_sn !"}
  105. c.ServeJSON()
  106. return
  107. }
  108. if len(DeviceData_Form.T_jointTab) < 1 {
  109. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_jointTab !"}
  110. c.ServeJSON()
  111. return
  112. }
  113. JSONR_r := Device.Data_List(DeviceData_Form.T_sn+"_"+DeviceData_Form.T_jointTab, DeviceData_Form.T_jsonFind, DeviceData_Form.T_jsonSort, DeviceData_Form.PageIndex, DeviceData_Form.PageSize)
  114. c.Data["json"] = JSONR_r
  115. c.ServeJSON()
  116. return
  117. }