config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package archive
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "strconv"
  7. "strings"
  8. "go-common/app/admin/main/videoup/model/archive"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. _confSQL = "SELECT value FROM archive_config WHERE state=0 AND name=?"
  14. _upconfSQL = "UPDATE archive_config SET value=?,remark=? WHERE name=?"
  15. _inconfSQL = "INSERT archive_config(value,remark,name,state) VALUE (?,?,?,0)"
  16. )
  17. // FansConf is fan round check types config.
  18. func (d *Dao) FansConf(c context.Context) (fans int64, err error) {
  19. row := d.rddb.QueryRow(c, _confSQL, archive.ConfForClick)
  20. var val string
  21. if err = row.Scan(&val); err != nil {
  22. if err == sql.ErrNoRows {
  23. err = nil
  24. } else {
  25. log.Error("row.Scan error(%v)", err)
  26. }
  27. return
  28. }
  29. if fans, err = strconv.ParseInt(val, 10, 64); err != nil {
  30. log.Error("strconv.ParseInt(%s) error(%v)", val, err)
  31. }
  32. return
  33. }
  34. // RoundTypeConf is typeid round check types config.
  35. func (d *Dao) RoundTypeConf(c context.Context) (roundTypes map[int16]struct{}, err error) {
  36. roundTypes = map[int16]struct{}{}
  37. row := d.rddb.QueryRow(c, _confSQL, archive.ConfForRoundType)
  38. var (
  39. val string
  40. tids []string
  41. tid int64
  42. )
  43. if err = row.Scan(&val); err != nil {
  44. if err == sql.ErrNoRows {
  45. err = nil
  46. } else {
  47. log.Error("row.Scan error(%v)", err)
  48. }
  49. return
  50. }
  51. tids = strings.Split(val, ",")
  52. for _, tidStr := range tids {
  53. if tid, err = strconv.ParseInt(tidStr, 10, 64); err != nil {
  54. log.Error("strconv.ParseInt(%s) error(%v)", tid, err)
  55. return
  56. }
  57. roundTypes[int16(tid)] = struct{}{}
  58. }
  59. return
  60. }
  61. // ThresholdConf is second types opposite first types.
  62. func (d *Dao) ThresholdConf(c context.Context) (tpThr map[int16]int, err error) {
  63. row := d.rddb.QueryRow(c, _confSQL, archive.ConfForThreshold)
  64. var value string
  65. if err = row.Scan(&value); err != nil {
  66. if err == sql.ErrNoRows {
  67. err = nil
  68. } else {
  69. log.Error("row.Scan() error(%v)", err)
  70. }
  71. return
  72. }
  73. if err = json.Unmarshal([]byte(value), &tpThr); err != nil {
  74. log.Error("json.Unmarshal(%s) error(%v)", value, err)
  75. return
  76. }
  77. return
  78. }
  79. // AuditTypesConf is audit types.
  80. func (d *Dao) AuditTypesConf(c context.Context) (atps map[int16]struct{}, err error) {
  81. row := d.rddb.QueryRow(c, _confSQL, archive.ConfForWaitAudit)
  82. var (
  83. value string
  84. typeIDs []int64
  85. )
  86. if err = row.Scan(&value); err != nil {
  87. if err == sql.ErrNoRows {
  88. err = nil
  89. } else {
  90. log.Error("row.Scan error(%v)", err)
  91. }
  92. return
  93. }
  94. typeIDs, err = xstr.SplitInts(value)
  95. if err != nil {
  96. log.Error("archive_config value(%s) xstr.SplitInts error(%v)", value, err)
  97. return
  98. }
  99. atps = map[int16]struct{}{}
  100. for _, typeid := range typeIDs {
  101. atps[int16(typeid)] = struct{}{}
  102. }
  103. return
  104. }
  105. // WeightVC 获取权重分值
  106. func (d *Dao) WeightVC(c context.Context) (wvc *archive.WeightVC, err error) {
  107. var value []byte
  108. row := d.rddb.QueryRow(c, _confSQL, archive.ConfForWeightVC)
  109. if err = row.Scan(&value); err != nil {
  110. if err == sql.ErrNoRows {
  111. err = nil
  112. } else {
  113. log.Error("row.Scan error(%v)", err)
  114. }
  115. return
  116. }
  117. wvc = new(archive.WeightVC)
  118. if err = json.Unmarshal(value, wvc); err != nil {
  119. log.Error("json.Unmarshal error(%v)", err)
  120. wvc = nil
  121. }
  122. return
  123. }
  124. // SetWeightVC 设置权重分值
  125. func (d *Dao) SetWeightVC(c context.Context, wvc *archive.WeightVC, desc string) (rows int64, err error) {
  126. var (
  127. valueb []byte
  128. res sql.Result
  129. )
  130. if valueb, err = json.Marshal(wvc); err != nil {
  131. log.Error("json.Marshal(%+v) error(%v)", wvc, err)
  132. return
  133. }
  134. if res, err = d.db.Exec(c, _upconfSQL, string(valueb), desc, archive.ConfForWeightVC); err != nil {
  135. log.Error("d.db.Exec(%s, %s, %s, %s) error(%v)", _upconfSQL, string(valueb), desc, archive.ConfForWeightVC, err)
  136. return
  137. }
  138. return res.RowsAffected()
  139. }
  140. // InWeightVC 插入
  141. func (d *Dao) InWeightVC(c context.Context, wvc *archive.WeightVC, desc string) (rows int64, err error) {
  142. var (
  143. valueb []byte
  144. res sql.Result
  145. )
  146. if valueb, err = json.Marshal(wvc); err != nil {
  147. log.Error("json.Marshal(%+v) error(%v)", wvc, err)
  148. return
  149. }
  150. if res, err = d.db.Exec(c, _inconfSQL, string(valueb), desc, archive.ConfForWeightVC); err != nil {
  151. log.Error("d.db.Exec(%s, %s, %s, %s) error(%v)", _inconfSQL, string(valueb), desc, archive.ConfForWeightVC, err)
  152. return
  153. }
  154. return res.LastInsertId()
  155. }