...
|
...
|
@@ -17,10 +17,10 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) error { |
|
|
parentDepart *models.Department
|
|
|
err error
|
|
|
)
|
|
|
if param.ParantID > 0 {
|
|
|
parentDepart, err = models.GetDepartmentById(param.ParantID)
|
|
|
if param.ParentID > 0 {
|
|
|
parentDepart, err = models.GetDepartmentById(param.ParentID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParantID, err)
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParentID, err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
...
|
...
|
@@ -42,16 +42,35 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) error { |
|
|
CompanyId: param.CompanyID,
|
|
|
Name: param.Name,
|
|
|
CreateAt: time.Now(),
|
|
|
ParentId: param.ParantID,
|
|
|
UpdateAt: time.Now(),
|
|
|
ParentId: param.ParentID,
|
|
|
Member: 0,
|
|
|
}
|
|
|
departmentAdd.SetRelation(parentDepart)
|
|
|
_, err = models.AddDepartment(departmentAdd)
|
|
|
departmentAdd.SetManages(param.Managers)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
_, err = models.AddDepartment(departmentAdd, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("AddDepartment err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
err = departmentAdd.SetRelation(parentDepart)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("SetRelation err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
err = models.UpdateDepartmentById(departmentAdd, []string{"Relation"}, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("UpdateDepartmentById err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
|
|
|
}
|
...
|
...
|
@@ -87,27 +106,27 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error { |
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
}
|
|
|
departUpdate.SetManages(param.Managers)
|
|
|
departUpdate.Name = param.Name
|
|
|
departUpdate.UpdateAt = time.Now()
|
|
|
err = models.UpdateDepartmentById(departUpdate, []string{"Manages", "Name", "UpdateAt"})
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("UpdateDepartmentById err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
//处理部门上级发生变化的情况
|
|
|
var (
|
|
|
newparent *models.Department
|
|
|
)
|
|
|
if departUpdate.ParentId != param.ParantID {
|
|
|
newparent, err = models.GetDepartmentById(param.ParantID)
|
|
|
if departUpdate.ParentId != param.ParentID {
|
|
|
newparent, err = models.GetDepartmentById(param.ParentID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParantID, err)
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParentID, err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
}
|
|
|
departUpdate.Manages = departUpdate.ParseManagesIds(param.Managers)
|
|
|
departUpdate.Name = param.Name
|
|
|
departUpdate.UpdateAt = time.Now()
|
|
|
err = models.UpdateDepartmentById(departUpdate, []string{"Manages", "Name", "UpdateAt"})
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("UpdateDepartmentById err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
//更新部门关系数据
|
|
|
err = departmentRelationUpdate(departUpdate, newparent)
|
|
|
if err != nil {
|
...
|
...
|
@@ -119,32 +138,44 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error { |
|
|
}
|
|
|
|
|
|
//DepartmentParentChange 处理部门上级发生变化的情况
|
|
|
func departmentRelationUpdate(old *models.Department, newparent *models.Department) error {
|
|
|
func departmentRelationUpdate(departUpdate *models.Department, newparent *models.Department) error {
|
|
|
if newparent == nil {
|
|
|
return nil
|
|
|
}
|
|
|
const (
|
|
|
//获取某个部门的下级部门 select ... for update
|
|
|
dataSql0 string = `SELECT id,relation FROM department WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`
|
|
|
//更新关系树
|
|
|
dataSql2 string = `update department set relation=? where id=?`
|
|
|
//更新departUpdate的parent_id
|
|
|
dataSql3 string = `update department set parent_id=? where id =?`
|
|
|
)
|
|
|
var (
|
|
|
departSubset []models.Department //子级部门
|
|
|
err error
|
|
|
oldrelation string = old.Relation
|
|
|
relationLike string = oldrelation + "%"
|
|
|
newRelation string = fmt.Sprintf("%s/%d", newparent.Relation, old.Id)
|
|
|
oldRelation string = departUpdate.Relation
|
|
|
relationLike string = oldRelation + "%"
|
|
|
newRelation string = fmt.Sprintf("%s/%d", newparent.Relation, departUpdate.Id)
|
|
|
)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
err = utils.ExcuteQueryAllWithOrmer(o, &departSubset, dataSql0, relationLike)
|
|
|
//修改部门的parent_id
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql3, newparent.Id, departUpdate.Id)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
|
|
|
//获取部门及子级部门的relation和id
|
|
|
err = utils.ExecuteQueryAllWithOrmer(o, &departSubset, dataSql0, relationLike)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
//修改部门及子级部门的relation
|
|
|
for i := range departSubset {
|
|
|
if departSubset[i].Id == newparent.Id {
|
|
|
//确认新的父级id是否合法
|
...
|
...
|
@@ -154,9 +185,9 @@ func departmentRelationUpdate(old *models.Department, newparent *models.Departme |
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
//重建关系树
|
|
|
s := strings.TrimPrefix(departSubset[i].Relation, oldrelation)
|
|
|
s := strings.TrimPrefix(departSubset[i].Relation, oldRelation)
|
|
|
departSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s))
|
|
|
err := utils.ExcuteSQLWithOrmer(o, dataSql2, departSubset[i].Relation, departSubset[i].Id)
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, departSubset[i].Relation, departSubset[i].Id)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
...
|
...
|
@@ -164,15 +195,68 @@ func departmentRelationUpdate(old *models.Department, newparent *models.Departme |
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func DepartmentDelete(parm protocol.RequestDepartmentDelete) error {
|
|
|
//DepartmentDelete 部门删除,删除整个分枝
|
|
|
func DepartmentDelete(param protocol.RequestDepartmentDelete) error {
|
|
|
var (
|
|
|
departDelete *models.Department
|
|
|
err error
|
|
|
)
|
|
|
departDelete, err = models.GetDepartmentById(param.ID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
if departDelete.CompanyId != param.CompanyID {
|
|
|
e := fmt.Errorf("departDelete.CompanyId != param.CompanyID")
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
const (
|
|
|
//获取部门子集 锁数据 select ... for update,
|
|
|
dataSql0 string = `SELECT id,relation,member FROM department WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`
|
|
|
dataSql2 string = `update department set delete_at=? where relation LIKE ?`
|
|
|
)
|
|
|
var (
|
|
|
departSubset []models.Department
|
|
|
relationLike string = departDelete.Relation + "%"
|
|
|
nowTime string = time.Now().String()
|
|
|
)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
err = utils.ExecuteQueryAllWithOrmer(o, &departSubset, dataSql0, relationLike)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("")
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
for _, v := range departSubset {
|
|
|
if v.Member > 0 {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("Member > 0 ")
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
}
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, nowTime, relationLike)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func DepartmentListAll() (protocol.ResponseDepartmentList, error) {
|
|
|
func DepartmentListAll(companyId int64) (protocol.ResponseDepartmentList, error) {
|
|
|
|
|
|
r := protocol.ResponseDepartmentList{}
|
|
|
return r, nil
|
|
|
} |
...
|
...
|
|