jyUser.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. if !ExistDecide(0, int64(0), userId, entAccountId) {
  137. userPositionMap := map[string]interface{}{
  138. "ent_id": 0,
  139. "user_id": userId,
  140. "account_id": userAccountId,
  141. "person_name": personName,
  142. "account_name": accountName,
  143. "type": 0,
  144. }
  145. userPositionId = BaseMysql.Insert(BasePosition, userPositionMap)
  146. if userPositionId == 0 {
  147. return false
  148. }
  149. }
  150. //企业职位
  151. //职位表添加
  152. if !ExistDecide(1, common.Int64All(info.EntId), userId, entAccountId) {
  153. entPositionMap := map[string]interface{}{
  154. "ent_id": info.EntId,
  155. "user_id": userId,
  156. "account_id": entAccountId,
  157. "person_name": personName,
  158. "account_name": accountName,
  159. "type": 1,
  160. }
  161. entPositionId = BaseMysql.Insert(BasePosition, entPositionMap)
  162. if entPositionId == 0 {
  163. return false
  164. }
  165. }
  166. } else {
  167. personId = common.Int64All((*userData)["person_id"])
  168. userId = common.Int64All((*userData)["id"])
  169. //企业账号信息查询
  170. accountData := BaseMysql.FindOne(BaseAccount, map[string]interface{}{
  171. "person_id": personId,
  172. "ent_id": info.EntId,
  173. "type": 1,
  174. }, "name,id", "")
  175. if accountData == nil || len(*accountData) == 0 {
  176. //添加企业账号
  177. entMap := map[string]interface{}{
  178. "ent_id": info.EntId,
  179. "name": info.EntName,
  180. "type": 1,
  181. "person_id": personId,
  182. }
  183. entAccountId = BaseMysql.Insert(BaseAccount, entMap)
  184. if entAccountId == 0 {
  185. return false
  186. }
  187. accountName = info.EntName
  188. } else {
  189. accountName = common.InterfaceToStr((*accountData)["name"])
  190. entAccountId = common.Int64All((*accountData)["id"])
  191. }
  192. //个人账号信息查询
  193. userAccountData := BaseMysql.FindOne(BaseAccount, map[string]interface{}{
  194. "person_id": personId,
  195. "ent_id": 0,
  196. "type": 0,
  197. }, "name,id", "")
  198. if userAccountData == nil || len(*userAccountData) == 0 {
  199. return false
  200. } else {
  201. userAccountId = common.Int64All((*userAccountData)["id"])
  202. }
  203. //自然人信息查询
  204. personData := BaseMysql.FindOne(BasePerson, map[string]interface{}{
  205. "id": personId,
  206. }, "name", "")
  207. if personData == nil || len(*personData) == 0 {
  208. return false
  209. }
  210. personName = common.InterfaceToStr((*personData)["name"])
  211. //职位表添加
  212. if !ExistDecide(1, common.Int64All(info.EntId), userId, entAccountId) {
  213. positionMap := map[string]interface{}{
  214. "ent_id": info.EntId,
  215. "user_id": userId,
  216. "account_id": entAccountId,
  217. "person_name": personName,
  218. "account_name": accountName,
  219. "type": 1,
  220. }
  221. entPositionId = BaseMysql.Insert(BasePosition, positionMap)
  222. if entPositionId == 0 {
  223. return false
  224. }
  225. }
  226. //个人职位信息查询
  227. userPositionData := BaseMysql.FindOne(BasePosition, map[string]interface{}{
  228. "ent_id": 0,
  229. "user_id": userId,
  230. "account_id": userAccountId,
  231. "type": 0,
  232. }, "id", "")
  233. if userPositionData == nil || len(*userPositionData) == 0 {
  234. return false
  235. } else {
  236. userPositionId = common.Int64All((*userPositionData)["id"])
  237. }
  238. }
  239. return true
  240. })
  241. res.Fool = fool
  242. res.EntPositionId = entPositionId
  243. res.UserPositionId = userPositionId
  244. return nil
  245. }
  246. //判断某个人之前是否在企业里边
  247. func ExistDecide(userType, entId, user_id, account_id int64) bool {
  248. if count := BaseMysql.Count(BasePosition, map[string]interface{}{
  249. "ent_id": entId,
  250. "user_id": user_id,
  251. "account_id": account_id,
  252. "type": userType,
  253. }); count > 0 {
  254. return true
  255. }
  256. return false
  257. }