jyUser.go 8.2 KB

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