You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

reward_operate_record.go 13 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/timeutil"
  4. "strconv"
  5. "strings"
  6. "xorm.io/builder"
  7. )
  8. type SourceType string
  9. const (
  10. SourceTypeAccomplishTask SourceType = "ACCOMPLISH_TASK"
  11. SourceTypeAdminOperate SourceType = "ADMIN_OPERATE"
  12. SourceTypeRunCloudbrainTask SourceType = "RUN_CLOUDBRAIN_TASK"
  13. )
  14. func (r SourceType) Name() string {
  15. switch r {
  16. case SourceTypeAccomplishTask:
  17. return "ACCOMPLISH_TASK"
  18. case SourceTypeAdminOperate:
  19. return "ADMIN_OPERATE"
  20. case SourceTypeRunCloudbrainTask:
  21. return "RUN_CLOUDBRAIN_TASK"
  22. default:
  23. return ""
  24. }
  25. }
  26. type RewardType string
  27. const (
  28. RewardTypePoint RewardType = "POINT"
  29. )
  30. func (r RewardType) Name() string {
  31. switch r {
  32. case RewardTypePoint:
  33. return "POINT"
  34. default:
  35. return ""
  36. }
  37. }
  38. func (r RewardType) Show() string {
  39. switch r {
  40. case RewardTypePoint:
  41. return "积分"
  42. default:
  43. return ""
  44. }
  45. }
  46. func GetRewardTypeInstance(s string) RewardType {
  47. switch s {
  48. case RewardTypePoint.Name():
  49. return RewardTypePoint
  50. default:
  51. return ""
  52. }
  53. }
  54. type RewardOperateType string
  55. func (r RewardOperateType) Name() string {
  56. switch r {
  57. case OperateTypeIncrease:
  58. return "INCREASE"
  59. case OperateTypeDecrease:
  60. return "DECREASE"
  61. default:
  62. return ""
  63. }
  64. }
  65. func (r RewardOperateType) Show() string {
  66. switch r {
  67. case OperateTypeIncrease:
  68. return "奖励"
  69. case OperateTypeDecrease:
  70. return "扣减"
  71. default:
  72. return ""
  73. }
  74. }
  75. func GetRewardOperateTypeInstance(s string) RewardOperateType {
  76. switch s {
  77. case OperateTypeIncrease.Name():
  78. return OperateTypeIncrease
  79. case OperateTypeDecrease.Name():
  80. return OperateTypeDecrease
  81. default:
  82. return ""
  83. }
  84. }
  85. const (
  86. OperateTypeIncrease RewardOperateType = "INCREASE"
  87. OperateTypeDecrease RewardOperateType = "DECREASE"
  88. OperateTypeNull RewardOperateType = "NIL"
  89. )
  90. const (
  91. OperateStatusOperating = "OPERATING"
  92. OperateStatusSucceeded = "SUCCEEDED"
  93. OperateStatusFailed = "FAILED"
  94. )
  95. const Semicolon = ";"
  96. type RewardOperateOrderBy string
  97. const (
  98. RewardOrderByIDDesc RewardOperateOrderBy = "reward_operate_record.id desc"
  99. )
  100. type RewardRecordList []*RewardOperateRecord
  101. type RewardRecordShowList []*RewardOperateRecordShow
  102. func (l RewardRecordShowList) loadAttribute(isAdmin bool) {
  103. l.loadAction()
  104. l.loadCloudbrain()
  105. if isAdmin {
  106. l.loadAdminLog()
  107. }
  108. }
  109. func (l RewardRecordShowList) loadAction() error {
  110. if len(l) == 0 {
  111. return nil
  112. }
  113. actionIds := make([]int64, 0)
  114. actionIdMap := make(map[int64]*RewardOperateRecordShow, 0)
  115. for _, r := range l {
  116. if r.SourceType != SourceTypeAccomplishTask.Name() {
  117. continue
  118. }
  119. i, _ := strconv.ParseInt(r.SourceId, 10, 64)
  120. actionIds = append(actionIds, i)
  121. actionIdMap[i] = r
  122. }
  123. actions, err := GetActionByIds(actionIds)
  124. if err != nil {
  125. return err
  126. }
  127. for _, v := range actions {
  128. actionIdMap[v.ID].Action = v.ToShow()
  129. }
  130. return nil
  131. }
  132. func (l RewardRecordShowList) loadCloudbrain() error {
  133. if len(l) == 0 {
  134. return nil
  135. }
  136. cloudbrainIds := make([]int64, 0)
  137. cloudbrainMap := make(map[int64]*RewardOperateRecordShow, 0)
  138. for _, r := range l {
  139. if r.SourceType != SourceTypeRunCloudbrainTask.Name() {
  140. continue
  141. }
  142. i, _ := strconv.ParseInt(r.SourceId, 10, 64)
  143. cloudbrainIds = append(cloudbrainIds, i)
  144. cloudbrainMap[i] = r
  145. }
  146. cloudbrains, err := GetCloudbrainByIds(cloudbrainIds)
  147. if err != nil {
  148. return err
  149. }
  150. var repoIds []int64
  151. var taskIds []int64
  152. for _, task := range cloudbrains {
  153. repoIds = append(repoIds, task.RepoID)
  154. taskIds = append(taskIds, task.ID)
  155. }
  156. repositoryMap, err := GetRepositoriesMapByIDs(repoIds)
  157. specMap, err := GetResourceSpecMapByCloudbrainIDs(taskIds)
  158. if err != nil {
  159. return err
  160. }
  161. for _, v := range cloudbrains {
  162. v.Repo = repositoryMap[v.RepoID]
  163. v.Spec = specMap[v.ID]
  164. cloudbrainMap[v.ID].Cloudbrain = v.ToShow()
  165. }
  166. return nil
  167. }
  168. func (l RewardRecordShowList) loadAdminLog() error {
  169. if len(l) == 0 {
  170. return nil
  171. }
  172. logIds := make([]string, 0)
  173. logMap := make(map[string]*RewardOperateRecordShow, 0)
  174. for _, r := range l {
  175. if r.SourceType != SourceTypeAdminOperate.Name() {
  176. continue
  177. }
  178. logIds = append(logIds, r.SourceId)
  179. logMap[r.SourceId] = r
  180. }
  181. adminLogs, err := GetRewardAdminLogByLogIds(logIds)
  182. if err != nil {
  183. return err
  184. }
  185. for _, v := range adminLogs {
  186. logMap[v.LogId].AdminLog = v.ToShow()
  187. }
  188. return nil
  189. }
  190. type RewardOperateRecord struct {
  191. ID int64 `xorm:"pk autoincr"`
  192. SerialNo string `xorm:"INDEX NOT NULL"`
  193. UserId int64 `xorm:"INDEX NOT NULL"`
  194. Amount int64 `xorm:"NOT NULL"`
  195. LossAmount int64
  196. Title string
  197. RewardType string `xorm:"NOT NULL"`
  198. SourceType string `xorm:"NOT NULL"`
  199. SourceId string `xorm:"INDEX NOT NULL"`
  200. SourceTemplateId string
  201. RequestId string `xorm:"INDEX NOT NULL"`
  202. OperateType string `xorm:"NOT NULL"`
  203. Status string `xorm:"NOT NULL"`
  204. Remark string
  205. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  206. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  207. LastOperateUnix timeutil.TimeStamp `xorm:"INDEX"`
  208. }
  209. type AdminRewardOperateReq struct {
  210. TargetUserId int64 `binding:"Required"`
  211. OperateType RewardOperateType `binding:"Required"`
  212. Amount int64 `binding:"Required;Range(1,100000)"`
  213. Remark string
  214. RewardType RewardType
  215. }
  216. type RewardOperateRecordShow struct {
  217. SerialNo string
  218. Status string
  219. OperateType string
  220. SourceId string
  221. Amount int64
  222. LossAmount int64
  223. BalanceAfter int64
  224. Remark string
  225. SourceType string
  226. UserName string
  227. LastOperateDate timeutil.TimeStamp
  228. UnitPrice int64
  229. SuccessCount int
  230. Action *ActionShow
  231. Cloudbrain *CloudbrainShow
  232. AdminLog *RewardAdminLogShow
  233. }
  234. func getPointOperateRecord(tl *RewardOperateRecord) (*RewardOperateRecord, error) {
  235. has, err := x.Get(tl)
  236. if err != nil {
  237. return nil, err
  238. } else if !has {
  239. return nil, ErrRecordNotExist{}
  240. }
  241. return tl, nil
  242. }
  243. func GetPointOperateRecordBySourceTypeAndRequestId(sourceType, requestId, operateType string) (*RewardOperateRecord, error) {
  244. t := &RewardOperateRecord{
  245. SourceType: sourceType,
  246. RequestId: requestId,
  247. OperateType: operateType,
  248. }
  249. return getPointOperateRecord(t)
  250. }
  251. func GetPointOperateRecordBySerialNo(serialNo string) (*RewardOperateRecord, error) {
  252. t := &RewardOperateRecord{
  253. SerialNo: serialNo,
  254. }
  255. return getPointOperateRecord(t)
  256. }
  257. func InsertRewardOperateRecord(tl *RewardOperateRecord) (int64, error) {
  258. return x.Insert(tl)
  259. }
  260. func UpdateRewardRecordToFinalStatus(sourceType, requestId, newStatus string) (int64, error) {
  261. r := &RewardOperateRecord{
  262. Status: newStatus,
  263. LastOperateUnix: timeutil.TimeStampNow(),
  264. }
  265. return x.Cols("status", "last_operate_unix").Where("source_type=? and request_id=? and status=?", sourceType, requestId, OperateStatusOperating).Update(r)
  266. }
  267. func SumRewardAmountInTaskPeriod(rewardType string, sourceType string, userId int64, period *PeriodResult) (int64, error) {
  268. var cond = builder.NewCond()
  269. if period != nil {
  270. cond = cond.And(builder.Gte{"created_unix": period.StartTime.Unix()})
  271. cond = cond.And(builder.Lt{"created_unix": period.EndTime.Unix()})
  272. }
  273. if sourceType != "" {
  274. cond = cond.And(builder.Eq{"source_type": sourceType})
  275. }
  276. cond = cond.And(builder.Eq{"reward_type": rewardType})
  277. cond = cond.And(builder.Eq{"user_id": userId})
  278. return x.Where(cond).SumInt(&RewardOperateRecord{}, "amount")
  279. }
  280. type RewardOperateContext struct {
  281. SourceType SourceType
  282. SourceId string
  283. SourceTemplateId string
  284. Title string
  285. Remark string
  286. Reward Reward
  287. TargetUserId int64
  288. RequestId string
  289. OperateType RewardOperateType
  290. RejectPolicy LimiterRejectPolicy
  291. PermittedNegative bool
  292. LossAmount int64
  293. }
  294. type Reward struct {
  295. Amount int64
  296. Type RewardType
  297. }
  298. type UserRewardOperationRedis struct {
  299. UserId int64
  300. Amount int64
  301. RewardType RewardType
  302. OperateType RewardOperateType
  303. }
  304. type UserRewardOperation struct {
  305. UserId int64
  306. Msg string
  307. }
  308. func AppendRemark(remark, appendStr string) string {
  309. return strings.TrimPrefix(remark+Semicolon+appendStr, Semicolon)
  310. }
  311. type RewardRecordListOpts struct {
  312. ListOptions
  313. UserId int64
  314. UserName string
  315. OperateType RewardOperateType
  316. RewardType RewardType
  317. SourceType string
  318. TaskType string
  319. SerialNo string
  320. OrderBy RewardOperateOrderBy
  321. IsAdmin bool
  322. Status string
  323. }
  324. func (opts *RewardRecordListOpts) toCond() builder.Cond {
  325. if opts.Page <= 0 {
  326. opts.Page = 1
  327. }
  328. if len(opts.OrderBy) == 0 {
  329. opts.OrderBy = RewardOrderByIDDesc
  330. }
  331. cond := builder.NewCond()
  332. if opts.UserId > 0 {
  333. cond = cond.And(builder.Eq{"reward_operate_record.user_id": opts.UserId})
  334. }
  335. if opts.OperateType != OperateTypeNull {
  336. cond = cond.And(builder.Eq{"reward_operate_record.operate_type": opts.OperateType.Name()})
  337. }
  338. if opts.SourceType != "" {
  339. cond = cond.And(builder.Eq{"reward_operate_record.source_type": opts.SourceType})
  340. }
  341. if opts.TaskType != "" {
  342. cond = cond.And(builder.Eq{"reward_operate_record.source_template_id": opts.TaskType})
  343. }
  344. if opts.SerialNo != "" {
  345. cond = cond.And(builder.Like{"reward_operate_record.serial_no", opts.SerialNo})
  346. }
  347. if opts.Status != "" {
  348. cond = cond.And(builder.Like{"reward_operate_record.status", opts.Status})
  349. }
  350. cond = cond.And(builder.Eq{"reward_operate_record.reward_type": opts.RewardType.Name()})
  351. cond = cond.And(builder.Gt{"reward_operate_record.amount": 0})
  352. return cond
  353. }
  354. type TestTT struct {
  355. SerialNo string
  356. UserId int64
  357. Amount int64
  358. UserName string
  359. }
  360. func GetRewardRecordShowList(opts *RewardRecordListOpts) (RewardRecordShowList, int64, error) {
  361. cond := opts.toCond()
  362. count, err := x.Where(cond).Count(&RewardOperateRecord{})
  363. if err != nil {
  364. return nil, 0, err
  365. }
  366. r := make([]*RewardOperateRecordShow, 0)
  367. err = x.Table("reward_operate_record").Cols("reward_operate_record.source_id", "reward_operate_record.serial_no",
  368. "reward_operate_record.status", "reward_operate_record.operate_type", "reward_operate_record.amount",
  369. "reward_operate_record.loss_amount", "reward_operate_record.remark", "reward_operate_record.source_type",
  370. "reward_operate_record.last_operate_unix as last_operate_date").
  371. Where(cond).Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).OrderBy(string(opts.OrderBy)).Find(&r)
  372. if err != nil {
  373. return nil, 0, err
  374. }
  375. RewardRecordShowList(r).loadAttribute(false)
  376. return r, count, nil
  377. }
  378. func GetAdminRewardRecordShowList(opts *RewardRecordListOpts) (RewardRecordShowList, int64, error) {
  379. cond := opts.toCond()
  380. count, err := x.Where(cond).Count(&RewardOperateRecord{})
  381. if err != nil {
  382. return nil, 0, err
  383. }
  384. r := make([]*RewardOperateRecordShow, 0)
  385. switch opts.OperateType {
  386. case OperateTypeIncrease:
  387. err = x.Table("reward_operate_record").Cols("reward_operate_record.source_id", "reward_operate_record.serial_no",
  388. "reward_operate_record.status", "reward_operate_record.operate_type", "reward_operate_record.amount",
  389. "reward_operate_record.loss_amount", "reward_operate_record.remark", "reward_operate_record.source_type",
  390. "reward_operate_record.last_operate_unix as last_operate_date", "public.user.name as user_name",
  391. "point_account_log.balance_after").
  392. Join("LEFT", "public.user", "reward_operate_record.user_id = public.user.id").
  393. Join("LEFT", "point_account_log", " reward_operate_record.serial_no = point_account_log.source_id").
  394. Where(cond).Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).OrderBy(string(opts.OrderBy)).Find(&r)
  395. case OperateTypeDecrease:
  396. err = x.Table("reward_operate_record").Cols("reward_operate_record.source_id", "reward_operate_record.serial_no",
  397. "reward_operate_record.status", "reward_operate_record.operate_type", "reward_operate_record.amount",
  398. "reward_operate_record.loss_amount", "reward_operate_record.remark", "reward_operate_record.source_type",
  399. "reward_operate_record.last_operate_unix as last_operate_date", "public.user.name as user_name",
  400. "reward_periodic_task.amount as unit_price", "reward_periodic_task.success_count").
  401. Join("LEFT", "public.user", "reward_operate_record.user_id = public.user.id").
  402. Join("LEFT", "reward_periodic_task", "reward_operate_record.serial_no = reward_periodic_task.operate_serial_no").
  403. Where(cond).Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).OrderBy(string(opts.OrderBy)).Find(&r)
  404. }
  405. if err != nil {
  406. return nil, 0, err
  407. }
  408. RewardRecordShowList(r).loadAttribute(true)
  409. return r, count, nil
  410. }