package hdl import ( "applet/app/admin/lib/validate" "applet/app/admin/md" "applet/app/db" model2 "applet/app/db/model" "applet/app/e" "github.com/gin-gonic/gin" "time" ) func DeviceList(c *gin.Context) { var req md.DeviceListReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } if req.Limit == 0 { req.Limit = 10 } if req.Page == 0 { req.Page = 10 } sess := db.Db.Desc("id") if req.Name != "" { sess.And("device.name like ?", "%"+req.Name+"%") } if req.EnterpriseName != "" { sess.And("enterprise.name like ?", "%"+req.EnterpriseName+"%") } if req.StartDate != "" { sess.And("device.create_at >=?", req.StartDate) } if req.EndDate != "" { sess.And("device.create_at <=?", req.EndDate) } var m []*db.DeviceWithEnterprise total, err := sess.Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, map[string]interface{}{ "list": m, "total": total, }, nil) return } func DeviceSave(c *gin.Context) { var req md.DeviceSaveReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } deviceDd := db.Device{} deviceDd.Set(req.DeviceSn) //查找 device_sn device, err := deviceDd.GetDevice() if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if device == nil { insertAffected, err1 := deviceDd.DeviceInsert(&model2.Device{ Name: req.Name, DeviceSn: req.DeviceSn, EnterpriseId: req.EnterpriseId, Memo: req.Memo, CreateAt: time.Now().Format("2006-01-02 15:04:05"), UpdateAt: time.Now().Format("2006-01-02 15:04:05"), }) if err1 != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if insertAffected <= 0 { e.OutErr(c, e.ERR_DB_ORM, "新增数据失败") return } } else { device.EnterpriseId = req.EnterpriseId device.Name = req.Name device.Memo = req.Memo device.UpdateAt = time.Now().Format("2006-01-02 15:04:05") updateAffected, err1 := deviceDd.DeviceUpdate(device, "enterprise_id", "name", "memo", "update_at") if err1 != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if updateAffected <= 0 { e.OutErr(c, e.ERR_DB_ORM, "更新数据失败") return } } e.OutSuc(c, "success", nil) return } func DeviceDelete(c *gin.Context) { deviceSn := c.Param("device_sn") deviceDd := db.Device{} deviceDd.Set(deviceSn) deviceDdAffected, err := deviceDd.DeviceDelete() if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if deviceDdAffected <= 0 { e.OutErr(c, e.ERR_DB_ORM, "删除数据失败") return } e.OutSuc(c, "success", nil) return }