user.go 3.5 KB

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