device.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package business
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "path"
  7. "strconv"
  8. "time"
  9. "github.com/360EntSecGroup-Skylar/excelize/v2"
  10. "github.com/gin-gonic/gin"
  11. "github.com/gin-gonic/gin/binding"
  12. //"device-manage/app/admin/models"
  13. "device-manage/app/admin/models/busmodels"
  14. "device-manage/tools"
  15. "device-manage/tools/app"
  16. )
  17. // @Summary 获取设备列表
  18. // @Description Get JSON
  19. // @Tags 设备列表
  20. // @Param name query string false "name"
  21. // @Param status query string false "status"
  22. // @Param deviceSn query string false "deviceSn"
  23. // @Param pageSize query int false "页条数"
  24. // @Param pageIndex query int false "页码"
  25. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  26. // @Router /api/v1/device/list/devicelist [get]
  27. // @Security Bearer
  28. func GetDeviceList(c *gin.Context) {
  29. var data busmodels.BusDevice
  30. var params busmodels.QueryParams
  31. err := c.MustBindWith(&params, binding.JSON)
  32. pageSize := params.PageSize
  33. pageIndex := params.PageIndex
  34. gids := params.Gids
  35. data.DeviceName = params.DeviceName
  36. data.DeviceSn = params.DeviceSn
  37. result, count, err := data.GetPage(pageSize, pageIndex, gids)
  38. tools.HasError(err, "", -1)
  39. app.PageOK(c, result, count, pageIndex, pageSize, "")
  40. }
  41. //获取设备统计信息 (在/离线设备数量)
  42. func GetDeviceStatistics(c *gin.Context) {
  43. var data busmodels.BusDevice
  44. var params busmodels.QueryParams
  45. err := c.MustBindWith(&params, binding.JSON)
  46. gids := params.Gids
  47. result, err := data.GetDeviceStatistics(gids)
  48. tools.HasError(err, "", -1)
  49. fmt.Println("GetDeviceStatistics")
  50. var res app.Response
  51. res.Data = result
  52. c.JSON(http.StatusOK, res.ReturnOK())
  53. }
  54. func InsertDevice(c *gin.Context) {
  55. var device busmodels.BusDevice
  56. err := c.MustBindWith(&device, binding.JSON)
  57. tools.HasError(err, "非法数据格式", 500)
  58. device.CreateBy = tools.GetUserIdStr(c)
  59. time := time.Now()
  60. device.OnlineTime = "00:00:00"
  61. device.TotallTime = "00:00:00"
  62. device.RegisterTime = time
  63. device.LastTime = time
  64. device.UpdatedAt = time
  65. device.CreatedAt = time
  66. id, err := device.Insert()
  67. fmt.Println(id)
  68. tools.HasError(err, "添加失败", 500)
  69. app.OK(c, id, "添加成功")
  70. }
  71. func GetDevice(c *gin.Context) {
  72. var data busmodels.BusDevice
  73. data.DeviceId, _ = tools.StringToInt(c.Param("deviceId"))
  74. result, err := data.Get()
  75. tools.HasError(err, "抱歉未找到相关信息", -1)
  76. var res app.Response
  77. res.Data = result
  78. c.JSON(http.StatusOK, res.ReturnOK())
  79. }
  80. func DeleteDevice(c *gin.Context) {
  81. var data busmodels.BusDevice
  82. data.UpdateBy = tools.GetUserIdStr(c)
  83. IDS := tools.IdsStrToIdsIntGroup("deviceId", c)
  84. fmt.Println(IDS)
  85. result, err := data.BatchDelete(IDS)
  86. tools.HasError(err, "删除失败", 500)
  87. app.OK(c, result, "删除成功")
  88. }
  89. //更新之前,应该先获取当前选项的数据, 得到 id以后才好i修改.
  90. func UpdateDevice(c *gin.Context) {
  91. var data busmodels.BusDevice
  92. err := c.Bind(&data)
  93. fmt.Println(data)
  94. tools.HasError(err, "数据解析失败", -1)
  95. data.UpdateBy = tools.GetUserIdStr(c)
  96. result, err := data.Update(data.DeviceId)
  97. tools.HasError(err, "修改失败", 500)
  98. app.OK(c, result, "修改成功")
  99. }
  100. func ImportDevice(c *gin.Context) {
  101. // 获取上传文件
  102. var device busmodels.BusDevice
  103. files, _ := c.FormFile("file")
  104. gid, _ := c.GetPostForm("groupId")
  105. device.GroupId, _ = strconv.Atoi(gid)
  106. // 设置文件需要保存的指定位置并设置保存的文件名字
  107. dst := path.Join("files", files.Filename)
  108. // 上传文件到指定的路径
  109. a := c.SaveUploadedFile(files, dst)
  110. if a != nil {
  111. c.JSON(200, gin.H{"shuju": a})
  112. }
  113. xlsx, err := excelize.OpenFile(dst)
  114. if err != nil {
  115. fmt.Println(err)
  116. os.Exit(1)
  117. }
  118. time := time.Now()
  119. device.CreateBy = tools.GetUserIdStr(c)
  120. device.OnlineTime = "00:00:00"
  121. device.TotallTime = "00:00:00"
  122. device.RegisterTime = time
  123. device.LastTime = time
  124. device.UpdatedAt = time
  125. device.CreatedAt = time
  126. // 获取excel中具体的列的值
  127. rows, _ := xlsx.GetRows("Sheet" + "1")
  128. // 循环刚刚获取到的表中的值
  129. for key, row := range rows {
  130. if key > 0 {
  131. //循环每一个列的值
  132. for _, colCell := range row {
  133. fmt.Print(colCell, "|")
  134. if colCell != "" {
  135. device.DeviceId = 0
  136. device.DeviceSn = colCell
  137. device.DeviceName = colCell
  138. if _, err := device.Insert(); err != nil {
  139. fmt.Println("insert db error:", device.DeviceSn)
  140. }
  141. }
  142. }
  143. }
  144. }
  145. app.OK(c, "", "导入成功")
  146. }