package entity import ( "app.yhyue.com/moapp/jybase/common" "database/sql" "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库添加数据 data := map[string]interface{}{ "i_appid": 2, "s_phone": info.Phone, "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 }