@@ -0,0 +1,121 @@ | |||
package db | |||
import ( | |||
"applet/app/db/gim/model" | |||
"applet/app/utils" | |||
"applet/app/utils/logx" | |||
"errors" | |||
"fmt" | |||
"reflect" | |||
"xorm.io/xorm" | |||
) | |||
// BatchSelectGroupUsers 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `GroupUserFindByParams` 方法 | |||
func BatchSelectGroupUsers(Db *xorm.Engine, params map[string]interface{}) (*[]model.GroupUser, error) { | |||
var GroupUserData []model.GroupUser | |||
if err := Db.In(utils.AnyToString(params["key"]), params["value"]). | |||
Find(&GroupUserData); err != nil { | |||
return nil, logx.Warn(err) | |||
} | |||
return &GroupUserData, nil | |||
} | |||
// GroupUserInsert 插入单条数据 | |||
func GroupUserInsert(Db *xorm.Engine, GroupUser *model.GroupUser) (int64, error) { | |||
_, err := Db.InsertOne(GroupUser) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return GroupUser.Id, nil | |||
} | |||
// BatchAddGroupUsers 批量新增数据 | |||
func BatchAddGroupUsers(Db *xorm.Engine, GroupUserData []*model.GroupUser) (int64, error) { | |||
affected, err := Db.Insert(GroupUserData) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return affected, nil | |||
} | |||
func GetGroupUserCount(Db *xorm.Engine) int { | |||
var GroupUser model.GroupUser | |||
session := Db.Where("") | |||
count, err := session.Count(&GroupUser) | |||
if err != nil { | |||
return 0 | |||
} | |||
return int(count) | |||
} | |||
// GroupUserDelete 删除记录 | |||
func GroupUserDelete(Db *xorm.Engine, id interface{}) (int64, error) { | |||
if reflect.TypeOf(id).Kind() == reflect.Slice { | |||
return Db.In("id", id).Delete(model.GroupUser{}) | |||
} else { | |||
return Db.Where("id = ?", id).Delete(model.GroupUser{}) | |||
} | |||
} | |||
// GroupUserUpdate 更新记录 | |||
func GroupUserUpdate(Db *xorm.Engine, id interface{}, GroupUser *model.GroupUser, forceColums ...string) (int64, error) { | |||
var ( | |||
affected int64 | |||
err error | |||
) | |||
if forceColums != nil { | |||
affected, err = Db.Where("id=?", id).Cols(forceColums...).Update(GroupUser) | |||
} else { | |||
affected, err = Db.Where("id=?", id).Update(GroupUser) | |||
} | |||
if err != nil { | |||
return 0, err | |||
} | |||
return affected, nil | |||
} | |||
// GroupUserGetOneByParams 通过传入的参数查询数据(单条) | |||
func GroupUserGetOneByParams(Db *xorm.Engine, memberType int, params map[string]interface{}) (*model.GroupUser, error) { | |||
var m model.GroupUser | |||
var query = fmt.Sprintf("%s =?", params["key"]) | |||
has, err := Db.Where(query, params["value"]).And("member_type = ?", memberType).Get(&m) | |||
if err != nil { | |||
return nil, logx.Error(err) | |||
} | |||
if has == false { | |||
return nil, nil | |||
} | |||
return &m, nil | |||
} | |||
// GroupUserFindByParams 通过传入的参数查询数据(多条) | |||
func GroupUserFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.GroupUser, error) { | |||
var m []model.GroupUser | |||
if params["value"] == nil { | |||
return nil, errors.New("参数有误") | |||
} | |||
if params["key"] == nil { | |||
//查询全部数据 | |||
err := Db.Find(&m) | |||
if err != nil { | |||
return nil, logx.Error(err) | |||
} | |||
return &m, nil | |||
} else { | |||
if reflect.TypeOf(params["value"]).Kind() == reflect.Slice { | |||
//指定In查询 | |||
if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil { | |||
return nil, logx.Warn(err) | |||
} | |||
return &m, nil | |||
} else { | |||
var query = fmt.Sprintf("%s =?", params["key"]) | |||
err := Db.Where(query, params["value"]).Find(&m) | |||
if err != nil { | |||
return nil, logx.Error(err) | |||
} | |||
return &m, nil | |||
} | |||
} | |||
} |
@@ -115,78 +115,3 @@ func UserFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model. | |||
} | |||
} | |||
// UserFindByLevel 查询等级数据(多条) | |||
//func UserFindByLevel(level int) (*[]model.User, error) { | |||
// var m []model.User | |||
// | |||
//} | |||
// 根据经纬度获取网点 | |||
// lng float64 经度 | |||
// lat float64 维度 | |||
func GetSelectedSiteWithLatLng(engine *xorm.Engine, lng, lat float64, page, pageSize string, scopeOfBranches int) (*[]model.User, error) { | |||
var regions []model.User | |||
where := `sqrt( ( (( %f - longitude)*PI()*12656*cos((( %f +latitude)/2)*PI()/180)/180) * (( %f - longitude)*PI()*12656*cos (((%f+latitude)/2)*PI()/180)/180) ) + ( ((%f-latitude)*PI()*12656/180) * ((%f-latitude)*PI()*12656/180) ) ) < ` + utils.IntToStr(scopeOfBranches) | |||
where = fmt.Sprintf(where, lng, lat, lng, lat, lat, lat) | |||
if page == "" && pageSize == "" { | |||
err := engine.Where(where).Find(®ions) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return ®ions, nil | |||
} | |||
p := utils.StrToInt(page) // 页码 | |||
ps := utils.StrToInt(pageSize) // 每页记录数 | |||
err := engine.Where(where).Limit(ps, (p-1)*ps).Find(®ions) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return ®ions, nil | |||
} | |||
// 根据经纬度获取最近的网点 | |||
// lng float64 经度 | |||
// lat float64 维度 | |||
func GetSelectedSiteWithLatLngNearClose(engine *xorm.Engine, lng, lat float64, scopeOfBranches int) (*model.User, error) { | |||
var region model.User | |||
where := `sqrt( ( (( %f - longitude)*PI()*12656*cos((( %f +latitude)/2)*PI()/180)/180) * (( %f - longitude)*PI()*12656*cos (((%f+latitude)/2)*PI()/180)/180) ) + ( ((%f-latitude)*PI()*12656/180) * ((%f-latitude)*PI()*12656/180) ) ) < ` + utils.IntToStr(scopeOfBranches) | |||
where = fmt.Sprintf(where, lng, lat, lng, lat, lat, lat) | |||
sel := `regional_agent_region.*, sqrt( ( (( %f - longitude)*PI()*12656*cos((( %f +latitude)/2)*PI()/180)/180) * (( %f - longitude)*PI()*12656*cos (((%f+latitude)/2)*PI()/180)/180) ) + ( ((%f-latitude)*PI()*12656/180) * ((%f-latitude)*PI()*12656/180) ) ) AS km` | |||
sel = fmt.Sprintf(sel, lng, lat, lng, lat, lat, lat) | |||
isHas, err := engine.Select(sel). | |||
Table("regional_agent_region").Where(where).Get(®ion) | |||
if err != nil { | |||
return nil, err | |||
} | |||
if !isHas { | |||
return nil, nil | |||
} | |||
return ®ion, nil | |||
} | |||
// 根据总后台的区id获取代理网点 | |||
func GetSiteListWithWebSiteRegionId(session *xorm.Session, websiteRegionId string) (*model.User, error) { | |||
var regionD *model.User | |||
isExist, err := session.Table("regional_agent_region"). | |||
Where("website_region_id = ?", websiteRegionId). | |||
Get(®ionD) | |||
if err != nil { | |||
return nil, err | |||
} | |||
if !isExist { | |||
return nil, nil | |||
} | |||
var regionSite *model.User | |||
isExist, err = session.Table("regional_agent_region"). | |||
Where("district_id = ?", regionD.Id).Get(®ionSite) | |||
if err != nil { | |||
return nil, err | |||
} | |||
if !isExist { | |||
return regionD, nil | |||
} | |||
return regionSite, nil | |||
} |
@@ -0,0 +1,11 @@ | |||
package model | |||
type GroupUser struct { | |||
Id int64 `json:"id"` | |||
GroupId int64 `json:"group_id"` | |||
UserId int64 `json:"user_id"` | |||
MemberType int `json:"member_type"` | |||
Status int `json:"status"` | |||
CreateTime string `json:"create_time"` | |||
UpdateTime string `json:"update_time"` | |||
} |
@@ -13,6 +13,7 @@ import ( | |||
"errors" | |||
"fmt" | |||
"github.com/streadway/amqp" | |||
"strconv" | |||
"time" | |||
) | |||
@@ -78,8 +79,18 @@ func handleDouShenUserRegisterConsumeForMyRecommender(msgData []byte) error { | |||
if fansGroup == nil { | |||
return errors.New("当前推荐人暂未拥有粉丝群,请联系管理员!!!") | |||
} else { | |||
userGroup, err2 := db2.GroupUserGetOneByParams(db.ImDb, 1, map[string]interface{}{ | |||
"key": "group_id", | |||
"value": fansGroup.GroupId, | |||
}) | |||
if err2 != nil { | |||
return err2 | |||
} | |||
if userGroup == nil { | |||
return errors.New("当前推荐人群暂未设置群主,请联系管理员!!!") | |||
} | |||
//加入群 | |||
_, err = utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).AddGroupMembers(utils.GetCtx("", "-1", "", msg.MasterId), &pb.AddGroupMembersReq{ | |||
_, err = utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).AddGroupMembers(utils.GetCtx("", strconv.FormatInt(userGroup.UserId, 10), "", msg.MasterId), &pb.AddGroupMembersReq{ | |||
GroupId: int64(fansGroup.GroupId), | |||
UserIds: []int64{gimUser.Id}, | |||
}) | |||
@@ -106,8 +106,18 @@ func handleDouShenUserRegisterConsumeForOfficial(msgData []byte) error { | |||
return err | |||
} | |||
} else { | |||
userGroup, err2 := db2.GroupUserGetOneByParams(db.ImDb, 1, map[string]interface{}{ | |||
"key": "group_id", | |||
"value": officialGroup.GroupId, | |||
}) | |||
if err2 != nil { | |||
return err2 | |||
} | |||
if userGroup == nil { | |||
return errors.New("当前官方群暂未设置群主,请联系管理员!!!") | |||
} | |||
//加入群 | |||
_, err = utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).AddGroupMembers(utils.GetCtx("", "-1", "74802", msg.MasterId), &pb.AddGroupMembersReq{ | |||
_, err = utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).AddGroupMembers(utils.GetCtx("", strconv.FormatInt(userGroup.UserId, 10), "", msg.MasterId), &pb.AddGroupMembersReq{ | |||
GroupId: int64(officialGroup.GroupId), | |||
UserIds: []int64{gimUser.Id}, | |||
}) | |||
@@ -13,6 +13,7 @@ import ( | |||
"errors" | |||
"fmt" | |||
"github.com/streadway/amqp" | |||
"strconv" | |||
"time" | |||
) | |||
@@ -78,8 +79,19 @@ func handleDouShenUserRegisterConsumeForOperationCenter(msgData []byte) error { | |||
if OperationGroup == nil { | |||
return errors.New("当前城市合伙人暂未拥有运营中心群,请联系管理员!!!") | |||
} else { | |||
userGroup, err2 := db2.GroupUserGetOneByParams(db.ImDb, 1, map[string]interface{}{ | |||
"key": "group_id", | |||
"value": OperationGroup.GroupId, | |||
}) | |||
if err2 != nil { | |||
return err2 | |||
} | |||
if userGroup == nil { | |||
return errors.New("当前官方群暂未设置群主,请联系管理员!!!") | |||
} | |||
//加入群 | |||
_, err = utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).AddGroupMembers(utils.GetCtx("", "-1", "", msg.MasterId), &pb.AddGroupMembersReq{ | |||
_, err = utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).AddGroupMembers(utils.GetCtx("", strconv.FormatInt(userGroup.UserId, 10), "", msg.MasterId), &pb.AddGroupMembersReq{ | |||
GroupId: int64(OperationGroup.GroupId), | |||
UserIds: []int64{gimUser.Id}, | |||
}) | |||