Browse Source

#1249

add models
tags/v1.22.9.2^2
chenyifan01 3 years ago
parent
commit
e553d02233
13 changed files with 262 additions and 0 deletions
  1. +32
    -0
      models/point_account.go
  2. +16
    -0
      models/point_account_log.go
  3. +23
    -0
      models/point_limit_config.go
  4. +36
    -0
      models/point_operate_record.go
  5. +28
    -0
      models/point_periodic_task.go
  6. +12
    -0
      models/point_task_accomplish_log.go
  7. +25
    -0
      models/point_task_config.go
  8. +9
    -0
      services/reward/account/account.go
  9. +4
    -0
      services/reward/operate/callback.go
  10. +45
    -0
      services/reward/operate/operator.go
  11. +16
    -0
      services/reward/point/point_operate.go
  12. +6
    -0
      services/reward/reward.go
  13. +10
    -0
      services/task/point_task.go

+ 32
- 0
models/point_account.go View File

@@ -0,0 +1,32 @@
package models

import "code.gitea.io/gitea/modules/timeutil"

type PointAccountStatus int

// Possible PointAccountStatus types.
const (
PointAccountNormal PointAccountStatus = iota + 1 // 1
PointAccountFreeze // 2
PointAccountDeleted // 3
)

type PointAccount struct {
ID int64 `xorm:"pk autoincr"`
Balance int64 `xorm:"NOT NULL DEFAULT 0"`
TotalEarned int64 `xorm:"NOT NULL DEFAULT 0"`
TotalConsumed int64 `xorm:"NOT NULL DEFAULT 0"`
UserId int64 `xorm:"INDEX NOT NULL"`
Status string `xorm:"NOT NULL"`
Version int64 `xorm:"NOT NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
}

func (account *PointAccount) Increase(amount int64) error {
return nil
}

func (account *PointAccount) Decrease(amount int64) error {
return nil
}

+ 16
- 0
models/point_account_log.go View File

@@ -0,0 +1,16 @@
package models

import "code.gitea.io/gitea/modules/timeutil"

type PointAccountLog struct {
ID int64 `xorm:"pk autoincr"`
AccountId int64 `xorm:"INDEX NOT NULL"`
UserId int64 `xorm:"INDEX NOT NULL"`
Type string `xorm:"NOT NULL"`
RelatedId string `xorm:"INDEX NOT NULL"`
PointsAmount int64 `xorm:"NOT NULL"`
AmountBefore int64 `xorm:"NOT NULL"`
AmountAfter int64 `xorm:"NOT NULL"`
AccountVersion int64 `xorm:"NOT NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
}

+ 23
- 0
models/point_limit_config.go View File

@@ -0,0 +1,23 @@
package models

import "code.gitea.io/gitea/modules/timeutil"

const (
LimitConfigRefreshRateOnce = "ONCE"
LimitConfigRefreshRateDaily = "DAILY"
)
const (
LimitTargetRangeAllUser = "ALL_USER"
LimitTargetRangeSingleUser = "SINGLE_USER"
)

type PointLimitConfig struct {
ID int64 `xorm:"pk autoincr"`
Tittle string
RefreshRate string `xorm:"NOT NULL"`
TargetRange string `xorm:"NOT NULL"`
LimitNum int64 `xorm:"NOT NULL"`
Creator int64 `xorm:"NOT NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"created"`
DeletedAt timeutil.TimeStamp `xorm:"deleted"`
}

+ 36
- 0
models/point_operate_record.go View File

@@ -0,0 +1,36 @@
package models

import "code.gitea.io/gitea/modules/timeutil"

type RewardSourceType string

const (
SourceTypeAccomplishPointTask RewardSourceType = "ACCOMPLISH_POINT_TASK"
SourceTypeAdminOperate RewardSourceType = "ADMIN_OPERATE"
SourceTypeRunCloudbrainTask RewardSourceType = "RUN_CLOUBRAIN_TASK"
)

const (
OperateTypeIncrease = "INCREASE_POINT"
OperateTypeDecrease = "DECREASE_POINT"
)

const (
OperateStatusOperating = "OPERATING"
OperateStatusSucceeded = "SUCCEEDED"
OperateStatusFailed = "FAILED"
)

type PointOperateRecord struct {
ID int64 `xorm:"pk autoincr"`
UserId int64 `xorm:"INDEX NOT NULL"`
PointsAmount int64 `xorm:"NOT NULL"`
RelatedType string `xorm:"NOT NULL"`
RelatedId string `xorm:"INDEX NOT NULL"`
OperateType string `xorm:"NOT NULL"`
OperateRate string `xorm:"NOT NULL default once"`
Status string `xorm:"NOT NULL"`
Remark string
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
}

+ 28
- 0
models/point_periodic_task.go View File

@@ -0,0 +1,28 @@
package models

import "code.gitea.io/gitea/modules/timeutil"

type PeriodicTaskStatus int

// Possible PeriodicTaskStatus types.
const (
PeriodicTaskStatusRunning PointAccountStatus = iota + 1 // 1
PeriodicTaskStatusSuccess // 2
PeriodicTaskStatusFailed // 3
)

type PeriodicTask struct {
ID int64 `xorm:"pk autoincr"`
Type string `xorm:"NOT NULL"`
OperateRecordId int64 `xorm:"INDEX NOT NULL"`
IntervalSecond int64 `xorm:"NOT NULL"`
PointsAmount int64 `xorm:"NOT NULL"`
NextExecuteTime timeutil.TimeStamp
SuccessCount int `xorm:"NOT NULL default 0"`
FailedCount int `xorm:"NOT NULL default 0"`
Status string `xorm:"NOT NULL"`
ExitCode string
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
FinishedUnix timeutil.TimeStamp `xorm:"INDEX"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
}

+ 12
- 0
models/point_task_accomplish_log.go View File

@@ -0,0 +1,12 @@
package models

import "code.gitea.io/gitea/modules/timeutil"

type PointTaskAccomplishLog struct {
ID int64 `xorm:"pk autoincr"`
ConfigId int64 `xorm:"NOT NULL"`
TaskCode int64 `xorm:"NOT NULL"`
UserId int64 `xorm:"INDEX NOT NULL"`
RelatedId int64 `xorm:"INDEX NOT NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
}

+ 25
- 0
models/point_task_config.go View File

@@ -0,0 +1,25 @@
package models

import (
"code.gitea.io/gitea/modules/timeutil"
)

const (
TaskConfigRefreshRateOnce = "ONCE"
TaskConfigRefreshRateDaily = "DAILY"
)

//PointTaskConfig Only add and delete are allowed, edit is not allowed
//so if you want to edit config for some task code,please delete first and add new one
type PointTaskConfig struct {
ID int64 `xorm:"pk autoincr"`
TaskCode string `xorm:"NOT NULL"`
Tittle string `xorm:"NOT NULL"`
RefreshRate string `xorm:"NOT NULL"`
Times int `xorm:"NOT NULL"`
AwardPoints int `xorm:"NOT NULL"`
Status int `xorm:"NOT NULL"`
Creator int64 `xorm:"NOT NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"created"`
DeletedAt timeutil.TimeStamp `xorm:"deleted"`
}

+ 9
- 0
services/reward/account/account.go View File

@@ -0,0 +1,9 @@
package account

func IncreaseAmount(userId int64, amount int64) error {
return nil
}

func DecreaseAmount(userId int64, amount int64) error {
return nil
}

+ 4
- 0
services/reward/operate/callback.go View File

@@ -0,0 +1,4 @@
package operate

type CallbackHandler struct {
}

+ 45
- 0
services/reward/operate/operator.go View File

@@ -0,0 +1,45 @@
package operate

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/services/reward"
)

type RewardOperateContext struct {
SourceType models.RewardSourceType
RelatedId string
Remark string
Reward reward.Reward
TargetUserId int64
RequestId string
}

type RewardOperateResponse int

const (
RewardOperateSuccess RewardOperateResponse = iota + 1
RewardOperateBalanceNotEnough
)

func (t RewardOperateResponse) IsSuccess() bool {
return t == RewardOperateSuccess
}

type RewardOperator interface {
IsOperated(ctx RewardOperateContext) bool
IsLimited(ctx RewardOperateContext) bool
Operate(ctx RewardOperateContext) error
}

func Operate(operator RewardOperator, ctx RewardOperateContext) error {
if operator.IsOperated(ctx) {
return nil
}
if operator.IsLimited(ctx) {
return nil
}
if err := operator.Operate(ctx); err != nil {
return err
}
return nil
}

+ 16
- 0
services/reward/point/point_operate.go View File

@@ -0,0 +1,16 @@
package point

import "code.gitea.io/gitea/services/reward/operate"

type PointOperator struct {
}

func (operator *PointOperator) IsOperated(ctx operate.RewardOperateContext) bool {
return true
}
func (operator *PointOperator) IsLimited(ctx operate.RewardOperateContext) bool {
return true
}
func (operator *PointOperator) Operate(ctx operate.RewardOperateContext) error {
return nil
}

+ 6
- 0
services/reward/reward.go View File

@@ -0,0 +1,6 @@
package reward

type Reward struct {
Amount int
Type string
}

+ 10
- 0
services/task/point_task.go View File

@@ -0,0 +1,10 @@
package task

func Accomplish() error {
//1、幂等性判断
//2、获取任务配置
//3、判断任务是否可以完成
//4、生成任务记录
//5、触发奖励发放
return nil
}

Loading…
Cancel
Save