package service import ( "fmt" "strings" . "app.yhyue.com/moapp/jybase/common" . "app.yhyue.com/moapp/jybase/mongodb" . "app.yhyue.com/moapp/jybase/mysql" . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter" ) //获取用户可切换的身份列表 func IdentityList(msl *Mysql, userId int64) []*Identity { result := []*Identity{} list := msl.SelectBySql(`SELECT DISTINCT a.id as position_id,c.person_id,a.person_name,a.account_id,a.type as position_type,a.ent_id,b.id as ent_account_id,d.phone from base_service.base_user d inner join base_service.base_position a on (d.id=? and d.id=a.user_id) inner join base_service.base_account c on (a.account_id=c.id) left join base_service.base_account b on (b.type=1 and b.person_id=0 and b.ent_id<>0 and c.ent_id=b.ent_id) order by a.id desc`, userId) if list != nil { m := map[int64]*Identity{} entIds := []string{} phone := "" for _, v := range *list { i := &Identity{ EntId: Int64All(v["ent_id"]), PersonId: Int64All(v["person_id"]), UserName: ObjToString(v["person_name"]), AccountId: Int64All(v["account_id"]), EntAccountId: Int64All(v["ent_account_id"]), PositionId: Int64All(v["position_id"]), PositionType: Int64All(v["position_type"]), UserId: userId, } if i.PositionType == 0 { i.Name = "个人版" m[0] = i } else if i.EntId > 0 { m[i.EntId] = i entIds = append(entIds, fmt.Sprint(i.EntId)) } if phone == "" { phone = ObjToString(v["phone"]) } } if len(entIds) > 0 { ents := msl.SelectBySql(`SELECT a.id,a.name,a.ent_id,a.niche_dis,a.role,b.name as ent_name,c.role_id,d.dept_id from jianyu.entniche_user a inner join jianyu.entniche_info b on (a.phone=? and b.id in (`+strings.Join(entIds, ",")+`) and a.ent_id=b.id) left join jianyu.entniche_user_role c on (a.id=c.user_id) LEFT JOIN jianyu.entniche_department_user d ON (d.user_id=a.id) order by b.createtime`, phone) if ents != nil { for _, v := range *ents { i := m[Int64All(v["ent_id"])] i.EntUserId = Int64All(v["id"]) i.EntUserName = ObjToString(v["name"]) i.Name = ObjToString(v["ent_name"]) i.EntRole = Int64All(v["role_id"]) i.EntNicheDis = Int64All(v["niche_dis"]) i.EntUserRole = ObjToString(v["role"]) i.EntDeptId = Int64All(v["dept_id"]) result = append(result, i) } } } if m[0] != nil { result = append(result, m[0]) } } return result } //根据账号id获取个人身份信息 func IdentityByUserId(msl *Mysql, userId int64) *Identity { list := msl.SelectBySql(`SELECT a.account_id,a.id as position_id,c.person_id,a.person_name,a.type as position_type from base_service.base_user d inner join base_service.base_position a on (d.id=? and a.type=0 and d.id=a.user_id) inner join base_service.base_account c on (c.type=0 and a.account_id=c.id) order by a.id desc limit 1`, userId) if list != nil && len(*list) > 0 { return &Identity{ PersonId: Int64All((*list)[0]["person_id"]), UserName: ObjToString((*list)[0]["person_name"]), AccountId: Int64All((*list)[0]["account_id"]), PositionId: Int64All((*list)[0]["position_id"]), PositionType: Int64All((*list)[0]["position_type"]), UserId: userId, } } return nil } //根据职位id获取身份信息 func IdentityByPositionId(msl *Mysql, positionId int64) *Identity { list := msl.SelectBySql(`SELECT a.id as position_id,c.person_id,a.person_name,a.account_id,a.type as position_type,a.ent_id,b.phone,b.id as user_id,d.id as ent_account_id from base_service.base_position a inner join base_service.base_user b on (a.id=? and a.user_id=b.id) inner join base_service.base_account c on (a.account_id=c.id) left join base_service.base_account d on (d.type=1 and d.person_id=0 and d.ent_id<>0 and d.ent_id=a.ent_id) limit 1`, positionId) if list != nil && len(*list) == 1 { identity := &Identity{ EntId: Int64All((*list)[0]["ent_id"]), PersonId: Int64All((*list)[0]["person_id"]), UserName: ObjToString((*list)[0]["person_name"]), AccountId: Int64All((*list)[0]["account_id"]), EntAccountId: Int64All((*list)[0]["ent_account_id"]), PositionId: positionId, PositionType: Int64All((*list)[0]["position_type"]), UserId: Int64All((*list)[0]["user_id"]), } if identity.PositionType == 0 { identity.Name = "个人版" } phone := ObjToString((*list)[0]["phone"]) if identity.EntId > 0 { ents := msl.SelectBySql(`SELECT a.id,a.name,a.ent_id,a.niche_dis,a.role,b.name as ent_name,c.role_id,d.dept_id from jianyu.entniche_user a inner join jianyu.entniche_info b on (a.phone=? and b.id=? and a.ent_id=b.id) left join jianyu.entniche_user_role c on (a.id=c.user_id) LEFT JOIN jianyu.entniche_department_user d ON (d.user_id=a.id) limit 1`, phone, identity.EntId) if ents != nil && len(*ents) > 0 { identity.EntUserId = Int64All((*ents)[0]["id"]) identity.EntUserName = ObjToString((*ents)[0]["name"]) identity.Name = ObjToString((*ents)[0]["ent_name"]) identity.EntRole = Int64All((*ents)[0]["role_id"]) identity.EntNicheDis = Int64All((*ents)[0]["niche_dis"]) identity.EntUserRole = ObjToString((*ents)[0]["role"]) identity.EntDeptId = Int64All((*ents)[0]["dept_id"]) } } return identity } return nil } //根据账户id获取身份信息 func IdentityByAccountId(msl *Mysql, accountId int64) *Identity { array := msl.SelectBySql(`SELECT type,person_id,ent_id from base_service.base_account where id=?`, accountId) if array == nil || len(*array) == 0 { return nil } else if Int64All((*array)[0]["type"]) == 1 && Int64All((*array)[0]["person_id"]) == 0 { return &Identity{ EntId: Int64All((*array)[0]["ent_id"]), EntAccountId: Int64All((*array)[0]["id"]), } } list := msl.SelectBySql(`SELECT a.id as position_id,c.person_id,a.person_name,a.type as position_type,a.ent_id,b.phone,b.id as user_id,d.id as ent_account_id from base_service.base_account c inner join base_service.base_user b on (c.id=? AND c.person_id=b.person_id) inner join base_service.base_position a on (a.account_id=c.id) left join base_service.base_account d on (d.type=1 and d.person_id=0 and d.ent_id<>0 and d.ent_id=a.ent_id) limit 1`, accountId) if list != nil && len(*list) == 1 { identity := &Identity{ EntId: Int64All((*list)[0]["ent_id"]), PersonId: Int64All((*list)[0]["person_id"]), UserName: ObjToString((*list)[0]["person_name"]), AccountId: accountId, EntAccountId: Int64All((*list)[0]["ent_account_id"]), PositionId: Int64All((*list)[0]["position_id"]), PositionType: Int64All((*list)[0]["position_type"]), UserId: Int64All((*list)[0]["user_id"]), } if identity.PositionType == 0 { identity.Name = "个人版" } phone := ObjToString((*list)[0]["phone"]) if identity.EntId > 0 { ents := msl.SelectBySql(`SELECT a.id,a.name,a.ent_id,a.niche_dis,a.role,b.name as ent_name,c.role_id,d.dept_id from jianyu.entniche_user a inner join jianyu.entniche_info b on (a.phone=? and b.id=? and a.ent_id=b.id) left join jianyu.entniche_user_role c on (a.id=c.user_id) LEFT JOIN jianyu.entniche_department_user d ON (d.user_id=a.id) limit 1`, phone, identity.EntId) if ents != nil && len(*ents) > 0 { identity.EntUserId = Int64All((*ents)[0]["id"]) identity.EntUserName = ObjToString((*ents)[0]["name"]) identity.Name = ObjToString((*ents)[0]["ent_name"]) identity.EntRole = Int64All((*ents)[0]["role_id"]) identity.EntNicheDis = Int64All((*ents)[0]["niche_dis"]) identity.EntUserRole = ObjToString((*ents)[0]["role"]) identity.EntDeptId = Int64All((*ents)[0]["dept_id"]) } } return identity } return nil } //根据企业员工id获取身份信息 func IdentityByEntUserId(msl *Mysql, mgo *MongodbSim, entUserId int64) *Identity { ents := msl.SelectBySql(`SELECT a.id,a.name,a.phone,a.ent_id,a.niche_dis,a.role,b.name as ent_name,c.role_id,d.dept_id from jianyu.entniche_user a inner join jianyu.entniche_info b on (a.id=? and a.ent_id=b.id) left join jianyu.entniche_user_role c on (a.id=c.user_id) LEFT JOIN jianyu.entniche_department_user d ON (d.user_id=a.id) limit 1`, entUserId) if ents == nil || len(*ents) == 0 { return nil } if entId, phone := Int64All((*ents)[0]["ent_id"]), ObjToString((*ents)[0]["phone"]); entId > 0 && phone != "" { users, ok := mgo.Find("user", map[string]interface{}{ "i_appid": 2, "$or": []map[string]interface{}{ map[string]interface{}{ "s_phone": phone, }, map[string]interface{}{ "s_m_phone": phone, }, }, }, `{"s_phone":-1}`, map[string]interface{}{ "base_user_id": 1, }, false, 0, 1) if !ok || users == nil { return nil } userId := Int64All((*users)[0]["base_user_id"]) list := msl.SelectBySql(`SELECT a.id as position_id,c.person_id,a.person_name,a.account_id,a.type as position_type,b.id as ent_account_id from base_service.base_position a inner join base_service.base_account c on (a.user_id=? and a.ent_id=? and a.type=1 and c.ent_id=? and c.type=1 and a.account_id=c.id) inner join base_service.base_account b on (b.type=1 and b.person_id=0 and b.ent_id=?) order by a.id desc limit 1`, userId, entId, entId, entId) if list != nil && len(*list) > 0 { identity := &Identity{ PersonId: Int64All((*list)[0]["person_id"]), UserName: ObjToString((*list)[0]["person_name"]), AccountId: Int64All((*list)[0]["account_id"]), EntAccountId: Int64All((*list)[0]["ent_account_id"]), PositionId: Int64All((*list)[0]["position_id"]), PositionType: Int64All((*list)[0]["position_type"]), UserId: userId, EntId: entId, EntUserId: Int64All((*ents)[0]["id"]), EntUserName: ObjToString((*ents)[0]["name"]), Name: ObjToString((*ents)[0]["ent_name"]), EntRole: Int64All((*ents)[0]["role_id"]), EntNicheDis: Int64All((*ents)[0]["niche_dis"]), EntUserRole: ObjToString((*ents)[0]["role"]), EntDeptId: Int64All((*ents)[0]["dept_id"]), } return identity } } return nil } //根据企业id获取身份信息 func IdentityByEntId(msl *Mysql, entId int64) *Identity { list := msl.SelectBySql(`select id from base_service.base_account where ent_id=? and type=1 and person_id=0 limit 1`, entId) if list == nil || len(*list) == 0 { return nil } return &Identity{ EntId: entId, EntAccountId: Int64All((*list)[0]["id"]), } }