| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package business
- import (
- "fmt"
- "net/http"
- "os"
- "path"
- "strconv"
- "time"
- "github.com/360EntSecGroup-Skylar/excelize/v2"
- "github.com/gin-gonic/gin"
- "github.com/gin-gonic/gin/binding"
- //"device-manage/app/admin/models"
- "device-manage/app/admin/models/busmodels"
- "device-manage/tools"
- "device-manage/tools/app"
- )
- // @Summary 获取设备列表
- // @Description Get JSON
- // @Tags 设备列表
- // @Param name query string false "name"
- // @Param status query string false "status"
- // @Param deviceSn query string false "deviceSn"
- // @Param pageSize query int false "页条数"
- // @Param pageIndex query int false "页码"
- // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/device/list/devicelist [get]
- // @Security Bearer
- func GetDeviceList(c *gin.Context) {
- var data busmodels.BusDevice
- var params busmodels.QueryParams
- err := c.MustBindWith(¶ms, binding.JSON)
- pageSize := params.PageSize
- pageIndex := params.PageIndex
- gids := params.Gids
- data.DeviceName = params.DeviceName
- data.DeviceSn = params.DeviceSn
- result, count, err := data.GetPage(pageSize, pageIndex, gids)
- tools.HasError(err, "", -1)
- app.PageOK(c, result, count, pageIndex, pageSize, "")
- }
- //获取设备统计信息 (在/离线设备数量)
- func GetDeviceStatistics(c *gin.Context) {
- var data busmodels.BusDevice
- var params busmodels.QueryParams
- err := c.MustBindWith(¶ms, binding.JSON)
- gids := params.Gids
- result, err := data.GetDeviceStatistics(gids)
- tools.HasError(err, "", -1)
- fmt.Println("GetDeviceStatistics")
- var res app.Response
- res.Data = result
- c.JSON(http.StatusOK, res.ReturnOK())
- }
- func InsertDevice(c *gin.Context) {
- var device busmodels.BusDevice
- err := c.MustBindWith(&device, binding.JSON)
- tools.HasError(err, "非法数据格式", 500)
- device.CreateBy = tools.GetUserIdStr(c)
- time := time.Now()
- device.OnlineTime = "00:00:00"
- device.TotallTime = "00:00:00"
- device.RegisterTime = time
- device.LastTime = time
- device.UpdatedAt = time
- device.CreatedAt = time
- id, err := device.Insert()
- fmt.Println(id)
- tools.HasError(err, "添加失败", 500)
- app.OK(c, id, "添加成功")
- }
- func GetDevice(c *gin.Context) {
- var data busmodels.BusDevice
- data.DeviceId, _ = tools.StringToInt(c.Param("deviceId"))
- result, err := data.Get()
- tools.HasError(err, "抱歉未找到相关信息", -1)
- var res app.Response
- res.Data = result
- c.JSON(http.StatusOK, res.ReturnOK())
- }
- func DeleteDevice(c *gin.Context) {
- var data busmodels.BusDevice
- data.UpdateBy = tools.GetUserIdStr(c)
- IDS := tools.IdsStrToIdsIntGroup("deviceId", c)
- fmt.Println(IDS)
- result, err := data.BatchDelete(IDS)
- tools.HasError(err, "删除失败", 500)
- app.OK(c, result, "删除成功")
- }
- //更新之前,应该先获取当前选项的数据, 得到 id以后才好i修改.
- func UpdateDevice(c *gin.Context) {
- var data busmodels.BusDevice
- err := c.Bind(&data)
- fmt.Println(data)
- tools.HasError(err, "数据解析失败", -1)
- data.UpdateBy = tools.GetUserIdStr(c)
- result, err := data.Update(data.DeviceId)
- tools.HasError(err, "修改失败", 500)
- app.OK(c, result, "修改成功")
- }
- func ImportDevice(c *gin.Context) {
- // 获取上传文件
- var device busmodels.BusDevice
- files, _ := c.FormFile("file")
- gid, _ := c.GetPostForm("groupId")
- device.GroupId, _ = strconv.Atoi(gid)
- // 设置文件需要保存的指定位置并设置保存的文件名字
- dst := path.Join("files", files.Filename)
- // 上传文件到指定的路径
- a := c.SaveUploadedFile(files, dst)
- if a != nil {
- c.JSON(200, gin.H{"shuju": a})
- }
- xlsx, err := excelize.OpenFile(dst)
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- time := time.Now()
- device.CreateBy = tools.GetUserIdStr(c)
- device.OnlineTime = "00:00:00"
- device.TotallTime = "00:00:00"
- device.RegisterTime = time
- device.LastTime = time
- device.UpdatedAt = time
- device.CreatedAt = time
- // 获取excel中具体的列的值
- rows, _ := xlsx.GetRows("Sheet" + "1")
- // 循环刚刚获取到的表中的值
- for key, row := range rows {
- if key > 0 {
- //循环每一个列的值
- for _, colCell := range row {
- fmt.Print(colCell, "|")
- if colCell != "" {
- device.DeviceId = 0
- device.DeviceSn = colCell
- device.DeviceName = colCell
- if _, err := device.Insert(); err != nil {
- fmt.Println("insert db error:", device.DeviceSn)
- }
- }
- }
- }
- }
- app.OK(c, "", "导入成功")
- }
|