jyUser.go 7.7 KB

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