From 663ecee434a01c077efe7c96921977070a299cdb Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Tue, 14 Mar 2023 11:06:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=B6=E6=97=8F=E4=BB=A3=E7=90=86=E9=99=90?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/diamond_m/dealer.go | 211 ++++++++++++++++++++++++ domain/model/groupPower_m/groupPower.go | 14 ++ domain/service/diamond_s/diamond.go | 28 +++- 3 files changed, 244 insertions(+), 9 deletions(-) create mode 100644 domain/model/diamond_m/dealer.go diff --git a/domain/model/diamond_m/dealer.go b/domain/model/diamond_m/dealer.go new file mode 100644 index 0000000..a46391b --- /dev/null +++ b/domain/model/diamond_m/dealer.go @@ -0,0 +1,211 @@ +package diamond_m + +import ( + "git.hilo.cn/hilo-common/domain" + "git.hilo.cn/hilo-common/mylogrus" + "git.hilo.cn/hilo-common/resource/mysql" + "gorm.io/gorm" + "hilo-group/common" + "hilo-group/myerr" +) + +type DiamondDealer struct { + mysql.Entity + UserId uint64 + Diamond uint32 + Wechat string + Whatsapp string + Contact string + Status int + HasInvite int + IsFamilyAgent int8 +} + +func (dealer *DiamondDealer) Save(db *gorm.DB) error { + if dealer.ID > 0 { + db = db.Omit("diamond", "status") + } else { + dealer.Status = common.SWITCH_ON + } + return db.Save(dealer).Error +} + +func (dealer *DiamondDealer) Get(db *gorm.DB) error { + return db.Where(dealer).First(dealer).Error +} + +func IsDiamondDealer(db *gorm.DB, userId uint64) (bool, error) { + dd := DiamondDealer{UserId: userId, Status: common.SWITCH_ON} + if err := dd.Get(db); err != nil { + if err == gorm.ErrRecordNotFound { + return false, nil + } else { + return false, err + } + } + return true, nil +} + +// 是否是家族代理 +func IsFamilyDiamondDealer(db *gorm.DB, userId uint64) (bool, error) { + dd := DiamondDealer{UserId: userId, Status: common.SWITCH_ON, IsFamilyAgent: 1} + if err := dd.Get(db); err != nil { + if err == gorm.ErrRecordNotFound { + return false, nil + } else { + return false, err + } + } + return true, nil +} + +func GetDiamondDealer(db *gorm.DB, userId uint64) (*DiamondDealer, error) { + dd := &DiamondDealer{UserId: userId, Status: common.SWITCH_ON} + if err := dd.Get(db); err != nil { + if err == gorm.ErrRecordNotFound { + return nil, nil + } else { + return nil, myerr.WrapErr(err) + } + } + return dd, nil +} + +func (dealer *DiamondDealer) GetAll(db *gorm.DB, status *uint8) ([]DiamondDealer, error) { + rows := make([]DiamondDealer, 0) + if status == nil { + if err := db.Where(dealer).Find(&rows).Error; err != nil { + return nil, err + } + } else if err := db.Where(dealer).Where("status = ?", *status).Find(&rows).Error; err != nil { + return nil, err + } + return rows, nil +} + +func (dealer *DiamondDealer) Remove(db *gorm.DB) (int64, error) { + result := db.Where(dealer).Delete(&DiamondDealer{}) + return result.RowsAffected, result.Error +} + +func (dealer *DiamondDealer) SafeReduceDiamond(db *gorm.DB, diamond uint32) (int64, error) { + result := db.Model(&DiamondDealer{}).Where(dealer).Where("diamond >= ?", diamond).UpdateColumn("diamond", gorm.Expr("diamond - ?", diamond)) + return result.RowsAffected, result.Error +} + +func (dealer *DiamondDealer) AddDiamond(db *gorm.DB, diamond uint32) (int64, error) { + result := db.Model(&DiamondDealer{}).Where(dealer).Where("status = ?", common.SWITCH_ON).UpdateColumn("diamond", gorm.Expr("diamond + ?", diamond)) + return result.RowsAffected, result.Error +} + +func (dealer *DiamondDealer) SetStatus(db *gorm.DB, status uint8) error { + return db.Model(&DiamondDealer{}).Where(dealer).Update("status", status).Error +} + +type DealerTransferDetail struct { + mysql.Entity + DealerId uint64 + ReceiverId uint64 + Diamond uint32 + Dollar uint +} + +func (dtd *DealerTransferDetail) Create(db *gorm.DB) error { + return db.Create(dtd).Error +} + +func (dfd *DealerTransferDetail) Find(db *gorm.DB, offset, limit int) ([]DealerTransferDetail, error) { + rows := make([]DealerTransferDetail, 0) + if err := db.Where(dfd).Order("created_time DESC").Offset(offset).Limit(limit).Find(&rows).Error; err != nil { + return nil, err + } + return rows, nil +} + +func (dfd *DealerTransferDetail) CountByDealer(db *gorm.DB, dealerIds []uint64) (map[uint64]uint, error) { + type summary struct { + DealerId uint64 + C uint + } + rows := make([]summary, 0) + if err := db.Model(&DealerTransferDetail{}).Where("dealer_id IN ?", dealerIds). + Select("dealer_id, COUNT(0) AS c").Group("dealer_id").Find(&rows).Error; err != nil { + return nil, err + } + result := make(map[uint64]uint, 0) + for _, i := range rows { + result[i.DealerId] = i.C + } + return result, nil +} + +func (dfd *DealerTransferDetail) Sum(db *gorm.DB) (uint, uint32, uint, error) { + type summary struct { + C uint + Diamond uint32 + Dollar uint + } + s := summary{} + if err := db.Model(&DealerTransferDetail{}).Where(dfd). + Select("COUNT(0) AS c, SUM(diamond) AS diamond, SUM(dollar) AS dollar").First(&s).Error; err != nil { + return 0, 0, 0, err + } + return s.C, s.Diamond, s.Dollar, nil +} + +type DealerChargeDetail struct { + mysql.Entity + DealerId uint64 + MgrId uint64 + Diamond uint32 + Dollar uint + PaymentMethod string + Type uint8 + Money float32 `json:"money"` // 支付的货币数值 + Currency string `json:"currency"` // 支付货币 + GoodsId string `json:"goodsId"` // 充值钻石套餐id/商品id +} + +func (dcd *DealerChargeDetail) Create(db *gorm.DB) error { + return db.Create(dcd).Error +} + +type DealerCountry struct { + DealerId uint64 + Country string +} + +func (dc *DealerCountry) Find(db *gorm.DB) ([]DealerCountry, error) { + rows := make([]DealerCountry, 0) + if err := db.Where(dc).Find(&rows).Error; err != nil { + return nil, err + } + return rows, nil +} + +func (dc *DealerCountry) Delete(db *gorm.DB) error { + return db.Where(dc).Delete(&DealerCountry{}).Error +} + +func (dc *DealerCountry) BatchInsert(db *gorm.DB, dealerId uint64, countries []string) error { + rows := make([]DealerCountry, 0) + for _, i := range countries { + rows = append(rows, DealerCountry{ + DealerId: dealerId, + Country: i, + }) + } + return db.Create(&rows).Error +} + +// 活动排行榜黑名单 获取所有已经上架代理币商的群组id +func GetAllValidActivityBlackImGroupIds(model *domain.Model) []mysql.Str { + //return []mysql.Str{"HTGS#a88745892", "@TGS#3UW6RFSIX"} + var imGroupIds []mysql.Str + subQuery := model.Db.WithContext(model).Model(DiamondDealer{}).Where("status = 1").Select("user_id") + if err := model.Db.WithContext(model).Table("group_info").Select("im_group_id").Where("owner in (?)", subQuery).Scan(&imGroupIds).Error; err != nil { + mylogrus.MyLog.Errorf("GetAllValidActivityBlackImGroupIds fail:%v", err) + } + imGroupIds = append(imGroupIds, "HTGS#a88745892", "@TGS#3UW6RFSIX") + return imGroupIds +} diff --git a/domain/model/groupPower_m/groupPower.go b/domain/model/groupPower_m/groupPower.go index 40ad661..60eb2cc 100644 --- a/domain/model/groupPower_m/groupPower.go +++ b/domain/model/groupPower_m/groupPower.go @@ -356,6 +356,20 @@ func IsGroupPowerHasMgrDealer(model *domain.Model, groupId mysql.ID) (bool, erro return false, nil } +// 某个家族是否有家族代理 +func IsGroupPowerHasFamilyAgent(model *domain.Model, groupId mysql.ID) (bool, error) { + var num int64 + if err := model.Db.Raw("select count(1) from diamond_dealer where status=1 and is_family_agent=1 and user_id in "+ + "(select user_id from group_power_user where group_power_id = ?)", groupId). + Scan(&num).Error; err != nil { + return false, myerr.WrapErr(err) + } + if num > 0 { + return true, nil + } + return false, nil +} + func GetGroupPowerMap(db *gorm.DB, userIds []mysql.ID) (map[mysql.ID]uint64, error) { rows := make([]GroupPowerUser, 0) if len(userIds) > 0 { diff --git a/domain/service/diamond_s/diamond.go b/domain/service/diamond_s/diamond.go index 7ca5ced..1b29129 100644 --- a/domain/service/diamond_s/diamond.go +++ b/domain/service/diamond_s/diamond.go @@ -3,7 +3,9 @@ package diamond_s import ( "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" + "git.hilo.cn/hilo-common/resource/mysql" "hilo-group/common" + "hilo-group/domain/model/diamond_m" "hilo-group/domain/model/groupPower_m" "hilo-group/domain/model/res_m" "hilo-group/myerr" @@ -27,26 +29,34 @@ func (this *DiamondService) CheckDealerTransferFamilyLimit(dealerId, userId uint if err != nil { return err } + // 代理是否是家族代理 + isFamilyAgent, err := diamond_m.IsFamilyDiamondDealer(mysql.Db, dealerId) + if err != nil { + return err + } + userFamily, err := groupPower_m.GetGroupPowerUserOrNil(model, userId) if err != nil { return err } - // 用户的家族是否有家族长,而且家族长是代理 - var hasDealerMgr bool + // 用户的家族是否有家族代理 + var userHasFamilyAgent bool if userFamily != nil { - hasDealerMgr, err = groupPower_m.IsGroupPowerHasMgrDealer(model, userFamily.GroupPowerId) + userHasFamilyAgent, err = groupPower_m.IsGroupPowerHasFamilyAgent(model, userFamily.GroupPowerId) if err != nil { return err } } - if dealerFamily != nil && dealerFamily.GroupPowerId > 0 { // 家族代理 - // 家族代理:只能向本家族成员转移钻石,如果向非家族成员转移钻石,则提示“非家族成员” - if userFamily == nil || userFamily.GroupPowerId != dealerFamily.GroupPowerId { + if dealerFamily != nil && dealerFamily.GroupPowerId > 0 { // 代理加入了家族 + if isFamilyAgent && (userFamily == nil || userFamily.GroupPowerId != dealerFamily.GroupPowerId) { // 是家族代理:只能向本家族成员转移钻石,如果向非家族成员转移钻石,则提示“非家族成员” return myerr.WrapErr(res_m.GetErrByLanguage(model.Db, common.MSG_ID_NOT_FAMILY_MEMBER, lang, bizerr.GroupPowerDealerNotMember)) } - } else { // 普通代理,没有加入家族的 - // 普通代理:不能向已经加入家族的成员(并且家族长是代理)转移钻石,如果转移则提示“此用户已有家族代理,不能出售钻石” - if hasDealerMgr { + if !isFamilyAgent && userHasFamilyAgent { // 不是家族代理:如果用户加入了家族,且家族中有家族代理,那么也不能,“此用户已有家族代理,不能出售钻石” + return myerr.WrapErr(res_m.GetErrByLanguage(model.Db, common.MSG_ID_DEALER_CAN_NOT_SOLE, lang, bizerr.GroupPowerDealerCanNotSole)) + } + } else { // 代理没有加入家族的 + // 普通代理:不能向已经加入家族的成员(并且家族有家族代理)转移钻石,如果转移则提示“此用户已有家族代理,不能出售钻石” + if userHasFamilyAgent { return myerr.WrapErr(res_m.GetErrByLanguage(model.Db, common.MSG_ID_DEALER_CAN_NOT_SOLE, lang, bizerr.GroupPowerDealerCanNotSole)) } } -- 2.22.0