...
|
...
|
@@ -6,6 +6,8 @@ import ( |
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/utils"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
)
|
...
|
...
|
@@ -17,57 +19,105 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepart |
|
|
|
|
|
}
|
|
|
|
|
|
func DepartmentEdit(param protocol.RequestDepartmentEdit) (protocol.ResponseDepartmentInfo, error) {
|
|
|
func DepartmentEdit(param protocol.RequestDepartmentEdit) error {
|
|
|
var (
|
|
|
depart *models.Department
|
|
|
err error
|
|
|
result protocol.ResponseDepartmentInfo
|
|
|
departUpdate *models.Department
|
|
|
err error
|
|
|
)
|
|
|
result = protocol.ResponseDepartmentInfo{
|
|
|
ID: param.ID,
|
|
|
Name: param.Name,
|
|
|
ParantID: param.ParantID,
|
|
|
}
|
|
|
depart, err = models.GetDepartmentById(param.ID)
|
|
|
|
|
|
departUpdate, err = models.GetDepartmentById(param.ID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ID, err)
|
|
|
log.Error(e.Error())
|
|
|
return result, protocol.NewErrWithMessage("1", e)
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
if depart.CompanyId != param.CompanyID {
|
|
|
e := fmt.Errorf("depart.CompanyId(%d) !=param.CompanyID(%d)", depart.CompanyId, param.CompanyID)
|
|
|
if departUpdate.CompanyId != param.CompanyID {
|
|
|
e := fmt.Errorf("depart.CompanyId(%d) !=param.CompanyID(%d)", departUpdate.CompanyId, param.CompanyID)
|
|
|
log.Error(e.Error())
|
|
|
return result, protocol.NewErrWithMessage("1", e)
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
//确认部门主管变更情况
|
|
|
var (
|
|
|
oldmanage []int64
|
|
|
)
|
|
|
oldmanage = depart.GetManagesIds()
|
|
|
oldmanage = departUpdate.GetManagesIds()
|
|
|
diffmanage := utils.ArrayInt64Diff(param.Managers, oldmanage)
|
|
|
for i := range diffmanage {
|
|
|
_, err = models.GetUserCompanyBy(diffmanage[i], param.CompanyID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetUserCompanyBy(%d,%d) err:%s", diffmanage[i], param.CompanyID, err)
|
|
|
log.Error(e.Error())
|
|
|
return result, protocol.NewErrWithMessage("1", e)
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
}
|
|
|
//处理部门上级发生变化的情况
|
|
|
if depart.ParentId != param.ParantID {
|
|
|
//oldRelation := strings.Split(depart.Relation, "")
|
|
|
var (
|
|
|
newparent *models.Department
|
|
|
)
|
|
|
if departUpdate.ParentId != param.ParantID {
|
|
|
newparent, err = models.GetDepartmentById(param.ParantID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParantID, err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
}
|
|
|
//更新部门数据
|
|
|
departmentInfoUpdate(depart, ¶m)
|
|
|
return result, nil
|
|
|
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 {
|
|
|
e := fmt.Errorf(" departmentRelationUpdate err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//DepartmentParentChange 处理部门上级发生变化的情况
|
|
|
func departmentInfoUpdate(old *models.Department, new *protocol.RequestDepartmentEdit) error {
|
|
|
|
|
|
func departmentRelationUpdate(old *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=?`
|
|
|
)
|
|
|
var (
|
|
|
departSubset []models.Department //子级部门
|
|
|
err error
|
|
|
oldrelation string = old.Relation
|
|
|
relationLike string = oldrelation + "%"
|
|
|
newRelation string = fmt.Sprintf("%s/%d", newparent.Relation, old.Id)
|
|
|
)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
|
|
|
err = utils.ExcuteQueryAllWithOrmer(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)
|
|
|
}
|
|
|
for i := range departSubset {
|
|
|
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)
|
|
|
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
|
|
|
}
|
...
|
...
|
|