...
|
...
|
@@ -18,6 +18,10 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepart |
|
|
returndata protocol.ResponseDepartmenrAdd
|
|
|
err error
|
|
|
)
|
|
|
if param.ParentID == 0 {
|
|
|
//部门必定有上级部门 ,至少是公司一级
|
|
|
return returndata, protocol.NewErrWithMessage("10042")
|
|
|
}
|
|
|
if param.ParentID > 0 {
|
|
|
parentDepart, err = models.GetDepartmentById(param.ParentID)
|
|
|
if err != nil {
|
...
|
...
|
@@ -31,14 +35,21 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepart |
|
|
return returndata, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
}
|
|
|
for _, v := range param.Managers {
|
|
|
_, err = models.GetUserCompanyBy(v, param.CompanyID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetUserCompanyBy(userid,companyid)[%d, %d] err:%s", v, param.CompanyID, err)
|
|
|
log.Error(e.Error())
|
|
|
return returndata, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
}
|
|
|
// for _, v := range param.Managers {
|
|
|
// uc, err := models.GetUserCompanyReal([]int64{v})
|
|
|
// if err != nil {
|
|
|
// e := fmt.Errorf("GetUserCompanyReal([]int64{%d}) err:%s", v, err)
|
|
|
// log.Error(e.Error())
|
|
|
// return returndata, protocol.NewErrWithMessage("1", e)
|
|
|
// }
|
|
|
// if len(uc) > 0 {
|
|
|
// if uc[0].CompanyId != param.CompanyID {
|
|
|
// e := fmt.Errorf("managers err")
|
|
|
// log.Error(e.Error())
|
|
|
// return returndata, protocol.NewErrWithMessage("1", e)
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
departmentAdd := &models.Department{
|
|
|
CompanyId: param.CompanyID,
|
|
|
Name: param.Name,
|
...
|
...
|
@@ -46,7 +57,7 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepart |
|
|
UpdateAt: time.Now(),
|
|
|
ParentId: param.ParentID,
|
|
|
}
|
|
|
departmentAdd.SetManages(param.Managers)
|
|
|
departmentAdd.SetManages(nil)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
_, err = models.AddDepartment(departmentAdd, o)
|
...
|
...
|
@@ -93,22 +104,46 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error { |
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
//校验数据
|
|
|
if ok := departUpdate.IsTopDepartment(); ok {
|
|
|
//公司一级不能修改上级,不能修改名称
|
|
|
param.Name = departUpdate.Name
|
|
|
param.ParentID = departUpdate.ParentId
|
|
|
} else {
|
|
|
if param.ParentID <= 0 {
|
|
|
return protocol.NewErrWithMessage("10042")
|
|
|
}
|
|
|
}
|
|
|
//确认部门主管变更情况
|
|
|
var (
|
|
|
oldmanage []int64
|
|
|
newManage []int64
|
|
|
)
|
|
|
oldmanage = departUpdate.GetManagesIds()
|
|
|
diffmanage := utils.ArrayInt64Diff(param.Managers, oldmanage)
|
|
|
for i := range diffmanage {
|
|
|
_, err = models.GetUserCompanyBy(diffmanage[i], param.CompanyID)
|
|
|
//oldmanage = departUpdate.GetManagesIds()
|
|
|
//diffmanage := utils.ArrayInt64Diff(param.Managers, oldmanage)
|
|
|
uc, err := models.GetUserCompanyReal(param.Managers)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetUserCompanyBy(userid,companyid)[%d,%d] err:%s", diffmanage[i], param.CompanyID, err)
|
|
|
e := fmt.Errorf("GetUserCompanyReal err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
return protocol.NewErrWithMessage("10041")
|
|
|
}
|
|
|
for i := range uc {
|
|
|
if uc[i].CompanyId != param.CompanyID {
|
|
|
e := fmt.Errorf("managers err")
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("10041")
|
|
|
}
|
|
|
//检查设置的主管是否是部门下的人员
|
|
|
ok := models.ExistUserDepart(param.ID, uc[i].Id)
|
|
|
if !ok {
|
|
|
log.Error("false=aExistUserDepart(departid,usercompanyid) [%d,%d]", param.ID, uc[i].Id)
|
|
|
return protocol.NewErrWithMessage("10041")
|
|
|
}
|
|
|
departUpdate.SetManages(param.Managers)
|
|
|
newManage = append(newManage, uc[i].Id)
|
|
|
}
|
|
|
departUpdate.SetManages(newManage)
|
|
|
|
|
|
departUpdate.Name = param.Name
|
|
|
|
|
|
departUpdate.UpdateAt = time.Now()
|
|
|
err = models.UpdateDepartmentById(departUpdate, []string{"Manages", "Name", "UpdateAt"})
|
|
|
if err != nil {
|
...
|
...
|
@@ -120,7 +155,9 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error { |
|
|
var (
|
|
|
newparent *models.Department
|
|
|
)
|
|
|
|
|
|
if departUpdate.ParentId != param.ParentID {
|
|
|
//检查上级部门是否合法
|
|
|
newparent, err = models.GetDepartmentById(param.ParentID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParentID, err)
|
...
|
...
|
@@ -188,7 +225,7 @@ func departmentRelationUpdate(departUpdate *models.Department, newparent *models |
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("departSubset[i].Id == newparent.Id")
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
return protocol.NewErrWithMessage("10042")
|
|
|
}
|
|
|
//重建关系树
|
|
|
s := strings.TrimPrefix(departSubset[i].Relation, oldRelation)
|
...
|
...
|
@@ -227,6 +264,10 @@ func DepartmentDelete(param protocol.RequestDepartmentDelete) error { |
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
//一级部门不能删除
|
|
|
if ok := p.IsTopDepartment(); ok {
|
|
|
continue
|
|
|
}
|
|
|
if p.CompanyId != param.CompanyID {
|
|
|
e := fmt.Errorf("CompanyID err")
|
|
|
log.Error(e.Error())
|
...
|
...
|
@@ -286,7 +327,7 @@ func DepartmentListAll(companyId int64) ([]protocol.ResponseDepartmentInfo, erro |
|
|
err error
|
|
|
)
|
|
|
const (
|
|
|
datasql0 string = `SELECT id, company_id,name,parent_id,managers,delete_at ` +
|
|
|
datasql0 string = `SELECT id, company_id,name,parent_id,member,managers ` +
|
|
|
` FROM department WHERE company_id = ? AND delete_at = 0`
|
|
|
)
|
|
|
err = utils.ExecuteQueryAll(&departmodels, datasql0, companyId)
|
...
|
...
|
|