|
|
package platform
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/utils"
|
|
|
"strings"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
)
|
|
|
|
|
|
type ModulePositionData struct {
|
|
|
Id int64 `json:"id"`
|
|
|
Name string `json:"nick_name"`
|
|
|
ParentId int64 `json:"parent_id"`
|
|
|
CompanyId int64 `json:"company_id"`
|
|
|
Status string `json:"status"`
|
|
|
}
|
|
|
|
|
|
var _ PlatformAction = ModulePositionData{}
|
|
|
|
|
|
func (m ModulePositionData) DoAction(code string) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//同步职位数据
|
|
|
func UpdatePosition(data ModulePositionData) error {
|
|
|
var (
|
|
|
positioninfo *models.Position
|
|
|
err error
|
|
|
parentPosition *models.Position
|
|
|
companyinfo *models.Company
|
|
|
)
|
|
|
positioninfo, err = models.GetPositionById(data.Id)
|
|
|
if err != nil {
|
|
|
log.Error("获取职位数据失败:%s", err)
|
|
|
return errors.New("获取职位数据失败")
|
|
|
}
|
|
|
companyinfo, err = models.GetCompanyByUCenter(data.CompanyId)
|
|
|
if err != nil {
|
|
|
log.Error("获取获取公司数据失败;%s", err)
|
|
|
return errors.New("获取获取公司数据失败")
|
|
|
}
|
|
|
//检查上级
|
|
|
if data.ParentId > 0 {
|
|
|
parentPosition, err = models.GetPositionById(data.ParentId)
|
|
|
if err != nil {
|
|
|
log.Error("获取父级职位数据失败;%s", err)
|
|
|
return errors.New("获取父级职位数据失败;")
|
|
|
}
|
|
|
if parentPosition.CompanyId == companyinfo.Id {
|
|
|
log.Error("父级职位公司不匹配")
|
|
|
return errors.New("父级职位公司不匹配")
|
|
|
}
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
positioninfo.Name = data.Name
|
|
|
err = models.UpdatePositionById(positioninfo, []string{"Name", "UpdateAt"}, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
return err
|
|
|
}
|
|
|
err = positionRelationUpdate(positioninfo, parentPosition, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
return err
|
|
|
}
|
|
|
o.Commit()
|
|
|
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
|
|
|
}
|
|
|
|
|
|
func AddPosition(data ModulePositionData) error {
|
|
|
var (
|
|
|
companyinfo *models.Company
|
|
|
err error
|
|
|
parentPosition *models.Position
|
|
|
)
|
|
|
companyinfo, err = models.GetCompanyByUCenter(data.CompanyId)
|
|
|
if err != nil {
|
|
|
log.Error("获取公司数据失败:s%", err)
|
|
|
return errors.New("无效公司")
|
|
|
}
|
|
|
if data.ParentId > 0 {
|
|
|
parentPosition, err = models.GetPositionById(data.ParentId)
|
|
|
if err != nil {
|
|
|
log.Error("获取父级职位失败:%s", err)
|
|
|
return errors.New("获取父级职位失败")
|
|
|
}
|
|
|
} else {
|
|
|
parentPosition = &models.Position{}
|
|
|
}
|
|
|
|
|
|
positioninfo := &models.Position{
|
|
|
Id: data.Id,
|
|
|
Name: data.Name,
|
|
|
ParentId: data.ParentId,
|
|
|
CompanyId: companyinfo.Id,
|
|
|
}
|
|
|
|
|
|
positioninfo.SetRelation(parentPosition)
|
|
|
_, err = models.AddPosition(positioninfo)
|
|
|
if err != nil {
|
|
|
log.Error("添加职位失败:%s", err)
|
|
|
return errors.New("添加职位失败")
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
func DeletePosition(data ModulePositionData) error {
|
|
|
|
|
|
return nil
|
|
|
} |
...
|
...
|
|