XMLWAN před 4 roky
rodič
revize
f97f66e62b
3 změnil soubory, kde provedl 41 přidání a 11 odebrání
  1. binární
      XT_New
  2. 22 11
      controllers/device_api_controller.go
  3. 19 0
      service/device_service.go

binární
XT_New Zobrazit soubor


+ 22 - 11
controllers/device_api_controller.go Zobrazit soubor

@@ -652,15 +652,22 @@ func (this *DeviceAPIController) CreateGroup() {
652 652
 	}
653 653
 
654 654
 	adminInfo := this.GetAdminUserInfo()
655
-	group, createErr := service.CreateDeviceGroup(adminInfo.CurrentOrgId, name)
656
-	if createErr != nil {
657
-		this.ErrorLog("创建设备分组失败:%v", createErr)
658
-		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
659
-		return
660
-	}
661
-	this.ServeSuccessJSON(map[string]interface{}{
662
-		"group": group,
663
-	})
655
+  _, errcode := service.GetDeviceGroupName(name, adminInfo.CurrentOrgId)
656
+  if errcode == gorm.ErrRecordNotFound{
657
+    group, createErr := service.CreateDeviceGroup(adminInfo.CurrentOrgId, name)
658
+    if createErr != nil {
659
+      this.ErrorLog("创建设备分组失败:%v", createErr)
660
+      this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
661
+      return
662
+    }
663
+    this.ServeSuccessJSON(map[string]interface{}{
664
+      "group": group,
665
+    })
666
+  }else if errcode == nil{
667
+    this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
668
+    return
669
+  }
670
+
664 671
 }
665 672
 
666 673
 // /api/device/group/modify [post] ModifyGroup
@@ -687,8 +694,12 @@ func (this *DeviceAPIController) ModifyGroup() {
687 694
 
688 695
 	group.Name = name
689 696
 	group.ModifyTime = time.Now().Unix()
690
-
691
-	updateErr := service.UpdateDeviceGroup(group)
697
+  byName, getGroupErr := service.GetUpdateDeviceGroupByName(name, adminInfo.CurrentOrgId)
698
+  if byName.ID > 0 && byName.ID != id {
699
+    this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
700
+    return
701
+  }
702
+  updateErr := service.UpdateDeviceGroup(group)
692 703
 	if updateErr != nil {
693 704
 		this.ErrorLog("修改设备分组失败:%v", updateErr)
694 705
 		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)

+ 19 - 0
service/device_service.go Zobrazit soubor

@@ -337,6 +337,20 @@ func GetDeviceGroupByID(orgID int64, groupID int64) (*models.DeviceGroup, error)
337 337
 	return &group, nil
338 338
 }
339 339
 
340
+func GetDeviceGroupName(name string,orgid int64)(*models.DeviceGroup,error)  {
341
+  group := models.DeviceGroup{}
342
+  err := XTReadDB().Model(&group).Where("name = ? and org_id = ? and status =1", name, orgid).Find(&group).Error
343
+  if err == gorm.ErrRecordNotFound {
344
+    return nil, err
345
+  }
346
+  if err != nil {
347
+    return nil, err
348
+  }
349
+
350
+  return &group, nil
351
+
352
+}
353
+
340 354
 func CreateDeviceGroup(orgID int64, name string) (*models.DeviceGroup, error) {
341 355
 	now := time.Now().Unix()
342 356
 	group := models.DeviceGroup{
@@ -353,6 +367,11 @@ func CreateDeviceGroup(orgID int64, name string) (*models.DeviceGroup, error) {
353 367
 	return &group, nil
354 368
 }
355 369
 
370
+func GetUpdateDeviceGroupByName(name string,orgid int64)(group models.DeviceGroup,err error)  {
371
+  err = XTReadDB().Model(&group).Where("name = ? and org_id = ? and status =1",name,orgid).Find(&group).Error
372
+  return group,err
373
+}
374
+
356 375
 func UpdateDeviceGroup(group *models.DeviceGroup) error {
357 376
 	err := writeDb.Save(group).Error
358 377
 	return err