package hdl import ( enum2 "applet/app/admin/enum" "applet/app/admin/lib/validate" "applet/app/admin/md" svc3 "applet/app/admin/svc" "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") admin := svc3.GetUser(c) if admin.IsSuperAdministrator != enum2.IsSuperAdministratorTure { adminWithEnterpriseDb := db.AdminWithEnterpriseDb{} adminWithEnterpriseDb.Set() adminWithEnterprise, err1 := adminWithEnterpriseDb.FindAdminWithEnterprise(admin.AdmId) if err1 != nil { e.OutErr(c, e.ERR_DB_ORM, err1.Error()) return } var enterPriseIds []int for _, v1 := range *adminWithEnterprise { enterPriseIds = append(enterPriseIds, v1.EnterpriseId) } if len(enterPriseIds) == 0 { return } sess.In("device.enterprise_id", enterPriseIds) } 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"). Join("LEFT", "merchant", "merchant.id = device.merchant_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, "merchant_id": v.Device.MerchantId, "memo": v.Device.Memo, "create_at": v.Device.CreateAt, "update_at": v.Device.UpdateAt, "enterprise_name": v.Enterprise.Name, "merchant_name": v.Merchant.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, MerchantId: req.MerchantId, 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.MerchantId = req.MerchantId device.UpdateAt = time.Now().Format("2006-01-02 15:04:05") updateAffected, err1 := deviceDd.DeviceUpdate(device, "enterprise_id", "merchant_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 } func EnterPriseMerchantList(c *gin.Context) { enterpriseId := c.DefaultQuery("enterprise_id", "0") var m []model2.Merchant if err := db.Db.Where("enterprise_id = ?", enterpriseId).Find(&m); err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, map[string]interface{}{ "list": m, }, nil) return }