...
|
...
|
@@ -5,37 +5,42 @@ import ( |
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/utils"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
)
|
|
|
|
|
|
//PositionAdd 添加职位
|
|
|
func PositionAdd(param protocol.RequestDepartmentAdd) error {
|
|
|
func PositionAdd(param protocol.RequestPositionAdd) (*protocol.ResponsePositionInfo, error) {
|
|
|
var (
|
|
|
parentPosition *models.Position
|
|
|
err error
|
|
|
nowTime = time.Now()
|
|
|
positioninfo *protocol.ResponsePositionInfo
|
|
|
)
|
|
|
positionAdd := &models.Position{
|
|
|
CompanyId: param.CompanyID,
|
|
|
Name: param.Name,
|
|
|
CreateAt: nowTime,
|
|
|
UpdateAt: nowTime,
|
|
|
DeleteAt: time.Unix(0, 0),
|
|
|
ParentId: param.ParentID,
|
|
|
}
|
|
|
if param.ParentID > 0 {
|
|
|
parentPosition, err = models.GetPositionById(param.ParentID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParentID, err)
|
|
|
e := fmt.Errorf("GetPositionById(%d) err:%s", param.ParentID, err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
return nil, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
if parentPosition.CompanyId != param.CompanyID {
|
|
|
e := fmt.Errorf("parentDepart.CompanyId != param.CompanyID")
|
|
|
e := fmt.Errorf("parentPosition.CompanyId != param.CompanyID")
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
return nil, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
}
|
|
|
positionAdd := &models.Position{
|
|
|
CompanyId: param.CompanyID,
|
|
|
Name: param.Name,
|
|
|
CreateAt: time.Now(),
|
|
|
UpdateAt: time.Now(),
|
|
|
ParentId: param.ParentID,
|
|
|
}
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
...
|
...
|
@@ -44,23 +49,224 @@ func PositionAdd(param protocol.RequestDepartmentAdd) error { |
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("AddPosition err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
return nil, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
err = positionAdd.SetRelation(parentPosition)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("SetRelation err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
return nil, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
err = models.UpdatePositionById(positionAdd, []string{"Relation"}, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("UpdatePositionById err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return nil, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
o.Commit()
|
|
|
positioninfo = &protocol.ResponsePositionInfo{
|
|
|
ID: positionAdd.Id,
|
|
|
CompanyID: positionAdd.CompanyId,
|
|
|
Name: positionAdd.Name,
|
|
|
ParentID: positionAdd.ParentId,
|
|
|
ParemtName: "",
|
|
|
CreateTime: nowTime.Unix(),
|
|
|
}
|
|
|
if parentPosition != nil {
|
|
|
positioninfo.ParemtName = parentPosition.Name
|
|
|
}
|
|
|
return positioninfo, nil
|
|
|
}
|
|
|
|
|
|
func PositionEdit(param protocol.RequestPositionEdit) (*protocol.ResponsePositionInfo, error) {
|
|
|
var (
|
|
|
positionUpdate *models.Position
|
|
|
err error
|
|
|
parentPosition *models.Position
|
|
|
positioninfo *protocol.ResponsePositionInfo
|
|
|
nowTime = time.Now()
|
|
|
)
|
|
|
positionUpdate, err = models.GetPositionById(param.ID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ID, err)
|
|
|
log.Error(e.Error())
|
|
|
return nil, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
if positionUpdate.CompanyId != param.CompanyID {
|
|
|
e := fmt.Errorf("depart.CompanyId(%d) !=param.CompanyID(%d)", positionUpdate.CompanyId, param.CompanyID)
|
|
|
log.Error(e.Error())
|
|
|
return nil, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
positionUpdate.Name = param.Name
|
|
|
positionUpdate.UpdateAt = nowTime
|
|
|
err = models.UpdatePositionById(positionUpdate, []string{"Name", "UpdateAt"})
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("UpdatePositionById err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return nil, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
//理部门上级发生变化的情况
|
|
|
if param.ParentID != 0 {
|
|
|
parentPosition, err = models.GetPositionById(param.ParentID)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParentID, err)
|
|
|
log.Error(e.Error())
|
|
|
return nil, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
if parentPosition.CompanyId != positionUpdate.CompanyId {
|
|
|
e := fmt.Errorf("ParentID err")
|
|
|
log.Error(e.Error())
|
|
|
return nil, protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
}
|
|
|
//更新部门关系数据
|
|
|
err = positionRelationUpdate(positionUpdate, parentPosition)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("positionRelationUpdate err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return nil, protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
positioninfo = &protocol.ResponsePositionInfo{
|
|
|
ID: positionUpdate.Id,
|
|
|
CompanyID: positionUpdate.CompanyId,
|
|
|
Name: positionUpdate.Name,
|
|
|
CreateTime: positionUpdate.CreateAt.Unix(),
|
|
|
}
|
|
|
if param.ParentID != 0 {
|
|
|
positioninfo.ParentID = parentPosition.Id
|
|
|
positioninfo.ParemtName = parentPosition.Name
|
|
|
}
|
|
|
|
|
|
return positioninfo, nil
|
|
|
}
|
|
|
|
|
|
//positionRelationUpdate 处理部门上级发生变化的情况
|
|
|
func positionRelationUpdate(positionUpdate *models.Position, newparent *models.Position) error {
|
|
|
if newparent == nil {
|
|
|
return nil
|
|
|
}
|
|
|
const (
|
|
|
//获取某个部门的下级部门 select ... for update
|
|
|
dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`
|
|
|
//更新关系树
|
|
|
dataSql2 string = `update position set relation=? where id=?`
|
|
|
//更新departUpdate的parent_id
|
|
|
dataSql3 string = `update position set parent_id=? where id=?`
|
|
|
)
|
|
|
var (
|
|
|
positionSubset []models.Position //子级部门
|
|
|
err error
|
|
|
oldRelation string = positionUpdate.Relation
|
|
|
relationLike string = oldRelation + "%"
|
|
|
newRelation string = fmt.Sprintf("%s/%d", newparent.Relation, positionUpdate.Id)
|
|
|
)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
//修改部门的parent_id
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql3, newparent.Id, positionUpdate.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, &positionSubset, 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 positionSubset {
|
|
|
if positionSubset[i].Id == newparent.Id {
|
|
|
//确认新的父级id是否合法
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("departSubset[i].Id == newparent.Id")
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
//重建关系树
|
|
|
s := strings.TrimPrefix(positionSubset[i].Relation, oldRelation)
|
|
|
positionSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s))
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, positionSubset[i].Relation, positionSubset[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
|
|
|
}
|
|
|
|
|
|
func PositionDelete(param protocol.RequestPositionDelete) error {
|
|
|
//检查是否可以被删除
|
|
|
var (
|
|
|
//根据参数获取的职位
|
|
|
positionDelete []*models.Position
|
|
|
//最终需要操作的职位
|
|
|
toDelete = make(map[int64]*models.Position)
|
|
|
)
|
|
|
const (
|
|
|
//获取部门子集,
|
|
|
dataSql0 string = `SELECT id,relation,member FROM position WHERE relation LIKE ? AND delete_at = 0 `
|
|
|
dataSql2 string = `update position set delete_at=? where relation LIKE ?`
|
|
|
)
|
|
|
for _, id := range param.IDs {
|
|
|
var p *models.Position
|
|
|
p, err := models.GetPositionById(id)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetPositionById(%d)", id)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
if p.CompanyId != param.CompanyID {
|
|
|
e := fmt.Errorf("CompanyID err")
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
positionDelete = append(positionDelete, p)
|
|
|
toDelete[p.Id] = p
|
|
|
}
|
|
|
for _, pos := range positionDelete {
|
|
|
var positionsubset []models.Position
|
|
|
relationLike := pos.Relation + "%"
|
|
|
err := utils.ExecuteQueryAll(&positionsubset, dataSql0, relationLike)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
for _, subset := range positionsubset {
|
|
|
if subset.Member > 0 {
|
|
|
e := fmt.Errorf("user in position,relation:%s", subset.Relation)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
if _, ok := toDelete[subset.Id]; ok {
|
|
|
delete(toDelete, subset.Id)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
nowTime := time.Now()
|
|
|
for _, d := range toDelete {
|
|
|
relationlike := d.Relation + "%"
|
|
|
err := utils.ExecuteSQLWithOrmer(o, dataSql2, nowTime.String(), 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
|
|
|
} |
...
|
...
|
|