作者 tangxvhui

数据同步功能调整

... ... @@ -12,18 +12,18 @@ import (
)
type Department struct {
Id int64 `orm:"column(id);auto"`
CompanyId int64 `orm:"column(company_id)" description:"公司id"`
Name string `orm:"column(name);size(30)" description:"部门名称"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
ParentId int64 `orm:"column(parent_id)" description:"父级id"`
Relation string `orm:"column(relation);size(1024)" description:"父子级关系树"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
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)"`
Id int64 `orm:"column(id);auto"`
CompanyId int64 `orm:"column(company_id)" description:"公司id"`
Name string `orm:"column(name);size(30)" description:"部门名称"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
ParentId int64 `orm:"column(parent_id)" description:"父级id"`
Relation string `orm:"column(relation);size(1024)" description:"父子级关系树"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
Manages string `orm:"column(managers)" description:"部门负责人id列表 json 数组 []"` //存user_company_id
IsTop int8 `orm:"column(is_top)" `
Level int `orm:"column(level)" `
BusinessDepartmentId int64 `orm:"column(business_department_id)"`
}
func (t *Department) TableName() string {
... ... @@ -224,7 +224,7 @@ func GetDepartmentByBusinessId(businessId int64) (v *Department, err error) {
v = &Department{}
o := orm.NewOrm()
err = o.QueryTable(&Department{}).
Filter("business_admin_id", businessId).
Filter("business_department_id", businessId).
Filter("delete_at", 0).
One(v)
return v, err
... ...
package models
import (
"fmt"
"oppmg/utils"
"time"
"github.com/astaxie/beego/orm"
)
type DepartmentCharge struct {
Id int64 `orm:"column(id);auto"`
CompanyId int64 `orm:"column(company_id)" description:"公司id"`
DepartmentId int64 `orm:"column(department_id)" description:"部门id"`
UserCompanyId int64 `orm:"column(user_company_id)" description:"用户id"`
CreateTime time.Time `orm:"column(create_time);type(timestamp)"`
Enabled int8 `orm:"column(enabled)"`
}
func (t *DepartmentCharge) TableName() string {
return "department_charge"
}
func init() {
orm.RegisterModel(new(DepartmentCharge))
}
const (
DEPARTMENT_CHARGE_ENABLE_YES int8 = 1
DEPARTMENT_CHARGE_ENABLE_NO int8 = 2
)
// AddDepartmentCharge insert a new DepartmentCharge into database and returns
// last inserted Id on success.
func AddDepartmentCharge(m *DepartmentCharge) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// UpdateDepartmentCharge updates DepartmentCharge by Id and returns error if
// the record to be updated doesn't exist
func UpdateDepartmentChargeById(m *DepartmentCharge) (err error) {
o := orm.NewOrm()
v := DepartmentCharge{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
func GetDepartmentCharge(departmentId int64) ([]DepartmentCharge, error) {
var (
data []DepartmentCharge
err error
)
o := orm.NewOrm()
_, err = o.QueryTable(&DepartmentCharge{}).
Filter("department_id", departmentId).
Filter("enabled", 1).
All(&data)
if err == orm.ErrNoRows {
return data, nil
}
return data, err
}
//ChangeDepartmentCharge 变更部门的主管
func ChangeDepartmentCharge(companyId int64, departmentId int64, userCompanyid []int64, om orm.Ormer) error {
var (
err error
charges []DepartmentCharge
oldChargeIds []int64
)
charges, err = GetDepartmentCharge(departmentId)
if err != nil {
return fmt.Errorf("获取部门主管数据失败:%s", err)
}
for _, v := range charges {
oldChargeIds = append(oldChargeIds, v.Id)
}
var (
delIds []int64
addIds []int64
addCharges []DepartmentCharge
)
delIds = utils.ArrayInt64Diff(oldChargeIds, userCompanyid)
addIds = utils.ArrayInt64Diff(userCompanyid, oldChargeIds)
nowTime := time.Now()
for _, v := range addIds {
m := DepartmentCharge{
CompanyId: companyId,
UserCompanyId: v,
DepartmentId: departmentId,
Enabled: DEPARTMENT_CHARGE_ENABLE_YES,
CreateTime: nowTime,
}
addCharges = append(addCharges, m)
}
if len(delIds) > 0 {
_, err = om.QueryTable(&DepartmentCharge{}).
Filter("department_id", departmentId).
Filter("user_company_id__in", delIds).
Update(orm.Params{
"enable": DEPARTMENT_CHARGE_ENABLE_NO,
})
if err != nil {
return fmt.Errorf("更新department_charge数据失败,err:%s", err)
}
}
if len(addCharges) > 0 {
_, err = om.InsertMulti(10, addCharges)
if err != nil {
return fmt.Errorf("添加department_charge数据失败,err:%s", err)
}
}
return nil
}
... ...
... ... @@ -8,17 +8,17 @@ import (
)
type Position struct {
Id int64 `orm:"column(id);auto" description:"职位表id"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
Name string `orm:"column(name);size(100)" description:"职位名称"`
ParentId int64 `orm:"column(parent_id)" description:"父级id"`
Relation string `orm:"column(relation);size(1000)" description:"父子级关系树"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
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 int64 `orm:"column(id);auto" description:"职位表id"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
Name string `orm:"column(name);size(100)" description:"职位名称"`
ParentId int64 `orm:"column(parent_id)" description:"父级id"`
Relation string `orm:"column(relation);size(1000)" description:"父子级关系树"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
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)"`
BusinessPositionId int64 `orm:"column(business_position_id)"`
}
//关联企业总后台的id
... ... @@ -107,7 +107,7 @@ func GetPositionByBusinessId(businessId int64) (v *Position, err error) {
o := orm.NewOrm()
v = &Position{}
err = o.QueryTable(&Position{}).
Filter("business_admin_id", businessId).
Filter("business_position_id", businessId).
Filter("delete_at", 0).
One(v)
return v, err
... ...
package platform
import "errors"
import (
"encoding/json"
"errors"
"fmt"
"oppmg/common/log"
"oppmg/models"
"github.com/astaxie/beego/orm"
)
// 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"`
Id int64 `json:"id"` //id
Name string `json:"name"` //公司名称名称
AdminId int64 `json:"admin_id"` //主管理员id
Logo string `json:"logo"`
Charge []int64 `json:"charge"`
}
var _ PlatformAction = ModuleCompanytData{}
... ... @@ -20,9 +24,70 @@ var _ PlatformAction = ModuleCompanytData{}
func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error {
switch code {
case "edit":
var (
data ModuleCompanytData
err error
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return UpdateCompanyData(data)
case "setCompanyCharge":
var (
data ModuleCompanytData
err error
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return SetCompanyCharge(data)
default:
return errors.New("action not found")
}
// return nil
}
// SetCompanyCharge 更新公司那一级的部门的管理员
func SetCompanyCharge(data ModuleCompanytData) error {
var (
err error
companyData *models.Company
departmentData *models.Department
)
companyData, err = models.GetCompanyByUCenter(data.Id)
if err != nil {
log.Error("获取公司数据失败,user_center_id=%d,err:%s", data.Id, err)
return errors.New("获取公司数据失败")
}
departmentData, err = models.GetTopDepartmentByCompany(companyData.Id)
if err != nil {
log.Error("获取公司一级部门数据失败,company_id=%d, err:%s", companyData.Id, err)
return errors.New("获取公司一级部门数据失败")
}
// 获取公司管理员
o := orm.NewOrm()
o.Begin()
err = models.ChangeDepartmentCharge(data.Id, departmentData.Id, data.Charge, o)
if err != nil {
o.Rollback()
log.Error("变更公司管理员失败,err:%s", err)
return errors.New("变更公司管理员失败")
}
//更新department数据
bt, _ := json.Marshal(data.Charge)
departmentData.Manages = string(bt)
err = models.UpdateDepartmentById(departmentData, []string{"Manages"}, o)
if err != nil {
o.Rollback()
log.Error("变更公司管理员失败,err:%s", err)
return errors.New("变更公司管理员失败")
}
o.Commit()
return nil
}
func UpdateCompanyData(data ModuleCompanytData) error {
return nil
}
... ...
... ... @@ -15,12 +15,13 @@ import (
// ModuleDeparmentData 主管理平台发送过来的数据
type ModuleDeparmentData struct {
Id int64 `json:"id"` //id
Name string `json:"name"` //部门名称
ParentId int64 `json:"parent_id"` //父级id
CompanyId int64 `json:"company_id"`
Path string `json:"path"`
Level int `json:"level"`
Id int64 `json:"id"` //id
Name string `json:"name"` //部门名称
ParentId int64 `json:"parent_id"` //父级id
CompanyId int64 `json:"company_id"`
Path string `json:"path"`
Level int `json:"level"`
Charge []int64 `json:"charge"` //部门主管
}
var _ PlatformAction = ModuleDeparmentData{}
... ... @@ -163,9 +164,9 @@ func departmentRelationUpdate(departmentUpdate models.Department, newparent mode
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)
log.Error("更新position发生错误,bussiness_admin_id=%d,id=%d,err:%s", departmentSubset[i].BusinessDepartmentId, departmentSubset[i].Id, err)
o.Rollback()
return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", departmentSubset[i].BusinessAdminId)
return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", departmentSubset[i].BusinessDepartmentId)
}
}
o.Commit()
... ...
... ... @@ -164,9 +164,9 @@ func positionRelationUpdate(positionUpdate models.Position, newparent models.Pos
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)
log.Error("更新position发生错误,bussiness_admin_id=%d,id=%d,err:%s", positionSubset[i].BusinessPositionId, positionSubset[i].Id, err)
o.Rollback()
return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", positionSubset[i].BusinessAdminId)
return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", positionSubset[i].BusinessPositionId)
}
}
o.Commit()
... ...