user.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "openid": this.Openid,
  18. "unionid": this.Unionid,
  19. "create_time": time.Now().Format("2006-01-02 15:04:05"),
  20. })
  21. status, msg := 0, ""
  22. if userId > 0 {
  23. status = 1
  24. } else {
  25. msg = "新增用户失败"
  26. }
  27. return &UserAddResp{
  28. ErrorCode: 1,
  29. ErrorMsg: msg,
  30. Data: &UserAdds{Status: int64(status), Id: userId},
  31. }
  32. }
  33. func UserUpdate(this *UserIdReq) *ExamineResp {
  34. ok := UserUpdates(this)
  35. status, msg := 0, ""
  36. if ok {
  37. status = 1
  38. } else {
  39. msg = "更新用户失败"
  40. }
  41. return &ExamineResp{
  42. ErrorCode: 1,
  43. ErrorMsg: msg,
  44. Data: &ExamineData{Status: int64(status)},
  45. }
  46. }
  47. func UserDel(this *UserIdReq) *ExamineResp {
  48. ok := UserDels(this)
  49. status, msg := 0, ""
  50. if ok {
  51. status = 1
  52. } else {
  53. msg = "删除用户失败"
  54. }
  55. return &ExamineResp{
  56. ErrorCode: 1,
  57. ErrorMsg: msg,
  58. Data: &ExamineData{Status: int64(status)},
  59. }
  60. }
  61. func UserUpdates(this *UserIdReq) bool {
  62. ok := false
  63. flag := entity.BaseMysql.ExecTx("", func(tx *sql.Tx) bool {
  64. set := map[string]interface{}{}
  65. if this.Phone != "" {
  66. set["phone"] = this.Phone
  67. }
  68. if this.Nickname != "" {
  69. set["nickname"] = this.Nickname
  70. }
  71. if this.Headimg != "" {
  72. set["headimg"] = this.Headimg
  73. }
  74. if this.Company != "" {
  75. set["company"] = this.Company
  76. }
  77. if this.Position != "" {
  78. set["position"] = this.Position
  79. }
  80. if this.Password != "" {
  81. set["password"] = this.Password
  82. }
  83. if this.Openid != "" {
  84. set["openid"] = this.Openid
  85. }
  86. if this.Unionid != "" {
  87. set["unionid"] = this.Unionid
  88. }
  89. ok1 := entity.BaseMysql.UpdateByTx(tx, entity.UserTable, map[string]interface{}{"id": this.Id}, set)
  90. snapshot := entity.BaseMysql.InsertByTx(tx, entity.UserSnapshotTable, map[string]interface{}{
  91. "appid": this.Appid,
  92. "user_id": this.Id,
  93. "phone": this.Phone,
  94. "nickname": this.Nickname,
  95. "headimg": this.Headimg,
  96. "company": this.Company,
  97. "position": this.Position,
  98. "password": this.Password,
  99. "openid": this.Openid,
  100. "unionid": this.Unionid,
  101. "create_time": time.Now().Format("2006-01-02 15:04:05"),
  102. })
  103. return ok1 && snapshot > 0
  104. })
  105. if flag {
  106. ok = true
  107. }
  108. return ok
  109. }
  110. func UserDels(this *UserIdReq) bool {
  111. ok := false
  112. userData := entity.BaseMysql.FindOne(entity.UserTable, map[string]interface{}{"id": this.Id}, "", "")
  113. if userData != nil && len(*userData) > 0 {
  114. flag := entity.BaseMysql.ExecTx("", func(tx *sql.Tx) bool {
  115. thisdata := *userData
  116. ok1 := entity.BaseMysql.DeleteByTx(tx, entity.UserTable, map[string]interface{}{"id": this.Id})
  117. snapshot := entity.BaseMysql.InsertByTx(tx, entity.UserSnapshotTable, map[string]interface{}{
  118. "appid": thisdata["appid"],
  119. "user_id": this.Id,
  120. "phone": thisdata["phone"],
  121. "nickname": thisdata["nickname"],
  122. "headimg": thisdata["headimg"],
  123. "company": thisdata["company"],
  124. "position": thisdata["position"],
  125. "password": thisdata["password"],
  126. "openid": thisdata["openid"],
  127. "unionid": thisdata["unionid"],
  128. "create_time": time.Now().Format("2006-01-02 15:04:05"),
  129. })
  130. return ok1 && snapshot > 0
  131. })
  132. if flag {
  133. ok = true
  134. }
  135. }
  136. return ok
  137. }