diff --git a/cv/diamond_cv/diamond.go b/cv/diamond_cv/diamond.go index 4530fe67ecec36e9245955b135e1816de440f7b6..29dd1a4059be5d7e8e3f9663cc46bb548e371b02 100644 --- a/cv/diamond_cv/diamond.go +++ b/cv/diamond_cv/diamond.go @@ -1,15 +1,9 @@ package diamond_cv import ( - "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" . "git.hilo.cn/hilo-common/utils" - "gorm.io/gorm" - "hilo-group/_const/enum/diamond_e" - "hilo-group/domain/model/bean_m" "hilo-group/domain/model/diamond_m" - "hilo-group/myerr" - "strconv" ) type CvDiamond struct { @@ -26,33 +20,6 @@ type CvDiamondBean struct { BeanNum string `json:"beanNum"` } -func GetDiamondBean(userId mysql.ID) (*CvDiamondBean, error) { - var diamondAccount diamond_m.DiamondAccount - err := mysql.Db.Where(&diamond_m.DiamondAccount{ - UserId: userId, - }).First(&diamondAccount).Error - if err != nil { - if err == gorm.ErrRecordNotFound { - diamondAccount = diamond_m.DiamondAccount{ - DiamondNum: 0, - } - } - return nil, err - } - - var beanAccount bean_m.BeanAccount - err = mysql.Db.Where(&bean_m.BeanAccount{ - UserId: userId, - }).First(&beanAccount).Error - if err != nil { - if err == gorm.ErrRecordNotFound { - beanAccount = bean_m.BeanAccount{ - BeanNum: 0, - } - } - } - return &CvDiamondBean{DiamondNum: diamondAccount.DiamondNum, BeanNum: strconv.FormatFloat(float64(beanAccount.BeanNum)/100, 'f', 2, 64)}, nil -} type CvDiamondDetail struct { //1:增加 2:减少 @@ -74,113 +41,4 @@ func GetDiamond(userId mysql.ID) (*CvDiamond, error) { return nil, err } return &CvDiamond{DiamondNum: NumToUint32(&diamondAccount.DiamondNum), PinkDiamondNum: NumToUint32(&diamondAccount.PinkDiamondNum)}, nil -} - -func GetDiamondBalances(userIds []mysql.ID) (map[mysql.ID]mysql.Num, error) { - result := make(map[mysql.ID]mysql.Num, len(userIds)) - data := make([]diamond_m.DiamondAccount, 0) - err := mysql.Db.Where("user_id IN ?", userIds).Find(&data).Error - if err != nil { - return nil, err - } - for _, i := range data { - result[i.UserId] = i.DiamondNum - } - return result, nil -} - -//充值记录 -func GetDiamondBuyList(userId mysql.ID, pageSize int, pageIndex int) ([]*CvDiamondDetail, error) { - var diamondAccountDetails []diamond_m.DiamondAccountDetail - if err := mysql.Db.Model(&diamond_m.DiamondAccountDetail{}). - Where("user_id = ? AND operate_type in (?)", userId, []uint8{diamond_e.BuyDiamond, diamond_e.DealerTransfer, diamond_e.Checkout, diamond_e.PayerMax, diamond_e.Paypal}). - Order("id desc").Limit(pageSize).Offset((pageIndex - 1) * pageSize).Find(&diamondAccountDetails).Error; err != nil { - return nil, err - } - var cvDiamondDetails []*CvDiamondDetail - - for i := 0; i < len(diamondAccountDetails); i++ { - unixTime := diamondAccountDetails[i].CreatedTime.Unix() - cvDiamondDetails = append(cvDiamondDetails, &CvDiamondDetail{ - AddReduce: TypeToUint8(&diamondAccountDetails[i].AddReduce), - OperateType: TypeToUint8(&diamondAccountDetails[i].OperateType), - DiamondNum: NumToUint32(&diamondAccountDetails[i].Num), - CreatedTime: &unixTime, - }) - } - return cvDiamondDetails, nil -} - -//钻石明细,不包括充值 -func GetDiamondDetailList(model *domain.Model, userId mysql.ID, pageSize int, pageIndex int) ([]*CvDiamondDetail, error) { - diamondAccountDetails := make([]*diamond_m.DiamondAccountDetail, 0) - offset := (pageIndex - 1) * pageSize - optList := []int{int(diamond_e.BuyDiamond), int(diamond_e.DealerTransfer), int(diamond_e.Checkout), int(diamond_e.PayerMax)} - details := make([]*diamond_m.DiamondAccountDetail, 0) - if offset == 0 { // 首页请求数据,获取 pageSize*3 条过滤返回 - sql := "select * from diamond_account_detail where user_id = ? order by id desc limit ?" - if err := mysql.Db.WithContext(model).Raw(sql, userId, pageSize*3).Find(&details).Error; err != nil { - return nil, myerr.WrapErr(err) - } - notInMap := make(map[int]bool) - for _, v := range optList { - notInMap[v] = true - } - for _, v := range details { - if _, ok := notInMap[int(v.OperateType)]; !ok { - diamondAccountDetails = append(diamondAccountDetails, v) - } - } - if len(diamondAccountDetails) > pageSize { - diamondAccountDetails = diamondAccountDetails[:pageSize] - } - } - // 非首页,或者首页没取满 pageSize 条 - if offset > 0 || (len(details) == pageSize*3 && len(diamondAccountDetails) < pageSize) { - diamondAccountDetails = make([]*diamond_m.DiamondAccountDetail, 0) - sql := "select * from diamond_account_detail where user_id = ? and operate_type not in (?) order by id desc limit ?,?" - if err := mysql.Db.WithContext(model).Raw(sql, userId, optList, offset, pageSize). - Find(&diamondAccountDetails).Error; err != nil { - return nil, myerr.WrapErr(err) - } - } - - //if err := mysql.Db.Table("diamond_account_detail FORCE INDEX(Index_1)"). - // Where("user_id = ? AND operate_type not in (?)", userId, - // []int{int(diamond_m2.BuyDiamond), int(diamond_m2.DealerTransfer), int(diamond_m2.Checkout), int(diamond_m2.PayerMax)}). - // Order("id desc").Limit(pageSize).Offset((pageIndex - 1) * pageSize).Find(&diamondAccountDetails).Error; err != nil { - // return nil, myerr.WrapErr(err) - //} - cvDiamondDetails := []*CvDiamondDetail{} - for i := 0; i < len(diamondAccountDetails); i++ { - unixTime := diamondAccountDetails[i].CreatedTime.Unix() - cvDiamondDetails = append(cvDiamondDetails, &CvDiamondDetail{ - AddReduce: TypeToUint8(&diamondAccountDetails[i].AddReduce), - OperateType: TypeToUint8(&diamondAccountDetails[i].OperateType), - DiamondNum: NumToUint32(&diamondAccountDetails[i].Num), - CreatedTime: &unixTime, - }) - } - return cvDiamondDetails, nil -} - -//粉钻流水,包含充值 -func GetPinkDiamondDetailList(userId mysql.ID, pageSize int, pageIndex int) ([]*CvDiamondDetail, error) { - var diamondAccountDetails []diamond_m.DiamondPinkAccountDetail - if err := mysql.Db.Model(&diamond_m.DiamondPinkAccountDetail{}). - Where("user_id = ?", userId). - Order("id desc").Limit(pageSize).Offset((pageIndex - 1) * pageSize).Find(&diamondAccountDetails).Error; err != nil { - return nil, myerr.WrapErr(err) - } - var cvDiamondDetails []*CvDiamondDetail - for i := 0; i < len(diamondAccountDetails); i++ { - unixTime := diamondAccountDetails[i].CreatedTime.Unix() - cvDiamondDetails = append(cvDiamondDetails, &CvDiamondDetail{ - AddReduce: TypeToUint8(&diamondAccountDetails[i].AddReduce), - OperateType: TypeToUint8(&diamondAccountDetails[i].OperateType), - DiamondNum: NumToUint32(&diamondAccountDetails[i].Num), - CreatedTime: &unixTime, - }) - } - return cvDiamondDetails, nil -} +} \ No newline at end of file diff --git a/domain/model/diamond_m/diamond.go b/domain/model/diamond_m/diamond.go index 76929c1982e48997e2d967c344fc2392c3fcaaef..d58d1ea73de142e8c61bfbcc0d5a14305eef8384 100644 --- a/domain/model/diamond_m/diamond.go +++ b/domain/model/diamond_m/diamond.go @@ -1,13 +1,13 @@ package diamond_m import ( + "fmt" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mylogrus" "git.hilo.cn/hilo-common/resource/mysql" "git.hilo.cn/hilo-common/utils" "gorm.io/gorm" "hilo-group/_const/enum/diamond_e" - "hilo-group/domain/model" "hilo-group/myerr" "hilo-group/myerr/bizerr" "strconv" @@ -40,6 +40,11 @@ type DiamondAccountDetail struct { diamondAccount *DiamondAccount `gorm:"-"` } +func (DiamondAccountDetail) TableName() string { + month := time.Now().Format("200601") + return fmt.Sprintf("diamond_account_detail_%s", month) +} + // 粉钻详情 type DiamondPinkAccountDetail struct { mysql.Entity @@ -110,7 +115,7 @@ func (diamondAccount *DiamondAccount) addDiamondAccountDetail(operateType diamon var count int64 if diamondOperateSet.FrequencyDay == -1 { if diamondOperateSet.FrequencyNum != -1 { - diamondAccount.Db.Model(&DiamondAccountDetail{}).Where(&DiamondAccountDetail{ + diamondAccount.DB().Table(DiamondAccountDetail{}.TableName()).Where(&DiamondAccountDetail{ UserId: diamondAccount.UserId, OperateType: operateType, }).Count(&count) @@ -125,7 +130,7 @@ func (diamondAccount *DiamondAccount) addDiamondAccountDetail(operateType diamon return nil, myerr.WrapErr(err) } //一天的次数 - diamondAccount.Db.Model(&DiamondAccountDetail{}).Where(&DiamondAccountDetail{ + diamondAccount.DB().Table(DiamondAccountDetail{}.TableName()).Where(&DiamondAccountDetail{ UserId: diamondAccount.UserId, OperateType: operateType, }).Where("created_time >= ? ", beginTime).Count(&count) @@ -133,7 +138,7 @@ func (diamondAccount *DiamondAccount) addDiamondAccountDetail(operateType diamon return nil, bizerr.DiamondFrequency } //终极拦截,利用 - diamondAccount.SetCheckUpdateCondition(" EXISTS (SELECT * from (SELECT COUNT(1) as n from diamond_account_detail d where d.user_id = " + strconv.FormatUint(diamondAccount.UserId, 10) + " and d.operate_type = " + strconv.FormatUint(uint64(operateType), 10) + " and d.created_time >= from_unixtime(" + strconv.FormatInt(utils.GetZeroTime(time.Now()).Unix(), 10) + ")) t where t.n < " + strconv.Itoa(diamondOperateSet.FrequencyNum) + " )") + diamondAccount.SetCheckUpdateCondition(" EXISTS (SELECT * from (SELECT COUNT(1) as n from " + DiamondAccountDetail{}.TableName() + " d where d.user_id = " + strconv.FormatUint(diamondAccount.UserId, 10) + " and d.operate_type = " + strconv.FormatUint(uint64(operateType), 10) + " and d.created_time >= from_unixtime(" + strconv.FormatInt(utils.GetZeroTime(time.Now()).Unix(), 10) + ")) t where t.n < " + strconv.Itoa(diamondOperateSet.FrequencyNum) + " )") } //-1,代表值无效,由参数给与 @@ -197,7 +202,10 @@ func (diamondAccountDetail *DiamondAccountDetail) PersistentNoInTransactional() } //持久化diamondAccountDetail - if err := model.Persistent(diamondAccountDetail.Db, diamondAccountDetail); err != nil { + //if err := model.Persistent(diamondAccountDetail.Db, diamondAccountDetail); err != nil { + // return myerr.WrapErr(err) + //} + if err := diamondAccountDetail.DB().Table(diamondAccountDetail.TableName()).Save(diamondAccountDetail).Error; err != nil { return myerr.WrapErr(err) } //改变diamondAccount值 @@ -245,7 +253,10 @@ func (diamondAccountDetail *DiamondAccountDetail) Persistent() error { } //持久化diamondAccountDetail - if err := model.Persistent(diamondAccountDetail.Db, diamondAccountDetail); err != nil { + //if err := model.Persistent(diamondAccountDetail.Db, diamondAccountDetail); err != nil { + // return myerr.WrapErr(err) + //} + if err := diamondAccountDetail.DB().Table(diamondAccountDetail.TableName()).Save(diamondAccountDetail).Error; err != nil { return myerr.WrapErr(err) } //改变diamondAccount值 diff --git a/route/group_r/group_mic.go b/route/group_r/group_mic.go index 8484b949e7ce5988fa0e166772f32dc5b5d09631..0b6d89892dde33275af8b4063e45c43d4dbf8104 100644 --- a/route/group_r/group_mic.go +++ b/route/group_r/group_mic.go @@ -439,7 +439,7 @@ func GroupMicTaskInviteDialog(c *gin.Context) (*mycontext.MyContext, error) { endTime := beginTime.AddDate(0, 0, 1) var n int64 - if err := mysql.Db.Model(&diamond_m.DiamondAccountDetail{}).Where(&diamond_m.DiamondAccountDetail{ + if err := mysql.Db.Table(diamond_m.DiamondAccountDetail{}.TableName()).Where(&diamond_m.DiamondAccountDetail{ UserId: userId, AddReduce: mysql.ADD, }).Where("Created_Time <= ? and Created_Time >= ?", endTime, beginTime).Where("(Operate_Type = ? or Operate_Type = ?)", diamond_e.DailyInAppVip, diamond_e.DailyInAppCommon).Count(&n).Error; err != nil {