作者 tangxvhui

与企业后台对接相关

@@ -26,13 +26,17 @@ func (c PlatformController) UpdateData() { @@ -26,13 +26,17 @@ func (c PlatformController) UpdateData() {
26 log.Error("json 解析失败 err:%s", err) 26 log.Error("json 解析失败 err:%s", err)
27 return 27 return
28 } 28 }
29 - // m, err := platform.NewPlatformAction(param.Module)  
30 - // if err != nil {  
31 - // msg = protocol.NewReturnResponse(nil, err)  
32 - // return  
33 - // }  
34 - // err = m.DoAction(param.Action, []byte(param.Data))  
35 - 29 + m, err := platform.NewPlatformAction(param.Module)
  30 + if err != nil {
  31 + log.Error("同步数据发生错误,err%s", err)
  32 + return
  33 + }
  34 + bt, err := json.Marshal(param.Data)
  35 + if err != nil {
  36 + log.Error("解析param.data数据失败")
  37 + }
  38 + err = m.DoAction(param.Action, bt)
  39 + log.Error("同步数据发生错误,module=%s,action=%s,err%s", param.Module, param.Action, err)
36 return 40 return
37 } 41 }
38 42
@@ -19,6 +19,22 @@ type UserCompany struct { @@ -19,6 +19,22 @@ type UserCompany struct {
19 UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"` 19 UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
20 Enable int8 `orm:"column(enable)"` 20 Enable int8 `orm:"column(enable)"`
21 DeleteAt time.Time `orm:"column(delete_at)"` 21 DeleteAt time.Time `orm:"column(delete_at)"`
  22 + OpenId int64 `orm:"column(open_id)" description:"统一用户中心uid"`
  23 + Sex int8 `orm:"column(sex)" description:"性别:0保密 1男 2女"`
  24 + JobNum string `orm:"column(job_num);size(255)" description:"员工工号"`
  25 + Phone string `orm:"column(phone);size(11)" description:"用户手机,登录手机号"`
  26 + PrivatePhone string `orm:"column(private_phone);size(11)" description:"私人手机"`
  27 + Email string `orm:"column(email);size(255)" description:"邮箱"`
  28 + ExtensionNum string `orm:"column(extension_num);size(255)" description:"分机号"`
  29 + EntryTime time.Time `orm:"column(entry_time);type(date);null" description:"入职时间"`
  30 + Workspace string `orm:"column(workspace);size(255)" description:"工作地点"`
  31 + IsBusiness int8 `orm:"column(is_business)" description:"是否开启业务状态 1是 0否"`
  32 + //Status int8 `orm:"column(status)" description:"状态 1正常 2禁用"`
  33 + Avatar string `orm:"column(avatar);size(255)" description:"头像"`
  34 + Remarks string `orm:"column(remarks);size(255)" description:"备注"`
  35 + AdminType int8 `orm:"column(admin_type)" description:"1普通用户 2主管理员"`
  36 + ChargeStatus int8 `orm:"column(charge_status)" description:"是否为当前公司主管 1 是2 否"`
  37 + ExtraText string `orm:"column(extra_text);null" description:"自定义参数数据"`
22 } 38 }
23 39
24 func (t *UserCompany) TableName() string { 40 func (t *UserCompany) TableName() string {
@@ -8,8 +8,7 @@ import ( @@ -8,8 +8,7 @@ import (
8 ) 8 )
9 9
10 type UserPosition struct { 10 type UserPosition struct {
11 - Id int64 `orm:"column(id)" description:"唯一键值"`  
12 - //UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"` 11 + Id int64 `orm:"column(id)" description:"唯一键值"`
13 PositionId int64 `orm:"column(position_id)" description:"表position.id 职位编号"` 12 PositionId int64 `orm:"column(position_id)" description:"表position.id 职位编号"`
14 CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"` 13 CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
15 CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"` 14 CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
@@ -116,6 +116,7 @@ func initCompany(centerCompany protocol.CenterCompanyInfo, admininfo *models.Use @@ -116,6 +116,7 @@ func initCompany(centerCompany protocol.CenterCompanyInfo, admininfo *models.Use
116 } 116 }
117 } else if err == orm.ErrNoRows { 117 } else if err == orm.ErrNoRows {
118 newCompany = &models.Company{ 118 newCompany = &models.Company{
  119 + Id: centerCompany.CompanyId,
119 Name: centerCompany.CompanyName, 120 Name: centerCompany.CompanyName,
120 AdminId: admininfo.Id, 121 AdminId: admininfo.Id,
121 UserCenterId: centerCompany.CompanyId, 122 UserCenterId: centerCompany.CompanyId,
@@ -8,9 +8,9 @@ type PlatformAction interface { @@ -8,9 +8,9 @@ type PlatformAction interface {
8 8
9 //从主管理平台接收数据数据并处理数据 9 //从主管理平台接收数据数据并处理数据
10 type CommonProtocol struct { 10 type CommonProtocol struct {
11 - Module string `json:"module"` //模块  
12 - Action string `json:"action"` //动作  
13 - Data string `json:"data"` //json数据 11 + Module string `json:"module"` //模块
  12 + Action string `json:"action"` //动作
  13 + Data interface{} `json:"data"` //json数据
14 } 14 }
15 15
16 var actionmap = map[string]PlatformAction{ 16 var actionmap = map[string]PlatformAction{
@@ -12,11 +12,11 @@ import ( @@ -12,11 +12,11 @@ import (
12 12
13 // ModuleCompanytData 主管理平台发送过来的数据 13 // ModuleCompanytData 主管理平台发送过来的数据
14 type ModuleCompanytData struct { 14 type ModuleCompanytData struct {
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"` 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"`
20 } 20 }
21 21
22 var _ PlatformAction = ModuleCompanytData{} 22 var _ PlatformAction = ModuleCompanytData{}
@@ -89,15 +89,21 @@ func SetCompanyCharge(data ModuleCompanytData) error { @@ -89,15 +89,21 @@ func SetCompanyCharge(data ModuleCompanytData) error {
89 } 89 }
90 90
91 func UpdateCompanyData(data ModuleCompanytData) error { 91 func UpdateCompanyData(data ModuleCompanytData) error {
92 - // var (  
93 - // companyData *models.Company  
94 - // err error  
95 - // )  
96 - // companyData, err = models.GetCompanyByUCenter(data.Id)  
97 - // if err != nil {  
98 - // log.Error("获取企业数据失败,user_center_id:%d ,err:%s", data.Id, err)  
99 - // return errors.New("获取企业数据失败")  
100 - // }  
101 - // companyData.Logo = 92 + var (
  93 + companyData *models.Company
  94 + err error
  95 + )
  96 + companyData, err = models.GetCompanyByUCenter(data.Id)
  97 + if err != nil {
  98 + log.Error("获取企业数据失败,user_center_id:%d ,err:%s", data.Id, err)
  99 + return errors.New("获取企业数据失败")
  100 + }
  101 + companyData.Logo = data.Logo
  102 + companyData.Name = data.Name
  103 + err = models.UpdateCompanyById(companyData, []string{"Logo", "Name"})
  104 + if err != nil {
  105 + log.Error("更新公司数据失败:%s", err)
  106 + return errors.New("更新公司数据失败")
  107 + }
102 return nil 108 return nil
103 } 109 }
@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
7 "oppmg/common/log" 7 "oppmg/common/log"
8 "oppmg/models" 8 "oppmg/models"
9 "oppmg/utils" 9 "oppmg/utils"
  10 + "sort"
10 "strings" 11 "strings"
11 "time" 12 "time"
12 13
@@ -24,6 +25,12 @@ type ModuleDeparmentData struct { @@ -24,6 +25,12 @@ type ModuleDeparmentData struct {
24 Charge []int64 `json:"charge"` //部门主管 25 Charge []int64 `json:"charge"` //部门主管
25 } 26 }
26 27
  28 +type SortDepartmentByLevel []ModuleDeparmentData
  29 +
  30 +func (a SortDepartmentByLevel) Len() int { return len(a) }
  31 +func (a SortDepartmentByLevel) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  32 +func (a SortDepartmentByLevel) Less(i, j int) bool { return a[i].Level < a[j].Level }
  33 +
27 var _ PlatformAction = ModuleDeparmentData{} 34 var _ PlatformAction = ModuleDeparmentData{}
28 35
29 //DoAction PlatformAction 的接口实现 36 //DoAction PlatformAction 的接口实现
@@ -48,7 +55,8 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error { @@ -48,7 +55,8 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error {
48 if err != nil { 55 if err != nil {
49 return fmt.Errorf("数据解析失败:%s", err) 56 return fmt.Errorf("数据解析失败:%s", err)
50 } 57 }
51 - return AddDepartmentData(data) 58 + datas := []ModuleDeparmentData{data}
  59 + return AddDepartmentData(datas)
52 case "batchDelete": 60 case "batchDelete":
53 var ( 61 var (
54 err error 62 err error
@@ -64,6 +72,16 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error { @@ -64,6 +72,16 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error {
64 return fmt.Errorf("参数错误") 72 return fmt.Errorf("参数错误")
65 } 73 }
66 return DeleteDepartmentData(ids.Ids) 74 return DeleteDepartmentData(ids.Ids)
  75 + case "import":
  76 + var (
  77 + data []ModuleDeparmentData
  78 + err error
  79 + )
  80 + err = json.Unmarshal(jsondata, &data)
  81 + if err != nil {
  82 + return fmt.Errorf("数据解析失败:%s", err)
  83 + }
  84 + return AddDepartmentData(data)
67 default: 85 default:
68 return errors.New("action not found") 86 return errors.New("action not found")
69 } 87 }
@@ -156,8 +174,8 @@ func departmentRelationUpdate(departmentUpdate models.Department, newparent mode @@ -156,8 +174,8 @@ func departmentRelationUpdate(departmentUpdate models.Department, newparent mode
156 //重建关系树 174 //重建关系树
157 s := strings.TrimLeft(departmentSubset[i].Relation, oldRelation) 175 s := strings.TrimLeft(departmentSubset[i].Relation, oldRelation)
158 departmentSubset[i].Relation = fmt.Sprintf("%s%s", newRelation, s) 176 departmentSubset[i].Relation = fmt.Sprintf("%s%s", newRelation, s)
159 - s1 := strings.Split(departmentSubset[i].Relation, "/")  
160 - departmentSubset[i].Level = len(s1) - 1 177 + s1 := strings.Split(departmentSubset[i].Relation, ",")
  178 + departmentSubset[i].Level = len(s1) - 2
161 } 179 }
162 180
163 //修改部门及子级部门的relation 181 //修改部门及子级部门的relation
@@ -174,50 +192,63 @@ func departmentRelationUpdate(departmentUpdate models.Department, newparent mode @@ -174,50 +192,63 @@ func departmentRelationUpdate(departmentUpdate models.Department, newparent mode
174 } 192 }
175 193
176 //AddDepartmentData ... 194 //AddDepartmentData ...
177 -func AddDepartmentData(data ModuleDeparmentData) error { 195 +func AddDepartmentData(data []ModuleDeparmentData) error {
  196 + if len(data) == 0 {
  197 + return nil
  198 + }
  199 + sort.Sort(SortDepartmentByLevel(data))
178 var ( 200 var (
179 - companyinfo *models.Company  
180 - err error 201 + err error
  202 + companyData *models.Company
  203 + topDepartment *models.Department
181 ) 204 )
182 - companyinfo, err = models.GetCompanyByUCenter(data.CompanyId) 205 + companyData, err = models.GetCompanyByUCenter(data[0].CompanyId)
183 if err != nil { 206 if err != nil {
184 - e := fmt.Errorf("获取公司数据失败,id=%d,err:%s", data.CompanyId, err)  
185 - log.Error(e.Error()) 207 + log.Error("获取公司数据失败,user_center_id=%d,err:%s", data[0].CompanyId, err)
186 return errors.New("获取公司数据失败") 208 return errors.New("获取公司数据失败")
187 } 209 }
188 - departmentData := &models.Department{  
189 - Id: data.Id,  
190 - CompanyId: companyinfo.Id,  
191 - Name: data.Name,  
192 - Manages: "[]",  
193 - Level: data.Level, 210 + topDepartment, err = models.GetTopDepartmentByCompany(companyData.Id)
  211 + if err != nil {
  212 + log.Error("获取公司一级部门数据失败;company_id=%d,err:%s", companyData.Id, err)
  213 + return errors.New("获取公司一级部门数据失败")
194 } 214 }
195 - var (  
196 - parentDepart *models.Department  
197 - )  
198 - if data.ParentId > 0 {  
199 - parentDepart, err = models.GetDepartmentByBusinessId(data.ParentId)  
200 - if err != nil {  
201 - log.Error("获取父级职位数据失败,business_admin_id=%d,err:%s", data.ParentId, err)  
202 - return errors.New("获取父级职位数据失败") 215 + o := orm.NewOrm()
  216 + o.Begin()
  217 + for _, v := range data {
  218 + departmentData := &models.Department{
  219 + Id: v.Id,
  220 + CompanyId: companyData.Id,
  221 + Name: v.Name,
  222 + Manages: "[]",
203 } 223 }
204 - departmentData.ParentId = parentDepart.Id  
205 - }  
206 - if data.ParentId == 0 {  
207 - parentDepart, err = models.GetTopDepartmentByCompany(companyinfo.Id) 224 + if v.ParentId > 0 {
  225 + parentDepart := &models.Department{}
  226 + err = o.QueryTable(&models.Department{}).
  227 + Filter("business_department_id", v.ParentId).
  228 + Filter("delete_at", 0).
  229 + One(v)
  230 + if err != nil {
  231 + log.Error("获取父级职位数据失败,business_admin_id=%d,err:%s", v.ParentId, err)
  232 + return errors.New("获取父级职位数据失败")
  233 + }
  234 + departmentData.ParentId = parentDepart.Id
  235 + departmentData.SetRelation(parentDepart)
  236 + }
  237 + if v.ParentId == 0 {
  238 + departmentData.ParentId = topDepartment.Id
  239 + departmentData.SetRelation(topDepartment)
  240 + }
  241 + s := strings.Split(departmentData.Relation, ",")
  242 + departmentData.Level = len(s) - 2
  243 + _, err = models.AddDepartment(departmentData, o)
208 if err != nil { 244 if err != nil {
209 - log.Error("获取公司一级部门数据失败:company_id=%d,err:%s", companyinfo.Id, err)  
210 - return errors.New("获取公司一级部门数据失败") 245 + e := fmt.Errorf("存储部门数据失败,err:%s", err)
  246 + o.Rollback()
  247 + log.Error(e.Error())
  248 + return errors.New("存储部门数据失败")
211 } 249 }
212 - departmentData.ParentId = parentDepart.Id  
213 } 250 }
214 - departmentData.SetRelation(parentDepart)  
215 - _, err = models.AddDepartment(departmentData)  
216 - if err != nil {  
217 - e := fmt.Errorf("存储部门数据失败,err:%s", err)  
218 - log.Error(e.Error())  
219 - }  
220 - 251 + o.Commit()
221 return nil 252 return nil
222 } 253 }
223 254
@@ -15,16 +15,60 @@ import ( @@ -15,16 +15,60 @@ import (
15 15
16 //同步 人员数据 16 //同步 人员数据
17 17
18 -//-------------------- 18 +type UserDepartData struct {
  19 + Id int64 `json:"id"`
  20 + CompanyId int64 `json:"company_id"`
  21 + DepartmentId int64 `json:"department_id"`
  22 + UserId int64 `json:"user_id"`
  23 +}
  24 +
  25 +type UserPositionData struct {
  26 + Id int64 `json:"id"`
  27 + CompanyId int64 `json:"company_id"`
  28 + PositionId int64 `json:"position_id"`
  29 + UserId int64 `json:"user_id"`
  30 +}
19 31
  32 +//--------------------
20 type ModuleEmployee struct { 33 type ModuleEmployee struct {
21 - Userid int64 `json:"user_id"`  
22 - UCenterId int64 `json:"ucenter_id"` //同一用户中心的用户id  
23 - Phone string `json:"phone"`  
24 - Nickname string `json:"nick_name"`  
25 - Companyid int64 `json:"company_id"` //同意用户中心的company_id  
26 - DepartmentId []int64 `json:"department_id"`  
27 - Position []int64 `json:"position"` 34 + Id int64 `json:"id"` //用户的id,对应本地的user_company_id
  35 + OpenId int64 `json:"open_id"` //同一用户中心的用户id
  36 + Phone string `json:"phone"`
  37 + Name string `json:"name"`
  38 + Sex int8 `json:"sex"`
  39 + JobNum string `json:"job_num"`
  40 + PrivatePhone string `json:"private_phone"` //私人手机
  41 + CompanyId int64 `json:"company_id"` //总后台的company_id
  42 + Email string `json:"email"`
  43 + ExtensionNum string `json:"extension_num"` //分机号
  44 + EntryTime string `json:"entry_time"` //入职时间
  45 + WorkSpace string `json:"work_space"`
  46 + IsBusiness int8 `json:"is_business"`
  47 + Status int8 `json:"status"`
  48 + Avatar string `json:"avatar"`
  49 + ExtraText string `json:"extra_text"`
  50 + Remarks string `json:"remarks"`
  51 + AdminType int8 `json:"admin_type"`
  52 + ChargeStatus int8 `json:"charge_status"`
  53 + UserDepartments []UserDepartData `json:"user_departments"`
  54 + UserPositions []UserPositionData `json:"user_positions"`
  55 +}
  56 +
  57 +type DeleteUserData struct {
  58 + CompanyId int64 `json:"company_id"`
  59 + Ids []int64 `json:"ids"`
  60 +}
  61 +
  62 +type ForbidAllowUserData struct {
  63 + CompanyId int64 `json:"company_id"`
  64 + Ids []int64 `json:"ids"`
  65 + Status int8 `json:"status"`
  66 +}
  67 +
  68 +type RemoveUserDepartmentData struct {
  69 + CompanyId int64 `json:"company_id"`
  70 + UserIds []int64 `json:"user_ids"`
  71 + DepartmentIds []int64 `json:"department_ids"`
28 } 72 }
29 73
30 var _ PlatformAction = ModuleEmployee{} 74 var _ PlatformAction = ModuleEmployee{}
@@ -33,34 +77,73 @@ var _ PlatformAction = ModuleEmployee{} @@ -33,34 +77,73 @@ var _ PlatformAction = ModuleEmployee{}
33 func (m ModuleEmployee) DoAction(code string, jsondata []byte) error { 77 func (m ModuleEmployee) DoAction(code string, jsondata []byte) error {
34 switch code { 78 switch code {
35 case "edit": 79 case "edit":
36 - // return UpdateDepartmentData(m) 80 + var (
  81 + data ModuleEmployee
  82 + err error
  83 + )
  84 + err = json.Unmarshal(jsondata, &data)
  85 + if err != nil {
  86 + return fmt.Errorf("数据解析失败:%s", err)
  87 + }
  88 + return UpdateEmployeeData(data)
37 case "add": 89 case "add":
38 var ( 90 var (
39 - data []ModuleEmployee 91 + data ModuleEmployee
40 err error 92 err error
41 ) 93 )
42 err = json.Unmarshal(jsondata, &data) 94 err = json.Unmarshal(jsondata, &data)
43 if err != nil { 95 if err != nil {
44 return fmt.Errorf("数据解析失败:%s", err) 96 return fmt.Errorf("数据解析失败:%s", err)
45 } 97 }
46 - return AddEmployeeData(data)  
47 - case "delete": 98 + datas := []ModuleEmployee{data}
  99 + return AddEmployeeData(datas)
  100 + case "batchDelete":
48 var ( 101 var (
49 - err error  
50 - ids []int64 102 + err error
  103 + data DeleteUserData
51 ) 104 )
52 - err = json.Unmarshal(jsondata, &ids) 105 + err = json.Unmarshal(jsondata, &data)
53 if err != nil { 106 if err != nil {
54 return fmt.Errorf("数据解析失败:%s", err) 107 return fmt.Errorf("数据解析失败:%s", err)
55 } 108 }
56 - if len(ids) == 0 {  
57 - return fmt.Errorf("参数错误") 109 + return DeleteEmployeeData(data)
  110 + case "batchForbid":
  111 + //批量禁用、启用
  112 + var (
  113 + err error
  114 + data ForbidAllowUserData
  115 + )
  116 + err = json.Unmarshal(jsondata, &data)
  117 + if err != nil {
  118 + return fmt.Errorf("数据解析失败:%s", err)
  119 + }
  120 + return ForbidAllowEmployeeData(data)
  121 + case "batchRemove":
  122 + //移动通讯录用户部门
  123 + var (
  124 + err error
  125 + data RemoveUserDepartmentData
  126 + )
  127 + err = json.Unmarshal(jsondata, &data)
  128 + if err != nil {
  129 + return fmt.Errorf("数据解析失败:%s", err)
58 } 130 }
59 - return DeleteEmployeeData(ids) 131 + return RemoveUserDepartment(data)
  132 + case "import":
  133 + //导入用户
  134 + var (
  135 + data []ModuleEmployee
  136 + err error
  137 + )
  138 + err = json.Unmarshal(jsondata, &data)
  139 + if err != nil {
  140 + return fmt.Errorf("数据解析失败:%s", err)
  141 + }
  142 + return AddEmployeeData(data)
60 default: 143 default:
61 return errors.New("action not found") 144 return errors.New("action not found")
62 } 145 }
63 - return nil 146 + // return nil
64 } 147 }
65 148
66 // AddEmployeeData ... 149 // AddEmployeeData ...
@@ -76,9 +159,10 @@ func AddEmployeeData(data []ModuleEmployee) error { @@ -76,9 +159,10 @@ func AddEmployeeData(data []ModuleEmployee) error {
76 userpositionData []*models.UserPosition 159 userpositionData []*models.UserPosition
77 nowTime = time.Now() 160 nowTime = time.Now()
78 ) 161 )
79 - companydata, err = models.GetCompanyByUCenter(data[0].Companyid) 162 +
  163 + companydata, err = models.GetCompanyByUCenter(data[0].CompanyId)
80 if err != nil { 164 if err != nil {
81 - log.Error("GetCompanyByUCenter获取公司数据失败,ucenterId=%d,err:%s", data[0].Companyid, err) 165 + log.Error("GetCompanyByUCenter获取公司数据失败,ucenterId=%d,err:%s", data[0].CompanyId, err)
82 return errors.New("获取公司数据失败") 166 return errors.New("获取公司数据失败")
83 } 167 }
84 168
@@ -86,7 +170,7 @@ func AddEmployeeData(data []ModuleEmployee) error { @@ -86,7 +170,7 @@ func AddEmployeeData(data []ModuleEmployee) error {
86 var ( 170 var (
87 userdata *models.User 171 userdata *models.User
88 ) 172 )
89 - userdata, err = models.GetUserByUCenter(v.UCenterId) 173 + userdata, err = models.GetUserByUCenter(v.OpenId)
90 if err != nil && err != orm.ErrNoRows { 174 if err != nil && err != orm.ErrNoRows {
91 log.Error("获取用户数据失败,err:%s", err) 175 log.Error("获取用户数据失败,err:%s", err)
92 return errors.New("获取用户数据失败") 176 return errors.New("获取用户数据失败")
@@ -94,8 +178,8 @@ func AddEmployeeData(data []ModuleEmployee) error { @@ -94,8 +178,8 @@ func AddEmployeeData(data []ModuleEmployee) error {
94 if err == orm.ErrNoRows { 178 if err == orm.ErrNoRows {
95 //添加用户 179 //添加用户
96 userdata = &models.User{ 180 userdata = &models.User{
97 - UserCenterId: v.UCenterId,  
98 - NickName: v.Nickname, 181 + UserCenterId: v.OpenId,
  182 + NickName: v.Name,
99 Phone: v.Phone, 183 Phone: v.Phone,
100 } 184 }
101 _, err = models.AddUser(userdata) 185 _, err = models.AddUser(userdata)
@@ -104,31 +188,47 @@ func AddEmployeeData(data []ModuleEmployee) error { @@ -104,31 +188,47 @@ func AddEmployeeData(data []ModuleEmployee) error {
104 return err 188 return err
105 } 189 }
106 } else { 190 } else {
107 - userdata.NickName = v.Nickname 191 + userdata.NickName = v.Name
108 userdata.Phone = v.Phone 192 userdata.Phone = v.Phone
109 models.UpdateUserById(userdata, []string{"NickName", "Phone"}) 193 models.UpdateUserById(userdata, []string{"NickName", "Phone"})
110 } 194 }
111 uc := &models.UserCompany{ 195 uc := &models.UserCompany{
112 - Id: v.Userid,  
113 - CompanyId: companydata.Id,  
114 - UserId: userdata.Id,  
115 - NickName: v.Nickname, 196 + Id: v.Id,
  197 + CompanyId: companydata.Id,
  198 + UserId: userdata.Id,
  199 + NickName: v.Name,
  200 + OpenId: v.OpenId,
  201 + Sex: v.Sex,
  202 + JobNum: v.JobNum,
  203 + Phone: v.Phone,
  204 + PrivatePhone: v.PrivatePhone,
  205 + Email: v.Email,
  206 + ExtensionNum: v.ExtensionNum,
  207 + Workspace: v.WorkSpace,
  208 + IsBusiness: v.IsBusiness,
  209 + Enable: v.Status,
  210 + Avatar: v.Avatar,
  211 + Remarks: v.Remarks,
  212 + AdminType: v.AdminType,
  213 + ChargeStatus: v.ChargeStatus,
  214 + ExtraText: v.ExtraText,
116 } 215 }
  216 + uc.EntryTime, _ = time.ParseInLocation("2006-01-02", v.EntryTime, time.Local)
117 usercompanydata = append(usercompanydata, uc) 217 usercompanydata = append(usercompanydata, uc)
118 - for i := range v.DepartmentId { 218 + for i := range v.UserDepartments {
119 d := &models.UserDepartment{ 219 d := &models.UserDepartment{
120 CompanyId: companydata.Id, 220 CompanyId: companydata.Id,
121 - UserCompanyId: v.Userid,  
122 - DepartmentId: v.DepartmentId[i], 221 + UserCompanyId: v.UserDepartments[i].UserId,
  222 + DepartmentId: v.UserDepartments[i].DepartmentId,
123 CreateTime: nowTime, 223 CreateTime: nowTime,
124 } 224 }
125 userdepartmentData = append(userdepartmentData, d) 225 userdepartmentData = append(userdepartmentData, d)
126 } 226 }
127 - for i := range v.Position { 227 + for i := range v.UserPositions {
128 p := &models.UserPosition{ 228 p := &models.UserPosition{
129 CompanyId: companydata.Id, 229 CompanyId: companydata.Id,
130 - UserCompanyId: v.Userid,  
131 - PositionId: v.Position[i], 230 + UserCompanyId: v.UserPositions[i].UserId,
  231 + PositionId: v.UserPositions[i].PositionId,
132 CreateAt: nowTime, 232 CreateAt: nowTime,
133 } 233 }
134 userpositionData = append(userpositionData, p) 234 userpositionData = append(userpositionData, p)
@@ -143,7 +243,7 @@ func AddEmployeeData(data []ModuleEmployee) error { @@ -143,7 +243,7 @@ func AddEmployeeData(data []ModuleEmployee) error {
143 o.Rollback() 243 o.Rollback()
144 return e 244 return e
145 } 245 }
146 - //添加部门职位 246 + //添加用户的职位
147 _, err = o.InsertMulti(20, &userpositionData) 247 _, err = o.InsertMulti(20, &userpositionData)
148 if err != nil { 248 if err != nil {
149 e := fmt.Errorf("插入user_position数据失败,err:%s", err) 249 e := fmt.Errorf("插入user_position数据失败,err:%s", err)
@@ -151,6 +251,7 @@ func AddEmployeeData(data []ModuleEmployee) error { @@ -151,6 +251,7 @@ func AddEmployeeData(data []ModuleEmployee) error {
151 o.Rollback() 251 o.Rollback()
152 return e 252 return e
153 } 253 }
  254 + //添加用户的部门
154 _, err = o.InsertMulti(20, &userdepartmentData) 255 _, err = o.InsertMulti(20, &userdepartmentData)
155 if err != nil { 256 if err != nil {
156 e := fmt.Errorf("批量插入user_department数据失败,err:%s", err) 257 e := fmt.Errorf("批量插入user_department数据失败,err:%s", err)
@@ -170,19 +271,19 @@ func UpdateEmployeeData(data ModuleEmployee) error { @@ -170,19 +271,19 @@ func UpdateEmployeeData(data ModuleEmployee) error {
170 companyInfo *models.Company 271 companyInfo *models.Company
171 userdata *models.User 272 userdata *models.User
172 ) 273 )
173 - userdata, err = models.GetUserByUCenter(data.UCenterId) 274 + userdata, err = models.GetUserByUCenter(data.OpenId)
174 if err != nil { 275 if err != nil {
175 e := fmt.Errorf("获取用户数据失败:%s", err) 276 e := fmt.Errorf("获取用户数据失败:%s", err)
176 log.Error(e.Error()) 277 log.Error(e.Error())
177 return e 278 return e
178 } 279 }
179 - companyInfo, err = models.GetCompanyByUCenter(data.Companyid) 280 + companyInfo, err = models.GetCompanyByUCenter(data.CompanyId)
180 if err != nil { 281 if err != nil {
181 e := fmt.Errorf("获取公司数据失败:%s", err) 282 e := fmt.Errorf("获取公司数据失败:%s", err)
182 log.Error(e.Error()) 283 log.Error(e.Error())
183 return e 284 return e
184 } 285 }
185 - ucompany, err = models.GetUserCompanyById(data.Userid) 286 + ucompany, err = models.GetUserCompanyById(data.Id)
186 if err != nil { 287 if err != nil {
187 e := fmt.Errorf("获取用户和公司的关联数据失败:%s", err) 288 e := fmt.Errorf("获取用户和公司的关联数据失败:%s", err)
188 log.Error(e.Error()) 289 log.Error(e.Error())
@@ -191,30 +292,52 @@ func UpdateEmployeeData(data ModuleEmployee) error { @@ -191,30 +292,52 @@ func UpdateEmployeeData(data ModuleEmployee) error {
191 o := orm.NewOrm() 292 o := orm.NewOrm()
192 o.Begin() 293 o.Begin()
193 //更新用户数据 294 //更新用户数据
194 - userdata.NickName = data.Nickname 295 + userdata.NickName = data.Name
195 userdata.Phone = data.Phone 296 userdata.Phone = data.Phone
  297 +
196 err = models.UpdateUserById(userdata, []string{"NickName", "Phone"}, o) 298 err = models.UpdateUserById(userdata, []string{"NickName", "Phone"}, o)
197 if err != nil { 299 if err != nil {
198 o.Rollback() 300 o.Rollback()
199 log.Error("UpdateUserById err:%s", err) 301 log.Error("UpdateUserById err:%s", err)
200 return errors.New("更新用户数据失败") 302 return errors.New("更新用户数据失败")
201 } 303 }
202 - ucompany.NickName = data.Nickname  
203 - models.UpdateUserCompanyById(ucompany, []string{"NickName"}, o) 304 + ucompany.NickName = data.Name
  305 + ucompany.NickName = data.Name
  306 + ucompany.OpenId = data.OpenId
  307 + ucompany.Sex = data.Sex
  308 + ucompany.JobNum = data.JobNum
  309 + ucompany.Phone = data.Phone
  310 + ucompany.PrivatePhone = data.PrivatePhone
  311 + ucompany.Email = data.Email
  312 + ucompany.ExtensionNum = data.ExtensionNum
  313 + ucompany.Workspace = data.WorkSpace
  314 + ucompany.IsBusiness = data.IsBusiness
  315 + ucompany.Enable = data.Status
  316 + ucompany.Avatar = data.Avatar
  317 + ucompany.Remarks = data.Remarks
  318 + ucompany.AdminType = data.AdminType
  319 + ucompany.ChargeStatus = data.ChargeStatus
  320 + ucompany.ExtraText = data.ExtraText
  321 + cols := []string{"NickName", "OpenId", "Sex", "JobNum",
  322 + "Phone", "PrivatePhone", "Email", "ExtensionNum", "Workspace",
  323 + "IsBusiness", "Status", "Avatar", "Remarks", "AdminType", "ChargeStatus",
  324 + "ExtraText",
  325 + }
  326 + err = models.UpdateUserCompanyById(ucompany, cols, o)
204 if err != nil { 327 if err != nil {
205 o.Rollback() 328 o.Rollback()
206 log.Error("UpdateUserCompanyById err:%s", err) 329 log.Error("UpdateUserCompanyById err:%s", err)
207 return errors.New("更新用户数据失败") 330 return errors.New("更新用户数据失败")
208 } 331 }
209 //添加部门 332 //添加部门
210 - err = editUserDepart(data.Userid, companyInfo.Id, data.DepartmentId, o) 333 + err = editUserDepart(data.Id, companyInfo.Id, data.UserDepartments, o)
211 if err != nil { 334 if err != nil {
212 o.Rollback() 335 o.Rollback()
213 log.Error("editUserDepart err:%s", err) 336 log.Error("editUserDepart err:%s", err)
214 return protocol.NewErrWithMessage("1") 337 return protocol.NewErrWithMessage("1")
215 } 338 }
216 //添加职位 339 //添加职位
217 - err = editUserPosition(data.Userid, companyInfo.Id, data.Position, o) 340 + err = editUserPosition(data.Id, companyInfo.Id, data.UserPositions, o)
218 if err != nil { 341 if err != nil {
219 o.Rollback() 342 o.Rollback()
220 log.Error("editUserDepart err:%s", err) 343 log.Error("editUserDepart err:%s", err)
@@ -224,89 +347,126 @@ func UpdateEmployeeData(data ModuleEmployee) error { @@ -224,89 +347,126 @@ func UpdateEmployeeData(data ModuleEmployee) error {
224 return nil 347 return nil
225 } 348 }
226 349
227 -func editUserDepart(usercompanyid int64, companyid int64, departids []int64, o orm.Ormer) error { 350 +func editUserDepart(usercompanyid int64, companyid int64, userDepart []UserDepartData, o orm.Ormer) error {
228 var ( 351 var (
229 oldDepartIds []int64 352 oldDepartIds []int64
  353 + newDepartIds []int64
230 err error 354 err error
231 delDepartIds []int64 355 delDepartIds []int64
232 addDepartIds []int64 356 addDepartIds []int64
  357 + addDepart []models.UserDepartment
233 ) 358 )
234 - dataSql := `SELECT department_id FROM user_department WHERE enable_status=1 AND user_company_id=?`  
235 - dataSql2 := `UPDATE user_department SET enable_status = 2 WHERE user_company_id=? AND department_id=?`  
236 - datasql3 := `INSERT INTO user_department (user_company_id,department_id,company_id,enable_status,create_time) VALUES (?,?,?,?,?)` 359 + for i := range userDepart {
  360 + newDepartIds = append(newDepartIds, userDepart[i].DepartmentId)
  361 + }
  362 + dataSql := `SELECT department_id FROM user_department WHERE enable_status=1 AND user_company_id=? `
237 err = utils.ExecuteQueryAllWithOrmer(o, &oldDepartIds, dataSql, usercompanyid) 363 err = utils.ExecuteQueryAllWithOrmer(o, &oldDepartIds, dataSql, usercompanyid)
238 if err != nil { 364 if err != nil {
239 e := fmt.Errorf("Execute SQL err:%s", err) 365 e := fmt.Errorf("Execute SQL err:%s", err)
240 log.Error(e.Error()) 366 log.Error(e.Error())
241 return e 367 return e
242 } 368 }
243 - delDepartIds = utils.ArrayInt64Diff(oldDepartIds, departids)  
244 - addDepartIds = utils.ArrayInt64Diff(departids, oldDepartIds)  
245 - for _, v := range delDepartIds {  
246 - err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v) 369 + delDepartIds = utils.ArrayInt64Diff(oldDepartIds, newDepartIds)
  370 + addDepartIds = utils.ArrayInt64Diff(newDepartIds, oldDepartIds)
  371 + nowTime := time.Now()
  372 + for _, v := range addDepartIds {
  373 + m := models.UserDepartment{
  374 + UserCompanyId: usercompanyid,
  375 + DepartmentId: v,
  376 + EnableStatus: models.USER_DEPARTMENT_ENABLE_YES,
  377 + CompanyId: companyid,
  378 + CreateTime: nowTime,
  379 + }
  380 + addDepart = append(addDepart, m)
  381 + }
  382 + if len(delDepartIds) > 0 {
  383 + _, err = o.QueryTable(&models.UserDepartment{}).
  384 + Filter("id__in", delDepartIds).
  385 + Update(orm.Params{
  386 + "enable_status": models.USER_DEPARTMENT_ENABLE_NO,
  387 + })
247 if err != nil { 388 if err != nil {
248 - e := fmt.Errorf("Execute SQL err:%s", err)  
249 - log.Error(e.Error())  
250 - return e 389 + log.Error("删除用户部门失败: 用户id=%d,err:%s", usercompanyid, err)
  390 + return errors.New("删除用户部门失败")
251 } 391 }
252 } 392 }
253 - nowTime := time.Now().String()  
254 - for _, v := range addDepartIds {  
255 - err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime) 393 + if len(addDepart) > 0 {
  394 + _, err = o.InsertMulti(10, addDepart)
256 if err != nil { 395 if err != nil {
257 - e := fmt.Errorf("Execute SQL err:%s", err)  
258 - log.Error(e.Error())  
259 - return e 396 + log.Error("添加用户的部门失败 用户id=%d,err:%s", usercompanyid, err)
  397 + return errors.New("添加用户的部门失败")
260 } 398 }
261 } 399 }
262 return nil 400 return nil
263 } 401 }
264 402
265 -func editUserPosition(usercompanyid int64, companyid int64, positionids []int64, o orm.Ormer) error { 403 +func editUserPosition(usercompanyid int64, companyid int64, userPosition []UserPositionData, o orm.Ormer) error {
266 var ( 404 var (
267 oldPositionIds []int64 405 oldPositionIds []int64
  406 + newPositionIds []int64
268 err error 407 err error
269 delPositionIds []int64 408 delPositionIds []int64
270 addPositionIds []int64 409 addPositionIds []int64
  410 + addPosition []models.UserPosition
271 ) 411 )
  412 + for _, v := range userPosition {
  413 + newPositionIds = append(newPositionIds, v.PositionId)
  414 + }
272 dataSql := `SELECT position_id FROM user_position WHERE enable_status=1 AND user_company_id=?` 415 dataSql := `SELECT position_id FROM user_position WHERE enable_status=1 AND user_company_id=?`
273 - dataSql2 := `UPDATE user_position SET enable_status = 2 WHERE user_company_id=? AND position_id=?`  
274 - datasql3 := `INSERT INTO user_position (user_company_id,position_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`  
275 err = utils.ExecuteQueryAllWithOrmer(o, &oldPositionIds, dataSql, usercompanyid) 416 err = utils.ExecuteQueryAllWithOrmer(o, &oldPositionIds, dataSql, usercompanyid)
276 if err != nil { 417 if err != nil {
277 e := fmt.Errorf("Execute SQL err:%s", err) 418 e := fmt.Errorf("Execute SQL err:%s", err)
278 log.Error(e.Error()) 419 log.Error(e.Error())
279 return e 420 return e
280 } 421 }
281 - delPositionIds = utils.ArrayInt64Diff(oldPositionIds, positionids)  
282 - addPositionIds = utils.ArrayInt64Diff(positionids, oldPositionIds)  
283 - for _, v := range delPositionIds {  
284 - err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v) 422 + delPositionIds = utils.ArrayInt64Diff(oldPositionIds, newPositionIds)
  423 + addPositionIds = utils.ArrayInt64Diff(newPositionIds, oldPositionIds)
  424 + nowTime := time.Now()
  425 + for _, v := range addPositionIds {
  426 + m := models.UserPosition{
  427 + UserCompanyId: usercompanyid,
  428 + PositionId: v,
  429 + EnableStatus: models.USER_DEPARTMENT_ENABLE_YES,
  430 + CompanyId: companyid,
  431 + CreateAt: nowTime,
  432 + }
  433 + addPosition = append(addPosition, m)
  434 + }
  435 + if len(delPositionIds) > 0 {
  436 + _, err = o.QueryTable(&models.UserPosition{}).
  437 + Filter("id__in", delPositionIds).
  438 + Update(orm.Params{
  439 + "enable_status": models.USER_POSITION_ENABLE_NO,
  440 + })
285 if err != nil { 441 if err != nil {
286 - e := fmt.Errorf("Execute SQL err:%s", err)  
287 - log.Error(e.Error())  
288 - return e 442 + log.Error("删除用户职位失败: 用户id=%d,err:%s", usercompanyid, err)
  443 + return errors.New("删除用户部门失败")
289 } 444 }
290 } 445 }
291 - nowTime := time.Now().String()  
292 - for _, v := range addPositionIds {  
293 - err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime) 446 + if len(addPosition) > 0 {
  447 + _, err = o.InsertMulti(10, addPosition)
294 if err != nil { 448 if err != nil {
295 - e := fmt.Errorf("Execute SQL err:%s", err)  
296 - log.Error(e.Error())  
297 - return e 449 + log.Error("添加用户的职位失败 用户id=%d,err:%s", usercompanyid, err)
  450 + return errors.New("添加用户的部门失败")
298 } 451 }
299 } 452 }
300 return nil 453 return nil
301 } 454 }
302 455
303 // DeleteEmployeeData ... 456 // DeleteEmployeeData ...
304 -func DeleteEmployeeData(ids []int64) error { 457 +func DeleteEmployeeData(data DeleteUserData) error {
  458 + companyData, err := models.GetCompanyByUCenter(data.CompanyId)
  459 + if err != nil {
  460 + log.Error("h获取公司数据失败,user_center_id=%d,err:%s", data.CompanyId, err)
  461 + return errors.New("获取公司数据失败")
  462 + }
305 o := orm.NewOrm() 463 o := orm.NewOrm()
306 - _, err := o.QueryTable(&models.UserCompany{}).  
307 - Filter("id__in", ids). 464 + _, err = o.QueryTable(&models.UserCompany{}).
  465 + Filter("id__in", data.Ids).
  466 + Filter("company_id", companyData.Id).
308 Update(orm.Params{ 467 Update(orm.Params{
309 - "delete_at": time.Now().String(), 468 + "delete_at": time.Now().Format("2006-01-02 15:04:05"),
  469 + "enable": models.USERCOMPANY_ENABLE_NO,
310 }) 470 })
311 if err != nil { 471 if err != nil {
312 log.Error("更新user_company数据失败:%s", err) 472 log.Error("更新user_company数据失败:%s", err)
@@ -314,3 +474,69 @@ func DeleteEmployeeData(ids []int64) error { @@ -314,3 +474,69 @@ func DeleteEmployeeData(ids []int64) error {
314 } 474 }
315 return nil 475 return nil
316 } 476 }
  477 +
  478 +func ForbidAllowEmployeeData(data ForbidAllowUserData) error {
  479 + companyData, err := models.GetCompanyByUCenter(data.CompanyId)
  480 + if err != nil {
  481 + log.Error("h获取公司数据失败,user_center_id=%d,err:%s", data.CompanyId, err)
  482 + return errors.New("获取公司数据失败")
  483 + }
  484 + var status int8
  485 + switch data.Status {
  486 + case 1:
  487 + status = models.USERCOMPANY_ENABLE_YES
  488 + case 2:
  489 + status = models.USERCOMPANY_ENABLE_NO
  490 + }
  491 + o := orm.NewOrm()
  492 + _, err = o.QueryTable(&models.UserCompany{}).
  493 + Filter("id__in", data.Ids).
  494 + Filter("company_id", companyData.Id).
  495 + Update(orm.Params{
  496 + "enable": status,
  497 + "update_at": time.Now().Format("2006-01-02 15:04:05"),
  498 + })
  499 + if err != nil {
  500 + log.Error("更新user_company数据失败:%s", err)
  501 + return errors.New("删除user_company数据失败")
  502 + }
  503 + return nil
  504 +}
  505 +
  506 +func RemoveUserDepartment(data RemoveUserDepartmentData) error {
  507 + var (
  508 + err error
  509 + companyData *models.Company
  510 + )
  511 + userDepartmentDatas := make(map[int64][]UserDepartData)
  512 + companyData, err = models.GetCompanyByUCenter(data.CompanyId)
  513 + if err != nil {
  514 + log.Error("h获取公司数据失败,user_center_id=%d,err:%s", data.CompanyId, err)
  515 + return errors.New("获取公司数据失败")
  516 + }
  517 + for _, v := range data.UserIds {
  518 + var userDapart []UserDepartData
  519 + for _, vv := range data.DepartmentIds {
  520 + m := UserDepartData{
  521 + CompanyId: companyData.Id,
  522 + UserId: v,
  523 + DepartmentId: vv,
  524 + }
  525 + userDapart = append(userDapart, m)
  526 + }
  527 + userDepartmentDatas[v] = userDapart
  528 + }
  529 +
  530 + o := orm.NewOrm()
  531 + o.Begin()
  532 + for k, v := range userDepartmentDatas {
  533 + err = editUserDepart(k, companyData.Id, v, o)
  534 + if err != nil {
  535 + o.Rollback()
  536 + log.Error("更新用户部门失败;user_company_id=%d,err:%s", k, err)
  537 + return errors.New("更新用户部门失败")
  538 + }
  539 + }
  540 + o.Commit()
  541 + return nil
  542 +}