user.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package service
  2. import (
  3. "database/sql"
  4. "time"
  5. "app.yhyue.com/moapp/jybase/common"
  6. "bp.jydev.jianyu360.cn/BaseService/userCenter/entity"
  7. . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
  8. )
  9. func UserAdd(this *UserAddReq) *UserAddResp {
  10. userId := int64(0)
  11. ok := entity.BaseMysql.ExecTx("新增用户相关表字段", func(tx *sql.Tx) bool {
  12. //自然人
  13. personId := entity.BaseMysql.InsertByTx(tx, entity.BasePerson, map[string]interface{}{
  14. "name": this.Nickname,
  15. "headimg": this.Headimg,
  16. "idcard": this.IdCard,
  17. "address": this.Address,
  18. "sex": this.Sex,
  19. "contact": this.Phone,
  20. "create_time": time.Now().Format("2006-01-02 15:04:05"),
  21. "update_time": time.Now().Format("2006-01-02 15:04:05"),
  22. })
  23. //账户表
  24. accountId := entity.BaseMysql.InsertByTx(tx, entity.BaseAccount, map[string]interface{}{
  25. "person_id": personId,
  26. "type": 0,
  27. })
  28. //用户表
  29. baseUserId := entity.BaseMysql.InsertByTx(tx, entity.UserTable, map[string]interface{}{
  30. "appid": this.Appid,
  31. "person_id": personId,
  32. "phone": this.Phone,
  33. "password": this.Password,
  34. "s_openid": this.SOpenid,
  35. "a_openid": this.AOpenid,
  36. "unionid": this.Unionid,
  37. "create_time": time.Now().Format("2006-01-02 15:04:05"),
  38. "update_time": time.Now().Format("2006-01-02 15:04:05"),
  39. })
  40. //职位表
  41. positionId := entity.BaseMysql.InsertByTx(tx, entity.BasePosition, map[string]interface{}{
  42. "type": 0,
  43. "account_id": accountId,
  44. "user_id": baseUserId,
  45. "person_name": this.Nickname,
  46. })
  47. userId = baseUserId
  48. return personId > 0 && accountId > 0 && baseUserId > 0 && positionId > 0
  49. })
  50. status, msg := 0, ""
  51. if userId > 0 && ok {
  52. status = 1
  53. } else {
  54. msg = "新增用户失败"
  55. }
  56. return &UserAddResp{
  57. ErrorCode: entity.SuccessCode,
  58. ErrorMsg: msg,
  59. Data: &UserAdds{Status: int64(status), Id: userId},
  60. }
  61. }
  62. func UserUpdate(this *UserIdReq) *ExamineResp {
  63. ok := UserUpdates(this)
  64. status, msg := 0, ""
  65. if ok {
  66. status = 1
  67. } else {
  68. msg = "更新用户失败"
  69. }
  70. return &ExamineResp{
  71. ErrorCode: entity.SuccessCode,
  72. ErrorMsg: msg,
  73. Data: &ExamineData{Status: int64(status)},
  74. }
  75. }
  76. func UserDel(this *UserIdReq) *ExamineResp {
  77. ok := UserDels(this)
  78. status, msg := 0, ""
  79. if ok {
  80. status = 1
  81. } else {
  82. msg = "删除用户失败"
  83. }
  84. return &ExamineResp{
  85. ErrorCode: entity.SuccessCode,
  86. ErrorMsg: msg,
  87. Data: &ExamineData{Status: int64(status)},
  88. }
  89. }
  90. func UserUpdates(this *UserIdReq) bool {
  91. ok := false
  92. flag := entity.BaseMysql.ExecTx("", func(tx *sql.Tx) bool {
  93. set := map[string]interface{}{}
  94. if this.Phone != "" {
  95. set["phone"] = this.Phone
  96. } else {
  97. set["phone"] = ""
  98. }
  99. if this.Nickname != "" {
  100. set["nickname"] = this.Nickname
  101. } else {
  102. set["nickname"] = ""
  103. }
  104. if this.Headimg != "" {
  105. set["headimg"] = this.Headimg
  106. } else {
  107. set["headimg"] = ""
  108. }
  109. if this.Company != "" {
  110. set["company"] = this.Company
  111. } else {
  112. set["company"] = ""
  113. }
  114. if this.Position != "" {
  115. set["position"] = this.Position
  116. } else {
  117. set["position"] = ""
  118. }
  119. if this.Password != "" {
  120. set["password"] = this.Password
  121. } else {
  122. set["password"] = ""
  123. }
  124. if this.AOpenid != "" {
  125. set["a_openid"] = this.AOpenid
  126. } else {
  127. set["a_openid"] = ""
  128. }
  129. if this.SOpenid != "" {
  130. set["s_openid"] = this.SOpenid
  131. } else {
  132. set["s_openid"] = ""
  133. }
  134. if this.Unionid != "" {
  135. set["unionid"] = this.Unionid
  136. } else {
  137. set["unionid"] = ""
  138. }
  139. //base_user
  140. ok1 := entity.BaseMysql.UpdateByTx(tx, entity.UserTable, map[string]interface{}{"id": this.Id}, set)
  141. //查询出personid
  142. ok2 := true
  143. ok3 := true
  144. rdata := entity.BaseMysql.SelectBySqlByTx(tx, `select person_id from base_user where id =?`, this.Id)
  145. if rdata != nil && len(*rdata) > 0 {
  146. person_id := common.Int64All((*rdata)[0]["person_id"])
  147. ok2 = entity.BaseMysql.UpdateByTx(tx, entity.BasePerson, map[string]interface{}{
  148. "id": person_id,
  149. }, map[string]interface{}{
  150. "contact": this.Phone,
  151. "name": this.Nickname,
  152. "headimg": this.Headimg,
  153. })
  154. //获取accountid
  155. mdata := entity.BaseMysql.SelectBySqlByTx(tx, `select id from base_account where person_id =?`, person_id)
  156. if mdata != nil && len(*mdata) > 0 {
  157. accountId := common.Int64All((*mdata)[0]["id"])
  158. ok3 = entity.BaseMysql.UpdateByTx(tx, entity.BasePosition, map[string]interface{}{
  159. "user_id": this.Id,
  160. "account_id": accountId,
  161. }, map[string]interface{}{
  162. "person_name": this.Nickname,
  163. })
  164. }
  165. }
  166. return ok1 && ok2 && ok3
  167. })
  168. if flag {
  169. ok = true
  170. }
  171. return ok
  172. }
  173. func UserDels(this *UserIdReq) bool {
  174. return entity.BaseMysql.ExecTx("", func(tx *sql.Tx) bool {
  175. _, e1 := entity.BaseMysql.ExecBySqlByTx(tx, `insert into base_user_backup select * from base_user where id=?`, this.Id)
  176. _, e2 := entity.BaseMysql.ExecBySqlByTx(tx, `insert into base_position_backup select * from base_position where user_id=?`, this.Id)
  177. _, e3 := entity.BaseMysql.ExecBySqlByTx(tx, `insert into base_person_backup select a.* from base_person a inner join base_user b on (b.id=? and a.id=b.person_id)`, this.Id)
  178. _, e4 := entity.BaseMysql.ExecBySqlByTx(tx, `insert into base_account_backup select a.* from base_account a inner join base_user b on (b.id=? and a.person_id=b.person_id)`, this.Id)
  179. d1 := entity.BaseMysql.UpdateOrDeleteBySqlByTx(tx, `delete a from base_person a inner join base_user b on (b.id=? and a.id=b.person_id)`, this.Id)
  180. d2 := entity.BaseMysql.UpdateOrDeleteBySqlByTx(tx, `delete a from base_account a inner join base_user b on (b.id=? and a.person_id=b.person_id)`, this.Id)
  181. d3 := entity.BaseMysql.UpdateOrDeleteBySqlByTx(tx, `delete from base_user where id=?`, this.Id)
  182. d4 := entity.BaseMysql.UpdateOrDeleteBySqlByTx(tx, `delete from base_position where user_id=?`, this.Id)
  183. return e1 == nil && e2 == nil && e3 == nil && e4 == nil && d1 >= 0 && d2 >= 0 && d3 >= 0 && d4 >= 0
  184. })
  185. }
  186. //
  187. /*
  188. int64 error_code = 1;
  189. string error_msg = 2;
  190. int64 personId=3;//自然人id
  191. int64 userAccountId=4;//个人账户id
  192. int64 entAccountId =5; //企业账户id
  193. int64 entUserAccountId =6;//企业雇员账户id
  194. int64 userPositionId =7; // 个人职位id
  195. int64 entUserPositionId =8;// 企业雇员职位id
  196. string userName=9; //昵称
  197. */
  198. func UserIdentity(this *UserIdentityReq) *UserIdentityResp {
  199. resp := &UserIdentityResp{}
  200. //个人账户
  201. data := entity.BaseMysql.SelectBySql(`SELECT a.person_id personId,c.id userAccountId ,d.id userPositionId,b.name userName FROM base_user a
  202. INNER JOIN base_person b ON a.person_id =b.id
  203. INNER JOIN base_account c ON c.person_id = b.id AND c.type=0
  204. INNER JOIN base_position d ON d.account_id = c.id AND d.user_id = a.id AND d.type=0
  205. WHERE a.id =? AND a.appid =?`, this.BaseUserId, this.AppId)
  206. if data != nil && len(*data) > 0 {
  207. r := (*data)[0]
  208. resp.PersonId = common.Int64All(r["personId"])
  209. resp.UserAccountId = common.Int64All(r["userAccountId"])
  210. resp.UserPositionId = common.Int64All(r["userPositionId"])
  211. resp.UserName = common.ObjToString(r["userName"])
  212. } else {
  213. resp.ErrorCode = -1
  214. resp.ErrorMsg = "暂无数据"
  215. }
  216. //企业账户
  217. data2 := entity.BaseMysql.SelectBySql(`select c.id entUserAccountId ,d.id entUserPositionId, e.id entAccountId from base_user a
  218. inner join base_person b on a.person_id =b.id
  219. inner join base_account c on c.person_id = b.id and c.type=1
  220. inner join base_position d on d.account_id = c.id and d.user_id = a.id and d.type=1
  221. inner join base_account e on e.type=1 and (e.person_id is null or e.person_id =0)
  222. where a.id =? and a.appid =? and c.ent_id =?`, this.BaseUserId, this.AppId, this.EntId)
  223. if data2 != nil && len(*data2) > 0 {
  224. r := (*data2)[0]
  225. resp.EntAccountId = common.Int64All(r["entAccountId"])
  226. resp.EntUserAccountId = common.Int64All(r["entUserAccountId"])
  227. resp.EntUserPositionId = common.Int64All(r["entUserPositionId"])
  228. }
  229. return resp
  230. }