...
|
...
|
@@ -6,6 +6,8 @@ import ( |
|
|
"fmt"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/utils"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
...
|
...
|
@@ -45,7 +47,7 @@ func (m ModulePositionData) DoAction(code string, jsondata []byte) error { |
|
|
return fmt.Errorf("数据解析失败:%s", err)
|
|
|
}
|
|
|
return AddPosition(data)
|
|
|
case "delete":
|
|
|
case "batchDelete":
|
|
|
var (
|
|
|
err error
|
|
|
)
|
...
|
...
|
@@ -79,73 +81,97 @@ func (m ModulePositionData) validate() error { |
|
|
//UpdatePosition 同步职位数据
|
|
|
//TODO 父级数据变更
|
|
|
func UpdatePosition(data ModulePositionData) error {
|
|
|
positioninfo, err := models.GetPositionById(data.Id)
|
|
|
positioninfo, err := models.GetPositionByBusinessId(data.Id)
|
|
|
if err != nil {
|
|
|
log.Error("获取职位数据失败:%s", err)
|
|
|
return fmt.Errorf("获取职位数据失败,Id=%d", data.Id)
|
|
|
log.Error("获取职位数据失败,business_admin_id=%d ,err:%s", data.Id, err)
|
|
|
return fmt.Errorf("获取职位数据失败")
|
|
|
}
|
|
|
positioninfo.Name = data.Name
|
|
|
// positioninfo.ParentId = data.ParentId
|
|
|
// positioninfo.Relation = data.Path
|
|
|
err = models.UpdatePositionById(positioninfo, []string{"Name"})
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
if positioninfo.ParentId == 0 && data.ParentId == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
var (
|
|
|
newParentPosition *models.Position
|
|
|
)
|
|
|
if data.ParentId > 0 {
|
|
|
newParentPosition, err = models.GetPositionByBusinessId(data.ParentId)
|
|
|
if err != nil {
|
|
|
log.Error("获取父级职位数据失败,business_admin_id=%d,err:%s", data.ParentId, err)
|
|
|
return errors.New("获取父级职位数据失败")
|
|
|
}
|
|
|
} else {
|
|
|
newParentPosition = &models.Position{}
|
|
|
}
|
|
|
if positioninfo.ParentId != newParentPosition.Id {
|
|
|
// 更新父级
|
|
|
err = positionRelationUpdate(*positioninfo, *newParentPosition)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
//应对父级发生变化的情况
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//positionRelationUpdate 处理部门上级发生变化的情况
|
|
|
// func positionRelationUpdate(positionUpdate *models.Position, newparent *models.Position, o orm.Ormer) error {
|
|
|
// 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
|
|
|
// )
|
|
|
// if newparent == nil || newparent.Id == 0 {
|
|
|
// //修改节点为顶层节点的情况
|
|
|
// newparent = &models.Position{}
|
|
|
// newRelation = fmt.Sprintf("%d", positionUpdate.Id)
|
|
|
// } else {
|
|
|
// newRelation = fmt.Sprintf("%s/%d", newparent.Relation, positionUpdate.Id)
|
|
|
// }
|
|
|
// //修改部门的parent_id
|
|
|
// err = utils.ExecuteSQLWithOrmer(o, dataSql3, newparent.Id, positionUpdate.Id)
|
|
|
// if err != nil {
|
|
|
// e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
// log.Error(e.Error())
|
|
|
// return errors.New("更新数据失败")
|
|
|
// }
|
|
|
// //获取部门及子级部门的relation和id
|
|
|
// err = utils.ExecuteQueryAllWithOrmer(o, &positionSubset, dataSql0, relationLike)
|
|
|
// if err != nil {
|
|
|
// e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
// log.Error(e.Error())
|
|
|
// return errors.New("更新数据失败")
|
|
|
// }
|
|
|
// //修改部门及子级部门的relation
|
|
|
// for i := range positionSubset {
|
|
|
// //重建关系树
|
|
|
// 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 {
|
|
|
// e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
// log.Error(e.Error())
|
|
|
// return errors.New("更新数据失败")
|
|
|
// }
|
|
|
// }
|
|
|
// return nil
|
|
|
// }
|
|
|
//positionRelationUpdate 处理职位上级发生变化的情况
|
|
|
func positionRelationUpdate(positionUpdate models.Position, newparent models.Position) error {
|
|
|
const (
|
|
|
//获取某个职位的下级职位 锁数据 select ... for update
|
|
|
dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`
|
|
|
//更新关系树
|
|
|
// 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
|
|
|
)
|
|
|
if newparent.Id == 0 {
|
|
|
//修改节点为顶层节点的情况
|
|
|
newRelation = fmt.Sprintf("%d/", positionUpdate.Id)
|
|
|
} else {
|
|
|
newRelation = fmt.Sprintf("%s%d/", newparent.Relation, positionUpdate.Id)
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
//获取部门及子级部门的relation和id
|
|
|
err = utils.ExecuteQueryAllWithOrmer(o, &positionSubset, dataSql0, relationLike)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return errors.New("获取部门及子级部门失败")
|
|
|
}
|
|
|
for i := range positionSubset {
|
|
|
if positionSubset[i].Id == positionUpdate.Id {
|
|
|
positionSubset[i].ParentId = newparent.Id
|
|
|
}
|
|
|
//重建关系树
|
|
|
s := strings.TrimLeft(positionSubset[i].Relation, oldRelation)
|
|
|
positionSubset[i].Relation = fmt.Sprintf("%s%s", newRelation, s)
|
|
|
s1 := strings.Split(positionSubset[i].Relation, "/")
|
|
|
positionSubset[i].Level = len(s1) - 1
|
|
|
}
|
|
|
|
|
|
//修改部门及子级部门的relation
|
|
|
for i := range positionSubset {
|
|
|
err = models.UpdatePositionById(positionSubset[i], []string{"ParentId", "Relation"}, o)
|
|
|
if err != nil {
|
|
|
log.Error("更新position发生错误,bussiness_admin_id=%d,id=%d,err:%s", positionSubset[i].BusinessAdminId, positionSubset[i].Id, err)
|
|
|
o.Rollback()
|
|
|
return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", positionSubset[i].BusinessAdminId)
|
|
|
}
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func AddPosition(data ModulePositionData) error {
|
|
|
var (
|
...
|
...
|
@@ -157,21 +183,31 @@ func AddPosition(data ModulePositionData) error { |
|
|
log.Error("获取公司数据失败:s%", err)
|
|
|
return errors.New("无效公司")
|
|
|
}
|
|
|
|
|
|
positioninfo := &models.Position{
|
|
|
Id: data.Id,
|
|
|
Name: data.Name,
|
|
|
ParentId: data.ParentId,
|
|
|
CompanyId: companyinfo.Id,
|
|
|
Relation: data.Path, //TODO 格式转换
|
|
|
Level: data.Level,
|
|
|
}
|
|
|
var (
|
|
|
parentPosition *models.Position
|
|
|
)
|
|
|
if data.ParentId > 0 {
|
|
|
parentPosition, err = models.GetPositionByBusinessId(data.ParentId)
|
|
|
if err != nil {
|
|
|
log.Error("获取父级职位数据失败,business_admin_id=%d,err:%s", data.ParentId, err)
|
|
|
return errors.New("获取父级职位数据失败")
|
|
|
}
|
|
|
positioninfo.ParentId = parentPosition.Id
|
|
|
}
|
|
|
|
|
|
positioninfo.SetRelation(parentPosition)
|
|
|
s := strings.Split(positioninfo.Relation, "/")
|
|
|
positioninfo.Level = len(s) - 1
|
|
|
_, err = models.AddPosition(positioninfo)
|
|
|
if err != nil {
|
|
|
log.Error("添加职位失败:%s", err)
|
|
|
return errors.New("添加职位失败")
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
|
...
|
...
|
|