作者 唐旭辉

修改员工编辑功能

@@ -13,6 +13,7 @@ type UserCompany struct { @@ -13,6 +13,7 @@ type UserCompany struct {
13 Id int64 `orm:"column(id);auto" description:"唯一标识"` 13 Id int64 `orm:"column(id);auto" description:"唯一标识"`
14 CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"` 14 CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
15 UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"` 15 UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
  16 + NickName string `orm:"column(nick_name)"`
16 ChanceTotal int `orm:"column(chance_total)" description:"发表机会数"` 17 ChanceTotal int `orm:"column(chance_total)" description:"发表机会数"`
17 CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"` 18 CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"`
18 CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` 19 CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
@@ -53,6 +54,15 @@ func init() { @@ -53,6 +54,15 @@ func init() {
53 orm.RegisterModel(new(UserCompany)) 54 orm.RegisterModel(new(UserCompany))
54 } 55 }
55 56
  57 +func GetUserCompanyById(id int64) (v *UserCompany, err error) {
  58 + o := orm.NewOrm()
  59 + v = &UserCompany{Id: id}
  60 + if err = o.Read(v); err == nil {
  61 + return v, nil
  62 + }
  63 + return nil, err
  64 +}
  65 +
56 // AddUserCompany insert a new UserCompany into database and returns 66 // AddUserCompany insert a new UserCompany into database and returns
57 // last inserted Id on success. 67 // last inserted Id on success.
58 func AddUserCompany(m *UserCompany, o orm.Ormer) (id int64, err error) { 68 func AddUserCompany(m *UserCompany, o orm.Ormer) (id int64, err error) {
@@ -66,16 +76,19 @@ func AddUserCompany(m *UserCompany, o orm.Ormer) (id int64, err error) { @@ -66,16 +76,19 @@ func AddUserCompany(m *UserCompany, o orm.Ormer) (id int64, err error) {
66 76
67 // UpdateUserCompany updates UserCompany by Id and returns error if 77 // UpdateUserCompany updates UserCompany by Id and returns error if
68 // the record to be updated doesn't exist 78 // the record to be updated doesn't exist
69 -func UpdateUserCompanyById(m *UserCompany) (err error) {  
70 - o := orm.NewOrm()  
71 - v := UserCompany{Id: m.Id}  
72 - // ascertain id exists in the database  
73 - if err = o.Read(&v); err == nil { 79 +func UpdateUserCompanyById(m *UserCompany, col []string, om ...orm.Ormer) (err error) {
  80 + var o orm.Ormer
  81 + if len(om) > 0 {
  82 + o = om[0]
  83 + } else {
  84 + o = orm.NewOrm()
  85 + }
74 var num int64 86 var num int64
75 - if num, err = o.Update(m); err == nil { 87 + m.UpdateAt = time.Now()
  88 + if num, err = o.Update(m, col...); err == nil {
76 fmt.Println("Number of records updated in database:", num) 89 fmt.Println("Number of records updated in database:", num)
77 } 90 }
78 - } 91 +
79 return 92 return
80 } 93 }
81 94
@@ -9,7 +9,7 @@ import ( @@ -9,7 +9,7 @@ import (
9 9
10 type UserDepartment struct { 10 type UserDepartment struct {
11 Id int64 `orm:"column(id);auto" description:"主键"` 11 Id int64 `orm:"column(id);auto" description:"主键"`
12 - UserId int64 `orm:"column(user_id)" description:"用户id"` 12 + //UserId int64 `orm:"column(user_id)" description:"用户id"`
13 CompanyId int64 `orm:"column(company_id)" description:"公司id"` 13 CompanyId int64 `orm:"column(company_id)" description:"公司id"`
14 DepartmentId int64 `orm:"column(department_id)" description:"部门id"` 14 DepartmentId int64 `orm:"column(department_id)" description:"部门id"`
15 CreateTime time.Time `orm:"column(create_time);type(timestamp);null" description:"创建时间"` 15 CreateTime time.Time `orm:"column(create_time);type(timestamp);null" description:"创建时间"`
@@ -9,7 +9,7 @@ import ( @@ -9,7 +9,7 @@ import (
9 9
10 type UserPosition struct { 10 type UserPosition struct {
11 Id int64 `orm:"column(id)" description:"唯一键值"` 11 Id int64 `orm:"column(id)" description:"唯一键值"`
12 - UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"` 12 + //UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
13 PositionId int64 `orm:"column(position_id)" description:"表position.id 职位编号"` 13 PositionId int64 `orm:"column(position_id)" description:"表position.id 职位编号"`
14 CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"` 14 CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
15 CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"` 15 CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
@@ -9,7 +9,6 @@ import ( @@ -9,7 +9,6 @@ import (
9 type UserRole struct { 9 type UserRole struct {
10 Id int `orm:"column(id)"` 10 Id int `orm:"column(id)"`
11 RoleId int64 `orm:"column(role_id)"` 11 RoleId int64 `orm:"column(role_id)"`
12 - UserId int64 `orm:"column(user_id)"`  
13 EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"` 12 EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
14 CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"` 13 CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
15 UserCompanyId int64 `orm:"column(user_company_id)"` 14 UserCompanyId int64 `orm:"column(user_company_id)"`
@@ -43,7 +43,7 @@ type PositionBase struct { @@ -43,7 +43,7 @@ type PositionBase struct {
43 //DepartUserBase 下拉选择列表-部门下的人员 43 //DepartUserBase 下拉选择列表-部门下的人员
44 type DepartUserBase struct { 44 type DepartUserBase struct {
45 UserCompanyId int64 `json:"id" orm:"column(user_company_id)"` 45 UserCompanyId int64 `json:"id" orm:"column(user_company_id)"`
46 - Name string `json:"name" orm:"-"` 46 + NickName string `json:"name" orm:"column(nick_name)"`
47 } 47 }
48 48
49 //部门和人员混合 49 //部门和人员混合
@@ -89,7 +89,7 @@ type RequestUserAdd struct { @@ -89,7 +89,7 @@ type RequestUserAdd struct {
89 89
90 //RequestUserEdit 编辑用户 90 //RequestUserEdit 编辑用户
91 type RequestUserEdit struct { 91 type RequestUserEdit struct {
92 - ID int64 `json:"id"` 92 + UserCompanyID int64 `json:"user_company_id"`
93 RequestUserAdd 93 RequestUserAdd
94 } 94 }
95 95
@@ -290,7 +290,7 @@ func initUserDedaprtmet(newDepartment *models.Department, newUserCompany *models @@ -290,7 +290,7 @@ func initUserDedaprtmet(newDepartment *models.Department, newUserCompany *models
290 //添加部门 290 //添加部门
291 newUserDepartment = &models.UserDepartment{ 291 newUserDepartment = &models.UserDepartment{
292 UserCompanyId: newUserCompany.Id, 292 UserCompanyId: newUserCompany.Id,
293 - UserId: newUserCompany.UserId, 293 + //UserId: newUserCompany.UserId,
294 CompanyId: newUserCompany.CompanyId, 294 CompanyId: newUserCompany.CompanyId,
295 DepartmentId: newDepartment.Id, 295 DepartmentId: newDepartment.Id,
296 } 296 }
@@ -394,7 +394,6 @@ func initUserRole(newUserCompany *models.UserCompany, newRole *models.Role, o or @@ -394,7 +394,6 @@ func initUserRole(newUserCompany *models.UserCompany, newRole *models.Role, o or
394 RoleId: newRole.Id, 394 RoleId: newRole.Id,
395 UserCompanyId: newUserCompany.Id, 395 UserCompanyId: newUserCompany.Id,
396 CompanyId: newUserCompany.CompanyId, 396 CompanyId: newUserCompany.CompanyId,
397 - UserId: newUserCompany.UserId,  
398 } 397 }
399 _, err = models.AddUserRole(newUserRole, o) 398 _, err = models.AddUserRole(newUserRole, o)
400 if err != nil { 399 if err != nil {
@@ -386,33 +386,16 @@ func GetDepartmentUser(companyid int64, departmentid int64) ([]protocol.DepartUs @@ -386,33 +386,16 @@ func GetDepartmentUser(companyid int64, departmentid int64) ([]protocol.DepartUs
386 log.Error("deparment.CompanyId err") 386 log.Error("deparment.CompanyId err")
387 return nil, protocol.NewErrWithMessage("1") 387 return nil, protocol.NewErrWithMessage("1")
388 } 388 }
389 - const dataSql string = `SELECT b.user_id,b.id AS user_company_id FROM user_department AS a 389 + const dataSql string = `SELECT b.nick_name,a.user_company_id FROM user_department AS a
390 JOIN user_company AS b ON a.user_company_id = b.id 390 JOIN user_company AS b ON a.user_company_id = b.id
391 - WHERE a.department_id=? AND b.delete_at=0 `  
392 - type UsercompanyId struct {  
393 - UserCompanyId int64 `orm:"column(user_company_id)"`  
394 - UserId int64 `orm:"column(user_id)"`  
395 - } 391 + WHERE a.department_id=? AND b.delete_at=0 AND a.enable_status = 1 `
396 var ( 392 var (
397 - usercompanyData []UsercompanyId  
398 returnData []protocol.DepartUserBase 393 returnData []protocol.DepartUserBase
399 ) 394 )
400 - err = utils.ExecuteQueryAll(&usercompanyData, dataSql, department.Id) 395 + err = utils.ExecuteQueryAll(&returnData, dataSql, department.Id)
401 if err != nil { 396 if err != nil {
402 log.Error("EXECUTE SQL err:%s", err) 397 log.Error("EXECUTE SQL err:%s", err)
403 return nil, protocol.NewErrWithMessage("1") 398 return nil, protocol.NewErrWithMessage("1")
404 } 399 }
405 - for _, v := range usercompanyData {  
406 - udata, err := models.GetUserById(v.UserId)  
407 - if err != nil {  
408 - log.Error("获取user数据失败:%s", err)  
409 - continue  
410 - }  
411 - d := protocol.DepartUserBase{  
412 - UserCompanyId: v.UserCompanyId,  
413 - Name: udata.NickName,  
414 - }  
415 - returnData = append(returnData, d)  
416 - }  
417 return returnData, nil 400 return returnData, nil
418 } 401 }
@@ -15,11 +15,10 @@ import ( @@ -15,11 +15,10 @@ import (
15 15
16 func UserAdd(param protocol.RequestUserAdd) error { 16 func UserAdd(param protocol.RequestUserAdd) error {
17 var ( 17 var (
18 - userm *models.User  
19 err error 18 err error
20 ) 19 )
21 - //校验用户  
22 - err = validCompanyUser(param.CompanyId, param.Phone) 20 + //校验用户是否存在
  21 + err = existCompanyUser(param.CompanyId, param.Phone)
23 if err != nil { 22 if err != nil {
24 return err 23 return err
25 } 24 }
@@ -41,34 +40,41 @@ func UserAdd(param protocol.RequestUserAdd) error { @@ -41,34 +40,41 @@ func UserAdd(param protocol.RequestUserAdd) error {
41 log.Error(err.Error()) 40 log.Error(err.Error())
42 return err 41 return err
43 } 42 }
44 - userm = &models.User{  
45 - NickName: param.Name,  
46 - Phone: param.Phone,  
47 - } 43 + var (
  44 + userId int64
  45 + userCompanyid int64
  46 + )
48 o := orm.NewOrm() 47 o := orm.NewOrm()
49 o.Begin() 48 o.Begin()
50 - err = registUser(userm, param.CompanyId, o) 49 + //添加用户
  50 + userId, err = addNewUser(param.Name, param.Phone, o)
51 if err != nil { 51 if err != nil {
52 o.Rollback() 52 o.Rollback()
53 - log.Error("registUser err:%s", err) 53 + log.Error("addNewUser err:%s", err)
  54 + return protocol.NewErrWithMessage("1")
  55 + }
  56 + //添加用户公司关系
  57 + userCompanyid, err = addUserCompany(userId, param.CompanyId, param.Name, o)
  58 + if err != nil {
  59 + log.Error("addUserCompany err:%s", err)
54 return protocol.NewErrWithMessage("1") 60 return protocol.NewErrWithMessage("1")
55 } 61 }
56 //添加角色 62 //添加角色
57 - err = editUserRole(userm.Id, param.CompanyId, param.Roles, o) 63 + err = editUserRole(userCompanyid, userId, param.CompanyId, param.Roles, o)
58 if err != nil { 64 if err != nil {
59 o.Rollback() 65 o.Rollback()
60 log.Error("editUserRole err:%s", err) 66 log.Error("editUserRole err:%s", err)
61 return protocol.NewErrWithMessage("1") 67 return protocol.NewErrWithMessage("1")
62 } 68 }
63 //添加部门 69 //添加部门
64 - err = editUserDepart(userm.Id, param.CompanyId, param.Departments, o) 70 + err = editUserDepart(userCompanyid, userId, param.CompanyId, param.Departments, o)
65 if err != nil { 71 if err != nil {
66 o.Rollback() 72 o.Rollback()
67 log.Error("editUserDepart err:%s", err) 73 log.Error("editUserDepart err:%s", err)
68 return protocol.NewErrWithMessage("1") 74 return protocol.NewErrWithMessage("1")
69 } 75 }
70 //添加职位 76 //添加职位
71 - err = editUserDepart(userm.Id, param.CompanyId, param.Positions, o) 77 + err = editUserPosition(userCompanyid, userId, param.CompanyId, param.Positions, o)
72 if err != nil { 78 if err != nil {
73 o.Rollback() 79 o.Rollback()
74 log.Error("editUserDepart err:%s", err) 80 log.Error("editUserDepart err:%s", err)
@@ -141,7 +147,7 @@ func validCompanyPosition(companyid int64, positionid []int64) error { @@ -141,7 +147,7 @@ func validCompanyPosition(companyid int64, positionid []int64) error {
141 return nil 147 return nil
142 } 148 }
143 149
144 -func validCompanyUser(companyid int64, phone string) error { 150 +func existCompanyUser(companyid int64, phone string) error {
145 userdata, err := models.GetUserByPhone(phone) 151 userdata, err := models.GetUserByPhone(phone)
146 if err == orm.ErrNoRows { 152 if err == orm.ErrNoRows {
147 return nil 153 return nil
@@ -159,56 +165,93 @@ func validCompanyUser(companyid int64, phone string) error { @@ -159,56 +165,93 @@ func validCompanyUser(companyid int64, phone string) error {
159 } 165 }
160 166
161 //registUser 注册用户 167 //registUser 注册用户
162 -func registUser(userIn *models.User, companyid int64, o orm.Ormer) error { 168 +// func registUser(userIn *models.User, companyid int64, o orm.Ormer) error {
  169 +// var (
  170 +// err error
  171 +// usrData *models.User
  172 +// )
  173 +// usrData, err = models.GetUserByPhone(userIn.Phone)
  174 +// if err != nil && err != orm.ErrNoRows {
  175 +// return err
  176 +// }
  177 +// if err == orm.ErrNoRows {
  178 +// //用户不存在 添加用户
  179 +// _, err := models.AddUser(userIn, o)
  180 +// if err != nil {
  181 +// return err
  182 +// }
  183 +// }
  184 +// if err == nil {
  185 +// // 用户存在,更新用户
  186 +// usrData.NickName = userIn.NickName
  187 +// err = models.UpdateUserById(usrData, []string{"NickName"}, o)
  188 +// if err != nil {
  189 +// return err
  190 +// }
  191 +// }
  192 +// musercompany := &models.UserCompany{
  193 +// CompanyId: companyid,
  194 +// UserId: userIn.Id,
  195 +// }
  196 +
  197 +// _, err = models.AddUserCompany(musercompany, o)
  198 +// if err != nil {
  199 +// return err
  200 +// }
  201 +// return nil
  202 +// }
  203 +
  204 +func addNewUser(name string, phone string, o orm.Ormer) (userid int64, err error) {
163 var ( 205 var (
164 - err error  
165 usrData *models.User 206 usrData *models.User
166 ) 207 )
167 - usrData, err = models.GetUserByPhone(userIn.Phone) 208 + usrData, err = models.GetUserByPhone(phone)
168 if err != nil && err != orm.ErrNoRows { 209 if err != nil && err != orm.ErrNoRows {
169 - return err 210 + return 0, err
170 } 211 }
171 if err == orm.ErrNoRows { 212 if err == orm.ErrNoRows {
172 //用户不存在 添加用户 213 //用户不存在 添加用户
173 - _, err := models.AddUser(userIn, o) 214 + usrData = &models.User{
  215 + Phone: phone,
  216 + NickName: name,
  217 + }
  218 + _, err := models.AddUser(usrData, o)
174 if err != nil { 219 if err != nil {
175 - return err 220 + return 0, err
176 } 221 }
  222 + return usrData.Id, nil
177 } 223 }
178 if err == nil { 224 if err == nil {
179 - // 用户存在,更新用户  
180 - usrData.NickName = userIn.NickName  
181 - err = models.UpdateUserById(usrData, []string{"NickName"}, o)  
182 - if err != nil {  
183 - return err  
184 - } 225 + // 用户存在,
  226 + return usrData.Id, nil
185 } 227 }
186 - musercompany := &models.UserCompany{  
187 - CompanyId: companyid,  
188 - UserId: userIn.Id, 228 + return 0, err
  229 +}
  230 +
  231 +func addUserCompany(userId int64, companyId int64, nickname string, o orm.Ormer) (id int64, err error) {
  232 + m := &models.UserCompany{
  233 + UserId: userId,
  234 + CompanyId: companyId,
  235 + NickName: nickname,
189 } 236 }
190 - // ok := models.ExistUserCompany(usrData.Id, companyid)  
191 - // if !ok {  
192 - _, err = models.AddUserCompany(musercompany, o) 237 + id, err = models.AddUserCompany(m, o)
193 if err != nil { 238 if err != nil {
194 - return err 239 + return 0, err
195 } 240 }
196 - // }  
197 - // err = models.EnableUserCompany(usrData.Id, companyid)  
198 - return nil 241 + return id, err
199 } 242 }
200 243
201 -func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Ormer) error { 244 +func editUserDepart(usercompanyid int64, userid int64, companyid int64, departids []int64, o orm.Ormer) error {
202 var ( 245 var (
203 oldDepartIds []int64 246 oldDepartIds []int64
204 err error 247 err error
205 delDepartIds []int64 248 delDepartIds []int64
206 addDepartIds []int64 249 addDepartIds []int64
207 ) 250 )
208 - dataSql := `SELECT department_id FROM user_department WHERE enable_status=1 AND company_id=? AND user_id=?`  
209 - dataSql2 := `UPDATE user_department SET enable_status = 2 WHERE company_id = ? AND user_id=? AND department_id=?`  
210 - datasql3 := `INSERT INTO user_department (user_id,department_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`  
211 - err = utils.ExecuteQueryAllWithOrmer(o, &oldDepartIds, dataSql, companyid, userid) 251 + dataSql := `SELECT department_id FROM user_department WHERE enable_status=1 AND user_company_id=?`
  252 + dataSql2 := `UPDATE user_department SET enable_status = 2 WHERE user_company_id=? AND department_id=?`
  253 + datasql3 := `INSERT INTO user_department (user_company_id,department_id,company_id,enable_status,create_time) VALUES (?,?,?,?,?)`
  254 + err = utils.ExecuteQueryAllWithOrmer(o, &oldDepartIds, dataSql, usercompanyid)
212 if err != nil { 255 if err != nil {
213 e := fmt.Errorf("Execute SQL err:%s", err) 256 e := fmt.Errorf("Execute SQL err:%s", err)
214 log.Error(e.Error()) 257 log.Error(e.Error())
@@ -217,7 +260,7 @@ func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Orme @@ -217,7 +260,7 @@ func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Orme
217 delDepartIds = utils.ArrayInt64Diff(oldDepartIds, departids) 260 delDepartIds = utils.ArrayInt64Diff(oldDepartIds, departids)
218 addDepartIds = utils.ArrayInt64Diff(departids, oldDepartIds) 261 addDepartIds = utils.ArrayInt64Diff(departids, oldDepartIds)
219 for _, v := range delDepartIds { 262 for _, v := range delDepartIds {
220 - err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v) 263 + err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v)
221 if err != nil { 264 if err != nil {
222 e := fmt.Errorf("Execute SQL err:%s", err) 265 e := fmt.Errorf("Execute SQL err:%s", err)
223 log.Error(e.Error()) 266 log.Error(e.Error())
@@ -226,7 +269,7 @@ func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Orme @@ -226,7 +269,7 @@ func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Orme
226 } 269 }
227 nowTime := time.Now().String() 270 nowTime := time.Now().String()
228 for _, v := range addDepartIds { 271 for _, v := range addDepartIds {
229 - err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime) 272 + err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
230 if err != nil { 273 if err != nil {
231 e := fmt.Errorf("Execute SQL err:%s", err) 274 e := fmt.Errorf("Execute SQL err:%s", err)
232 log.Error(e.Error()) 275 log.Error(e.Error())
@@ -236,17 +279,17 @@ func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Orme @@ -236,17 +279,17 @@ func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Orme
236 return nil 279 return nil
237 } 280 }
238 281
239 -func editUserPosition(userid int64, companyid int64, positionids []int64, o orm.Ormer) error { 282 +func editUserPosition(usercompanyid int64, userid int64, companyid int64, positionids []int64, o orm.Ormer) error {
240 var ( 283 var (
241 oldPositionIds []int64 284 oldPositionIds []int64
242 err error 285 err error
243 delPositionIds []int64 286 delPositionIds []int64
244 addPositionIds []int64 287 addPositionIds []int64
245 ) 288 )
246 - dataSql := `SELECT position_id FROM user_position WHERE enable_status=1 AND company_id=? AND user_id=?`  
247 - dataSql2 := `UPDATE user_position SET enable_status = 2 WHERE company_id = ? AND user_id=? AND position_id=?`  
248 - datasql3 := `INSERT INTO user_position (user_id,position_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`  
249 - err = utils.ExecuteQueryAllWithOrmer(o, &oldPositionIds, dataSql, companyid, userid) 289 + dataSql := `SELECT position_id FROM user_position WHERE enable_status=1 AND user_company_id=?`
  290 + dataSql2 := `UPDATE user_position SET enable_status = 2 WHERE user_company_id=? AND position_id=?`
  291 + datasql3 := `INSERT INTO user_position (user_company_id,position_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`
  292 + err = utils.ExecuteQueryAllWithOrmer(o, &oldPositionIds, dataSql, usercompanyid)
250 if err != nil { 293 if err != nil {
251 e := fmt.Errorf("Execute SQL err:%s", err) 294 e := fmt.Errorf("Execute SQL err:%s", err)
252 log.Error(e.Error()) 295 log.Error(e.Error())
@@ -255,7 +298,7 @@ func editUserPosition(userid int64, companyid int64, positionids []int64, o orm. @@ -255,7 +298,7 @@ func editUserPosition(userid int64, companyid int64, positionids []int64, o orm.
255 delPositionIds = utils.ArrayInt64Diff(oldPositionIds, positionids) 298 delPositionIds = utils.ArrayInt64Diff(oldPositionIds, positionids)
256 addPositionIds = utils.ArrayInt64Diff(positionids, oldPositionIds) 299 addPositionIds = utils.ArrayInt64Diff(positionids, oldPositionIds)
257 for _, v := range delPositionIds { 300 for _, v := range delPositionIds {
258 - err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v) 301 + err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v)
259 if err != nil { 302 if err != nil {
260 e := fmt.Errorf("Execute SQL err:%s", err) 303 e := fmt.Errorf("Execute SQL err:%s", err)
261 log.Error(e.Error()) 304 log.Error(e.Error())
@@ -264,7 +307,7 @@ func editUserPosition(userid int64, companyid int64, positionids []int64, o orm. @@ -264,7 +307,7 @@ func editUserPosition(userid int64, companyid int64, positionids []int64, o orm.
264 } 307 }
265 nowTime := time.Now().String() 308 nowTime := time.Now().String()
266 for _, v := range addPositionIds { 309 for _, v := range addPositionIds {
267 - err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime) 310 + err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
268 if err != nil { 311 if err != nil {
269 e := fmt.Errorf("Execute SQL err:%s", err) 312 e := fmt.Errorf("Execute SQL err:%s", err)
270 log.Error(e.Error()) 313 log.Error(e.Error())
@@ -273,17 +316,17 @@ func editUserPosition(userid int64, companyid int64, positionids []int64, o orm. @@ -273,17 +316,17 @@ func editUserPosition(userid int64, companyid int64, positionids []int64, o orm.
273 } 316 }
274 return nil 317 return nil
275 } 318 }
276 -func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) error { 319 +func editUserRole(usercompanyid int64, userid int64, companyid int64, roleids []int64, o orm.Ormer) error {
277 var ( 320 var (
278 oldRoleIds []int64 321 oldRoleIds []int64
279 err error 322 err error
280 delRoleIds []int64 323 delRoleIds []int64
281 addRoleIds []int64 324 addRoleIds []int64
282 ) 325 )
283 - dataSql := `SELECT role_id FROM user_role WHERE enable_status=1 AND company_id=? AND user_id=?`  
284 - dataSql2 := `UPDATE user_role SET enable_status = 2 WHERE company_id =? AND user_id=? AND role_id=?`  
285 - datasql3 := `INSERT INTO user_role (user_id,role_id,company_id,enable_status) VALUES (?,?,?,?)`  
286 - err = utils.ExecuteQueryAllWithOrmer(o, &oldRoleIds, dataSql, companyid, userid) 326 + dataSql := `SELECT role_id FROM user_role WHERE enable_status=1 AND user_company_id=?`
  327 + dataSql2 := `UPDATE user_role SET enable_status = 2 WHERE user_company_id=? AND role_id=?`
  328 + datasql3 := `INSERT INTO user_role (user_company_id,role_id,company_id,enable_status) VALUES (?,?,?,?)`
  329 + err = utils.ExecuteQueryAllWithOrmer(o, &oldRoleIds, dataSql, usercompanyid)
287 if err != nil { 330 if err != nil {
288 e := fmt.Errorf("Execute SQL err:%s", err) 331 e := fmt.Errorf("Execute SQL err:%s", err)
289 log.Error(e.Error()) 332 log.Error(e.Error())
@@ -292,7 +335,7 @@ func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) e @@ -292,7 +335,7 @@ func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) e
292 delRoleIds = utils.ArrayInt64Diff(oldRoleIds, roleids) 335 delRoleIds = utils.ArrayInt64Diff(oldRoleIds, roleids)
293 addRoleIds = utils.ArrayInt64Diff(roleids, oldRoleIds) 336 addRoleIds = utils.ArrayInt64Diff(roleids, oldRoleIds)
294 for _, v := range delRoleIds { 337 for _, v := range delRoleIds {
295 - err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v) 338 + err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v)
296 if err != nil { 339 if err != nil {
297 e := fmt.Errorf("Execute SQL err:%s", err) 340 e := fmt.Errorf("Execute SQL err:%s", err)
298 log.Error(e.Error()) 341 log.Error(e.Error())
@@ -300,7 +343,7 @@ func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) e @@ -300,7 +343,7 @@ func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) e
300 } 343 }
301 } 344 }
302 for _, v := range addRoleIds { 345 for _, v := range addRoleIds {
303 - err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES) 346 + err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES)
304 if err != nil { 347 if err != nil {
305 e := fmt.Errorf("Execute SQL err:%s", err) 348 e := fmt.Errorf("Execute SQL err:%s", err)
306 log.Error(e.Error()) 349 log.Error(e.Error())
@@ -310,12 +353,38 @@ func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) e @@ -310,12 +353,38 @@ func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) e
310 return nil 353 return nil
311 } 354 }
312 355
  356 +func editUserCompany(userCompanyid int64, userid int64, nickname string, o orm.Ormer) error {
  357 + m := &models.UserCompany{
  358 + Id: userCompanyid,
  359 + NickName: nickname,
  360 + UserId: userid,
  361 + }
  362 +
  363 + err := models.UpdateUserCompanyById(m, []string{"NickName", "UserId"}, o)
  364 + return err
  365 +}
  366 +
  367 +//校验user和公司的关系是否正确
  368 +func validUserCompany(userCompanyid int64, companyid int64) (*models.UserCompany, error) {
  369 + ucompany, err := models.GetUserCompanyById(userCompanyid)
  370 + if err != nil {
  371 + log.Error("GetUserCompanyById err;%s", err)
  372 + return nil, protocol.NewErrWithMessage("1")
  373 + }
  374 + if ucompany.CompanyId != companyid {
  375 + log.Error("用户与公司不关联")
  376 + return nil, protocol.NewErrWithMessage("1")
  377 + }
  378 + return ucompany, nil
  379 +}
  380 +
313 func UserEdit(param protocol.RequestUserEdit) error { 381 func UserEdit(param protocol.RequestUserEdit) error {
314 var ( 382 var (
315 - userm *models.User  
316 err error 383 err error
  384 + ucompany *models.UserCompany
317 ) 385 )
318 - err = validCompanyUser(param.CompanyId, param.Phone) 386 + //校验公司和用户的关联
  387 + ucompany, err = validUserCompany(param.UserCompanyID, param.CompanyId)
319 if err != nil { 388 if err != nil {
320 return err 389 return err
321 } 390 }
@@ -323,44 +392,64 @@ func UserEdit(param protocol.RequestUserEdit) error { @@ -323,44 +392,64 @@ func UserEdit(param protocol.RequestUserEdit) error {
323 err = validCompanyRole(param.CompanyId, param.Roles) 392 err = validCompanyRole(param.CompanyId, param.Roles)
324 if err != nil { 393 if err != nil {
325 log.Error(err.Error()) 394 log.Error(err.Error())
326 - return protocol.NewErrWithMessage("10031") 395 + return err
327 } 396 }
328 //校验部门 397 //校验部门
329 err = validCompanyDepart(param.CompanyId, param.Departments) 398 err = validCompanyDepart(param.CompanyId, param.Departments)
330 if err != nil { 399 if err != nil {
331 log.Error(err.Error()) 400 log.Error(err.Error())
332 - return protocol.NewErrWithMessage("10032") 401 + return err
333 } 402 }
334 //校验职位 403 //校验职位
335 err = validCompanyPosition(param.CompanyId, param.Positions) 404 err = validCompanyPosition(param.CompanyId, param.Positions)
336 if err != nil { 405 if err != nil {
337 log.Error(err.Error()) 406 log.Error(err.Error())
338 - return protocol.NewErrWithMessage("10033") 407 + return err
339 } 408 }
340 - userm = &models.User{  
341 - Id: param.ID,  
342 - NickName: param.Name,  
343 - Phone: param.Phone, 409 + //检查用户手机号是否生变更
  410 + var (
  411 + usrData *models.User
  412 + )
  413 + usrData, err = models.GetUserById(ucompany.UserId)
  414 + if err != nil {
  415 + log.Error("获取用户数据失败:%s", err)
  416 + return protocol.NewErrWithMessage("1")
344 } 417 }
345 o := orm.NewOrm() 418 o := orm.NewOrm()
346 o.Begin() 419 o.Begin()
347 - // TODO 用户数据更新 420 + if usrData.Phone != param.Phone {
  421 + //手机号变更
  422 + uid, err := addNewUser(param.Name, param.Phone, o)
  423 + if err != nil {
  424 + o.Rollback()
  425 + log.Error("手机号变更,新增用户失败;%s", err)
  426 + return protocol.NewErrWithMessage("1")
  427 + }
  428 + ucompany.UserId = uid
  429 + }
  430 + //用户数据更新
  431 + err = editUserCompany(param.UserCompanyID, ucompany.UserId, param.Name, o)
  432 + if err != nil {
  433 + o.Rollback()
  434 + log.Error("editUserCompany err:%s", err)
  435 + return protocol.NewErrWithMessage("1")
  436 + }
348 //添加角色 437 //添加角色
349 - err = editUserRole(userm.Id, param.CompanyId, param.Roles, o) 438 + err = editUserRole(param.UserCompanyID, ucompany.UserId, param.CompanyId, param.Roles, o)
350 if err != nil { 439 if err != nil {
351 o.Rollback() 440 o.Rollback()
352 log.Error("editUserRole err:%s", err) 441 log.Error("editUserRole err:%s", err)
353 return protocol.NewErrWithMessage("1") 442 return protocol.NewErrWithMessage("1")
354 } 443 }
355 //添加部门 444 //添加部门
356 - err = editUserDepart(userm.Id, param.CompanyId, param.Departments, o) 445 + err = editUserDepart(param.UserCompanyID, ucompany.UserId, param.CompanyId, param.Departments, o)
357 if err != nil { 446 if err != nil {
358 o.Rollback() 447 o.Rollback()
359 log.Error("editUserDepart err:%s", err) 448 log.Error("editUserDepart err:%s", err)
360 return protocol.NewErrWithMessage("1") 449 return protocol.NewErrWithMessage("1")
361 } 450 }
362 //添加职位 451 //添加职位
363 - err = editUserDepart(userm.Id, param.CompanyId, param.Positions, o) 452 + err = editUserPosition(param.UserCompanyID, ucompany.UserId, param.CompanyId, param.Positions, o)
364 if err != nil { 453 if err != nil {
365 o.Rollback() 454 o.Rollback()
366 log.Error("editUserDepart err:%s", err) 455 log.Error("editUserDepart err:%s", err)
@@ -415,7 +504,7 @@ func UserDelete(userCompanyids []int64, companyid int64) error { @@ -415,7 +504,7 @@ func UserDelete(userCompanyids []int64, companyid int64) error {
415 504
416 func UserList(param protocol.RequestUserList) (protocol.ResponseUserList, error) { 505 func UserList(param protocol.RequestUserList) (protocol.ResponseUserList, error) {
417 506
418 - datasql := `SELECT a.id as user_company_id,a.company_id,a.user_id,a.enable, b.phone,b.nick_name 507 + datasql := `SELECT a.id as user_company_id,a.enable, b.phone,b.nick_name
419 FROM user_company AS a 508 FROM user_company AS a
420 LEFT JOIN user AS b ON a.user_id = b.id 509 LEFT JOIN user AS b ON a.user_id = b.id
421 WHERE a.company_id=? AND a.delete_at = 0 ` 510 WHERE a.company_id=? AND a.delete_at = 0 `