dealer.go 5.92 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 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
}