|
|
package platform
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/utils"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"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"`
|
|
|
Ids []int64 `json:"ids"`
|
|
|
Id int64 `json:"id"`
|
|
|
Name string `json:"nick_name"`
|
|
|
ParentId int64 `json:"parent_id"`
|
|
|
CompanyId int64 `json:"company_id"`
|
|
|
Relation string `json:"relation"`
|
|
|
}
|
|
|
|
|
|
var _ PlatformAction = ModulePositionData{}
|
|
|
|
|
|
//DoAction PlatformAction 的接口实现
|
|
|
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)
|
|
|
func (m ModulePositionData) DoAction(code string, jsondata []byte) error {
|
|
|
switch code {
|
|
|
case "edit":
|
|
|
var (
|
|
|
err error
|
|
|
data []ModulePositionData
|
|
|
)
|
|
|
err = json.Unmarshal(jsondata, &data)
|
|
|
if err != nil {
|
|
|
log.Error("获取父级职位数据失败;%s", err)
|
|
|
return errors.New("获取父级职位数据失败;")
|
|
|
return fmt.Errorf("数据解析失败:%s", err)
|
|
|
}
|
|
|
if parentPosition.CompanyId == companyinfo.Id {
|
|
|
log.Error("父级职位公司不匹配")
|
|
|
return errors.New("父级职位公司不匹配")
|
|
|
return UpdatePosition(data)
|
|
|
case "add":
|
|
|
var (
|
|
|
data []ModulePositionData
|
|
|
err error
|
|
|
)
|
|
|
err = json.Unmarshal(jsondata, &data)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("数据解析失败:%s", err)
|
|
|
}
|
|
|
return AddPosition(data)
|
|
|
case "delete":
|
|
|
var (
|
|
|
err error
|
|
|
ids []int64
|
|
|
)
|
|
|
err = json.Unmarshal(jsondata, &ids)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("数据解析失败:%s", err)
|
|
|
}
|
|
|
if len(ids) == 0 {
|
|
|
return fmt.Errorf("参数错误")
|
|
|
}
|
|
|
|
|
|
return DeletePosition(ids)
|
|
|
default:
|
|
|
return errors.New("action not found")
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
positioninfo.Name = data.Name
|
|
|
err = models.UpdatePositionById(positioninfo, []string{"Name", "UpdateAt"}, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func (m ModulePositionData) validate() error {
|
|
|
if m.Id <= 0 {
|
|
|
return errors.New("错误:id<=0 ")
|
|
|
}
|
|
|
err = positionRelationUpdate(positioninfo, parentPosition, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
return err
|
|
|
if len(m.Name) == 0 {
|
|
|
return errors.New("错误:name长度为0")
|
|
|
}
|
|
|
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("更新数据失败")
|
|
|
//同步职位数据
|
|
|
func UpdatePosition(data []ModulePositionData) error {
|
|
|
if len(data) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
//修改部门及子级部门的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)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
for _, v := range data {
|
|
|
positioninfo, err := models.GetPositionById(v.Id)
|
|
|
if err != nil {
|
|
|
log.Error("获取职位数据失败:%s", err)
|
|
|
return fmt.Errorf("获取职位数据失败,Id=%d", v.Id)
|
|
|
}
|
|
|
positioninfo.Name = v.Name
|
|
|
positioninfo.ParentId = v.ParentId
|
|
|
positioninfo.Relation = v.Relation
|
|
|
err = models.UpdatePositionById(positioninfo, []string{"Name", "ParentId", "Relation"}, o)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return errors.New("更新数据失败")
|
|
|
o.Rollback()
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func AddPosition(data ModulePositionData) error {
|
|
|
//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 {
|
|
|
if len(data) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
var (
|
|
|
companyinfo *models.Company
|
|
|
err error
|
|
|
parentPosition *models.Position
|
|
|
companyinfo *models.Company
|
|
|
err error
|
|
|
isRollback bool
|
|
|
)
|
|
|
companyinfo, err = models.GetCompanyByUCenter(data.CompanyId)
|
|
|
companyinfo, err = models.GetCompanyByUCenter(data[0].CompanyId)
|
|
|
if err != nil {
|
|
|
log.Error("获取公司数据失败:s%", err)
|
|
|
return errors.New("无效公司")
|
|
|
}
|
|
|
if data.ParentId > 0 {
|
|
|
parentPosition, err = models.GetPositionById(data.ParentId)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
for _, v := range data {
|
|
|
positioninfo := &models.Position{
|
|
|
Id: v.Id,
|
|
|
Name: v.Name,
|
|
|
ParentId: v.ParentId,
|
|
|
CompanyId: companyinfo.Id,
|
|
|
Relation: v.Relation, //TODO 格式转换
|
|
|
}
|
|
|
_, err = models.AddPosition(positioninfo)
|
|
|
if err != nil {
|
|
|
log.Error("获取父级职位失败:%s", err)
|
|
|
return errors.New("获取父级职位失败")
|
|
|
log.Error("添加职位失败:%s", err)
|
|
|
isRollback = true
|
|
|
break
|
|
|
}
|
|
|
} 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)
|
|
|
if isRollback {
|
|
|
o.Rollback()
|
|
|
return errors.New("添加职位失败")
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func DeletePosition(ids []int64, ucompanyid int64) error {
|
|
|
//检查是否可以被删除
|
|
|
var (
|
|
|
//根据参数获取的职位
|
|
|
positionDelete []*models.Position
|
|
|
//最终需要操作的职位
|
|
|
toDelete = make(map[int64]*models.Position)
|
|
|
companyinfo *models.Company
|
|
|
err error
|
|
|
)
|
|
|
const (
|
|
|
//获取部门子集,
|
|
|
dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 `
|
|
|
)
|
|
|
companyinfo, err = models.GetCompanyByUCenter(ucompanyid)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("获取公司数据失败")
|
|
|
log.Error(e.Error())
|
|
|
return e
|
|
|
}
|
|
|
for _, id := range ids {
|
|
|
var p *models.Position
|
|
|
p, err := models.GetPositionById(id)
|
|
|
if err != nil {
|
|
|
log.Error("获取职位失败Id:%d,err:%s", id, err)
|
|
|
continue
|
|
|
}
|
|
|
if p.CompanyId != companyinfo.Id {
|
|
|
log.Error("公司id不匹配")
|
|
|
return errors.New("公司id不匹配")
|
|
|
}
|
|
|
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 e
|
|
|
}
|
|
|
for k, subset := range positionsubset {
|
|
|
if _, ok := toDelete[subset.Id]; !ok {
|
|
|
toDelete[subset.Id] = &positionsubset[k]
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
var (
|
|
|
deleteIds []string
|
|
|
dataSql2 string = `update position set delete_at=%s where id IN (%s)`
|
|
|
nowTime string = time.Now().Format("2006-01-02 15:04:05")
|
|
|
)
|
|
|
for k, _ := range toDelete {
|
|
|
deleteIds = append(deleteIds, fmt.Sprint(k))
|
|
|
}
|
|
|
if len(deleteIds) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
dataSql2 = fmt.Sprintf(dataSql2, nowTime, strings.Join(deleteIds, ","))
|
|
|
// DeletePosition ...
|
|
|
func DeletePosition(ids []int64) error {
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2)
|
|
|
_, err := o.QueryTable(&models.Position{}).
|
|
|
Filter("id__in", ids).
|
|
|
Update(orm.Params{
|
|
|
"delete_at": time.Now().Format("2006-01-02 15:04:05"),
|
|
|
})
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("EXCUTE SQL ERR:%s", err)
|
|
|
return e
|
|
|
log.Error("更新position数据失败,err:%s", err)
|
|
|
return errors.New("删除职位数据失败")
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// func DeletePosition(ids []int64, ucompanyid int64) error {
|
|
|
// //检查是否可以被删除
|
|
|
// var (
|
|
|
// //根据参数获取的职位
|
|
|
// positionDelete []*models.Position
|
|
|
// //最终需要操作的职位
|
|
|
// toDelete = make(map[int64]*models.Position)
|
|
|
// companyinfo *models.Company
|
|
|
// err error
|
|
|
// )
|
|
|
// const (
|
|
|
// //获取部门子集,
|
|
|
// dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 `
|
|
|
// )
|
|
|
// companyinfo, err = models.GetCompanyByUCenter(ucompanyid)
|
|
|
// if err != nil {
|
|
|
// e := fmt.Errorf("获取公司数据失败")
|
|
|
// log.Error(e.Error())
|
|
|
// return e
|
|
|
// }
|
|
|
// for _, id := range ids {
|
|
|
// var p *models.Position
|
|
|
// p, err := models.GetPositionById(id)
|
|
|
// if err != nil {
|
|
|
// log.Error("获取职位失败Id:%d,err:%s", id, err)
|
|
|
// continue
|
|
|
// }
|
|
|
// if p.CompanyId != companyinfo.Id {
|
|
|
// log.Error("公司id不匹配")
|
|
|
// return errors.New("公司id不匹配")
|
|
|
// }
|
|
|
// 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 e
|
|
|
// }
|
|
|
// for k, subset := range positionsubset {
|
|
|
// if _, ok := toDelete[subset.Id]; !ok {
|
|
|
// toDelete[subset.Id] = &positionsubset[k]
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// var (
|
|
|
// deleteIds []string
|
|
|
// dataSql2 string = `update position set delete_at=%s where id IN (%s)`
|
|
|
// nowTime string = time.Now().Format("2006-01-02 15:04:05")
|
|
|
// )
|
|
|
// for k, _ := range toDelete {
|
|
|
// deleteIds = append(deleteIds, fmt.Sprint(k))
|
|
|
// }
|
|
|
// if len(deleteIds) == 0 {
|
|
|
// return nil
|
|
|
// }
|
|
|
// dataSql2 = fmt.Sprintf(dataSql2, nowTime, strings.Join(deleteIds, ","))
|
|
|
// o := orm.NewOrm()
|
|
|
// o.Begin()
|
|
|
// err = utils.ExecuteSQLWithOrmer(o, dataSql2)
|
|
|
// if err != nil {
|
|
|
// e := fmt.Errorf("EXCUTE SQL ERR:%s", err)
|
|
|
// return e
|
|
|
// }
|
|
|
// o.Commit()
|
|
|
// return nil
|
|
|
// } |
...
|
...
|
|