cenvironment.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package controllers
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/astaxie/beego"
  6. validation "github.com/go-ozzo/ozzo-validation/v4"
  7. "lc/common/models"
  8. )
  9. type EnvironmentController struct {
  10. BaseController
  11. }
  12. type ReqEnvironment struct {
  13. Code string `json:"code"` //设备编号,禁止修改
  14. GID string `json:"gid"` //网关ID
  15. Tenant string `json:"tenant"` //租户ID
  16. Name string `json:"name"` //设备名称
  17. Brand int `json:"brand"` //品牌
  18. Model int `json:"model"` //型号
  19. State int `json:"state"` //1启用,0禁用
  20. }
  21. func (a ReqEnvironment) Validate() error {
  22. return validation.ValidateStruct(&a,
  23. validation.Field(&a.Code, validation.Required, validation.Length(4, 32)),
  24. validation.Field(&a.GID, validation.Required, validation.Length(4, 32)),
  25. validation.Field(&a.Tenant, validation.Required, validation.Length(4, 8)),
  26. validation.Field(&a.State, validation.In(0, 1)),
  27. )
  28. }
  29. type Environment struct {
  30. Code string `json:"code"` //设备编号,禁止修改
  31. GID string `json:"gid"` //网关ID
  32. Name string `json:"name"` //设备名称
  33. Brand int `json:"brand"` //品牌
  34. Model int `json:"model"` //型号
  35. State int `json:"state"` //1启用,0禁用
  36. }
  37. func (a Environment) Validate() error {
  38. return validation.ValidateStruct(&a,
  39. validation.Field(&a.Code, validation.Required, validation.Length(4, 32)),
  40. validation.Field(&a.GID, validation.Required, validation.Length(4, 32)),
  41. validation.Field(&a.State, validation.In(0, 1)),
  42. )
  43. }
  44. type ReqImportEnvironment struct {
  45. Tenant string `json:"tenant"` //租户ID
  46. List []Environment `json:"list"` //网关列表
  47. }
  48. func (a ReqImportEnvironment) Validate() error {
  49. return validation.ValidateStruct(&a,
  50. validation.Field(&a.Tenant, validation.Required, validation.Length(4, 32)),
  51. validation.Field(&a.List, validation.Required),
  52. )
  53. }
  54. // CreateEnvironment @Title 创建环境监测设备
  55. // @Description 创建环境监测设备
  56. // @Param body controllers.ReqEnvironment true "数据"
  57. // @Success 0 {int} BaseResponse.Code "成功"
  58. // @Failure 1 {int} BaseResponse.Code "失败"
  59. // @router /v1/create [post]
  60. func (o *EnvironmentController) CreateEnvironment() {
  61. var obj ReqEnvironment
  62. if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
  63. beego.Debug(string(o.Ctx.Input.RequestBody))
  64. o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
  65. return
  66. }
  67. if err := obj.Validate(); err != nil {
  68. beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
  69. o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
  70. return
  71. }
  72. oo := models.DeviceEnvironment{
  73. ID: obj.Code,
  74. GID: obj.GID,
  75. Name: obj.Name,
  76. Tenant: obj.Tenant,
  77. Brand: obj.Brand,
  78. Model: obj.Model,
  79. State: obj.State,
  80. }
  81. if err := oo.SaveFromWeb(); err != nil {
  82. o.Response(Failure, fmt.Sprintf("数据插入失败:%s", err.Error()), nil)
  83. return
  84. }
  85. o.Response(Success, "成功", oo.ID)
  86. }
  87. // UpdateEnvironment @Title 更新环境监测设备
  88. // @Description 更新环境监测设备
  89. // @Param body body controllers.ReqEnvironment true "数据"
  90. // @Success 0 {int} BaseResponse.Code "成功"
  91. // @Failure 1 {int} BaseResponse.Code "失败"
  92. // @router /v1/update [post]
  93. func (o *EnvironmentController) UpdateEnvironment() {
  94. var obj ReqEnvironment
  95. if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
  96. beego.Debug(string(o.Ctx.Input.RequestBody))
  97. o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
  98. return
  99. }
  100. if err := obj.Validate(); err != nil {
  101. beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
  102. o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
  103. return
  104. }
  105. oo := models.DeviceEnvironment{
  106. ID: obj.Code,
  107. GID: obj.GID,
  108. Name: obj.Name,
  109. Tenant: obj.Tenant,
  110. Brand: obj.Brand,
  111. Model: obj.Model,
  112. State: obj.State,
  113. }
  114. if err := oo.SaveFromWeb(); err != nil {
  115. o.Response(Failure, fmt.Sprintf("数据更新失败:%s", err.Error()), nil)
  116. return
  117. }
  118. o.Response(Success, "成功", oo.ID)
  119. }
  120. // DeleteEnvironment @Title 删除环境监测设备
  121. // @Description 删除环境监测设备
  122. // @Param code query string true "设备ID"
  123. // @Success 0 {int} BaseResponse.Code "成功"
  124. // @Failure 1 {int} BaseResponse.Code "失败"
  125. // @router /v1/delete [post]
  126. func (o *EnvironmentController) DeleteEnvironment() {
  127. code := strings.Trim(o.GetString("code"), " ")
  128. if err := validation.Validate(code, validation.Required, validation.Length(4, 100)); err != nil {
  129. beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
  130. o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
  131. return
  132. }
  133. c := models.DeviceEnvironment{
  134. ID: code,
  135. }
  136. if err := c.Delete(); err != nil {
  137. beego.Error(fmt.Sprintf("删除失败:%s", err.Error()))
  138. o.Response(Failure, fmt.Sprintf("数据删除失败:%s", err.Error()), nil)
  139. return
  140. }
  141. o.Response(Success, "成功", c.ID)
  142. }
  143. // ImportEnvironment @Title 导入环境监测设备
  144. // @Description 导入环境监测设备
  145. // @Param body body controllers.ReqImportEnvironment true "数据"
  146. // @Success 0 {int} BaseResponse.Code "成功"
  147. // @Failure 1 {int} BaseResponse.Code "失败"
  148. // @router /v1/import [post]
  149. func (o *EnvironmentController) ImportEnvironment() {
  150. var obj ReqImportEnvironment
  151. if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
  152. beego.Debug(string(o.Ctx.Input.RequestBody))
  153. o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
  154. return
  155. }
  156. if err := obj.Validate(); err != nil {
  157. beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
  158. o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
  159. return
  160. }
  161. var resp []RespImport
  162. for _, v := range obj.List {
  163. var aresp RespImport
  164. aresp.Code = v.Code
  165. oo := models.DeviceEnvironment{
  166. ID: v.Code,
  167. GID: v.GID,
  168. Name: v.Name,
  169. Tenant: obj.Tenant,
  170. Brand: v.Brand,
  171. Model: v.Model,
  172. State: v.State,
  173. }
  174. err := oo.SaveFromWeb()
  175. if err != nil {
  176. aresp.Error = err.Error()
  177. beego.Error(fmt.Sprintf("环境监测数据导入失败,code=%s,失败原因:%s", v.Code, err.Error()))
  178. }
  179. resp = append(resp, aresp)
  180. }
  181. o.Response(Success, "成功", resp)
  182. }