作者 tangxvhui

数据同步调整

... ... @@ -12,6 +12,7 @@ type PlatformController struct {
BaseController
}
//
func (c PlatformController) UpdateData() {
var msg *protocol.ResponseMessage
defer func() {
... ... @@ -23,13 +24,13 @@ func (c PlatformController) UpdateData() {
msg = protocol.BadRequestParam("1")
return
}
m, err := platform.NewPlatformAction(param.Module)
if err != nil {
msg = protocol.NewReturnResponse(nil, err)
return
}
err = m.DoAction(param.Action, []byte(param.Data))
msg = protocol.NewReturnResponse(nil, err)
// m, err := platform.NewPlatformAction(param.Module)
// if err != nil {
// msg = protocol.NewReturnResponse(nil, err)
// return
// }
// err = m.DoAction(param.Action, []byte(param.Data))
msg = protocol.NewMessage("0")
return
}
... ...
... ... @@ -23,6 +23,7 @@ type Department struct {
Manages string `orm:"column(managers)" description:"部门负责人id列表 json 数组 []"` //存user_company_id
IsTop int8 `orm:"column(is_top)" `
Level int `orm:"column(level)" `
BusinessAdminId int64 `orm:"column(business_admin_id)"`
}
func (t *Department) TableName() string {
... ... @@ -56,9 +57,11 @@ func (t *Department) SetRelation(parent *Department) error {
return errors.New("Id==0")
}
if parent == nil {
t.Relation = fmt.Sprintf("%d", t.Id)
t.Relation = fmt.Sprintf("%d/", t.Id)
} else if parent.Id == 0 {
t.Relation = fmt.Sprintf("%d/", t.Id)
} else {
t.Relation = fmt.Sprintf("%s/%d", parent.Relation, t.Id)
t.Relation = fmt.Sprintf("%s%d/", parent.Relation, t.Id)
}
return nil
}
... ... @@ -216,3 +219,25 @@ func ExistDepartmentName(parentId int64, dname string) bool {
Exist()
return ok
}
func GetDepartmentByBusinessId(businessId int64) (v *Department, err error) {
v = &Department{}
o := orm.NewOrm()
err = o.QueryTable(&Department{}).
Filter("business_admin_id", businessId).
Filter("delete_at", 0).
One(v)
return v, err
}
func GetTopDepartmentByCompany(companyid int64) (v *Department, err error) {
v = &Department{}
o := orm.NewOrm()
err = o.QueryTable(&Department{}).
Filter("company_id", companyid).
Filter("is_top", 1).
Filter("delete_at", 0).
One(v)
return v, err
}
... ...
... ... @@ -18,6 +18,7 @@ type Position struct {
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
EnableStatus string `orm:"column(enable_status);size(255)" description:"有效状态 1:有效 0:无效"`
Level int `orm:"column(level)"`
BusinessAdminId int64 `orm:"column(business_admin_id)"`
}
//关联企业总后台的id
... ... @@ -30,13 +31,10 @@ func init() {
}
func (t *Position) SetRelation(parent *Position) error {
// if t.Id == 0 {
// return errors.New("Id==0")
// }
if parent == nil || t.Id == 0 {
t.Relation = fmt.Sprintf("%d", t.Id)
if parent == nil {
t.Relation = fmt.Sprintf("%d/", t.Id)
} else {
t.Relation = fmt.Sprintf("%s/%d", parent.Relation, t.Id)
t.Relation = fmt.Sprintf("%s%d/", parent.Relation, t.Id)
}
return nil
}
... ... @@ -104,3 +102,13 @@ func ExistPositiontName(companyid int64, parentId int64, dname string) bool {
Exist()
return ok
}
func GetPositionByBusinessId(businessId int64) (v *Position, err error) {
o := orm.NewOrm()
v = &Position{}
err = o.QueryTable(&Position{}).
Filter("business_admin_id", businessId).
Filter("delete_at", 0).
One(v)
return v, err
}
... ...
... ... @@ -11,7 +11,7 @@ import (
func init() {
nsPlatform := beego.NewNamespace("/platform",
beego.NSBefore(middleware.LogRequestData),
beego.NSRouter("/update", &controllers.PlatformController{}, "post:UpdateData"),
beego.NSRouter("/action", &controllers.PlatformController{}, "post:UpdateData"),
beego.NSRouter("/admins_change", &controllers.PlatformController{}, "post:CompanyAdminChance"),
)
beego.AddNamespace(nsPlatform)
... ...
package platform
import "errors"
// ModuleCompanytData 主管理平台发送过来的数据
type ModuleCompanytData struct {
Id int64 `json:"id"` //id
Name string `json:"name"` //公司名称名称
AdminId int64 `json:"admin_id"` //主管理员id
Logo string `json:"logo"`
}
type DepartmentChargeData struct {
Id int64 `json:"id"`
Charge []int64 `json:"charge"`
}
var _ PlatformAction = ModuleCompanytData{}
func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error {
switch code {
case "edit":
case "setCompanyCharge":
default:
return errors.New("action not found")
}
return nil
}
... ...
... ... @@ -6,6 +6,8 @@ import (
"fmt"
"oppmg/common/log"
"oppmg/models"
"oppmg/utils"
"strings"
"time"
"github.com/astaxie/beego/orm"
... ... @@ -46,27 +48,28 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error {
return fmt.Errorf("数据解析失败:%s", err)
}
return AddDepartmentData(data)
case "delete":
case "batchDelete":
var (
err error
ids []int64
)
ids := struct {
Ids []int64 `json:"ids"`
}{}
err = json.Unmarshal(jsondata, &ids)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
if len(ids) == 0 {
if len(ids.Ids) == 0 {
return fmt.Errorf("参数错误")
}
return DeleteDepartmentData(ids)
return DeleteDepartmentData(ids.Ids)
default:
return errors.New("action not found")
}
return nil
// return nil
}
//同步 部门数据
//UpdateDepartmentData ....
func UpdateDepartmentData(data ModuleDeparmentData) error {
var (
... ... @@ -81,21 +84,94 @@ func UpdateDepartmentData(data ModuleDeparmentData) error {
return errors.New("获取部门数据失败")
}
departmentData.Name = data.Name
departmentData.ParentId = data.ParentId
departmentData.Relation = data.Path
departmentData.Level = data.Level
err = models.UpdateDepartmentById(departmentData, []string{"Name", "Manage", "ParentId", "Relation"})
err = models.UpdateDepartmentById(departmentData, []string{"Name"})
if err != nil {
e := fmt.Errorf("更新部门数据失败,err:%s", err)
log.Error(e.Error())
return errors.New("更新部门数据失败")
}
var (
newParentDepart *models.Department
)
if data.ParentId == 0 {
newParentDepart, err = models.GetTopDepartmentByCompany(departmentData.CompanyId)
if err != nil {
log.Error("获取公司一级部门数据失败:company_id=%d,err:%s", departmentData.CompanyId, err)
return errors.New("获取公司一级部门数据失败")
}
} else {
newParentDepart, err = models.GetDepartmentByBusinessId(data.ParentId)
if err != nil {
log.Error("获取父级数据失败,business_admin_id=%d,err:%s", data.ParentId, err)
}
}
if departmentData.ParentId != newParentDepart.Id {
//更新父级
err = departmentRelationUpdate(*departmentData, *newParentDepart)
if err != nil {
return err
}
}
return nil
}
//positionRelationUpdate 处理职位上级发生变化的情况
func departmentRelationUpdate(departmentUpdate models.Department, newparent models.Department) error {
const (
//获取某个职位的下级职位 锁数据 select ... for update
dataSql0 string = `SELECT id,relation FROM department WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`
//更新关系树
//dataSql2 string = `update department set relation=? where id=?`
//更新departUpdate的parent_id
//dataSql3 string = `update department set parent_id=? where id=?`
)
var (
departmentSubset []*models.Department //子级部门
err error
oldRelation string = departmentUpdate.Relation
relationLike string = oldRelation + "%"
newRelation string
)
if newparent.Id == 0 {
//修改节点为顶层节点的情况
newRelation = fmt.Sprintf("%d/", departmentUpdate.Id)
} else {
newRelation = fmt.Sprintf("%s%d/", newparent.Relation, departmentUpdate.Id)
}
o := orm.NewOrm()
o.Begin()
//获取部门及子级部门的relation和id
err = utils.ExecuteQueryAllWithOrmer(o, &departmentSubset, dataSql0, relationLike)
if err != nil {
e := fmt.Errorf("EXECUTE SQL err:%s", err)
log.Error(e.Error())
return errors.New("获取部门及子级部门失败")
}
for i := range departmentSubset {
if departmentSubset[i].Id == departmentUpdate.Id {
departmentSubset[i].ParentId = newparent.Id
}
//重建关系树
s := strings.TrimLeft(departmentSubset[i].Relation, oldRelation)
departmentSubset[i].Relation = fmt.Sprintf("%s%s", newRelation, s)
s1 := strings.Split(departmentSubset[i].Relation, "/")
departmentSubset[i].Level = len(s1) - 1
}
//修改部门及子级部门的relation
for i := range departmentSubset {
err = models.UpdateDepartmentById(departmentSubset[i], []string{"ParentId", "Relation"}, o)
if err != nil {
log.Error("更新position发生错误,bussiness_admin_id=%d,id=%d,err:%s", departmentSubset[i].BusinessAdminId, departmentSubset[i].Id, err)
o.Rollback()
return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", departmentSubset[i].BusinessAdminId)
}
}
o.Commit()
return nil
}
//AddDepartmentData ...
func AddDepartmentData(data ModuleDeparmentData) error {
var (
... ... @@ -108,22 +184,37 @@ func AddDepartmentData(data ModuleDeparmentData) error {
log.Error(e.Error())
return errors.New("获取公司数据失败")
}
departmentData := &models.Department{
Id: data.Id,
CompanyId: companyinfo.Id,
Name: data.Name,
ParentId: data.ParentId,
Manages: "[]",
Relation: data.Path, //TODO 格式转化
Level: data.Level,
}
var (
parentDepart *models.Department
)
if data.ParentId > 0 {
parentDepart, err = models.GetDepartmentByBusinessId(data.ParentId)
if err != nil {
log.Error("获取父级职位数据失败,business_admin_id=%d,err:%s", data.ParentId, err)
return errors.New("获取父级职位数据失败")
}
departmentData.ParentId = parentDepart.Id
}
if data.ParentId == 0 {
parentDepart, err = models.GetTopDepartmentByCompany(companyinfo.Id)
if err != nil {
log.Error("获取公司一级部门数据失败:company_id=%d,err:%s", companyinfo.Id, err)
return errors.New("获取公司一级部门数据失败")
}
departmentData.ParentId = parentDepart.Id
}
departmentData.SetRelation(parentDepart)
_, err = models.AddDepartment(departmentData)
if err != nil {
e := fmt.Errorf("存储部门数据失败,err:%s", err)
log.Error(e.Error())
}
return nil
... ...
... ... @@ -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
}
... ...