update.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "git.getensh.com/common/gopkgs/util"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "gorm.io/gorm"
  14. "property-system/consts"
  15. "property-system/errors"
  16. "property-system/impl/v1/permission"
  17. dbmodel "property-system/model"
  18. pb_v1 "property-system/pb/v1"
  19. "strings"
  20. "time"
  21. )
  22. func checkUserUpdateParam(req *pb_v1.UserUpdateRequest) error {
  23. switch {
  24. case req.Phone == "":
  25. return status.Error(10003, "联系人电话不能为空")
  26. case req.UserName == "":
  27. return status.Error(10003, "账户名不能为空")
  28. case req.GardenId < 1 || req.GardenName == "":
  29. return status.Error(10003, "小区不能为空")
  30. case req.RealName == "":
  31. return status.Error(10003, "真实姓名不能为空")
  32. case req.Cid < 1:
  33. return status.Error(10003, "物业公司不能为空")
  34. case req.Id < 1:
  35. return status.Error(10003, "用户id不能为空")
  36. case !req.ByCompany && req.Uid < 1:
  37. return status.Error(10003, "当前uid不能为空")
  38. case req.BasePermission == 0:
  39. return status.Error(10003, "业务权限不能为空")
  40. case req.GroupId == 0:
  41. return status.Error(10003, "管理后台权限组不能为空")
  42. }
  43. return nil
  44. }
  45. func checkPersonUpdateParam(req *pb_v1.UserUpdateRequest) error {
  46. switch {
  47. case req.Phone == "":
  48. //return status.Error(10003, "联系人电话不能为空")
  49. case req.UserName == "":
  50. //return status.Error(10003, "账户名不能为空")
  51. case req.RealName == "":
  52. //return status.Error(10003, "真实姓名不能为空")
  53. case req.Id < 1:
  54. return status.Error(10003, "用户id不能为空")
  55. }
  56. return nil
  57. }
  58. func PersonUpdate(ctx context.Context, req *pb_v1.UserUpdateRequest) (reply *pb_v1.UserUpdateReply, err error) {
  59. reply = &pb_v1.UserUpdateReply{}
  60. // 捕获各个task中的异常并返回给调用者
  61. defer func() {
  62. if r := recover(); r != nil {
  63. err = fmt.Errorf("%+v", r)
  64. e := &status.Status{}
  65. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  66. logger.Error("err",
  67. zap.String("system_err", err.Error()),
  68. zap.Stack("stacktrace"))
  69. }
  70. }
  71. }()
  72. err = checkPersonUpdateParam(req)
  73. if err != nil {
  74. return nil, err
  75. }
  76. now := time.Now()
  77. where := map[string]interface{}{
  78. "id": req.Id,
  79. }
  80. values := map[string]interface{}{
  81. //"email": req.Email,
  82. //"user": req.UserName,
  83. //"phone": req.Phone,
  84. "updated_at": now,
  85. //"real_name": req.RealName,
  86. }
  87. if req.UserName != "" {
  88. //values["user"] = req.UserName
  89. }
  90. if req.RealName != "" {
  91. //values["real_name"] = req.RealName
  92. }
  93. if req.Phone != "" {
  94. values["phone"] = req.Phone
  95. }
  96. if req.Email != "" {
  97. values["email"] = req.Email
  98. }
  99. if req.Gender > 0 {
  100. values["gender"] = req.Gender
  101. }
  102. src := &dbmodel.TUser{}
  103. err = src.Find(database.DB(), where)
  104. if err != nil && err != gorm.ErrRecordNotFound {
  105. return nil, errors.DataBaseError
  106. }
  107. if src.ID == 0 {
  108. return nil, errors.ErrRecordNotFound
  109. }
  110. if req.Uid == src.ID && !req.ByCompany {
  111. if req.GroupId != src.GroupId || req.BasePermission != src.BasePermission {
  112. return nil, errors.UserSelfError
  113. }
  114. }
  115. reply.Origin = &pb_v1.UserUpdateRequest{
  116. Id: req.Id,
  117. Cid: src.Cid,
  118. GardenId: src.GardenId,
  119. GroupId: src.GroupId,
  120. UserName: src.User,
  121. Email: src.Email,
  122. Phone: src.Phone,
  123. RealName: src.RealName,
  124. DepartmentId: src.DepartmentId,
  125. }
  126. user := dbmodel.TUser{}
  127. err = user.Update(database.DB(), where, values)
  128. if err != nil {
  129. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  130. return nil, errors.UserExist
  131. }
  132. return nil, errors.DataBaseError
  133. }
  134. return reply, nil
  135. }
  136. //
  137. func UserUpdate(ctx context.Context, req *pb_v1.UserUpdateRequest) (reply *pb_v1.UserUpdateReply, err error) {
  138. if req.Person {
  139. return PersonUpdate(ctx, req)
  140. }
  141. reply = &pb_v1.UserUpdateReply{}
  142. // 捕获各个task中的异常并返回给调用者
  143. defer func() {
  144. if r := recover(); r != nil {
  145. err = fmt.Errorf("%+v", r)
  146. e := &status.Status{}
  147. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  148. logger.Error("err",
  149. zap.String("system_err", err.Error()),
  150. zap.Stack("stacktrace"))
  151. }
  152. }
  153. }()
  154. err = checkUserUpdateParam(req)
  155. if err != nil {
  156. return nil, err
  157. }
  158. now := time.Now()
  159. passwd := ""
  160. if req.Password != "" {
  161. passwd, _ = util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  162. }
  163. where := map[string]interface{}{
  164. "id": req.Id,
  165. "cid": req.Cid,
  166. }
  167. values := map[string]interface{}{
  168. "garden_id": req.GardenId,
  169. "email": req.Email,
  170. "user": req.UserName,
  171. "phone": req.Phone,
  172. "updated_at": now,
  173. "garden_name": req.GardenName,
  174. "real_name": req.RealName,
  175. "department_id": req.DepartmentId,
  176. "gender": req.Gender,
  177. }
  178. if req.GroupId > 0 {
  179. values["group_id"] = req.GroupId
  180. }
  181. if req.BasePermission > 0 {
  182. values["base_permission"] = req.BasePermission
  183. }
  184. if passwd != "" {
  185. values["password"] = passwd
  186. }
  187. src := &dbmodel.TUser{}
  188. err = src.Find(database.DB(), where)
  189. if err != nil && err != gorm.ErrRecordNotFound {
  190. return nil, errors.DataBaseError
  191. }
  192. if src.ID == 0 {
  193. return nil, errors.ErrRecordNotFound
  194. }
  195. if req.Uid == src.ID && !req.ByCompany {
  196. if req.GroupId != src.GroupId || req.BasePermission != src.BasePermission {
  197. return nil, errors.UserSelfError
  198. }
  199. }
  200. reply.Origin = &pb_v1.UserUpdateRequest{
  201. Id: req.Id,
  202. Cid: req.Cid,
  203. GardenId: src.GardenId,
  204. GroupId: src.GroupId,
  205. UserName: src.User,
  206. Email: src.Email,
  207. Phone: src.Phone,
  208. RealName: src.RealName,
  209. DepartmentId: src.DepartmentId,
  210. }
  211. user := dbmodel.TUser{}
  212. db := database.DB().Begin()
  213. err = user.Update(db, where, values)
  214. if err != nil {
  215. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  216. db.Rollback()
  217. return nil, errors.UserExist
  218. }
  219. db.Rollback()
  220. return nil, errors.DataBaseError
  221. }
  222. if src.GroupId != req.GroupId {
  223. err = permission.PermissionChanged([]dbmodel.TUser{{ID: src.ID}})
  224. if err != nil {
  225. db.Rollback()
  226. return nil, err
  227. }
  228. }
  229. db.Commit()
  230. return reply, nil
  231. }