作者 tangxvhui

数据同步调整

@@ -12,6 +12,7 @@ type PlatformController struct { @@ -12,6 +12,7 @@ type PlatformController struct {
12 BaseController 12 BaseController
13 } 13 }
14 14
  15 +//
15 func (c PlatformController) UpdateData() { 16 func (c PlatformController) UpdateData() {
16 var msg *protocol.ResponseMessage 17 var msg *protocol.ResponseMessage
17 defer func() { 18 defer func() {
@@ -23,13 +24,13 @@ func (c PlatformController) UpdateData() { @@ -23,13 +24,13 @@ func (c PlatformController) UpdateData() {
23 msg = protocol.BadRequestParam("1") 24 msg = protocol.BadRequestParam("1")
24 return 25 return
25 } 26 }
26 - m, err := platform.NewPlatformAction(param.Module)  
27 - if err != nil {  
28 - msg = protocol.NewReturnResponse(nil, err)  
29 - return  
30 - }  
31 - err = m.DoAction(param.Action, []byte(param.Data))  
32 - msg = protocol.NewReturnResponse(nil, err) 27 + // m, err := platform.NewPlatformAction(param.Module)
  28 + // if err != nil {
  29 + // msg = protocol.NewReturnResponse(nil, err)
  30 + // return
  31 + // }
  32 + // err = m.DoAction(param.Action, []byte(param.Data))
  33 + msg = protocol.NewMessage("0")
33 return 34 return
34 } 35 }
35 36
@@ -12,17 +12,18 @@ import ( @@ -12,17 +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)" ` 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)"`
26 } 27 }
27 28
28 func (t *Department) TableName() string { 29 func (t *Department) TableName() string {
@@ -56,9 +57,11 @@ func (t *Department) SetRelation(parent *Department) error { @@ -56,9 +57,11 @@ func (t *Department) SetRelation(parent *Department) error {
56 return errors.New("Id==0") 57 return errors.New("Id==0")
57 } 58 }
58 if parent == nil { 59 if parent == nil {
59 - t.Relation = fmt.Sprintf("%d", t.Id) 60 + t.Relation = fmt.Sprintf("%d/", t.Id)
  61 + } else if parent.Id == 0 {
  62 + t.Relation = fmt.Sprintf("%d/", t.Id)
60 } else { 63 } else {
61 - t.Relation = fmt.Sprintf("%s/%d", parent.Relation, t.Id) 64 + t.Relation = fmt.Sprintf("%s%d/", parent.Relation, t.Id)
62 } 65 }
63 return nil 66 return nil
64 } 67 }
@@ -216,3 +219,25 @@ func ExistDepartmentName(parentId int64, dname string) bool { @@ -216,3 +219,25 @@ func ExistDepartmentName(parentId int64, dname string) bool {
216 Exist() 219 Exist()
217 return ok 220 return ok
218 } 221 }
  222 +
  223 +func GetDepartmentByBusinessId(businessId int64) (v *Department, err error) {
  224 + v = &Department{}
  225 + o := orm.NewOrm()
  226 + err = o.QueryTable(&Department{}).
  227 + Filter("business_admin_id", businessId).
  228 + Filter("delete_at", 0).
  229 + One(v)
  230 + return v, err
  231 +
  232 +}
  233 +
  234 +func GetTopDepartmentByCompany(companyid int64) (v *Department, err error) {
  235 + v = &Department{}
  236 + o := orm.NewOrm()
  237 + err = o.QueryTable(&Department{}).
  238 + Filter("company_id", companyid).
  239 + Filter("is_top", 1).
  240 + Filter("delete_at", 0).
  241 + One(v)
  242 + return v, err
  243 +}
@@ -8,16 +8,17 @@ import ( @@ -8,16 +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)"` 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)"`
21 } 22 }
22 23
23 //关联企业总后台的id 24 //关联企业总后台的id
@@ -30,13 +31,10 @@ func init() { @@ -30,13 +31,10 @@ func init() {
30 } 31 }
31 32
32 func (t *Position) SetRelation(parent *Position) error { 33 func (t *Position) SetRelation(parent *Position) error {
33 - // if t.Id == 0 {  
34 - // return errors.New("Id==0")  
35 - // }  
36 - if parent == nil || t.Id == 0 {  
37 - t.Relation = fmt.Sprintf("%d", t.Id) 34 + if parent == nil {
  35 + t.Relation = fmt.Sprintf("%d/", t.Id)
38 } else { 36 } else {
39 - t.Relation = fmt.Sprintf("%s/%d", parent.Relation, t.Id) 37 + t.Relation = fmt.Sprintf("%s%d/", parent.Relation, t.Id)
40 } 38 }
41 return nil 39 return nil
42 } 40 }
@@ -104,3 +102,13 @@ func ExistPositiontName(companyid int64, parentId int64, dname string) bool { @@ -104,3 +102,13 @@ func ExistPositiontName(companyid int64, parentId int64, dname string) bool {
104 Exist() 102 Exist()
105 return ok 103 return ok
106 } 104 }
  105 +
  106 +func GetPositionByBusinessId(businessId int64) (v *Position, err error) {
  107 + o := orm.NewOrm()
  108 + v = &Position{}
  109 + err = o.QueryTable(&Position{}).
  110 + Filter("business_admin_id", businessId).
  111 + Filter("delete_at", 0).
  112 + One(v)
  113 + return v, err
  114 +}
@@ -11,7 +11,7 @@ import ( @@ -11,7 +11,7 @@ import (
11 func init() { 11 func init() {
12 nsPlatform := beego.NewNamespace("/platform", 12 nsPlatform := beego.NewNamespace("/platform",
13 beego.NSBefore(middleware.LogRequestData), 13 beego.NSBefore(middleware.LogRequestData),
14 - beego.NSRouter("/update", &controllers.PlatformController{}, "post:UpdateData"), 14 + beego.NSRouter("/action", &controllers.PlatformController{}, "post:UpdateData"),
15 beego.NSRouter("/admins_change", &controllers.PlatformController{}, "post:CompanyAdminChance"), 15 beego.NSRouter("/admins_change", &controllers.PlatformController{}, "post:CompanyAdminChance"),
16 ) 16 )
17 beego.AddNamespace(nsPlatform) 17 beego.AddNamespace(nsPlatform)
1 package platform 1 package platform
  2 +
  3 +import "errors"
  4 +
  5 +// ModuleCompanytData 主管理平台发送过来的数据
  6 +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"`
  16 +}
  17 +
  18 +var _ PlatformAction = ModuleCompanytData{}
  19 +
  20 +func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error {
  21 + switch code {
  22 + case "edit":
  23 + case "setCompanyCharge":
  24 + default:
  25 + return errors.New("action not found")
  26 + }
  27 + return nil
  28 +}
@@ -6,6 +6,8 @@ import ( @@ -6,6 +6,8 @@ import (
6 "fmt" 6 "fmt"
7 "oppmg/common/log" 7 "oppmg/common/log"
8 "oppmg/models" 8 "oppmg/models"
  9 + "oppmg/utils"
  10 + "strings"
9 "time" 11 "time"
10 12
11 "github.com/astaxie/beego/orm" 13 "github.com/astaxie/beego/orm"
@@ -46,27 +48,28 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error { @@ -46,27 +48,28 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error {
46 return fmt.Errorf("数据解析失败:%s", err) 48 return fmt.Errorf("数据解析失败:%s", err)
47 } 49 }
48 return AddDepartmentData(data) 50 return AddDepartmentData(data)
49 - case "delete": 51 + case "batchDelete":
50 var ( 52 var (
51 err error 53 err error
52 - ids []int64  
53 ) 54 )
  55 + ids := struct {
  56 + Ids []int64 `json:"ids"`
  57 + }{}
54 err = json.Unmarshal(jsondata, &ids) 58 err = json.Unmarshal(jsondata, &ids)
55 if err != nil { 59 if err != nil {
56 return fmt.Errorf("数据解析失败:%s", err) 60 return fmt.Errorf("数据解析失败:%s", err)
57 } 61 }
58 - if len(ids) == 0 { 62 + if len(ids.Ids) == 0 {
59 return fmt.Errorf("参数错误") 63 return fmt.Errorf("参数错误")
60 } 64 }
61 - return DeleteDepartmentData(ids) 65 + return DeleteDepartmentData(ids.Ids)
62 default: 66 default:
63 return errors.New("action not found") 67 return errors.New("action not found")
64 } 68 }
65 - return nil 69 + // return nil
66 } 70 }
67 71
68 //同步 部门数据 72 //同步 部门数据
69 -  
70 //UpdateDepartmentData .... 73 //UpdateDepartmentData ....
71 func UpdateDepartmentData(data ModuleDeparmentData) error { 74 func UpdateDepartmentData(data ModuleDeparmentData) error {
72 var ( 75 var (
@@ -81,18 +84,91 @@ func UpdateDepartmentData(data ModuleDeparmentData) error { @@ -81,18 +84,91 @@ func UpdateDepartmentData(data ModuleDeparmentData) error {
81 return errors.New("获取部门数据失败") 84 return errors.New("获取部门数据失败")
82 } 85 }
83 departmentData.Name = data.Name 86 departmentData.Name = data.Name
84 - departmentData.ParentId = data.ParentId  
85 - departmentData.Relation = data.Path  
86 - departmentData.Level = data.Level  
87 -  
88 - err = models.UpdateDepartmentById(departmentData, []string{"Name", "Manage", "ParentId", "Relation"}) 87 + err = models.UpdateDepartmentById(departmentData, []string{"Name"})
89 if err != nil { 88 if err != nil {
90 -  
91 e := fmt.Errorf("更新部门数据失败,err:%s", err) 89 e := fmt.Errorf("更新部门数据失败,err:%s", err)
92 log.Error(e.Error()) 90 log.Error(e.Error())
93 return errors.New("更新部门数据失败") 91 return errors.New("更新部门数据失败")
94 } 92 }
  93 + var (
  94 + newParentDepart *models.Department
  95 + )
  96 + if data.ParentId == 0 {
  97 + newParentDepart, err = models.GetTopDepartmentByCompany(departmentData.CompanyId)
  98 + if err != nil {
  99 + log.Error("获取公司一级部门数据失败:company_id=%d,err:%s", departmentData.CompanyId, err)
  100 + return errors.New("获取公司一级部门数据失败")
  101 + }
  102 + } else {
  103 + newParentDepart, err = models.GetDepartmentByBusinessId(data.ParentId)
  104 + if err != nil {
  105 + log.Error("获取父级数据失败,business_admin_id=%d,err:%s", data.ParentId, err)
  106 + }
  107 + }
  108 + if departmentData.ParentId != newParentDepart.Id {
  109 + //更新父级
  110 + err = departmentRelationUpdate(*departmentData, *newParentDepart)
  111 + if err != nil {
  112 + return err
  113 + }
  114 + }
  115 +
  116 + return nil
  117 +}
  118 +
  119 +//positionRelationUpdate 处理职位上级发生变化的情况
  120 +func departmentRelationUpdate(departmentUpdate models.Department, newparent models.Department) error {
  121 + const (
  122 + //获取某个职位的下级职位 锁数据 select ... for update
  123 + dataSql0 string = `SELECT id,relation FROM department WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`
  124 + //更新关系树
  125 + //dataSql2 string = `update department set relation=? where id=?`
  126 + //更新departUpdate的parent_id
  127 + //dataSql3 string = `update department set parent_id=? where id=?`
  128 + )
  129 + var (
  130 + departmentSubset []*models.Department //子级部门
  131 + err error
  132 + oldRelation string = departmentUpdate.Relation
  133 + relationLike string = oldRelation + "%"
  134 + newRelation string
  135 + )
  136 + if newparent.Id == 0 {
  137 + //修改节点为顶层节点的情况
  138 + newRelation = fmt.Sprintf("%d/", departmentUpdate.Id)
  139 + } else {
  140 + newRelation = fmt.Sprintf("%s%d/", newparent.Relation, departmentUpdate.Id)
  141 + }
  142 + o := orm.NewOrm()
  143 + o.Begin()
  144 + //获取部门及子级部门的relation和id
  145 + err = utils.ExecuteQueryAllWithOrmer(o, &departmentSubset, dataSql0, relationLike)
  146 + if err != nil {
  147 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
  148 + log.Error(e.Error())
  149 + return errors.New("获取部门及子级部门失败")
  150 + }
  151 + for i := range departmentSubset {
  152 + if departmentSubset[i].Id == departmentUpdate.Id {
  153 + departmentSubset[i].ParentId = newparent.Id
  154 + }
  155 + //重建关系树
  156 + s := strings.TrimLeft(departmentSubset[i].Relation, oldRelation)
  157 + departmentSubset[i].Relation = fmt.Sprintf("%s%s", newRelation, s)
  158 + s1 := strings.Split(departmentSubset[i].Relation, "/")
  159 + departmentSubset[i].Level = len(s1) - 1
  160 + }
95 161
  162 + //修改部门及子级部门的relation
  163 + for i := range departmentSubset {
  164 + err = models.UpdateDepartmentById(departmentSubset[i], []string{"ParentId", "Relation"}, o)
  165 + if err != nil {
  166 + log.Error("更新position发生错误,bussiness_admin_id=%d,id=%d,err:%s", departmentSubset[i].BusinessAdminId, departmentSubset[i].Id, err)
  167 + o.Rollback()
  168 + return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", departmentSubset[i].BusinessAdminId)
  169 + }
  170 + }
  171 + o.Commit()
96 return nil 172 return nil
97 } 173 }
98 174
@@ -108,22 +184,37 @@ func AddDepartmentData(data ModuleDeparmentData) error { @@ -108,22 +184,37 @@ func AddDepartmentData(data ModuleDeparmentData) error {
108 log.Error(e.Error()) 184 log.Error(e.Error())
109 return errors.New("获取公司数据失败") 185 return errors.New("获取公司数据失败")
110 } 186 }
111 -  
112 departmentData := &models.Department{ 187 departmentData := &models.Department{
113 Id: data.Id, 188 Id: data.Id,
114 CompanyId: companyinfo.Id, 189 CompanyId: companyinfo.Id,
115 Name: data.Name, 190 Name: data.Name,
116 - ParentId: data.ParentId,  
117 Manages: "[]", 191 Manages: "[]",
118 - Relation: data.Path, //TODO 格式转化  
119 Level: data.Level, 192 Level: data.Level,
120 } 193 }
121 - 194 + var (
  195 + parentDepart *models.Department
  196 + )
  197 + if data.ParentId > 0 {
  198 + parentDepart, err = models.GetDepartmentByBusinessId(data.ParentId)
  199 + if err != nil {
  200 + log.Error("获取父级职位数据失败,business_admin_id=%d,err:%s", data.ParentId, err)
  201 + return errors.New("获取父级职位数据失败")
  202 + }
  203 + departmentData.ParentId = parentDepart.Id
  204 + }
  205 + if data.ParentId == 0 {
  206 + parentDepart, err = models.GetTopDepartmentByCompany(companyinfo.Id)
  207 + if err != nil {
  208 + log.Error("获取公司一级部门数据失败:company_id=%d,err:%s", companyinfo.Id, err)
  209 + return errors.New("获取公司一级部门数据失败")
  210 + }
  211 + departmentData.ParentId = parentDepart.Id
  212 + }
  213 + departmentData.SetRelation(parentDepart)
122 _, err = models.AddDepartment(departmentData) 214 _, err = models.AddDepartment(departmentData)
123 if err != nil { 215 if err != nil {
124 e := fmt.Errorf("存储部门数据失败,err:%s", err) 216 e := fmt.Errorf("存储部门数据失败,err:%s", err)
125 log.Error(e.Error()) 217 log.Error(e.Error())
126 -  
127 } 218 }
128 219
129 return nil 220 return nil
@@ -6,6 +6,8 @@ import ( @@ -6,6 +6,8 @@ import (
6 "fmt" 6 "fmt"
7 "oppmg/common/log" 7 "oppmg/common/log"
8 "oppmg/models" 8 "oppmg/models"
  9 + "oppmg/utils"
  10 + "strings"
9 "time" 11 "time"
10 12
11 "github.com/astaxie/beego/orm" 13 "github.com/astaxie/beego/orm"
@@ -45,7 +47,7 @@ func (m ModulePositionData) DoAction(code string, jsondata []byte) error { @@ -45,7 +47,7 @@ func (m ModulePositionData) DoAction(code string, jsondata []byte) error {
45 return fmt.Errorf("数据解析失败:%s", err) 47 return fmt.Errorf("数据解析失败:%s", err)
46 } 48 }
47 return AddPosition(data) 49 return AddPosition(data)
48 - case "delete": 50 + case "batchDelete":
49 var ( 51 var (
50 err error 52 err error
51 ) 53 )
@@ -79,73 +81,97 @@ func (m ModulePositionData) validate() error { @@ -79,73 +81,97 @@ func (m ModulePositionData) validate() error {
79 //UpdatePosition 同步职位数据 81 //UpdatePosition 同步职位数据
80 //TODO 父级数据变更 82 //TODO 父级数据变更
81 func UpdatePosition(data ModulePositionData) error { 83 func UpdatePosition(data ModulePositionData) error {
82 - positioninfo, err := models.GetPositionById(data.Id) 84 + positioninfo, err := models.GetPositionByBusinessId(data.Id)
83 if err != nil { 85 if err != nil {
84 - log.Error("获取职位数据失败:%s", err)  
85 - return fmt.Errorf("获取职位数据失败,Id=%d", data.Id) 86 + log.Error("获取职位数据失败,business_admin_id=%d ,err:%s", data.Id, err)
  87 + return fmt.Errorf("获取职位数据失败")
86 } 88 }
87 positioninfo.Name = data.Name 89 positioninfo.Name = data.Name
88 - // positioninfo.ParentId = data.ParentId  
89 - // positioninfo.Relation = data.Path  
90 err = models.UpdatePositionById(positioninfo, []string{"Name"}) 90 err = models.UpdatePositionById(positioninfo, []string{"Name"})
91 if err != nil { 91 if err != nil {
92 return err 92 return err
93 } 93 }
  94 + if positioninfo.ParentId == 0 && data.ParentId == 0 {
  95 + return nil
  96 + }
  97 + var (
  98 + newParentPosition *models.Position
  99 + )
  100 + if data.ParentId > 0 {
  101 + newParentPosition, err = models.GetPositionByBusinessId(data.ParentId)
  102 + if err != nil {
  103 + log.Error("获取父级职位数据失败,business_admin_id=%d,err:%s", data.ParentId, err)
  104 + return errors.New("获取父级职位数据失败")
  105 + }
  106 + } else {
  107 + newParentPosition = &models.Position{}
  108 + }
  109 + if positioninfo.ParentId != newParentPosition.Id {
  110 + // 更新父级
  111 + err = positionRelationUpdate(*positioninfo, *newParentPosition)
  112 + if err != nil {
  113 + return err
  114 + }
  115 + }
  116 + //应对父级发生变化的情况
94 return nil 117 return nil
95 } 118 }
96 119
97 -//positionRelationUpdate 处理部门上级发生变化的情况  
98 -// func positionRelationUpdate(positionUpdate *models.Position, newparent *models.Position, o orm.Ormer) error {  
99 -// const (  
100 -// //获取某个部门的下级部门 锁数据 select ... for update  
101 -// dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`  
102 -// //更新关系树  
103 -// dataSql2 string = `update position set relation=? where id=?`  
104 -// //更新departUpdate的parent_id  
105 -// dataSql3 string = `update position set parent_id=? where id=?`  
106 -// )  
107 -// var (  
108 -// positionSubset []models.Position //子级部门  
109 -// err error  
110 -// oldRelation string = positionUpdate.Relation  
111 -// relationLike string = oldRelation + "%"  
112 -// newRelation string  
113 -// )  
114 -// if newparent == nil || newparent.Id == 0 {  
115 -// //修改节点为顶层节点的情况  
116 -// newparent = &models.Position{}  
117 -// newRelation = fmt.Sprintf("%d", positionUpdate.Id)  
118 -// } else {  
119 -// newRelation = fmt.Sprintf("%s/%d", newparent.Relation, positionUpdate.Id)  
120 -// }  
121 -// //修改部门的parent_id  
122 -// err = utils.ExecuteSQLWithOrmer(o, dataSql3, newparent.Id, positionUpdate.Id)  
123 -// if err != nil {  
124 -// e := fmt.Errorf("EXECUTE SQL err:%s", err)  
125 -// log.Error(e.Error())  
126 -// return errors.New("更新数据失败")  
127 -// }  
128 -// //获取部门及子级部门的relation和id  
129 -// err = utils.ExecuteQueryAllWithOrmer(o, &positionSubset, dataSql0, relationLike)  
130 -// if err != nil {  
131 -// e := fmt.Errorf("EXECUTE SQL err:%s", err)  
132 -// log.Error(e.Error())  
133 -// return errors.New("更新数据失败")  
134 -// }  
135 -// //修改部门及子级部门的relation  
136 -// for i := range positionSubset {  
137 -// //重建关系树  
138 -// s := strings.TrimPrefix(positionSubset[i].Relation, oldRelation)  
139 -// positionSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s))  
140 -// err = utils.ExecuteSQLWithOrmer(o, dataSql2, positionSubset[i].Relation, positionSubset[i].Id)  
141 -// if err != nil {  
142 -// e := fmt.Errorf("EXECUTE SQL err:%s", err)  
143 -// log.Error(e.Error())  
144 -// return errors.New("更新数据失败")  
145 -// }  
146 -// }  
147 -// return nil  
148 -// } 120 +//positionRelationUpdate 处理职位上级发生变化的情况
  121 +func positionRelationUpdate(positionUpdate models.Position, newparent models.Position) error {
  122 + const (
  123 + //获取某个职位的下级职位 锁数据 select ... for update
  124 + dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`
  125 + //更新关系树
  126 + // string = `update position set relation=? where id=?`
  127 + //更新departUpdate的parent_id
  128 + //dataSql3 string = `update position set parent_id=? where id=?`
  129 + )
  130 + var (
  131 + positionSubset []*models.Position //子级部门
  132 + err error
  133 + oldRelation string = positionUpdate.Relation
  134 + relationLike string = oldRelation + "%"
  135 + newRelation string
  136 + )
  137 + if newparent.Id == 0 {
  138 + //修改节点为顶层节点的情况
  139 + newRelation = fmt.Sprintf("%d/", positionUpdate.Id)
  140 + } else {
  141 + newRelation = fmt.Sprintf("%s%d/", newparent.Relation, positionUpdate.Id)
  142 + }
  143 + o := orm.NewOrm()
  144 + o.Begin()
  145 + //获取部门及子级部门的relation和id
  146 + err = utils.ExecuteQueryAllWithOrmer(o, &positionSubset, dataSql0, relationLike)
  147 + if err != nil {
  148 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
  149 + log.Error(e.Error())
  150 + return errors.New("获取部门及子级部门失败")
  151 + }
  152 + for i := range positionSubset {
  153 + if positionSubset[i].Id == positionUpdate.Id {
  154 + positionSubset[i].ParentId = newparent.Id
  155 + }
  156 + //重建关系树
  157 + s := strings.TrimLeft(positionSubset[i].Relation, oldRelation)
  158 + positionSubset[i].Relation = fmt.Sprintf("%s%s", newRelation, s)
  159 + s1 := strings.Split(positionSubset[i].Relation, "/")
  160 + positionSubset[i].Level = len(s1) - 1
  161 + }
  162 +
  163 + //修改部门及子级部门的relation
  164 + for i := range positionSubset {
  165 + err = models.UpdatePositionById(positionSubset[i], []string{"ParentId", "Relation"}, o)
  166 + if err != nil {
  167 + log.Error("更新position发生错误,bussiness_admin_id=%d,id=%d,err:%s", positionSubset[i].BusinessAdminId, positionSubset[i].Id, err)
  168 + o.Rollback()
  169 + return fmt.Errorf("更新position发生错误,bussiness_admin_id=%d", positionSubset[i].BusinessAdminId)
  170 + }
  171 + }
  172 + o.Commit()
  173 + return nil
  174 +}
149 175
150 func AddPosition(data ModulePositionData) error { 176 func AddPosition(data ModulePositionData) error {
151 var ( 177 var (
@@ -157,21 +183,31 @@ func AddPosition(data ModulePositionData) error { @@ -157,21 +183,31 @@ func AddPosition(data ModulePositionData) error {
157 log.Error("获取公司数据失败:s%", err) 183 log.Error("获取公司数据失败:s%", err)
158 return errors.New("无效公司") 184 return errors.New("无效公司")
159 } 185 }
160 -  
161 positioninfo := &models.Position{ 186 positioninfo := &models.Position{
162 Id: data.Id, 187 Id: data.Id,
163 Name: data.Name, 188 Name: data.Name,
164 - ParentId: data.ParentId,  
165 CompanyId: companyinfo.Id, 189 CompanyId: companyinfo.Id,
166 - Relation: data.Path, //TODO 格式转换  
167 - Level: data.Level,  
168 } 190 }
  191 + var (
  192 + parentPosition *models.Position
  193 + )
  194 + if data.ParentId > 0 {
  195 + parentPosition, err = models.GetPositionByBusinessId(data.ParentId)
  196 + if err != nil {
  197 + log.Error("获取父级职位数据失败,business_admin_id=%d,err:%s", data.ParentId, err)
  198 + return errors.New("获取父级职位数据失败")
  199 + }
  200 + positioninfo.ParentId = parentPosition.Id
  201 + }
  202 +
  203 + positioninfo.SetRelation(parentPosition)
  204 + s := strings.Split(positioninfo.Relation, "/")
  205 + positioninfo.Level = len(s) - 1
169 _, err = models.AddPosition(positioninfo) 206 _, err = models.AddPosition(positioninfo)
170 if err != nil { 207 if err != nil {
171 log.Error("添加职位失败:%s", err) 208 log.Error("添加职位失败:%s", err)
172 return errors.New("添加职位失败") 209 return errors.New("添加职位失败")
173 } 210 }
174 -  
175 return nil 211 return nil
176 } 212 }
177 213