作者 唐旭辉

添加功能, 同步总平台的数据

... ... @@ -82,6 +82,7 @@ func UpdatePositionById(m *Position, col []string, om ...orm.Ormer) (err error)
if err = o.Read(&v); err == nil {
var num int64
m.UpdateAt = time.Now()
col = append(col, "UpdateAt")
if num, err = o.Update(m, col...); err == nil {
fmt.Println("Number of records updated in database:", num)
}
... ...
package platform
import "errors"
type PlatformAction interface {
DoAction(string) error
}
//从主管理平台接收数据数据并处理数据
type CommonProtocol struct {
Module string `json:"module"` //模块
Action string `json:"action"` //动作
Data string `json:"data"` //json数据
}
var actionmap = map[string]PlatformAction{
"department": ModuleDeparmentData{},
"position": ModulePositionData{},
"emplayee": ModuleEmployee{},
}
func NewPlatformAction(module string) (PlatformAction, error) {
if v, ok := actionmap[module]; ok {
return v, nil
}
return nil, errors.New("module cannot found")
}
... ...
package platform
import (
"errors"
)
// ModuleDeparmentData 主管理平台发送过来的数据
type ModuleDeparmentData struct {
Id int64 `json:"id"` //id
Name string `json:"name"` //部门名称
ParentId int64 `json:"parent_id"` //父级id
CompanyId int64 `json:"company_id"`
Manages []int64 `json:"manages"`
}
var _ PlatformAction = ModuleDeparmentData{}
func (m ModuleDeparmentData) DoAction(code string) error {
switch code {
case "edit":
return UpdateDepartmentData(m)
case "add":
return AddDepartmentData(m)
case "delete":
return DeleteDepartmentData(m)
default:
return errors.New("action not found")
}
}
//同步 部门数据
func UpdateDepartmentData(data ModuleDeparmentData) error {
return nil
}
func AddDepartmentData(data ModuleDeparmentData) error {
// var (
// companyinfo *models.Company
// err error
// )
// models.GetCompanyByUCenter(data.CompanyId)
return nil
}
func DeleteDepartmentData(data ModuleDeparmentData) error {
return nil
}
... ...
package platform
//同步 人员数据
//--------------------
type ModuleEmployee struct {
UCenterId int64 `json:"ucenter_id"` //同一用户中心的用户id
Nickname string `json:"nick_name"`
Companyid int64 `json:"company_id"` //
DepartmentId []int64 `json:"department_id"`
Position []int64 `json:"position"`
}
var _ PlatformAction = ModuleEmployee{}
func (m ModuleEmployee) DoAction(code string) error {
return nil
}
func UpdateEmployeeData(data ModuleEmployee) error {
return nil
}
func AddEmployeeData(data ModuleEmployee) error {
return nil
}
func DeleteEmployeeData(data ModuleEmployee) error {
return nil
}
... ...
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
}
... ...