作者 tangxvhui

总后台数据同步

@@ -3,7 +3,7 @@ package platform @@ -3,7 +3,7 @@ package platform
3 import "errors" 3 import "errors"
4 4
5 type PlatformAction interface { 5 type PlatformAction interface {
6 - DoAction(string) error 6 + DoAction(string, []byte) error
7 } 7 }
8 8
9 //从主管理平台接收数据数据并处理数据 9 //从主管理平台接收数据数据并处理数据
1 package platform 1 package platform
2 2
3 import ( 3 import (
  4 + "encoding/json"
4 "errors" 5 "errors"
5 "fmt" 6 "fmt"
6 "oppmg/common/log" 7 "oppmg/common/log"
7 "oppmg/models" 8 "oppmg/models"
  9 + "time"
  10 +
  11 + "github.com/astaxie/beego/orm"
8 ) 12 )
9 13
10 // ModuleDeparmentData 主管理平台发送过来的数据 14 // ModuleDeparmentData 主管理平台发送过来的数据
@@ -13,73 +17,145 @@ type ModuleDeparmentData struct { @@ -13,73 +17,145 @@ type ModuleDeparmentData struct {
13 Name string `json:"name"` //部门名称 17 Name string `json:"name"` //部门名称
14 ParentId int64 `json:"parent_id"` //父级id 18 ParentId int64 `json:"parent_id"` //父级id
15 CompanyId int64 `json:"company_id"` 19 CompanyId int64 `json:"company_id"`
  20 + Relation string `json:"relation"`
16 Manages []int64 `json:"manages"` 21 Manages []int64 `json:"manages"`
17 } 22 }
18 23
19 var _ PlatformAction = ModuleDeparmentData{} 24 var _ PlatformAction = ModuleDeparmentData{}
20 25
21 //DoAction PlatformAction 的接口实现 26 //DoAction PlatformAction 的接口实现
22 -func (m ModuleDeparmentData) DoAction(code string) error { 27 +func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error {
23 switch code { 28 switch code {
24 case "edit": 29 case "edit":
25 - return UpdateDepartmentData(m) 30 + var (
  31 + data []ModuleDeparmentData
  32 + err error
  33 + )
  34 + err = json.Unmarshal(jsondata, &data)
  35 + if err != nil {
  36 + return fmt.Errorf("数据解析失败:%s", err)
  37 + }
  38 + return UpdateDepartmentData(data)
26 case "add": 39 case "add":
27 - return AddDepartmentData(m) 40 + var (
  41 + data []ModuleDeparmentData
  42 + err error
  43 + )
  44 + err = json.Unmarshal(jsondata, &data)
  45 + if err != nil {
  46 + return fmt.Errorf("数据解析失败:%s", err)
  47 + }
  48 + return AddDepartmentData(data)
28 case "delete": 49 case "delete":
29 - return DeleteDepartmentData(m) 50 + var (
  51 + err error
  52 + ids []int64
  53 + )
  54 + err = json.Unmarshal(jsondata, &ids)
  55 + if err != nil {
  56 + return fmt.Errorf("数据解析失败:%s", err)
  57 + }
  58 + if len(ids) == 0 {
  59 + return fmt.Errorf("参数错误")
  60 + }
  61 + return DeleteDepartmentData(ids)
30 default: 62 default:
31 return errors.New("action not found") 63 return errors.New("action not found")
32 } 64 }
  65 + return nil
33 } 66 }
34 67
35 //同步 部门数据 68 //同步 部门数据
36 -func UpdateDepartmentData(data ModuleDeparmentData) error {  
37 69
  70 +//UpdateDepartmentData ....
  71 +func UpdateDepartmentData(data []ModuleDeparmentData) error {
  72 + var (
  73 + departmentData *models.Department
  74 + err error
  75 + )
  76 + o := orm.NewOrm()
  77 + o.Begin()
  78 + for _, v := range data {
  79 + departmentData, err = models.GetDepartmentById(v.Id)
  80 + if err != nil {
  81 + e := fmt.Errorf("获取部门数据失败,err:%s", err)
  82 + log.Error(e.Error())
  83 + return errors.New("获取部门数据失败")
  84 + }
  85 + departmentData.Name = v.Name
  86 + departmentData.ParentId = v.ParentId
  87 + departmentData.Relation = v.Relation
  88 + if bt, err := json.Marshal(v.Manages); err == nil {
  89 + departmentData.Manages = string(bt)
  90 + }
  91 + err = models.UpdateDepartmentById(departmentData, []string{"Name", "Manage", "ParentId", "Relation"}, o)
  92 + if err != nil {
  93 + o.Rollback()
  94 + e := fmt.Errorf("更新部门数据失败,err:%s", err)
  95 + log.Error(e.Error())
  96 + return errors.New("更新部门数据失败")
  97 + }
  98 + }
  99 + o.Commit()
38 return nil 100 return nil
39 } 101 }
40 102
41 //AddDepartmentData ... 103 //AddDepartmentData ...
42 -//TODO :部门管理员数据填充  
43 -func AddDepartmentData(data ModuleDeparmentData) error { 104 +func AddDepartmentData(data []ModuleDeparmentData) error {
  105 + if len(data) == 0 {
  106 + return nil
  107 + }
44 var ( 108 var (
45 - companyinfo *models.Company  
46 - err error  
47 - parentDepartment *models.Department 109 + companyinfo *models.Company
  110 + err error
48 ) 111 )
49 - companyinfo, err = models.GetCompanyByUCenter(data.CompanyId) 112 + companyinfo, err = models.GetCompanyByUCenter(data[0].CompanyId)
50 if err != nil { 113 if err != nil {
51 e := fmt.Errorf("获取公司数据失败,err:%s", err) 114 e := fmt.Errorf("获取公司数据失败,err:%s", err)
52 log.Error(e.Error()) 115 log.Error(e.Error())
53 return errors.New("获取公司数据失败") 116 return errors.New("获取公司数据失败")
54 -  
55 - }  
56 - departmentData := &models.Department{  
57 - Id: data.Id,  
58 - CompanyId: companyinfo.Id,  
59 - Name: data.Name,  
60 - ParentId: data.ParentId,  
61 - Manages: "[]",  
62 - Relation: fmt.Sprintf("%d", data.Id),  
63 } 117 }
64 - if data.ParentId > 0 {  
65 - parentDepartment, err = models.GetDepartmentById(data.ParentId) 118 + o := orm.NewOrm()
  119 + o.Begin()
  120 + var isRollback bool
  121 + for _, v := range data {
  122 + departmentData := &models.Department{
  123 + Id: v.Id,
  124 + CompanyId: companyinfo.Id,
  125 + Name: v.Name,
  126 + ParentId: v.ParentId,
  127 + Manages: "[]",
  128 + Relation: v.Relation, //TODO 格式转化
  129 + }
  130 + if bt, err := json.Marshal(v.Manages); err == nil {
  131 + departmentData.Manages = string(bt)
  132 + }
  133 + _, err = models.AddDepartment(departmentData, o)
66 if err != nil { 134 if err != nil {
67 - e := fmt.Errorf("获取父级部门数据失败,err:%s", err) 135 + e := fmt.Errorf("存储部门数据失败,err:%s", err)
68 log.Error(e.Error()) 136 log.Error(e.Error())
69 - return errors.New("获取父级部门数据失败") 137 + isRollback = true
70 } 138 }
71 - departmentData.Relation = fmt.Sprintf("%s/%d", parentDepartment.Relation, data.Id)  
72 } 139 }
73 - _, err = models.AddDepartment(departmentData)  
74 - if err != nil {  
75 -  
76 - e := fmt.Errorf("存储部门数据失败,err:%s", err)  
77 - log.Error(e.Error()) 140 + if isRollback {
  141 + o.Rollback()
78 return errors.New("存储部门数据失败") 142 return errors.New("存储部门数据失败")
79 } 143 }
  144 + o.Commit()
80 return nil 145 return nil
81 } 146 }
82 147
83 -func DeleteDepartmentData(data ModuleDeparmentData) error { 148 +//DeleteDepartmentData ...
  149 +func DeleteDepartmentData(ids []int64) error {
  150 + o := orm.NewOrm()
  151 + _, err := o.QueryTable(&models.Department{}).
  152 + Filter("id__in", ids).
  153 + Update(orm.Params{
  154 + "delete_at": time.Now().Format("2006-01-02 15:04:05"),
  155 + })
  156 + if err != nil {
  157 + log.Error("更新position数据失败,err:%s", err)
  158 + return errors.New("删除职位数据失败")
  159 + }
84 return nil 160 return nil
85 } 161 }
1 package platform 1 package platform
2 2
  3 +import (
  4 + "encoding/json"
  5 + "errors"
  6 + "fmt"
  7 + "oppmg/common/log"
  8 + "oppmg/models"
  9 + "time"
  10 +
  11 + "github.com/astaxie/beego/orm"
  12 +)
  13 +
3 //同步 人员数据 14 //同步 人员数据
4 15
5 //-------------------- 16 //--------------------
6 17
7 type ModuleEmployee struct { 18 type ModuleEmployee struct {
  19 + Userid int64 `json:"user_id"`
8 UCenterId int64 `json:"ucenter_id"` //同一用户中心的用户id 20 UCenterId int64 `json:"ucenter_id"` //同一用户中心的用户id
  21 + Phone string `json:"phone"`
9 Nickname string `json:"nick_name"` 22 Nickname string `json:"nick_name"`
10 - Companyid int64 `json:"company_id"` // 23 + Companyid int64 `json:"company_id"` //同意用户中心的company_id
11 DepartmentId []int64 `json:"department_id"` 24 DepartmentId []int64 `json:"department_id"`
12 Position []int64 `json:"position"` 25 Position []int64 `json:"position"`
13 } 26 }
@@ -15,22 +28,122 @@ type ModuleEmployee struct { @@ -15,22 +28,122 @@ type ModuleEmployee struct {
15 var _ PlatformAction = ModuleEmployee{} 28 var _ PlatformAction = ModuleEmployee{}
16 29
17 //DoAction PlatformAction 的接口实现 30 //DoAction PlatformAction 的接口实现
18 -func (m ModuleEmployee) DoAction(code string) error {  
19 - 31 +func (m ModuleEmployee) DoAction(code string, jsondata []byte) error {
  32 + switch code {
  33 + case "edit":
  34 + // return UpdateDepartmentData(m)
  35 + case "add":
  36 + var (
  37 + data []ModuleEmployee
  38 + err error
  39 + )
  40 + err = json.Unmarshal(jsondata, &data)
  41 + if err != nil {
  42 + return fmt.Errorf("数据解析失败:%s", err)
  43 + }
  44 + return AddEmployeeData(data)
  45 + case "delete":
  46 + var (
  47 + err error
  48 + ids []int64
  49 + )
  50 + err = json.Unmarshal(jsondata, &ids)
  51 + if err != nil {
  52 + return fmt.Errorf("数据解析失败:%s", err)
  53 + }
  54 + if len(ids) == 0 {
  55 + return fmt.Errorf("参数错误")
  56 + }
  57 + return DeleteEmployeeData(ids)
  58 + default:
  59 + return errors.New("action not found")
  60 + }
20 return nil 61 return nil
21 } 62 }
22 63
23 -// UpdateEmployeeData ...  
24 -func UpdateEmployeeData(data ModuleEmployee) error { 64 +// AddEmployeeData ...
  65 +func AddEmployeeData(data []ModuleEmployee) error {
  66 + if len(data) == 0 {
  67 + return nil
  68 + }
  69 + var (
  70 + companydata *models.Company
  71 + err error
  72 + usercompanydata []*models.UserCompany
  73 + userdepartmentData []*models.UserDepartment
  74 + userpositionmentData []*models.UserPosition
  75 + )
  76 + companydata, err = models.GetCompanyByUCenter(data[0].Companyid)
  77 + if err != nil {
  78 + log.Error("GetCompanyByUCenter获取公司数据失败,ucenterId=%d,err:%s", data[0].Companyid, err)
  79 + return errors.New("获取公司数据失败")
  80 + }
  81 +
  82 + for _, v := range data {
  83 + var (
  84 + userdata *models.User
  85 + )
  86 + userdata, err = models.GetUserByUCenter(v.UCenterId)
  87 + if err != nil && err != orm.ErrNoRows {
  88 + log.Error("获取用户数据失败,err:%s", err)
  89 + return errors.New("获取用户数据失败")
  90 + }
  91 + if err == orm.ErrNoRows {
  92 + //添加用户
  93 + userdata = &models.User{
  94 + UserCenterId: v.UCenterId,
  95 + NickName: v.Nickname,
  96 + Phone: v.Phone,
  97 + }
  98 + _, err = models.AddUser(userdata)
  99 + if err != nil {
  100 + log.Error("添加用户失败,err:%s", err)
  101 + return err
  102 + }
  103 + } else {
  104 + userdata.NickName = v.Nickname
  105 + userdata.Phone = v.Phone
  106 + models.UpdateUserById(userdata, []string{"NickName", "Phone"})
  107 + }
  108 + uc := &models.UserCompany{
  109 + Id: v.Userid,
  110 + CompanyId: companydata.Id,
  111 + UserId: userdata.Id,
  112 + NickName: v.Nickname,
  113 + }
  114 + usercompanydata = append(usercompanydata, uc)
  115 + }
  116 + o := orm.NewOrm()
  117 + o.Begin()
  118 + _, err = o.InsertMulti(20, &usercompanydata)
  119 + if err != nil {
  120 + log.Error("批量插入user_company数据失败,err:%s", err)
  121 + o.Rollback()
  122 + return err
  123 + }
  124 + //添加部门职位
  125 +
  126 + o.Commit()
25 return nil 127 return nil
26 } 128 }
27 129
28 -// AddEmployeeData ...  
29 -func AddEmployeeData(data ModuleEmployee) error { 130 +// UpdateEmployeeData ...
  131 +func UpdateEmployeeData(data []ModuleEmployee) error {
  132 +
30 return nil 133 return nil
31 } 134 }
32 135
33 // DeleteEmployeeData ... 136 // DeleteEmployeeData ...
34 -func DeleteEmployeeData(data ModuleEmployee) error { 137 +func DeleteEmployeeData(ids []int64) error {
  138 + o := orm.NewOrm()
  139 + _, err := o.QueryTable(&models.UserCompany{}).
  140 + Filter("id__in", ids).
  141 + Update(orm.Params{
  142 + "delete_at": time.Now().String(),
  143 + })
  144 + if err != nil {
  145 + log.Error("更新user_company数据失败:%s", err)
  146 + return errors.New("删除user_company数据失败")
  147 + }
35 return nil 148 return nil
36 } 149 }
1 package platform 1 package platform
2 2
3 import ( 3 import (
  4 + "encoding/json"
4 "errors" 5 "errors"
5 "fmt" 6 "fmt"
6 "oppmg/common/log" 7 "oppmg/common/log"
7 "oppmg/models" 8 "oppmg/models"
8 - "oppmg/utils"  
9 - "strings"  
10 "time" 9 "time"
11 10
12 "github.com/astaxie/beego/orm" 11 "github.com/astaxie/beego/orm"
13 ) 12 )
14 13
15 type ModulePositionData struct { 14 type ModulePositionData struct {
16 - Id int64 `json:"id"`  
17 - Name string `json:"nick_name"`  
18 - ParentId int64 `json:"parent_id"`  
19 - CompanyId int64 `json:"company_id"`  
20 - Ids []int64 `json:"ids"` 15 + Id int64 `json:"id"`
  16 + Name string `json:"nick_name"`
  17 + ParentId int64 `json:"parent_id"`
  18 + CompanyId int64 `json:"company_id"`
  19 + Relation string `json:"relation"`
21 } 20 }
22 21
23 var _ PlatformAction = ModulePositionData{} 22 var _ PlatformAction = ModulePositionData{}
24 23
25 //DoAction PlatformAction 的接口实现 24 //DoAction PlatformAction 的接口实现
26 -func (m ModulePositionData) DoAction(code string) error {  
27 - return nil  
28 -}  
29 -  
30 -//同步职位数据  
31 -func UpdatePosition(data ModulePositionData) error {  
32 - var (  
33 - positioninfo *models.Position  
34 - err error  
35 - parentPosition *models.Position  
36 - companyinfo *models.Company  
37 - )  
38 - positioninfo, err = models.GetPositionById(data.Id)  
39 - if err != nil {  
40 - log.Error("获取职位数据失败:%s", err)  
41 - return errors.New("获取职位数据失败")  
42 - }  
43 - companyinfo, err = models.GetCompanyByUCenter(data.CompanyId)  
44 - if err != nil {  
45 - log.Error("获取获取公司数据失败;%s", err)  
46 - return errors.New("获取获取公司数据失败")  
47 - }  
48 - //检查上级  
49 - if data.ParentId > 0 {  
50 - parentPosition, err = models.GetPositionById(data.ParentId) 25 +func (m ModulePositionData) DoAction(code string, jsondata []byte) error {
  26 + switch code {
  27 + case "edit":
  28 + var (
  29 + err error
  30 + data []ModulePositionData
  31 + )
  32 + err = json.Unmarshal(jsondata, &data)
51 if err != nil { 33 if err != nil {
52 - log.Error("获取父级职位数据失败;%s", err)  
53 - return errors.New("获取父级职位数据失败;") 34 + return fmt.Errorf("数据解析失败:%s", err)
54 } 35 }
55 - if parentPosition.CompanyId == companyinfo.Id {  
56 - log.Error("父级职位公司不匹配")  
57 - return errors.New("父级职位公司不匹配") 36 + return UpdatePosition(data)
  37 + case "add":
  38 + var (
  39 + data []ModulePositionData
  40 + err error
  41 + )
  42 + err = json.Unmarshal(jsondata, &data)
  43 + if err != nil {
  44 + return fmt.Errorf("数据解析失败:%s", err)
  45 + }
  46 + return AddPosition(data)
  47 + case "delete":
  48 + var (
  49 + err error
  50 + ids []int64
  51 + )
  52 + err = json.Unmarshal(jsondata, &ids)
  53 + if err != nil {
  54 + return fmt.Errorf("数据解析失败:%s", err)
58 } 55 }
  56 + if len(ids) == 0 {
  57 + return fmt.Errorf("参数错误")
  58 + }
  59 +
  60 + return DeletePosition(ids)
  61 + default:
  62 + return errors.New("action not found")
59 } 63 }
60 - o := orm.NewOrm()  
61 - o.Begin()  
62 - positioninfo.Name = data.Name  
63 - err = models.UpdatePositionById(positioninfo, []string{"Name", "UpdateAt"}, o)  
64 - if err != nil {  
65 - o.Rollback()  
66 - return err 64 +}
  65 +
  66 +func (m ModulePositionData) validate() error {
  67 + if m.Id <= 0 {
  68 + return errors.New("错误:id<=0 ")
67 } 69 }
68 - err = positionRelationUpdate(positioninfo, parentPosition, o)  
69 - if err != nil {  
70 - o.Rollback()  
71 - return err 70 + if len(m.Name) == 0 {
  71 + return errors.New("错误:name长度为0")
72 } 72 }
73 - o.Commit()  
74 return nil 73 return nil
75 } 74 }
76 75
77 -//positionRelationUpdate 处理部门上级发生变化的情况  
78 -func positionRelationUpdate(positionUpdate *models.Position, newparent *models.Position, o orm.Ormer) error {  
79 - const (  
80 - //获取某个部门的下级部门 锁数据 select ... for update  
81 - dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`  
82 - //更新关系树  
83 - dataSql2 string = `update position set relation=? where id=?`  
84 - //更新departUpdate的parent_id  
85 - dataSql3 string = `update position set parent_id=? where id=?`  
86 - )  
87 - var (  
88 - positionSubset []models.Position //子级部门  
89 - err error  
90 - oldRelation string = positionUpdate.Relation  
91 - relationLike string = oldRelation + "%"  
92 - newRelation string  
93 - )  
94 - if newparent == nil || newparent.Id == 0 {  
95 - //修改节点为顶层节点的情况  
96 - newparent = &models.Position{}  
97 - newRelation = fmt.Sprintf("%d", positionUpdate.Id)  
98 - } else {  
99 - newRelation = fmt.Sprintf("%s/%d", newparent.Relation, positionUpdate.Id)  
100 - }  
101 - //修改部门的parent_id  
102 - err = utils.ExecuteSQLWithOrmer(o, dataSql3, newparent.Id, positionUpdate.Id)  
103 - if err != nil {  
104 - e := fmt.Errorf("EXECUTE SQL err:%s", err)  
105 - log.Error(e.Error())  
106 - return errors.New("更新数据失败")  
107 - }  
108 - //获取部门及子级部门的relation和id  
109 - err = utils.ExecuteQueryAllWithOrmer(o, &positionSubset, dataSql0, relationLike)  
110 - if err != nil {  
111 - e := fmt.Errorf("EXECUTE SQL err:%s", err)  
112 - log.Error(e.Error())  
113 - return errors.New("更新数据失败") 76 +//同步职位数据
  77 +func UpdatePosition(data []ModulePositionData) error {
  78 + if len(data) == 0 {
  79 + return nil
114 } 80 }
115 - //修改部门及子级部门的relation  
116 - for i := range positionSubset {  
117 - //重建关系树  
118 - s := strings.TrimPrefix(positionSubset[i].Relation, oldRelation)  
119 - positionSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s))  
120 - err = utils.ExecuteSQLWithOrmer(o, dataSql2, positionSubset[i].Relation, positionSubset[i].Id) 81 + o := orm.NewOrm()
  82 + o.Begin()
  83 + for _, v := range data {
  84 + positioninfo, err := models.GetPositionById(v.Id)
  85 + if err != nil {
  86 + log.Error("获取职位数据失败:%s", err)
  87 + return fmt.Errorf("获取职位数据失败,Id=%d", v.Id)
  88 + }
  89 + positioninfo.Name = v.Name
  90 + positioninfo.ParentId = v.ParentId
  91 + positioninfo.Relation = v.Relation
  92 + err = models.UpdatePositionById(positioninfo, []string{"Name", "ParentId", "Relation"}, o)
121 if err != nil { 93 if err != nil {
122 - e := fmt.Errorf("EXECUTE SQL err:%s", err)  
123 - log.Error(e.Error())  
124 - return errors.New("更新数据失败") 94 + o.Rollback()
  95 + return err
125 } 96 }
126 } 97 }
  98 + o.Commit()
127 return nil 99 return nil
128 } 100 }
129 101
130 -func AddPosition(data ModulePositionData) error { 102 +//positionRelationUpdate 处理部门上级发生变化的情况
  103 +// func positionRelationUpdate(positionUpdate *models.Position, newparent *models.Position, o orm.Ormer) error {
  104 +// const (
  105 +// //获取某个部门的下级部门 锁数据 select ... for update
  106 +// dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`
  107 +// //更新关系树
  108 +// dataSql2 string = `update position set relation=? where id=?`
  109 +// //更新departUpdate的parent_id
  110 +// dataSql3 string = `update position set parent_id=? where id=?`
  111 +// )
  112 +// var (
  113 +// positionSubset []models.Position //子级部门
  114 +// err error
  115 +// oldRelation string = positionUpdate.Relation
  116 +// relationLike string = oldRelation + "%"
  117 +// newRelation string
  118 +// )
  119 +// if newparent == nil || newparent.Id == 0 {
  120 +// //修改节点为顶层节点的情况
  121 +// newparent = &models.Position{}
  122 +// newRelation = fmt.Sprintf("%d", positionUpdate.Id)
  123 +// } else {
  124 +// newRelation = fmt.Sprintf("%s/%d", newparent.Relation, positionUpdate.Id)
  125 +// }
  126 +// //修改部门的parent_id
  127 +// err = utils.ExecuteSQLWithOrmer(o, dataSql3, newparent.Id, positionUpdate.Id)
  128 +// if err != nil {
  129 +// e := fmt.Errorf("EXECUTE SQL err:%s", err)
  130 +// log.Error(e.Error())
  131 +// return errors.New("更新数据失败")
  132 +// }
  133 +// //获取部门及子级部门的relation和id
  134 +// err = utils.ExecuteQueryAllWithOrmer(o, &positionSubset, dataSql0, relationLike)
  135 +// if err != nil {
  136 +// e := fmt.Errorf("EXECUTE SQL err:%s", err)
  137 +// log.Error(e.Error())
  138 +// return errors.New("更新数据失败")
  139 +// }
  140 +// //修改部门及子级部门的relation
  141 +// for i := range positionSubset {
  142 +// //重建关系树
  143 +// s := strings.TrimPrefix(positionSubset[i].Relation, oldRelation)
  144 +// positionSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s))
  145 +// err = utils.ExecuteSQLWithOrmer(o, dataSql2, positionSubset[i].Relation, positionSubset[i].Id)
  146 +// if err != nil {
  147 +// e := fmt.Errorf("EXECUTE SQL err:%s", err)
  148 +// log.Error(e.Error())
  149 +// return errors.New("更新数据失败")
  150 +// }
  151 +// }
  152 +// return nil
  153 +// }
  154 +
  155 +func AddPosition(data []ModulePositionData) error {
  156 + if len(data) == 0 {
  157 + return nil
  158 + }
131 var ( 159 var (
132 - companyinfo *models.Company  
133 - err error  
134 - parentPosition *models.Position 160 + companyinfo *models.Company
  161 + err error
  162 + isRollback bool
135 ) 163 )
136 - companyinfo, err = models.GetCompanyByUCenter(data.CompanyId) 164 + companyinfo, err = models.GetCompanyByUCenter(data[0].CompanyId)
137 if err != nil { 165 if err != nil {
138 log.Error("获取公司数据失败:s%", err) 166 log.Error("获取公司数据失败:s%", err)
139 return errors.New("无效公司") 167 return errors.New("无效公司")
140 } 168 }
141 - if data.ParentId > 0 {  
142 - parentPosition, err = models.GetPositionById(data.ParentId) 169 + o := orm.NewOrm()
  170 + o.Begin()
  171 + for _, v := range data {
  172 + positioninfo := &models.Position{
  173 + Id: v.Id,
  174 + Name: v.Name,
  175 + ParentId: v.ParentId,
  176 + CompanyId: companyinfo.Id,
  177 + Relation: v.Relation, //TODO 格式转换
  178 + }
  179 + _, err = models.AddPosition(positioninfo)
143 if err != nil { 180 if err != nil {
144 - log.Error("获取父级职位失败:%s", err)  
145 - return errors.New("获取父级职位失败") 181 + log.Error("添加职位失败:%s", err)
  182 + isRollback = true
  183 + break
146 } 184 }
147 - } else {  
148 - parentPosition = &models.Position{}  
149 } 185 }
150 -  
151 - positioninfo := &models.Position{  
152 - Id: data.Id,  
153 - Name: data.Name,  
154 - ParentId: data.ParentId,  
155 - CompanyId: companyinfo.Id,  
156 - }  
157 -  
158 - positioninfo.SetRelation(parentPosition)  
159 - _, err = models.AddPosition(positioninfo)  
160 - if err != nil {  
161 - log.Error("添加职位失败:%s", err) 186 + if isRollback {
  187 + o.Rollback()
162 return errors.New("添加职位失败") 188 return errors.New("添加职位失败")
163 } 189 }
  190 + o.Commit()
164 return nil 191 return nil
165 } 192 }
166 193
167 -func DeletePosition(ids []int64, ucompanyid int64) error {  
168 - //检查是否可以被删除  
169 - var (  
170 - //根据参数获取的职位  
171 - positionDelete []*models.Position  
172 - //最终需要操作的职位  
173 - toDelete = make(map[int64]*models.Position)  
174 - companyinfo *models.Company  
175 - err error  
176 - )  
177 - const (  
178 - //获取部门子集,  
179 - dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 `  
180 - )  
181 - companyinfo, err = models.GetCompanyByUCenter(ucompanyid)  
182 - if err != nil {  
183 - e := fmt.Errorf("获取公司数据失败")  
184 - log.Error(e.Error())  
185 - return e  
186 - }  
187 - for _, id := range ids {  
188 - var p *models.Position  
189 - p, err := models.GetPositionById(id)  
190 - if err != nil {  
191 - log.Error("获取职位失败Id:%d,err:%s", id, err)  
192 - continue  
193 - }  
194 - if p.CompanyId != companyinfo.Id {  
195 - log.Error("公司id不匹配")  
196 - return errors.New("公司id不匹配")  
197 - }  
198 - positionDelete = append(positionDelete, p)  
199 - toDelete[p.Id] = p  
200 - }  
201 - for _, pos := range positionDelete {  
202 - var positionsubset []models.Position  
203 - relationLike := pos.Relation + "%"  
204 - err := utils.ExecuteQueryAll(&positionsubset, dataSql0, relationLike)  
205 - if err != nil {  
206 - e := fmt.Errorf("EXECUTE SQL err:%s", err)  
207 - log.Error(e.Error())  
208 - return e  
209 - }  
210 - for k, subset := range positionsubset {  
211 - if _, ok := toDelete[subset.Id]; !ok {  
212 - toDelete[subset.Id] = &positionsubset[k]  
213 - }  
214 - }  
215 - }  
216 - var (  
217 - deleteIds []string  
218 - dataSql2 string = `update position set delete_at=%s where id IN (%s)`  
219 - nowTime string = time.Now().Format("2006-01-02 15:04:05")  
220 - )  
221 - for k, _ := range toDelete {  
222 - deleteIds = append(deleteIds, fmt.Sprint(k))  
223 - }  
224 - if len(deleteIds) == 0 {  
225 - return nil  
226 - }  
227 - dataSql2 = fmt.Sprintf(dataSql2, nowTime, strings.Join(deleteIds, ",")) 194 +// DeletePosition ...
  195 +func DeletePosition(ids []int64) error {
228 o := orm.NewOrm() 196 o := orm.NewOrm()
229 - o.Begin()  
230 - err = utils.ExecuteSQLWithOrmer(o, dataSql2) 197 + _, err := o.QueryTable(&models.Position{}).
  198 + Filter("id__in", ids).
  199 + Update(orm.Params{
  200 + "delete_at": time.Now().Format("2006-01-02 15:04:05"),
  201 + })
231 if err != nil { 202 if err != nil {
232 - e := fmt.Errorf("EXCUTE SQL ERR:%s", err)  
233 - return e 203 + log.Error("更新position数据失败,err:%s", err)
  204 + return errors.New("删除职位数据失败")
234 } 205 }
235 - o.Commit()  
236 return nil 206 return nil
237 } 207 }
  208 +
  209 +// func DeletePosition(ids []int64, ucompanyid int64) error {
  210 +// //检查是否可以被删除
  211 +// var (
  212 +// //根据参数获取的职位
  213 +// positionDelete []*models.Position
  214 +// //最终需要操作的职位
  215 +// toDelete = make(map[int64]*models.Position)
  216 +// companyinfo *models.Company
  217 +// err error
  218 +// )
  219 +// const (
  220 +// //获取部门子集,
  221 +// dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 `
  222 +// )
  223 +// companyinfo, err = models.GetCompanyByUCenter(ucompanyid)
  224 +// if err != nil {
  225 +// e := fmt.Errorf("获取公司数据失败")
  226 +// log.Error(e.Error())
  227 +// return e
  228 +// }
  229 +// for _, id := range ids {
  230 +// var p *models.Position
  231 +// p, err := models.GetPositionById(id)
  232 +// if err != nil {
  233 +// log.Error("获取职位失败Id:%d,err:%s", id, err)
  234 +// continue
  235 +// }
  236 +// if p.CompanyId != companyinfo.Id {
  237 +// log.Error("公司id不匹配")
  238 +// return errors.New("公司id不匹配")
  239 +// }
  240 +// positionDelete = append(positionDelete, p)
  241 +// toDelete[p.Id] = p
  242 +// }
  243 +// for _, pos := range positionDelete {
  244 +// var positionsubset []models.Position
  245 +// relationLike := pos.Relation + "%"
  246 +// err := utils.ExecuteQueryAll(&positionsubset, dataSql0, relationLike)
  247 +// if err != nil {
  248 +// e := fmt.Errorf("EXECUTE SQL err:%s", err)
  249 +// log.Error(e.Error())
  250 +// return e
  251 +// }
  252 +// for k, subset := range positionsubset {
  253 +// if _, ok := toDelete[subset.Id]; !ok {
  254 +// toDelete[subset.Id] = &positionsubset[k]
  255 +// }
  256 +// }
  257 +// }
  258 +// var (
  259 +// deleteIds []string
  260 +// dataSql2 string = `update position set delete_at=%s where id IN (%s)`
  261 +// nowTime string = time.Now().Format("2006-01-02 15:04:05")
  262 +// )
  263 +// for k, _ := range toDelete {
  264 +// deleteIds = append(deleteIds, fmt.Sprint(k))
  265 +// }
  266 +// if len(deleteIds) == 0 {
  267 +// return nil
  268 +// }
  269 +// dataSql2 = fmt.Sprintf(dataSql2, nowTime, strings.Join(deleteIds, ","))
  270 +// o := orm.NewOrm()
  271 +// o.Begin()
  272 +// err = utils.ExecuteSQLWithOrmer(o, dataSql2)
  273 +// if err != nil {
  274 +// e := fmt.Errorf("EXCUTE SQL ERR:%s", err)
  275 +// return e
  276 +// }
  277 +// o.Commit()
  278 +// return nil
  279 +// }