diff --git a/app/admin/apis/sys_dept.go b/app/admin/apis/sys_dept.go index ee96a4a..ed38ebc 100644 --- a/app/admin/apis/sys_dept.go +++ b/app/admin/apis/sys_dept.go @@ -98,7 +98,7 @@ func (e SysDept) Insert(c *gin.Context) { req := dto.SysDeptControl{} err := e.MakeContext(c). MakeOrm(). - Bind(&req, binding.JSON, nil). + Bind(&req, binding.JSON). MakeService(&s.Service). Errors if err != nil { diff --git a/app/admin/models/sys_dept.go b/app/admin/models/sys_dept.go index 4de08b2..cedac5e 100644 --- a/app/admin/models/sys_dept.go +++ b/app/admin/models/sys_dept.go @@ -7,11 +7,11 @@ type SysDept struct { ParentId int `json:"parentId" gorm:""` //上级部门 DeptPath string `json:"deptPath" gorm:"size:255;"` // DeptName string `json:"deptName" gorm:"size:128;"` //部门名称 - Sort int `json:"sort" gorm:""` //排序 + Sort int `json:"sort" gorm:"size:4;"` //排序 Leader string `json:"leader" gorm:"size:128;"` //负责人 Phone string `json:"phone" gorm:"size:11;"` //手机 Email string `json:"email" gorm:"size:64;"` //邮箱 - Status string `json:"status" gorm:"size:4;"` //状态 + Status int `json:"status" gorm:"size:4;"` //状态 models.ControlBy models.ModelTime DataScope string `json:"dataScope" gorm:"-"` diff --git a/app/admin/service/dto/sys_dept.go b/app/admin/service/dto/sys_dept.go index e7c8ba7..d4b5fe2 100644 --- a/app/admin/service/dto/sys_dept.go +++ b/app/admin/service/dto/sys_dept.go @@ -25,18 +25,17 @@ func (m *SysDeptSearch) GetNeedSearch() interface{} { return *m } - // SysDeptControl 增、改使用的结构体 type SysDeptControl struct { DeptId int `uri:"id" comment:"编码"` // 编码 - ParentId int `form:"parentId" comment:"上级部门"` //上级部门 - DeptPath string `form:"deptPath" comment:""` //路径 - DeptName string `form:"deptName" comment:"部门名称"` //部门名称 - Sort int `form:"sort" comment:"排序"` //排序 - Leader string `form:"leader" comment:"负责人"` //负责人 - Phone string `form:"phone" comment:"手机"` //手机 - Email string `form:"email" comment:"邮箱"` //邮箱 - Status string `form:"status" comment:"状态"` //状态 + ParentId int `json:"parentId" comment:"上级部门"` //上级部门 + DeptPath string `json:"deptPath" comment:""` //路径 + DeptName string `json:"deptName" comment:"部门名称"` //部门名称 + Sort int `json:"sort" comment:"排序"` //排序 + Leader string `json:"leader" comment:"负责人"` //负责人 + Phone string `json:"phone" comment:"手机"` //手机 + Email string `json:"email" comment:"邮箱"` //邮箱 + Status int `json:"status" comment:"状态"` //状态 common.ControlBy } @@ -87,4 +86,4 @@ type DeptLabel struct { Id int `gorm:"-" json:"id"` Label string `gorm:"-" json:"label"` Children []DeptLabel `gorm:"-" json:"children"` -} \ No newline at end of file +} diff --git a/app/admin/service/sys_dept.go b/app/admin/service/sys_dept.go index 05238b3..d5d13e5 100644 --- a/app/admin/service/sys_dept.go +++ b/app/admin/service/sys_dept.go @@ -61,7 +61,15 @@ func (e *SysDept) Insert(c *dto.SysDeptControl) error { var err error var data models.SysDept c.Generate(&data) - err = e.Orm.Create(data).Error + tx := e.Orm.Debug().Begin() + defer func() { + if err != nil { + tx.Rollback() + } else { + tx.Commit() + } + }() + err = tx.Create(&data).Error if err != nil { e.Log.Errorf("db error:%s", err) return err @@ -69,14 +77,14 @@ func (e *SysDept) Insert(c *dto.SysDeptControl) error { deptPath := pkg.IntToString(data.DeptId) + "/" if data.ParentId != 0 { var deptP models.SysDept - e.Orm.First(&deptP, data.ParentId) + tx.First(&deptP, data.ParentId) deptPath = deptP.DeptPath + deptPath } else { deptPath = "/0/" + deptPath } var mp = map[string]string{} mp["dept_path"] = deptPath - if err := e.Orm.Update("dept_path", deptPath).Error; err != nil { + if err := tx.Model(&data).Update("dept_path", deptPath).Error; err != nil { e.Log.Errorf("db error:%s", err) return err } @@ -87,19 +95,27 @@ func (e *SysDept) Insert(c *dto.SysDeptControl) error { func (e *SysDept) Update(c *dto.SysDeptControl) error { var err error var model = models.SysDept{} - e.Orm.First(&model, c.GetId()) + tx := e.Orm.Debug().Begin() + defer func() { + if err != nil { + tx.Rollback() + } else { + tx.Commit() + } + }() + tx.First(&model, c.GetId()) c.Generate(&model) deptPath := pkg.IntToString(model.DeptId) + "/" if model.ParentId != 0 { var deptP models.SysDept - e.Orm.First(&deptP, model.ParentId) + tx.First(&deptP, model.ParentId) deptPath = deptP.DeptPath + deptPath } else { deptPath = "/0/" + deptPath } model.DeptPath = deptPath - db := e.Orm.Save(&model) + db := tx.Save(&model) if db.Error != nil { e.Log.Errorf("UpdateSysDept error:%s", err) return err diff --git a/cmd/migrate/migration/models/sys_dept.go b/cmd/migrate/migration/models/sys_dept.go index 2fc66a7..5f74c62 100644 --- a/cmd/migrate/migration/models/sys_dept.go +++ b/cmd/migrate/migration/models/sys_dept.go @@ -5,11 +5,11 @@ type SysDept struct { ParentId int `json:"parentId" gorm:""` //上级部门 DeptPath string `json:"deptPath" gorm:"size:255;"` // DeptName string `json:"deptName" gorm:"size:128;"` //部门名称 - Sort int `json:"sort" gorm:""` //排序 + Sort int `json:"sort" gorm:"size:4;"` //排序 Leader string `json:"leader" gorm:"size:128;"` //负责人 Phone string `json:"phone" gorm:"size:11;"` //手机 Email string `json:"email" gorm:"size:64;"` //邮箱 - Status string `json:"status" gorm:"size:4;"` //状态 + Status int `json:"status" gorm:"size:4;"` //状态 ControlBy ModelTime }