refactor🎨:部门创建添加事务,已经修改status字段为int类型
This commit is contained in:
1 parent
636294b6f5
commit
b97b3bc6d8
5 files changed
+36
-21
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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:"-"`
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user