jyUser.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package entity
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "database/sql"
  5. "time"
  6. )
  7. type JyUser struct{}
  8. type CompletionUserInfo struct {
  9. IsEnt bool `json:"isEnt"`
  10. PersonName string `json:"personName"`
  11. EntName string `json:"entName"`
  12. Phone string `json:"phone"`
  13. Mail string `json:"mail"`
  14. EntId int `json:"entId"`
  15. }
  16. type Resp struct {
  17. Fool bool `json:"fool"`
  18. EntPositionId int64 `json:"entPositionId"`
  19. UserPositionId int64 `json:"userpositionId"`
  20. }
  21. func (rpc *JyUser) AddUserInfo(info *CompletionUserInfo, res *Resp) error {
  22. userPositionId := int64(0)
  23. entPositionId := int64(0)
  24. fool := BaseMysql.ExecTx("", func(tx *sql.Tx) bool {
  25. users, ok := Mgo.Find("user", map[string]interface{}{
  26. "i_appid": 2,
  27. "$or": []map[string]interface{}{
  28. map[string]interface{}{
  29. "s_phone": info.Phone,
  30. },
  31. map[string]interface{}{
  32. "s_m_phone": info.Phone,
  33. },
  34. },
  35. }, `{"base_user_id":-1}`, `{"base_user_id":1}`, false, -1, -1)
  36. userData := &map[string]interface{}{}
  37. if ok && users != nil && len(*users) > 0 {
  38. userData = BaseMysql.FindOne(UserTable, map[string]interface{}{
  39. "id": (*users)[0]["base_user_id"],
  40. }, "person_id,id,nickname", "")
  41. }
  42. if info.IsEnt {
  43. //需要创建企业用户
  44. entMap := map[string]interface{}{
  45. "ent_id": info.EntId,
  46. "name": info.EntName,
  47. "type": 1,
  48. "person_id": 0,
  49. }
  50. accountId := BaseMysql.Insert(BaseAccount, entMap)
  51. if accountId == 0 {
  52. return false
  53. }
  54. } else {
  55. entData := Mysql.FindOne(Entniche_info, map[string]interface{}{"id": info.EntId}, "name", "")
  56. if entData == nil || len(*entData) == 0 {
  57. return false
  58. }
  59. info.EntName = common.InterfaceToStr((*entData)["name"])
  60. }
  61. personId := int64(0)
  62. userId := int64(0)
  63. userAccountId := int64(0)
  64. entAccountId := int64(0)
  65. personName := ""
  66. accountName := ""
  67. if userData == nil || len(*userData) == 0 {
  68. //没有用户需要创建用户
  69. //需要创建企业用户
  70. personMap := map[string]interface{}{
  71. "name": info.PersonName,
  72. "contact": info.Phone,
  73. "create_time": time.Now().Local(),
  74. "update_time": time.Now().Local(),
  75. }
  76. personId = BaseMysql.Insert(BasePerson, personMap)
  77. if personId == 0 {
  78. return false
  79. }
  80. //账户表添加
  81. entMap := map[string]interface{}{
  82. "ent_id": info.EntId,
  83. "name": info.EntName,
  84. "type": 1,
  85. "person_id": personId,
  86. }
  87. entAccountId = BaseMysql.Insert(BaseAccount, entMap)
  88. if entAccountId == 0 {
  89. return false
  90. }
  91. entUserMap := map[string]interface{}{
  92. "ent_id": 0,
  93. "name": "",
  94. "type": 0,
  95. "person_id": personId,
  96. }
  97. userAccountId = BaseMysql.Insert(BaseAccount, entUserMap)
  98. if userAccountId == 0 {
  99. return false
  100. }
  101. //用户表添加
  102. userMap := map[string]interface{}{
  103. "appid": "10000",
  104. "person_id": personId,
  105. "phone": info.Phone,
  106. "create_time": time.Now().Local(),
  107. "update_time": time.Now().Local(),
  108. }
  109. userId = BaseMysql.Insert(UserTable, userMap)
  110. if userId == 0 {
  111. return false
  112. }
  113. personName = info.PersonName
  114. accountName = info.EntName
  115. //mongo库添加数据
  116. data := map[string]interface{}{
  117. "i_appid": 2,
  118. "s_phone": info.Phone,
  119. "s_password": "",
  120. "l_registedate": time.Now().Unix(),
  121. "i_ts_guide": 2,
  122. "s_company": accountName,
  123. "o_jy": map[string]interface{}{
  124. "i_apppush": 1,
  125. "i_ratemode": 2,
  126. "l_modifydate": time.Now().Unix(),
  127. },
  128. "s_email": info.Mail,
  129. "s_regsource": "entbase",
  130. "s_platform": "entbase",
  131. "base_user_id": userId,
  132. }
  133. Mgo.Save("user", data)
  134. //个人职位
  135. //职位表添加
  136. userPositionMap := map[string]interface{}{
  137. "ent_id": 0,
  138. "user_id": userId,
  139. "account_id": userAccountId,
  140. "person_name": personName,
  141. "account_name": accountName,
  142. "type": 0,
  143. }
  144. userPositionId = BaseMysql.Insert(BasePosition, userPositionMap)
  145. if userPositionId == 0 {
  146. return false
  147. }
  148. //企业职位
  149. //职位表添加
  150. entPositionMap := map[string]interface{}{
  151. "ent_id": info.EntId,
  152. "user_id": userId,
  153. "account_id": entAccountId,
  154. "person_name": personName,
  155. "account_name": accountName,
  156. "type": 1,
  157. }
  158. entPositionId = BaseMysql.Insert(BasePosition, entPositionMap)
  159. if entPositionId == 0 {
  160. return false
  161. }
  162. } else {
  163. personId = common.Int64All((*userData)["person_id"])
  164. userId = common.Int64All((*userData)["id"])
  165. //企业账号信息查询
  166. accountData := BaseMysql.FindOne(BaseAccount, map[string]interface{}{
  167. "person_id": personId,
  168. "ent_id": info.EntId,
  169. "type": 1,
  170. }, "name,id", "")
  171. if accountData == nil || len(*accountData) == 0 {
  172. //添加企业账号
  173. entMap := map[string]interface{}{
  174. "ent_id": info.EntId,
  175. "name": info.EntName,
  176. "type": 1,
  177. "person_id": personId,
  178. }
  179. entAccountId = BaseMysql.Insert(BaseAccount, entMap)
  180. if entAccountId == 0 {
  181. return false
  182. }
  183. accountName = info.EntName
  184. } else {
  185. accountName = common.InterfaceToStr((*accountData)["name"])
  186. entAccountId = common.Int64All((*accountData)["id"])
  187. }
  188. //个人账号信息查询
  189. userAccountData := BaseMysql.FindOne(BaseAccount, map[string]interface{}{
  190. "person_id": personId,
  191. "ent_id": 0,
  192. "type": 0,
  193. }, "name,id", "")
  194. if userAccountData == nil || len(*userAccountData) == 0 {
  195. return false
  196. } else {
  197. userAccountId = common.Int64All((*userAccountData)["id"])
  198. }
  199. //自然人信息查询
  200. personData := BaseMysql.FindOne(BasePerson, map[string]interface{}{
  201. "id": personId,
  202. }, "name", "")
  203. if personData == nil || len(*personData) == 0 {
  204. return false
  205. }
  206. personName = common.InterfaceToStr((*personData)["name"])
  207. //职位表添加
  208. positionMap := map[string]interface{}{
  209. "ent_id": info.EntId,
  210. "user_id": userId,
  211. "account_id": entAccountId,
  212. "person_name": personName,
  213. "account_name": accountName,
  214. "type": 1,
  215. }
  216. entPositionId = BaseMysql.Insert(BasePosition, positionMap)
  217. if entPositionId == 0 {
  218. return false
  219. }
  220. //个人职位信息查询
  221. userPositionData := BaseMysql.FindOne(BasePosition, map[string]interface{}{
  222. "ent_id": 0,
  223. "user_id": userId,
  224. "account_id": userAccountId,
  225. "type": 0,
  226. }, "id", "")
  227. if userPositionData == nil || len(*userPositionData) == 0 {
  228. return false
  229. } else {
  230. userPositionId = common.Int64All((*userPositionData)["id"])
  231. }
  232. }
  233. return true
  234. })
  235. res.Fool = fool
  236. res.EntPositionId = entPositionId
  237. res.UserPositionId = userPositionId
  238. return nil
  239. }