作者 tangxvhui

数据同步功能调整

@@ -12,18 +12,18 @@ import ( @@ -12,18 +12,18 @@ import (
12 ) 12 )
13 13
14 type Department struct { 14 type Department struct {
15 - Id int64 `orm:"column(id);auto"`  
16 - CompanyId int64 `orm:"column(company_id)" description:"公司id"`  
17 - Name string `orm:"column(name);size(30)" description:"部门名称"`  
18 - CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`  
19 - ParentId int64 `orm:"column(parent_id)" description:"父级id"`  
20 - Relation string `orm:"column(relation);size(1024)" description:"父子级关系树"`  
21 - DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`  
22 - UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`  
23 - Manages string `orm:"column(managers)" description:"部门负责人id列表 json 数组 []"` //存user_company_id  
24 - IsTop int8 `orm:"column(is_top)" `  
25 - Level int `orm:"column(level)" `  
26 - BusinessAdminId int64 `orm:"column(business_admin_id)"` 15 + Id int64 `orm:"column(id);auto"`
  16 + CompanyId int64 `orm:"column(company_id)" description:"公司id"`
  17 + Name string `orm:"column(name);size(30)" description:"部门名称"`
  18 + CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
  19 + ParentId int64 `orm:"column(parent_id)" description:"父级id"`
  20 + Relation string `orm:"column(relation);size(1024)" description:"父子级关系树"`
  21 + DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
  22 + UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
  23 + Manages string `orm:"column(managers)" description:"部门负责人id列表 json 数组 []"` //存user_company_id
  24 + IsTop int8 `orm:"column(is_top)" `
  25 + Level int `orm:"column(level)" `
  26 + BusinessDepartmentId int64 `orm:"column(business_department_id)"`
27 } 27 }
28 28
29 func (t *Department) TableName() string { 29 func (t *Department) TableName() string {
@@ -224,7 +224,7 @@ func GetDepartmentByBusinessId(businessId int64) (v *Department, err error) { @@ -224,7 +224,7 @@ func GetDepartmentByBusinessId(businessId int64) (v *Department, err error) {
224 v = &Department{} 224 v = &Department{}
225 o := orm.NewOrm() 225 o := orm.NewOrm()
226 err = o.QueryTable(&Department{}). 226 err = o.QueryTable(&Department{}).
227 - Filter("business_admin_id", businessId). 227 + Filter("business_department_id", businessId).
228 Filter("delete_at", 0). 228 Filter("delete_at", 0).
229 One(v) 229 One(v)
230 return v, err 230 return v, err
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "oppmg/utils"
  6 + "time"
  7 +
  8 + "github.com/astaxie/beego/orm"
  9 +)
  10 +
  11 +type DepartmentCharge struct {
  12 + Id int64 `orm:"column(id);auto"`
  13 + CompanyId int64 `orm:"column(company_id)" description:"公司id"`
  14 + DepartmentId int64 `orm:"column(department_id)" description:"部门id"`
  15 + UserCompanyId int64 `orm:"column(user_company_id)" description:"用户id"`
  16 + CreateTime time.Time `orm:"column(create_time);type(timestamp)"`
  17 + Enabled int8 `orm:"column(enabled)"`
  18 +}
  19 +
  20 +func (t *DepartmentCharge) TableName() string {
  21 + return "department_charge"
  22 +}
  23 +
  24 +func init() {
  25 + orm.RegisterModel(new(DepartmentCharge))
  26 +}
  27 +
  28 +const (
  29 + DEPARTMENT_CHARGE_ENABLE_YES int8 = 1
  30 + DEPARTMENT_CHARGE_ENABLE_NO int8 = 2
  31 +)
  32 +
  33 +// AddDepartmentCharge insert a new DepartmentCharge into database and returns
  34 +// last inserted Id on success.
  35 +func AddDepartmentCharge(m *DepartmentCharge) (id int64, err error) {
  36 + o := orm.NewOrm()
  37 + id, err = o.Insert(m)
  38 + return
  39 +}
  40 +
  41 +// UpdateDepartmentCharge updates DepartmentCharge by Id and returns error if
  42 +// the record to be updated doesn't exist
  43 +func UpdateDepartmentChargeById(m *DepartmentCharge) (err error) {
  44 + o := orm.NewOrm()
  45 + v := DepartmentCharge{Id: m.Id}
  46 + // ascertain id exists in the database
  47 + if err = o.Read(&v); err == nil {
  48 + var num int64
  49 + if num, err = o.Update(m); err == nil {
  50 + fmt.Println("Number of records updated in database:", num)
  51 + }
  52 + }
  53 + return
  54 +}
  55 +
  56 +func GetDepartmentCharge(departmentId int64) ([]DepartmentCharge, error) {
  57 + var (
  58 + data []DepartmentCharge
  59 + err error
  60 + )
  61 + o := orm.NewOrm()
  62 + _, err = o.QueryTable(&DepartmentCharge{}).
  63 + Filter("department_id", departmentId).
  64 + Filter("enabled", 1).
  65 + All(&data)
  66 + if err == orm.ErrNoRows {
  67 + return data, nil
  68 + }
  69 + return data, err
  70 +}
  71 +
  72 +//ChangeDepartmentCharge 变更部门的主管
  73 +func ChangeDepartmentCharge(companyId int64, departmentId int64, userCompanyid []int64, om orm.Ormer) error {
  74 + var (
  75 + err error
  76 + charges []DepartmentCharge
  77 + oldChargeIds []int64
  78 + )
  79 + charges, err = GetDepartmentCharge(departmentId)
  80 + if err != nil {
  81 + return fmt.Errorf("获取部门主管数据失败:%s", err)
  82 + }
  83 + for _, v := range charges {
  84 + oldChargeIds = append(oldChargeIds, v.Id)
  85 + }
  86 + var (
  87 + delIds []int64
  88 + addIds []int64
  89 + addCharges []DepartmentCharge
  90 + )
  91 + delIds = utils.ArrayInt64Diff(oldChargeIds, userCompanyid)
  92 + addIds = utils.ArrayInt64Diff(userCompanyid, oldChargeIds)
  93 + nowTime := time.Now()
  94 + for _, v := range addIds {
  95 + m := DepartmentCharge{
  96 + CompanyId: companyId,
  97 + UserCompanyId: v,
  98 + DepartmentId: departmentId,
  99 + Enabled: DEPARTMENT_CHARGE_ENABLE_YES,
  100 + CreateTime: nowTime,
  101 + }
  102 + addCharges = append(addCharges, m)
  103 +
  104 + }
  105 + if len(delIds) > 0 {
  106 + _, err = om.QueryTable(&DepartmentCharge{}).
  107 + Filter("department_id", departmentId).
  108 + Filter("user_company_id__in", delIds).
  109 + Update(orm.Params{
  110 + "enable": DEPARTMENT_CHARGE_ENABLE_NO,
  111 + })
  112 + if err != nil {
  113 + return fmt.Errorf("更新department_charge数据失败,err:%s", err)
  114 + }
  115 + }
  116 + if len(addCharges) > 0 {
  117 + _, err = om.InsertMulti(10, addCharges)
  118 + if err != nil {
  119 + return fmt.Errorf("添加department_charge数据失败,err:%s", err)
  120 + }
  121 + }
  122 +
  123 + return nil
  124 +}
@@ -8,17 +8,17 @@ import ( @@ -8,17 +8,17 @@ import (
8 ) 8 )
9 9
10 type Position struct { 10 type Position struct {
11 - Id int64 `orm:"column(id);auto" description:"职位表id"`  
12 - CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`  
13 - Name string `orm:"column(name);size(100)" description:"职位名称"`  
14 - ParentId int64 `orm:"column(parent_id)" description:"父级id"`  
15 - Relation string `orm:"column(relation);size(1000)" description:"父子级关系树"`  
16 - CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`  
17 - UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`  
18 - DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`  
19 - EnableStatus string `orm:"column(enable_status);size(255)" description:"有效状态 1:有效 0:无效"`  
20 - Level int `orm:"column(level)"`  
21 - BusinessAdminId int64 `orm:"column(business_admin_id)"` 11 + Id int64 `orm:"column(id);auto" description:"职位表id"`
  12 + CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
  13 + Name string `orm:"column(name);size(100)" description:"职位名称"`
  14 + ParentId int64 `orm:"column(parent_id)" description:"父级id"`
  15 + Relation string `orm:"column(relation);size(1000)" description:"父子级关系树"`
  16 + CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
  17 + UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
  18 + DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
  19 + EnableStatus string `orm:"column(enable_status);size(255)" description:"有效状态 1:有效 0:无效"`
  20 + Level int `orm:"column(level)"`
  21 + BusinessPositionId int64 `orm:"column(business_position_id)"`
22 } 22 }
23 23
24 //关联企业总后台的id 24 //关联企业总后台的id
@@ -107,7 +107,7 @@ func GetPositionByBusinessId(businessId int64) (v *Position, err error) { @@ -107,7 +107,7 @@ func GetPositionByBusinessId(businessId int64) (v *Position, err error) {
107 o := orm.NewOrm() 107 o := orm.NewOrm()
108 v = &Position{} 108 v = &Position{}
109 err = o.QueryTable(&Position{}). 109 err = o.QueryTable(&Position{}).
110 - Filter("business_admin_id", businessId). 110 + Filter("business_position_id", businessId).
111 Filter("delete_at", 0). 111 Filter("delete_at", 0).
112 One(v) 112 One(v)
113 return v, err 113 return v, err
1 package platform 1 package platform
2 2
3 -import "errors" 3 +import (
  4 + "encoding/json"
  5 + "errors"
  6 + "fmt"
  7 + "oppmg/common/log"
  8 + "oppmg/models"
  9 +
  10 + "github.com/astaxie/beego/orm"
  11 +)
4 12
5 // ModuleCompanytData 主管理平台发送过来的数据 13 // ModuleCompanytData 主管理平台发送过来的数据
6 type ModuleCompanytData struct { 14 type ModuleCompanytData struct {
7 - Id int64 `json:"id"` //id  
8 - Name string `json:"name"` //公司名称名称  
9 - AdminId int64 `json:"admin_id"` //主管理员id  
10 - Logo string `json:"logo"`  
11 -}  
12 -  
13 -type DepartmentChargeData struct {  
14 - Id int64 `json:"id"`  
15 - Charge []int64 `json:"charge"` 15 + Id int64 `json:"id"` //id
  16 + Name string `json:"name"` //公司名称名称
  17 + AdminId int64 `json:"admin_id"` //主管理员id
  18 + Logo string `json:"logo"`
  19 + Charge []int64 `json:"charge"`
16 } 20 }
17 21
18 var _ PlatformAction = ModuleCompanytData{} 22 var _ PlatformAction = ModuleCompanytData{}
@@ -20,9 +24,70 @@ var _ PlatformAction = ModuleCompanytData{} @@ -20,9 +24,70 @@ var _ PlatformAction = ModuleCompanytData{}
20 func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error { 24 func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error {
21 switch code { 25 switch code {
22 case "edit": 26 case "edit":
  27 + var (
  28 + data ModuleCompanytData
  29 + err error
  30 + )
  31 + err = json.Unmarshal(jsondata, &data)
  32 + if err != nil {
  33 + return fmt.Errorf("数据解析失败:%s", err)
  34 + }
  35 + return UpdateCompanyData(data)
23 case "setCompanyCharge": 36 case "setCompanyCharge":
  37 + var (
  38 + data ModuleCompanytData
  39 + err error
  40 + )
  41 + err = json.Unmarshal(jsondata, &data)
  42 + if err != nil {
  43 + return fmt.Errorf("数据解析失败:%s", err)
  44 + }
  45 + return SetCompanyCharge(data)
24 default: 46 default:
25 return errors.New("action not found") 47 return errors.New("action not found")
26 } 48 }
  49 + // return nil
  50 +}
  51 +
  52 +// SetCompanyCharge 更新公司那一级的部门的管理员
  53 +func SetCompanyCharge(data ModuleCompanytData) error {
  54 + var (
  55 + err error
  56 + companyData *models.Company
  57 + departmentData *models.Department
  58 + )
  59 + companyData, err = models.GetCompanyByUCenter(data.Id)
  60 + if err != nil {
  61 + log.Error("获取公司数据失败,user_center_id=%d,err:%s", data.Id, err)
  62 + return errors.New("获取公司数据失败")
  63 + }
  64 + departmentData, err = models.GetTopDepartmentByCompany(companyData.Id)
  65 + if err != nil {
  66 + log.Error("获取公司一级部门数据失败,company_id=%d, err:%s", companyData.Id, err)
  67 + return errors.New("获取公司一级部门数据失败")
  68 + }
  69 + // 获取公司管理员
  70 + o := orm.NewOrm()
  71 + o.Begin()
  72 + err = models.ChangeDepartmentCharge(data.Id, departmentData.Id, data.Charge, o)
  73 + if err != nil {
  74 + o.Rollback()
  75 + log.Error("变更公司管理员失败,err:%s", err)
  76 + return errors.New("变更公司管理员失败")
  77 + }
  78 + //更新department数据
  79 + bt, _ := json.Marshal(data.Charge)
  80 + departmentData.Manages = string(bt)
  81 + err = models.UpdateDepartmentById(departmentData, []string{"Manages"}, o)
  82 + if err != nil {
  83 + o.Rollback()
  84 + log.Error("变更公司管理员失败,err:%s", err)
  85 + return errors.New("变更公司管理员失败")
  86 + }
  87 + o.Commit()
  88 + return nil
  89 +}
  90 +
  91 +func UpdateCompanyData(data ModuleCompanytData) error {
27 return nil 92 return nil
28 } 93 }
@@ -15,12 +15,13 @@ import ( @@ -15,12 +15,13 @@ import (
15 15
16 // ModuleDeparmentData 主管理平台发送过来的数据 16 // ModuleDeparmentData 主管理平台发送过来的数据
17 type ModuleDeparmentData struct { 17 type ModuleDeparmentData struct {
18 - Id int64 `json:"id"` //id  
19 - Name string `json:"name"` //部门名称  
20 - ParentId int64 `json:"parent_id"` //父级id  
21 - CompanyId int64 `json:"company_id"`  
22 - Path string `json:"path"`  
23 - Level int `json:"level"` 18 + Id int64 `json:"id"` //id
  19 + Name string `json:"name"` //部门名称
  20 + ParentId int64 `json:"parent_id"` //父级id
  21 + CompanyId int64 `json:"company_id"`
  22 + Path string `json:"path"`
  23 + Level int `json:"level"`
  24 + Charge []int64 `json:"charge"` //部门主管
24 } 25 }
25 26
26 var _ PlatformAction = ModuleDeparmentData{} 27 var _ PlatformAction = ModuleDeparmentData{}
@@ -163,9 +164,9 @@ func departmentRelationUpdate(departmentUpdate models.Department, newparent mode @@ -163,9 +164,9 @@ func departmentRelationUpdate(departmentUpdate models.Department, newparent mode
163 for i := range departmentSubset { 164 for i := range departmentSubset {
164 err = models.UpdateDepartmentById(departmentSubset[i], []string{"ParentId", "Relation"}, o) 165 err = models.UpdateDepartmentById(departmentSubset[i], []string{"ParentId", "Relation"}, o)
165 if err != nil { 166 if err != nil {
166 - log.Error("更新position发生错误,bussiness_admin_id=%d,id=%d,err:%s", departmentSubset[i].BusinessAdminId, departmentSubset[i].Id, err) 167 + log.Error("更新position发生错误,bussiness_admin_id=%d,id=%d,err:%s", departmentSubset[i].BusinessDepartmentId, departmentSubset[i].Id, err)
167 o.Rollback() 168 o.Rollback()
168 - return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", departmentSubset[i].BusinessAdminId) 169 + return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", departmentSubset[i].BusinessDepartmentId)
169 } 170 }
170 } 171 }
171 o.Commit() 172 o.Commit()
@@ -164,9 +164,9 @@ func positionRelationUpdate(positionUpdate models.Position, newparent models.Pos @@ -164,9 +164,9 @@ func positionRelationUpdate(positionUpdate models.Position, newparent models.Pos
164 for i := range positionSubset { 164 for i := range positionSubset {
165 err = models.UpdatePositionById(positionSubset[i], []string{"ParentId", "Relation"}, o) 165 err = models.UpdatePositionById(positionSubset[i], []string{"ParentId", "Relation"}, o)
166 if err != nil { 166 if err != nil {
167 - log.Error("更新position发生错误,bussiness_admin_id=%d,id=%d,err:%s", positionSubset[i].BusinessAdminId, positionSubset[i].Id, err) 167 + log.Error("更新position发生错误,bussiness_admin_id=%d,id=%d,err:%s", positionSubset[i].BusinessPositionId, positionSubset[i].Id, err)
168 o.Rollback() 168 o.Rollback()
169 - return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", positionSubset[i].BusinessAdminId) 169 + return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", positionSubset[i].BusinessPositionId)
170 } 170 }
171 } 171 }
172 o.Commit() 172 o.Commit()