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("device.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. Join("LEFT", "enterprise", "enterprise.id = device.enterprise_id").Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } var resp []interface{} for _, v := range m { resp = append(resp, map[string]interface{}{ "id": v.Device.Id, "name": v.Device.Name, "device_sn": v.Device.DeviceSn, "enterprise_id": v.Device.EnterpriseId, "memo": v.Device.Memo, "create_at": v.Device.CreateAt, "update_at": v.Device.UpdateAt, "enterprise_name": v.Enterprise.Name, }) } e.OutSuc(c, map[string]interface{}{ "list": resp, "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 }