gift.go 7.42 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
package gift_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"hilo-group/_const/enum/gift_e"
	"hilo-group/domain/model/res_m"
	"hilo-group/myerr"
	"time"
)

/**
 * 礼物操作
 **/
type GiftOperate struct {
	mysql.Entity
	*domain.Model      `gorm:"-"`
	ResGiftId          mysql.ID
	GiftN              mysql.Num
	SendUserId         mysql.ID
	ReceiveUserId      mysql.ID
	SendUserDiamond    mysql.Num
	ReceiveUserDiamond mysql.Num
	ReceiveUserBean    mysql.Num
	SceneType          gift_e.GiftOperateSceneType
	SceneUid           mysql.Str
}

// 用户收取礼物分类统计(for 礼物墙)
type GiftCountUser struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserId        mysql.ID
	ResGiftId     mysql.ID
	Num           mysql.Num
	addNum        mysql.Num `gorm:"-"`
}

/**
 * 送礼物
 **/
func SendGift(model *domain.Model, giftId mysql.ID, giftN mysql.Num, sendUserId mysql.ID, receiveUserId mysql.ID, sceneType gift_e.GiftOperateSceneType, sceneUid mysql.Str) (*GiftOperate, *res_m.ResGift, error) {
	resGift, err := res_m.GetGiftWithValid(model, giftId)
	if err != nil {
		return nil, nil, err
	}
	return &GiftOperate{
		Model:              model,
		ResGiftId:          giftId,
		GiftN:              giftN,
		SendUserId:         sendUserId,
		ReceiveUserId:      receiveUserId,
		SendUserDiamond:    resGift.DiamondNum * giftN,
		ReceiveUserDiamond: resGift.ReceiveDiamondNum * giftN,
		ReceiveUserBean:    resGift.BeanNum * giftN,
		SceneType:          sceneType,
		SceneUid:           sceneUid,
	}, resGift, nil
}

func GetCountGiftOperateByDay(model *domain.Model, userId uint64, beginTime, endTime time.Time) (int64, error) {
	var n int64 = 0
	if err := model.Db.Model(&GiftOperate{}).Where(&GiftOperate{
		SendUserId: userId,
	}).Where("created_time >= ? and created_time <= ?", beginTime, endTime).Distinct("Receive_User_Id").Count(&n).Error; err != nil {
		return 0, err
	}
	return n, nil
}

func GetGiftSummaryByDay(userId uint64, sceneType gift_e.GiftOperateSceneType, beginTime, endTime time.Time) (map[string]uint64, error) {
	c := GiftOperate{ReceiveUserId: userId, SceneType: sceneType}
	type summary struct {
		Date string
		Sum  uint64
	}
	records := make([]summary, 0)
	err := mysql.Db.Model(&GiftOperate{}).
		Select("DATE_FORMAT(created_time, '%Y-%m-%d') AS date, SUM(receive_user_bean) AS sum").
		Where(c).Where("created_time BETWEEN ? AND ?", beginTime, endTime).
		Group("date").Find(&records).Error
	if err != nil {
		return nil, err
	}
	result := make(map[string]uint64, len(records))
	for _, i := range records {
		result[i.Date] = i.Sum
	}
	return result, err
}

func GetGiftAllSummaryByDay(userId uint64, beginTime, endTime time.Time) (map[string]uint64, error) {
	c := GiftOperate{ReceiveUserId: userId}
	type summary struct {
		Date string
		Sum  uint64
	}
	records := make([]summary, 0)
	err := mysql.Db.Model(&GiftOperate{}).
		Select("DATE_FORMAT(created_time, '%Y-%m-%d') AS date, SUM(receive_user_bean) AS sum").
		Where(c).Where("created_time BETWEEN ? AND ?", beginTime, endTime).
		Group("date").Find(&records).Error
	if err != nil {
		return nil, err
	}
	result := make(map[string]uint64, len(records))
	for _, i := range records {
		result[i.Date] = i.Sum
	}
	return result, err
}

func (g *GiftOperate) GetConsumeByRange(beginTime, endTime time.Time) (uint32, uint64, error) {
	type summary struct {
		C       uint32
		Consume uint64
	}
	rows := make([]summary, 0)
	err := g.Db.Model(g).
		Select("COUNT(DISTINCT(send_user_id)) AS c, SUM(send_user_diamond) AS Consume").
		Where(g).Where("created_time BETWEEN ? AND ?", beginTime, endTime).
		Find(&rows).Error
	if err != nil {
		return 0, 0, err
	}
	if len(rows) <= 0 {
		return 0, 0, nil
	}
chenweijian's avatar
chenweijian committed
130

chenweijian's avatar
chenweijian committed
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
	//if beginTime.Unix() >= 1691337600 && beginTime.Unix() < 1691942400 { // 因为这周水果机被部分人刷币,所以这里做了特殊的处理
	//	reduceMap := map[string]uint64{
	//		"HTGS#a63226380": 20000000,
	//		//"HTGS#a42641278": 10000000,
	//		"@TGS#3ZZ5GZLHA": 27141539,
	//		"HTGS#a81630128": 8955410,
	//		"HTGS#a42300598": 50211301,
	//		"HTGS#a40088696": 10000000,
	//		"HTGS#a21700997": 14352310,
	//		"HTGS#a83608384": 49644203,
	//		"@TGS#33GDXTSIH": 50000000,
	//		"HTGS#a50538513": 15000000,
	//		"HTGS#a77282385": 15000000,
	//		"HTGS#a59437326": 10000000,
	//		"HTGS#a16909405": 10000000,
	//		"HTGS#a44104431": 16861206,
	//		"HTGS#a56794634": 59591313,
	//		"HTGS#a11286025": 31232311,
	//		"HTGS#a17238550": 52309338,
	//		"HTGS#a83592361": 79545067,
	//		"HTGS#a39882521": 10255093,
	//	}
	//	if reduceNum, ok := reduceMap[g.SceneUid]; ok {
	//		if g.Log != nil {
	//			g.Log.Infof("fruit diamond reduceMap,groupId:%v,reduceNum:%v", g.SceneUid, reduceNum)
	//		}
	//		if rows[0].Consume >= reduceNum {
	//			rows[0].Consume -= reduceNum
	//		} else {
	//			rows[0].Consume = 0
	//		}
	//	}
	//}
hujiebin's avatar
hujiebin committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	return rows[0].C, rows[0].Consume, nil
}

type SceneConsumeSummary struct {
	SceneUid string
	C        uint32
	Consume  uint64
}

func (g *GiftOperate) BatchGetConsumeByRange(beginTime, endTime time.Time) ([]SceneConsumeSummary, error) {
	rows := make([]SceneConsumeSummary, 0)
	err := g.Db.Model(g).
		Select("scene_uid, COUNT(DISTINCT(send_user_id)) AS c, SUM(send_user_diamond) AS Consume").
		Where(g).Where("created_time BETWEEN ? AND ?", beginTime, endTime).
		Group("scene_uid").Find(&rows).Error
	if err != nil {
		return nil, err
	}
	return rows, nil
}

chenweijian's avatar
chenweijian committed
185 186 187 188 189 190 191 192 193 194 195
func (g *GiftOperate) BatchGetSupportList(beginTime, endTime time.Time) ([]uint64, error) {
	userIds := make([]uint64, 0)
	err := g.DB().Model(g).
		Select("DISTINCT(send_user_id) AS user_id").
		Where(g).Where("created_time BETWEEN ? AND ?", beginTime, endTime).Pluck("user_id", &userIds).Error
	if err != nil {
		return nil, err
	}
	return userIds, nil
}

hujiebin's avatar
hujiebin committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
func SumSendGift(model *domain.Model, sendUserId mysql.ID, giftId mysql.ID) (uint32, error) {
	type Result struct {
		N uint32
	}
	sum := Result{}
	if err := model.Db.Raw("SELECT SUM(o.gift_n) as n from gift_operate o where o.send_user_id = ? and o.res_gift_id = ?", sendUserId, giftId).Scan(&sum).Error; err != nil {
		return 0, myerr.WrapErr(err)
	} else {
		return sum.N, nil
	}
}

type GiftPrivateRecord struct {
	mysql.Entity
	ResGiftId     mysql.ID
	GiftN         mysql.Num
	DiamondNum    mysql.Num
	SendUserId    mysql.ID
	ReceiveUserId mysql.ID
	RefId         mysql.ID                     //其实就是diamondSendAccountDetailId
	OriginType    gift_e.GiftPrivateRecordType //来源类型
	OriginId      mysql.ID                     //来源ID
}

func (gpr *GiftPrivateRecord) Create(db *gorm.DB) error {
	return db.Create(gpr).Error
}

func (gpr *GiftPrivateRecord) Find(db *gorm.DB) ([]GiftPrivateRecord, error) {
	rows := make([]GiftPrivateRecord, 0)
	if err := db.Where(gpr).Order("created_time DESC").Find(&rows).Error; err != nil {
		return nil, err
	}
	return rows, nil
}

func (gpr *GiftPrivateRecord) Remove(db *gorm.DB) (int64, error) {
	result := db.Delete(gpr)
	return result.RowsAffected, result.Error
}

func GetGiftOperate(resGift *res_m.ResGift, giftId mysql.ID, giftN mysql.Num, sendUserId mysql.ID,
	receiveUserId mysql.ID, sceneType gift_e.GiftOperateSceneType, sceneUid mysql.Str) *GiftOperate {
	return &GiftOperate{
		ResGiftId:          giftId,
		GiftN:              giftN,
		SendUserId:         sendUserId,
		ReceiveUserId:      receiveUserId,
		SendUserDiamond:    resGift.DiamondNum * giftN,
		ReceiveUserDiamond: resGift.ReceiveDiamondNum * giftN,
		ReceiveUserBean:    resGift.BeanNum * giftN,
		SceneType:          sceneType,
		SceneUid:           sceneUid,
	}
}