user.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package service
  2. import (
  3. "database/sql"
  4. "time"
  5. "bp.jydev.jianyu360.cn/BaseService/userCenter/entity"
  6. . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
  7. )
  8. func UserAdd(this *UserAddReq) *UserAddResp {
  9. userId := entity.BaseMysql.Insert(entity.UserTable, map[string]interface{}{
  10. "appid": this.Appid,
  11. "phone": this.Phone,
  12. "nickname": this.Nickname,
  13. "headimg": this.Headimg,
  14. "company": this.Company,
  15. "position": this.Position,
  16. "password": this.Password,
  17. "s_openid": this.SOpenid,
  18. "a_openid": this.AOpenid,
  19. "unionid": this.Unionid,
  20. "create_time": time.Now().Format("2006-01-02 15:04:05"),
  21. })
  22. status, msg := 0, ""
  23. if userId > 0 {
  24. status = 1
  25. } else {
  26. msg = "新增用户失败"
  27. }
  28. return &UserAddResp{
  29. ErrorCode: entity.SuccessCode,
  30. ErrorMsg: msg,
  31. Data: &UserAdds{Status: int64(status), Id: userId},
  32. }
  33. }
  34. func UserUpdate(this *UserIdReq) *ExamineResp {
  35. ok := UserUpdates(this)
  36. status, msg := 0, ""
  37. if ok {
  38. status = 1
  39. } else {
  40. msg = "更新用户失败"
  41. }
  42. return &ExamineResp{
  43. ErrorCode: entity.SuccessCode,
  44. ErrorMsg: msg,
  45. Data: &ExamineData{Status: int64(status)},
  46. }
  47. }
  48. func UserDel(this *UserIdReq) *ExamineResp {
  49. ok := UserDels(this)
  50. status, msg := 0, ""
  51. if ok {
  52. status = 1
  53. } else {
  54. msg = "删除用户失败"
  55. }
  56. return &ExamineResp{
  57. ErrorCode: entity.SuccessCode,
  58. ErrorMsg: msg,
  59. Data: &ExamineData{Status: int64(status)},
  60. }
  61. }
  62. func UserUpdates(this *UserIdReq) bool {
  63. ok := false
  64. flag := entity.BaseMysql.ExecTx("", func(tx *sql.Tx) bool {
  65. set := map[string]interface{}{}
  66. if this.Phone != "" {
  67. set["phone"] = this.Phone
  68. } else {
  69. set["phone"] = ""
  70. }
  71. if this.Nickname != "" {
  72. set["nickname"] = this.Nickname
  73. } else {
  74. set["nickname"] = ""
  75. }
  76. if this.Headimg != "" {
  77. set["headimg"] = this.Headimg
  78. } else {
  79. set["headimg"] = ""
  80. }
  81. if this.Company != "" {
  82. set["company"] = this.Company
  83. } else {
  84. set["company"] = ""
  85. }
  86. if this.Position != "" {
  87. set["position"] = this.Position
  88. } else {
  89. set["position"] = ""
  90. }
  91. if this.Password != "" {
  92. set["password"] = this.Password
  93. } else {
  94. set["password"] = ""
  95. }
  96. if this.AOpenid != "" {
  97. set["a_openid"] = this.AOpenid
  98. } else {
  99. set["a_openid"] = ""
  100. }
  101. if this.SOpenid != "" {
  102. set["s_openid"] = this.SOpenid
  103. } else {
  104. set["s_openid"] = ""
  105. }
  106. if this.Unionid != "" {
  107. set["unionid"] = this.Unionid
  108. } else {
  109. set["unionid"] = ""
  110. }
  111. ok1 := entity.BaseMysql.UpdateByTx(tx, entity.UserTable, map[string]interface{}{"id": this.Id}, set)
  112. snapshot := entity.BaseMysql.InsertByTx(tx, entity.UserSnapshotTable, map[string]interface{}{
  113. "appid": this.Appid,
  114. "user_id": this.Id,
  115. "phone": this.Phone,
  116. "nickname": this.Nickname,
  117. "headimg": this.Headimg,
  118. "company": this.Company,
  119. "position": this.Position,
  120. "password": this.Password,
  121. "s_openid": this.SOpenid,
  122. "a_openid": this.AOpenid,
  123. "unionid": this.Unionid,
  124. "create_time": time.Now().Format("2006-01-02 15:04:05"),
  125. })
  126. return ok1 && snapshot > 0
  127. })
  128. if flag {
  129. ok = true
  130. }
  131. return ok
  132. }
  133. func UserDels(this *UserIdReq) bool {
  134. ok := false
  135. userData := entity.BaseMysql.FindOne(entity.UserTable, map[string]interface{}{"id": this.Id}, "", "")
  136. if userData != nil && len(*userData) > 0 {
  137. flag := entity.BaseMysql.ExecTx("", func(tx *sql.Tx) bool {
  138. thisdata := *userData
  139. ok1 := entity.BaseMysql.DeleteByTx(tx, entity.UserTable, map[string]interface{}{"id": this.Id})
  140. snapshot := entity.BaseMysql.InsertByTx(tx, entity.UserSnapshotTable, map[string]interface{}{
  141. "appid": thisdata["appid"],
  142. "user_id": this.Id,
  143. "phone": thisdata["phone"],
  144. "nickname": thisdata["nickname"],
  145. "headimg": thisdata["headimg"],
  146. "company": thisdata["company"],
  147. "position": thisdata["position"],
  148. "password": thisdata["password"],
  149. "s_openid": thisdata["s_openid"],
  150. "a_openid": thisdata["a_openid"],
  151. "unionid": thisdata["unionid"],
  152. "create_time": time.Now().Format("2006-01-02 15:04:05"),
  153. })
  154. return ok1 && snapshot > 0
  155. })
  156. if flag {
  157. ok = true
  158. }
  159. }
  160. return ok
  161. }