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.

task_config.go 6.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/timeutil"
  4. "fmt"
  5. "xorm.io/builder"
  6. )
  7. const (
  8. PeriodNotCycle = "NOT_CYCLE"
  9. PeriodDaily = "DAILY"
  10. )
  11. //PointTaskConfig Only add and delete are allowed, edit is not allowed
  12. //so if you want to edit config for some task code,please delete first and add new one
  13. type TaskConfig struct {
  14. ID int64 `xorm:"pk autoincr"`
  15. TaskCode string `xorm:"NOT NULL"`
  16. Title string
  17. AwardType string `xorm:"NOT NULL"`
  18. AwardAmount int64 `xorm:"NOT NULL"`
  19. CreatorId int64 `xorm:"NOT NULL"`
  20. CreatorName string
  21. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  22. DeletedAt timeutil.TimeStamp `xorm:"deleted"`
  23. DeleterId int64
  24. DeleterName string
  25. }
  26. type TaskConfigWithLimit struct {
  27. ID int64
  28. TaskCode string
  29. Title string
  30. AwardType string
  31. AwardAmount int64
  32. Creator string
  33. IsDeleted bool
  34. CreatedUnix timeutil.TimeStamp
  35. DeleteAt timeutil.TimeStamp
  36. Limiters []*LimitConfigVO
  37. }
  38. type TaskConfigWithLimitResponse struct {
  39. Records []*TaskConfigWithSingleLimit
  40. Total int64
  41. PageSize int
  42. Page int
  43. }
  44. type TaskConfigWithSingleLimit struct {
  45. ID int64
  46. TaskCode string
  47. AwardType string
  48. AwardAmount int64
  49. Creator string
  50. IsDeleted bool
  51. CreatedUnix timeutil.TimeStamp
  52. DeleteAt timeutil.TimeStamp
  53. RefreshRate string
  54. LimitNum int64
  55. }
  56. type TaskAndLimiterConfig struct {
  57. TaskConfig TaskConfig `xorm:"extends"`
  58. LimitConfig LimitConfig `xorm:"extends"`
  59. }
  60. type PointRule struct {
  61. UserDailyLimit int64
  62. TaskRules []TaskRule
  63. }
  64. type TaskRule struct {
  65. TaskCode string
  66. AwardType string
  67. AwardAmount int64
  68. RefreshRate string
  69. LimitNum int64
  70. }
  71. func (TaskAndLimiterConfig) TableName() string {
  72. return "task_config"
  73. }
  74. type BatchLimitConfigVO struct {
  75. ConfigList []TaskConfigWithLimit
  76. }
  77. func getTaskConfig(t *TaskConfig) (*TaskConfig, error) {
  78. has, err := x.Get(t)
  79. if err != nil {
  80. return nil, err
  81. } else if !has {
  82. return nil, ErrRecordNotExist{}
  83. }
  84. return t, nil
  85. }
  86. func GetTaskConfigByTaskCode(taskCode string) (*TaskConfig, error) {
  87. t := &TaskConfig{
  88. TaskCode: taskCode,
  89. }
  90. return getTaskConfig(t)
  91. }
  92. func GetTaskConfigByID(id int64) (*TaskConfig, error) {
  93. t := &TaskConfig{
  94. ID: id,
  95. }
  96. return getTaskConfig(t)
  97. }
  98. func GetTaskConfigList() ([]*TaskConfig, error) {
  99. r := make([]*TaskConfig, 0)
  100. err := x.Find(&r)
  101. if err != nil {
  102. return nil, err
  103. }
  104. if len(r) == 0 {
  105. return nil, ErrRecordNotExist{}
  106. }
  107. return r, nil
  108. }
  109. type GetTaskConfigOpts struct {
  110. ListOptions
  111. Status int //1 normal 2 deleted
  112. ActionType int
  113. }
  114. func GetTaskConfigPageWithDeleted(opt GetTaskConfigOpts) ([]*TaskAndLimiterConfig, int64, error) {
  115. if opt.Page <= 0 {
  116. opt.Page = 1
  117. }
  118. cond := builder.NewCond()
  119. if opt.ActionType > 0 {
  120. cond = cond.And(builder.Eq{"task_code": fmt.Sprint(opt.ActionType)})
  121. }
  122. var count int64
  123. var err error
  124. if opt.Status == 1 {
  125. subCond := builder.NewCond()
  126. subCond = subCond.Or(builder.IsNull{"task_config.deleted_at"})
  127. subCond = subCond.Or(builder.Eq{"task_config.deleted_at": 0})
  128. cond = cond.And(subCond)
  129. } else if opt.Status == 2 {
  130. cond = cond.And(builder.Gt{"task_config.deleted_at": 0})
  131. }
  132. count, err = x.Unscoped().Where(cond).Count(&TaskConfig{})
  133. if err != nil {
  134. return nil, 0, err
  135. }
  136. r := make([]*TaskAndLimiterConfig, 0)
  137. err = x.Join("LEFT", "limit_config", "task_config.id = limit_config.related_id").
  138. Unscoped().Where(cond).Limit(opt.PageSize, (opt.Page-1)*opt.PageSize).
  139. OrderBy("task_config.deleted_at desc,task_config.id desc").Find(&r)
  140. if len(r) == 0 {
  141. return nil, 0, ErrRecordNotExist{}
  142. }
  143. return r, count, nil
  144. }
  145. func EditTaskConfig(config TaskConfigWithLimit, doer *User) error {
  146. sess := x.NewSession()
  147. defer sess.Close()
  148. //delete old task config
  149. p := &TaskConfig{
  150. ID: config.ID,
  151. }
  152. _, err := sess.Delete(p)
  153. if err != nil {
  154. sess.Rollback()
  155. return err
  156. }
  157. //update deleter
  158. p.DeleterId = doer.ID
  159. p.DeleterName = doer.Name
  160. sess.Where("id = ?", config.ID).Unscoped().Update(p)
  161. //add new config
  162. t := &TaskConfig{
  163. TaskCode: config.TaskCode,
  164. Title: config.Title,
  165. AwardType: config.AwardType,
  166. AwardAmount: config.AwardAmount,
  167. CreatorId: doer.ID,
  168. CreatorName: doer.Name,
  169. }
  170. _, err = sess.InsertOne(t)
  171. if err != nil {
  172. sess.Rollback()
  173. return err
  174. }
  175. //delete old limiter config
  176. lp := &LimitConfig{
  177. RelatedId: config.ID,
  178. }
  179. _, err = sess.Delete(lp)
  180. if err != nil {
  181. sess.Rollback()
  182. return err
  183. }
  184. lp.DeleterName = doer.Name
  185. lp.DeleterId = doer.ID
  186. //update deleter
  187. sess.Where("related_id = ?", config.ID).Unscoped().Update(lp)
  188. //add new limiter config
  189. if config.Limiters != nil && len(config.Limiters) > 0 {
  190. for _, v := range config.Limiters {
  191. //add new config
  192. l := &LimitConfig{
  193. Title: v.Title,
  194. RefreshRate: v.RefreshRate,
  195. Scope: v.Scope,
  196. LimitNum: v.LimitNum,
  197. LimitCode: config.TaskCode,
  198. LimitType: LimitTypeTask.Name(),
  199. CreatorId: doer.ID,
  200. CreatorName: doer.Name,
  201. RelatedId: t.ID,
  202. }
  203. _, err = sess.Insert(l)
  204. if err != nil {
  205. sess.Rollback()
  206. return err
  207. }
  208. }
  209. }
  210. sess.Commit()
  211. return nil
  212. }
  213. func NewTaskConfig(config TaskConfigWithLimit, doer *User) error {
  214. sess := x.NewSession()
  215. defer sess.Close()
  216. //add new config
  217. t := &TaskConfig{
  218. TaskCode: config.TaskCode,
  219. Title: config.Title,
  220. AwardType: config.AwardType,
  221. AwardAmount: config.AwardAmount,
  222. CreatorId: doer.ID,
  223. CreatorName: doer.Name,
  224. }
  225. _, err := sess.InsertOne(t)
  226. if err != nil {
  227. sess.Rollback()
  228. return err
  229. }
  230. //add new limiter config
  231. if config.Limiters != nil && len(config.Limiters) > 0 {
  232. for _, v := range config.Limiters {
  233. //add new config
  234. l := &LimitConfig{
  235. RelatedId: t.ID,
  236. Title: v.Title,
  237. RefreshRate: v.RefreshRate,
  238. Scope: v.Scope,
  239. LimitNum: v.LimitNum,
  240. LimitCode: config.TaskCode,
  241. LimitType: LimitTypeTask.Name(),
  242. CreatorId: doer.ID,
  243. CreatorName: doer.Name,
  244. }
  245. _, err = sess.Insert(l)
  246. if err != nil {
  247. sess.Rollback()
  248. return err
  249. }
  250. }
  251. }
  252. sess.Commit()
  253. return nil
  254. }
  255. func DelTaskConfig(id int64, doer *User) error {
  256. sess := x.NewSession()
  257. defer sess.Close()
  258. //delete old task config
  259. p := &TaskConfig{
  260. ID: id,
  261. }
  262. _, err := sess.Delete(p)
  263. if err != nil {
  264. sess.Rollback()
  265. return err
  266. }
  267. //update deleter
  268. p.DeleterId = doer.ID
  269. p.DeleterName = doer.Name
  270. sess.Where("id = ?", id).Unscoped().Update(p)
  271. //delete old limiter config
  272. lp := &LimitConfig{
  273. RelatedId: id,
  274. }
  275. _, err = sess.Delete(lp)
  276. if err != nil {
  277. sess.Rollback()
  278. return err
  279. }
  280. lp.DeleterName = doer.Name
  281. lp.DeleterId = doer.ID
  282. //update deleter
  283. sess.Where("related_id = ?", id).Unscoped().Update(lp)
  284. sess.Commit()
  285. return nil
  286. }