123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- package entity
- import (
- "app.yhyue.com/moapp/jybase/common"
- "database/sql"
- "regexp"
- "time"
- )
- type JyUser struct{}
- type CompletionUserInfo struct {
- IsEnt bool `json:"isEnt"`
- PersonName string `json:"personName"`
- EntName string `json:"entName"`
- Phone string `json:"phone"`
- Mail string `json:"mail"`
- EntId int `json:"entId"`
- }
- type Resp struct {
- Fool bool `json:"fool"`
- EntPositionId int64 `json:"entPositionId"`
- UserPositionId int64 `json:"userpositionId"`
- }
- func (rpc *JyUser) AddUserInfo(info *CompletionUserInfo, res *Resp) error {
- userPositionId := int64(0)
- entPositionId := int64(0)
- fool := BaseMysql.ExecTx("", func(tx *sql.Tx) bool {
- users, ok := Mgo.Find("user", map[string]interface{}{
- "i_appid": 2,
- "$or": []map[string]interface{}{
- map[string]interface{}{
- "s_phone": info.Phone,
- },
- map[string]interface{}{
- "s_m_phone": info.Phone,
- },
- },
- }, `{"base_user_id":-1}`, `{"base_user_id":1}`, false, -1, -1)
- userData := &map[string]interface{}{}
- if ok && users != nil && len(*users) > 0 {
- userData = BaseMysql.FindOne(UserTable, map[string]interface{}{
- "id": (*users)[0]["base_user_id"],
- }, "person_id,id,nickname", "")
- }
- if info.IsEnt {
- //需要创建企业用户
- entMap := map[string]interface{}{
- "ent_id": info.EntId,
- "name": info.EntName,
- "type": 1,
- "person_id": 0,
- }
- accountId := BaseMysql.Insert(BaseAccount, entMap)
- if accountId == 0 {
- return false
- }
- } else {
- entData := Mysql.FindOne(Entniche_info, map[string]interface{}{"id": info.EntId}, "name", "")
- if entData == nil || len(*entData) == 0 {
- return false
- }
- info.EntName = common.InterfaceToStr((*entData)["name"])
- }
- personId := int64(0)
- userId := int64(0)
- userAccountId := int64(0)
- entAccountId := int64(0)
- personName := ""
- accountName := ""
- if userData == nil || len(*userData) == 0 {
- //没有用户需要创建用户
- //需要创建企业用户
- personMap := map[string]interface{}{
- "name": info.PersonName,
- "contact": info.Phone,
- "create_time": time.Now().Local(),
- "update_time": time.Now().Local(),
- }
- personId = BaseMysql.Insert(BasePerson, personMap)
- if personId == 0 {
- return false
- }
- //账户表添加
- entMap := map[string]interface{}{
- "ent_id": info.EntId,
- "name": info.EntName,
- "type": 1,
- "person_id": personId,
- }
- entAccountId = BaseMysql.Insert(BaseAccount, entMap)
- if entAccountId == 0 {
- return false
- }
- entUserMap := map[string]interface{}{
- "ent_id": 0,
- "name": "",
- "type": 0,
- "person_id": personId,
- }
- userAccountId = BaseMysql.Insert(BaseAccount, entUserMap)
- if userAccountId == 0 {
- return false
- }
- //用户表添加
- userMap := map[string]interface{}{
- "appid": "10000",
- "person_id": personId,
- "phone": info.Phone,
- "create_time": time.Now().Local(),
- "update_time": time.Now().Local(),
- }
- userId = BaseMysql.Insert(UserTable, userMap)
- if userId == 0 {
- return false
- }
- personName = info.PersonName
- accountName = info.EntName
- //mongo库添加数据
- entData := Mysql.FindOne(Entniche_info, map[string]interface{}{"id": info.EntId}, "virtual_account_rule", "")
- if entData == nil || len(*entData) == 0 {
- return false
- }
- s_password := ""
- virtual_account_rule := common.InterfaceToStr((*entData)["virtual_account_rule"])
- if virtual_account_rule != "" && len(virtual_account_rule) > 0 {
- if regexp.MustCompile(virtual_account_rule).MatchString(info.Phone) {
- s_password = common.GetMd5String("123456")
- }
- }
- data := map[string]interface{}{
- "i_appid": 2,
- "s_phone": info.Phone,
- "s_password": s_password,
- "l_registedate": time.Now().Unix(),
- "i_ts_guide": 2,
- "s_company": accountName,
- "o_jy": map[string]interface{}{
- "i_apppush": 1,
- "i_ratemode": 2,
- "l_modifydate": time.Now().Unix(),
- },
- "s_email": info.Mail,
- "s_regsource": "entbase",
- "s_platform": "entbase",
- "base_user_id": userId,
- }
- Mgo.Save("user", data)
- //个人职位
- //职位表添加
- if !ExistDecide(0, int64(0), userId, entAccountId) {
- userPositionMap := map[string]interface{}{
- "ent_id": 0,
- "user_id": userId,
- "account_id": userAccountId,
- "person_name": personName,
- "account_name": accountName,
- "type": 0,
- }
- userPositionId = BaseMysql.Insert(BasePosition, userPositionMap)
- if userPositionId == 0 {
- return false
- }
- }
- //企业职位
- //职位表添加
- if !ExistDecide(1, common.Int64All(info.EntId), userId, entAccountId) {
- entPositionMap := map[string]interface{}{
- "ent_id": info.EntId,
- "user_id": userId,
- "account_id": entAccountId,
- "person_name": personName,
- "account_name": accountName,
- "type": 1,
- }
- entPositionId = BaseMysql.Insert(BasePosition, entPositionMap)
- if entPositionId == 0 {
- return false
- }
- }
- } else {
- personId = common.Int64All((*userData)["person_id"])
- userId = common.Int64All((*userData)["id"])
- //企业账号信息查询
- accountData := BaseMysql.FindOne(BaseAccount, map[string]interface{}{
- "person_id": personId,
- "ent_id": info.EntId,
- "type": 1,
- }, "name,id", "")
- if accountData == nil || len(*accountData) == 0 {
- //添加企业账号
- entMap := map[string]interface{}{
- "ent_id": info.EntId,
- "name": info.EntName,
- "type": 1,
- "person_id": personId,
- }
- entAccountId = BaseMysql.Insert(BaseAccount, entMap)
- if entAccountId == 0 {
- return false
- }
- accountName = info.EntName
- } else {
- accountName = common.InterfaceToStr((*accountData)["name"])
- entAccountId = common.Int64All((*accountData)["id"])
- }
- //个人账号信息查询
- userAccountData := BaseMysql.FindOne(BaseAccount, map[string]interface{}{
- "person_id": personId,
- "ent_id": 0,
- "type": 0,
- }, "name,id", "")
- if userAccountData == nil || len(*userAccountData) == 0 {
- return false
- } else {
- userAccountId = common.Int64All((*userAccountData)["id"])
- }
- //自然人信息查询
- personData := BaseMysql.FindOne(BasePerson, map[string]interface{}{
- "id": personId,
- }, "name", "")
- if personData == nil || len(*personData) == 0 {
- return false
- }
- personName = common.InterfaceToStr((*personData)["name"])
- //职位表添加
- if !ExistDecide(1, common.Int64All(info.EntId), userId, entAccountId) {
- positionMap := map[string]interface{}{
- "ent_id": info.EntId,
- "user_id": userId,
- "account_id": entAccountId,
- "person_name": personName,
- "account_name": accountName,
- "type": 1,
- }
- entPositionId = BaseMysql.Insert(BasePosition, positionMap)
- if entPositionId == 0 {
- return false
- }
- }
- //个人职位信息查询
- userPositionData := BaseMysql.FindOne(BasePosition, map[string]interface{}{
- "ent_id": 0,
- "user_id": userId,
- "account_id": userAccountId,
- "type": 0,
- }, "id", "")
- if userPositionData == nil || len(*userPositionData) == 0 {
- return false
- } else {
- userPositionId = common.Int64All((*userPositionData)["id"])
- }
- }
- return true
- })
- res.Fool = fool
- res.EntPositionId = entPositionId
- res.UserPositionId = userPositionId
- return nil
- }
- //判断某个人之前是否在企业里边
- func ExistDecide(userType, entId, user_id, account_id int64) bool {
- if count := BaseMysql.Count(BasePosition, map[string]interface{}{
- "ent_id": entId,
- "user_id": user_id,
- "account_id": account_id,
- "type": userType,
- }); count > 0 {
- return true
- }
- return false
- }
|