作者 tangxvhui

总后台数据同步

... ... @@ -3,7 +3,7 @@ package platform
import "errors"
type PlatformAction interface {
DoAction(string) error
DoAction(string, []byte) error
}
//从主管理平台接收数据数据并处理数据
... ...
package platform
import (
"encoding/json"
"errors"
"fmt"
"oppmg/common/log"
"oppmg/models"
"time"
"github.com/astaxie/beego/orm"
)
// ModuleDeparmentData 主管理平台发送过来的数据
... ... @@ -13,73 +17,145 @@ type ModuleDeparmentData struct {
Name string `json:"name"` //部门名称
ParentId int64 `json:"parent_id"` //父级id
CompanyId int64 `json:"company_id"`
Relation string `json:"relation"`
Manages []int64 `json:"manages"`
}
var _ PlatformAction = ModuleDeparmentData{}
//DoAction PlatformAction 的接口实现
func (m ModuleDeparmentData) DoAction(code string) error {
func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error {
switch code {
case "edit":
return UpdateDepartmentData(m)
var (
data []ModuleDeparmentData
err error
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return UpdateDepartmentData(data)
case "add":
return AddDepartmentData(m)
var (
data []ModuleDeparmentData
err error
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return AddDepartmentData(data)
case "delete":
return DeleteDepartmentData(m)
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 DeleteDepartmentData(ids)
default:
return errors.New("action not found")
}
return nil
}
//同步 部门数据
func UpdateDepartmentData(data ModuleDeparmentData) error {
//UpdateDepartmentData ....
func UpdateDepartmentData(data []ModuleDeparmentData) error {
var (
departmentData *models.Department
err error
)
o := orm.NewOrm()
o.Begin()
for _, v := range data {
departmentData, err = models.GetDepartmentById(v.Id)
if err != nil {
e := fmt.Errorf("获取部门数据失败,err:%s", err)
log.Error(e.Error())
return errors.New("获取部门数据失败")
}
departmentData.Name = v.Name
departmentData.ParentId = v.ParentId
departmentData.Relation = v.Relation
if bt, err := json.Marshal(v.Manages); err == nil {
departmentData.Manages = string(bt)
}
err = models.UpdateDepartmentById(departmentData, []string{"Name", "Manage", "ParentId", "Relation"}, o)
if err != nil {
o.Rollback()
e := fmt.Errorf("更新部门数据失败,err:%s", err)
log.Error(e.Error())
return errors.New("更新部门数据失败")
}
}
o.Commit()
return nil
}
//AddDepartmentData ...
//TODO :部门管理员数据填充
func AddDepartmentData(data ModuleDeparmentData) error {
func AddDepartmentData(data []ModuleDeparmentData) error {
if len(data) == 0 {
return nil
}
var (
companyinfo *models.Company
err error
parentDepartment *models.Department
companyinfo *models.Company
err error
)
companyinfo, err = models.GetCompanyByUCenter(data.CompanyId)
companyinfo, err = models.GetCompanyByUCenter(data[0].CompanyId)
if err != nil {
e := fmt.Errorf("获取公司数据失败,err:%s", err)
log.Error(e.Error())
return errors.New("获取公司数据失败")
}
departmentData := &models.Department{
Id: data.Id,
CompanyId: companyinfo.Id,
Name: data.Name,
ParentId: data.ParentId,
Manages: "[]",
Relation: fmt.Sprintf("%d", data.Id),
}
if data.ParentId > 0 {
parentDepartment, err = models.GetDepartmentById(data.ParentId)
o := orm.NewOrm()
o.Begin()
var isRollback bool
for _, v := range data {
departmentData := &models.Department{
Id: v.Id,
CompanyId: companyinfo.Id,
Name: v.Name,
ParentId: v.ParentId,
Manages: "[]",
Relation: v.Relation, //TODO 格式转化
}
if bt, err := json.Marshal(v.Manages); err == nil {
departmentData.Manages = string(bt)
}
_, err = models.AddDepartment(departmentData, o)
if err != nil {
e := fmt.Errorf("获取父级部门数据失败,err:%s", err)
e := fmt.Errorf("存储部门数据失败,err:%s", err)
log.Error(e.Error())
return errors.New("获取父级部门数据失败")
isRollback = true
}
departmentData.Relation = fmt.Sprintf("%s/%d", parentDepartment.Relation, data.Id)
}
_, err = models.AddDepartment(departmentData)
if err != nil {
e := fmt.Errorf("存储部门数据失败,err:%s", err)
log.Error(e.Error())
if isRollback {
o.Rollback()
return errors.New("存储部门数据失败")
}
o.Commit()
return nil
}
func DeleteDepartmentData(data ModuleDeparmentData) error {
//DeleteDepartmentData ...
func DeleteDepartmentData(ids []int64) error {
o := orm.NewOrm()
_, err := o.QueryTable(&models.Department{}).
Filter("id__in", ids).
Update(orm.Params{
"delete_at": time.Now().Format("2006-01-02 15:04:05"),
})
if err != nil {
log.Error("更新position数据失败,err:%s", err)
return errors.New("删除职位数据失败")
}
return nil
}
... ...
package platform
import (
"encoding/json"
"errors"
"fmt"
"oppmg/common/log"
"oppmg/models"
"time"
"github.com/astaxie/beego/orm"
)
//同步 人员数据
//--------------------
type ModuleEmployee struct {
Userid int64 `json:"user_id"`
UCenterId int64 `json:"ucenter_id"` //同一用户中心的用户id
Phone string `json:"phone"`
Nickname string `json:"nick_name"`
Companyid int64 `json:"company_id"` //
Companyid int64 `json:"company_id"` //同意用户中心的company_id
DepartmentId []int64 `json:"department_id"`
Position []int64 `json:"position"`
}
... ... @@ -15,22 +28,122 @@ type ModuleEmployee struct {
var _ PlatformAction = ModuleEmployee{}
//DoAction PlatformAction 的接口实现
func (m ModuleEmployee) DoAction(code string) error {
func (m ModuleEmployee) DoAction(code string, jsondata []byte) error {
switch code {
case "edit":
// return UpdateDepartmentData(m)
case "add":
var (
data []ModuleEmployee
err error
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return AddEmployeeData(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 DeleteEmployeeData(ids)
default:
return errors.New("action not found")
}
return nil
}
// UpdateEmployeeData ...
func UpdateEmployeeData(data ModuleEmployee) error {
// AddEmployeeData ...
func AddEmployeeData(data []ModuleEmployee) error {
if len(data) == 0 {
return nil
}
var (
companydata *models.Company
err error
usercompanydata []*models.UserCompany
userdepartmentData []*models.UserDepartment
userpositionmentData []*models.UserPosition
)
companydata, err = models.GetCompanyByUCenter(data[0].Companyid)
if err != nil {
log.Error("GetCompanyByUCenter获取公司数据失败,ucenterId=%d,err:%s", data[0].Companyid, err)
return errors.New("获取公司数据失败")
}
for _, v := range data {
var (
userdata *models.User
)
userdata, err = models.GetUserByUCenter(v.UCenterId)
if err != nil && err != orm.ErrNoRows {
log.Error("获取用户数据失败,err:%s", err)
return errors.New("获取用户数据失败")
}
if err == orm.ErrNoRows {
//添加用户
userdata = &models.User{
UserCenterId: v.UCenterId,
NickName: v.Nickname,
Phone: v.Phone,
}
_, err = models.AddUser(userdata)
if err != nil {
log.Error("添加用户失败,err:%s", err)
return err
}
} else {
userdata.NickName = v.Nickname
userdata.Phone = v.Phone
models.UpdateUserById(userdata, []string{"NickName", "Phone"})
}
uc := &models.UserCompany{
Id: v.Userid,
CompanyId: companydata.Id,
UserId: userdata.Id,
NickName: v.Nickname,
}
usercompanydata = append(usercompanydata, uc)
}
o := orm.NewOrm()
o.Begin()
_, err = o.InsertMulti(20, &usercompanydata)
if err != nil {
log.Error("批量插入user_company数据失败,err:%s", err)
o.Rollback()
return err
}
//添加部门职位
o.Commit()
return nil
}
// AddEmployeeData ...
func AddEmployeeData(data ModuleEmployee) error {
// UpdateEmployeeData ...
func UpdateEmployeeData(data []ModuleEmployee) error {
return nil
}
// DeleteEmployeeData ...
func DeleteEmployeeData(data ModuleEmployee) error {
func DeleteEmployeeData(ids []int64) error {
o := orm.NewOrm()
_, err := o.QueryTable(&models.UserCompany{}).
Filter("id__in", ids).
Update(orm.Params{
"delete_at": time.Now().String(),
})
if err != nil {
log.Error("更新user_company数据失败:%s", err)
return errors.New("删除user_company数据失败")
}
return nil
}
... ...
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
// }
... ...