task_config.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package gorm
  2. import (
  3. "context"
  4. taskmod "go-common/app/admin/main/aegis/model/task"
  5. "go-common/library/log"
  6. )
  7. // AddConfig add config
  8. func (d *Dao) AddConfig(c context.Context, config *taskmod.Config, confJSON interface{}) (err error) {
  9. if config.ConfType == taskmod.TaskConfigRangeWeight { //粉丝数,等待时长,分组权重配置去重
  10. name := "%" + confJSON.(*taskmod.RangeWeightConfig).Name + "%"
  11. err = d.orm.Table("task_config").Where("business_id=? AND flow_id=? AND conf_type=? AND conf_json LIKE ?", config.BusinessID, config.FlowID, taskmod.TaskConfigRangeWeight, name).
  12. Assign(map[string]interface{}{
  13. "conf_json": config.ConfJSON,
  14. "btime": config.Btime,
  15. "etime": config.Etime,
  16. "uid": config.UID,
  17. "uname": config.Uname,
  18. "description": config.Description,
  19. }).FirstOrCreate(config).Error
  20. return err
  21. }
  22. return d.orm.Create(config).Error
  23. }
  24. // UpdateConfig update config
  25. func (d *Dao) UpdateConfig(c context.Context, config *taskmod.Config) (err error) {
  26. return d.orm.Model(&taskmod.Config{}).Where("id=?", config.ID).Update(config).Error
  27. }
  28. // SetStateConfig update config
  29. func (d *Dao) SetStateConfig(c context.Context, id int64, state int8) (err error) {
  30. return d.orm.Model(&taskmod.Config{}).Where("id=?", id).Update("state", state).Error
  31. }
  32. // QueryConfigs list config
  33. func (d *Dao) QueryConfigs(c context.Context, queryParams *taskmod.QueryParams) (configs []*taskmod.Config, count int64, err error) {
  34. db := d.orm.Model(&taskmod.Config{}).Where("conf_type=?", queryParams.ConfType).Where("state=?", queryParams.State)
  35. if queryParams.BusinessID > 0 {
  36. db = db.Where("business_id=?", queryParams.BusinessID)
  37. }
  38. if queryParams.FlowID > 0 {
  39. db = db.Where("flow_id=?", queryParams.FlowID)
  40. }
  41. if len(queryParams.Btime) > 0 && len(queryParams.Etime) > 0 {
  42. db = db.Where("mtime>=? AND mtime<=?", queryParams.Btime, queryParams.Etime)
  43. }
  44. if len(queryParams.ConfName) > 0 {
  45. db = db.Where("conf_json LIKE '%" + queryParams.ConfName + "%'")
  46. }
  47. if err = db.Count(&count).Offset((queryParams.Pn - 1) * queryParams.Ps).Order("mtime DESC").Limit(queryParams.Ps).Find(&configs).Error; err != nil {
  48. log.Error("query error(%v)", err)
  49. return
  50. }
  51. return
  52. }
  53. // DeleteConfig delete config
  54. func (d *Dao) DeleteConfig(c context.Context, id int64) (err error) {
  55. return d.orm.Where("id=?", id).Delete(&taskmod.Config{}).Error
  56. }