jyUser.go 8.0 KB

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