contest_data.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package model
  2. import (
  3. "fmt"
  4. "strings"
  5. "go-common/library/xstr"
  6. )
  7. const (
  8. _cDataInsertSQL = "INSERT INTO es_contests_data(cid,url,point_data) VALUES %s"
  9. _cDataEditSQL = "UPDATE es_contests_data SET url = CASE %s END,point_data = CASE %s END WHERE id IN (%s)"
  10. )
  11. // ContestData .
  12. type ContestData struct {
  13. ID int64 `json:"id"`
  14. CID int64 `json:"cid" gorm:"column:cid"`
  15. URL string `json:"url"`
  16. PointData int `json:"point_data"`
  17. IsDeleted int `json:"is_deleted"`
  18. }
  19. // TableName es_contests_data.
  20. func (t ContestData) TableName() string {
  21. return "es_contests_data"
  22. }
  23. // BatchAddCDataSQL .
  24. func BatchAddCDataSQL(cID int64, data []*ContestData) string {
  25. if len(data) == 0 {
  26. return ""
  27. }
  28. var rowStrings []string
  29. for _, v := range data {
  30. rowStrings = append(rowStrings, fmt.Sprintf("(%d,'%s',%d)", cID, v.URL, v.PointData))
  31. }
  32. return fmt.Sprintf(_cDataInsertSQL, strings.Join(rowStrings, ","))
  33. }
  34. // BatchEditCDataSQL .
  35. func BatchEditCDataSQL(cDatas []*ContestData) string {
  36. if len(cDatas) == 0 {
  37. return ""
  38. }
  39. var (
  40. urlStr, pDataStr string
  41. ids []int64
  42. )
  43. for _, module := range cDatas {
  44. urlStr = fmt.Sprintf("%s WHEN id = %d THEN '%s'", urlStr, module.ID, module.URL)
  45. pDataStr = fmt.Sprintf("%s WHEN id = %d THEN '%d'", pDataStr, module.ID, module.PointData)
  46. ids = append(ids, module.ID)
  47. }
  48. return fmt.Sprintf(_cDataEditSQL, urlStr, pDataStr, xstr.JoinInts(ids))
  49. }