mcn_recommend.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package up
  2. import (
  3. "bytes"
  4. "context"
  5. "database/sql"
  6. "fmt"
  7. "sync"
  8. "go-common/app/admin/main/mcn/model"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. // private condition
  14. _inMcnUpRecommendSQL = `INSERT mcn_up_recommend_pool(up_mid,fans_count,archive_count,play_count_accumulate,play_count_average,active_tid,source) VALUES (?,?,?,?,?,?,2)
  15. ON DUPLICATE KEY UPDATE fans_count=?, archive_count=?, play_count_accumulate=?, play_count_average=?, active_tid=?, state=1, source=2, fans_count_increase_month=0,
  16. last_archive_time='1970-01-01 08:00:00', generate_time='1970-01-01 08:00:00'`
  17. _upMcnUpRecommendOPSQL = "UPDATE mcn_up_recommend_pool SET state = ? WHERE up_mid IN (%s)"
  18. _mcnUpRecommendsSQL = `SELECT id,up_mid,fans_count,fans_count_increase_month,archive_count,
  19. play_count_accumulate,play_count_average,active_tid,last_archive_time,state,source,generate_time,ctime,mtime FROM mcn_up_recommend_pool %s`
  20. _mcnUpRecommendTotalSQL = "SELECT COUNT(1) FROM mcn_up_recommend_pool %s"
  21. _mcnUpBindMidsSQL = "SELECT up_mid FROM mcn_up WHERE up_mid IN (%s) AND state IN (2,10,11,15)"
  22. _mcnUpRecommendMidSQL = `SELECT id,up_mid,fans_count,fans_count_increase_month,archive_count,
  23. play_count_accumulate,play_count_average,active_tid,last_archive_time,state,source,generate_time,ctime,mtime FROM mcn_up_recommend_pool WHERE up_mid = ?`
  24. _mcnUpRecommendMidsSQL = `SELECT id,up_mid,fans_count,fans_count_increase_month,archive_count,
  25. play_count_accumulate,play_count_average,active_tid,last_archive_time,state,source,generate_time,ctime,mtime FROM mcn_up_recommend_pool WHERE up_mid IN (%s)`
  26. // common condition
  27. _orderByConditionSQL = " %s ORDER BY %s %s LIMIT ?,?"
  28. _orderByConditionNotLimitSQL = " %s ORDER BY %s %s"
  29. _orderByNoConditionSQL = " ORDER BY %s %s LIMIT ?,?"
  30. _orderByNoConditionNotLimitSQL = " ORDER BY %s %s"
  31. )
  32. // AddMcnUpRecommend .
  33. func (d *Dao) AddMcnUpRecommend(c context.Context, arg *model.McnUpRecommendPool) (rows int64, err error) {
  34. var res sql.Result
  35. if res, err = d.db.Exec(c, _inMcnUpRecommendSQL, arg.UpMid, arg.FansCount, arg.ArchiveCount, arg.PlayCountAccumulate, arg.PlayCountAverage, arg.ActiveTid,
  36. arg.FansCount, arg.ArchiveCount, arg.PlayCountAccumulate, arg.PlayCountAverage, arg.ActiveTid); err != nil {
  37. return rows, err
  38. }
  39. return res.RowsAffected()
  40. }
  41. // UpMcnUpsRecommendOP .
  42. func (d *Dao) UpMcnUpsRecommendOP(c context.Context, upMids []int64, state model.MCNUPRecommendState) (rows int64, err error) {
  43. var res sql.Result
  44. if res, err = d.db.Exec(c, fmt.Sprintf(_upMcnUpRecommendOPSQL, xstr.JoinInts(upMids)), state); err != nil {
  45. return rows, err
  46. }
  47. return res.RowsAffected()
  48. }
  49. // McnUpRecommends .
  50. func (d *Dao) McnUpRecommends(c context.Context, arg *model.MCNUPRecommendReq) (res []*model.McnUpRecommendPool, err error) {
  51. sql, values := d.buildUpRecommendSQL("list", arg)
  52. rows, err := d.db.Query(c, sql, values...)
  53. if err != nil {
  54. return
  55. }
  56. defer rows.Close()
  57. for rows.Next() {
  58. m := &model.McnUpRecommendPool{}
  59. err = rows.Scan(&m.ID, &m.UpMid, &m.FansCount, &m.FansCountIncreaseMonth, &m.ArchiveCount, &m.PlayCountAccumulate, &m.PlayCountAverage,
  60. &m.ActiveTid, &m.LastArchiveTime, &m.State, &m.Source, &m.GenerateTime, &m.Ctime, &m.Mtime)
  61. if err != nil {
  62. if err == xsql.ErrNoRows {
  63. err = nil
  64. }
  65. res = nil
  66. return
  67. }
  68. res = append(res, m)
  69. }
  70. return
  71. }
  72. // McnUpRecommendTotal .
  73. func (d *Dao) McnUpRecommendTotal(c context.Context, arg *model.MCNUPRecommendReq) (count int, err error) {
  74. sql, values := d.buildUpRecommendSQL("count", arg)
  75. row := d.db.QueryRow(c, sql, values...)
  76. if err = row.Scan(&count); err != nil {
  77. if err == xsql.ErrNoRows {
  78. err = nil
  79. return
  80. }
  81. }
  82. return
  83. }
  84. // McnUpBindMids .
  85. func (d *Dao) McnUpBindMids(c context.Context, mids []int64) (bmids []int64, err error) {
  86. rows, err := d.db.Query(c, fmt.Sprintf(_mcnUpBindMidsSQL, xstr.JoinInts(mids)))
  87. if err != nil {
  88. return
  89. }
  90. defer rows.Close()
  91. for rows.Next() {
  92. var upMids int64
  93. err = rows.Scan(&upMids)
  94. if err != nil {
  95. if err == xsql.ErrNoRows {
  96. err = nil
  97. }
  98. return
  99. }
  100. bmids = append(bmids, upMids)
  101. }
  102. return
  103. }
  104. // McnUpRecommendMid .
  105. func (d *Dao) McnUpRecommendMid(c context.Context, mid int64) (m *model.McnUpRecommendPool, err error) {
  106. row := d.db.QueryRow(c, _mcnUpRecommendMidSQL, mid)
  107. m = &model.McnUpRecommendPool{}
  108. err = row.Scan(&m.ID, &m.UpMid, &m.FansCount, &m.FansCountIncreaseMonth, &m.ArchiveCount, &m.PlayCountAccumulate, &m.PlayCountAverage,
  109. &m.ActiveTid, &m.LastArchiveTime, &m.State, &m.Source, &m.GenerateTime, &m.Ctime, &m.Mtime)
  110. if err != nil {
  111. if err == xsql.ErrNoRows {
  112. err = nil
  113. }
  114. m = nil
  115. return
  116. }
  117. return
  118. }
  119. // McnUpRecommendMids .
  120. func (d *Dao) McnUpRecommendMids(c context.Context, mids []int64) (mrp map[int64]*model.McnUpRecommendPool, err error) {
  121. rows, err := d.db.Query(c, fmt.Sprintf(_mcnUpRecommendMidsSQL, xstr.JoinInts(mids)))
  122. if err != nil {
  123. return
  124. }
  125. defer rows.Close()
  126. mrp = make(map[int64]*model.McnUpRecommendPool, len(mids))
  127. for rows.Next() {
  128. m := &model.McnUpRecommendPool{}
  129. err = rows.Scan(&m.ID, &m.UpMid, &m.FansCount, &m.FansCountIncreaseMonth, &m.ArchiveCount, &m.PlayCountAccumulate, &m.PlayCountAverage,
  130. &m.ActiveTid, &m.LastArchiveTime, &m.State, &m.Source, &m.GenerateTime, &m.Ctime, &m.Mtime)
  131. if err != nil {
  132. if err == xsql.ErrNoRows {
  133. err = nil
  134. }
  135. mrp = nil
  136. return
  137. }
  138. mrp[m.UpMid] = m
  139. }
  140. return
  141. }
  142. // buildUpRecommendSQL build a up recommend sql string.
  143. func (d *Dao) buildUpRecommendSQL(tp string, arg *model.MCNUPRecommendReq) (sql string, values []interface{}) {
  144. values = make([]interface{}, 0, 11)
  145. var (
  146. cond []string
  147. condStr string
  148. )
  149. if arg.TID != 0 {
  150. cond = append(cond, "active_tid = ?")
  151. values = append(values, arg.TID)
  152. }
  153. if arg.UpMid != 0 {
  154. cond = append(cond, "up_mid = ?")
  155. values = append(values, arg.UpMid)
  156. }
  157. if arg.FansMin != 0 {
  158. cond = append(cond, "fans_count >= ?")
  159. values = append(values, arg.FansMin)
  160. }
  161. if arg.FansMax != 0 {
  162. cond = append(cond, "fans_count <= ?")
  163. values = append(values, arg.FansMax)
  164. }
  165. if arg.PlayMin != 0 {
  166. cond = append(cond, "play_count_accumulate >= ?")
  167. values = append(values, arg.PlayMin)
  168. }
  169. if arg.PlayMax != 0 {
  170. cond = append(cond, "play_count_accumulate <= ?")
  171. values = append(values, arg.PlayMax)
  172. }
  173. if arg.PlayAverageMin != 0 {
  174. cond = append(cond, "play_count_average >= ?")
  175. values = append(values, arg.PlayAverageMin)
  176. }
  177. if arg.PlayAverageMax != 0 {
  178. cond = append(cond, "play_count_average <= ?")
  179. values = append(values, arg.PlayAverageMax)
  180. }
  181. if arg.State != model.MCNUPRecommendStateUnKnown {
  182. cond = append(cond, "state = ?")
  183. values = append(values, arg.State)
  184. } else {
  185. cond = append(cond, "state IN (1,2,3)")
  186. }
  187. if arg.Source != model.MCNUPRecommendSourceUnKnown {
  188. cond = append(cond, "source = ?")
  189. values = append(values, arg.Source)
  190. }
  191. condStr = d.joinStringSQL(cond)
  192. switch tp {
  193. case "count":
  194. if condStr != "" {
  195. sql = fmt.Sprintf(_mcnUpRecommendTotalSQL+" %s", "WHERE", condStr)
  196. return
  197. }
  198. sql = fmt.Sprintf(_mcnUpRecommendTotalSQL, condStr)
  199. case "list":
  200. // 导出
  201. if arg.Export == model.ResponeModelCSV {
  202. if condStr != "" {
  203. sql = fmt.Sprintf(_mcnUpRecommendsSQL+_orderByConditionNotLimitSQL, "WHERE", condStr, arg.Order, arg.Sort)
  204. return
  205. }
  206. sql = fmt.Sprintf(_mcnUpRecommendsSQL+_orderByNoConditionNotLimitSQL, condStr, arg.Order, arg.Sort)
  207. return
  208. }
  209. // 非导出
  210. if condStr != "" {
  211. sql = fmt.Sprintf(_mcnUpRecommendsSQL+_orderByConditionSQL, "WHERE", condStr, arg.Order, arg.Sort)
  212. } else {
  213. sql = fmt.Sprintf(_mcnUpRecommendsSQL+_orderByNoConditionSQL, condStr, arg.Order, arg.Sort)
  214. }
  215. limit, offset := arg.PageArg.CheckPageValidation()
  216. values = append(values, offset, limit)
  217. }
  218. return
  219. }
  220. func (d *Dao) joinStringSQL(is []string) string {
  221. if len(is) == 0 {
  222. return ""
  223. }
  224. if len(is) == 1 {
  225. return is[0]
  226. }
  227. var bfPool = sync.Pool{
  228. New: func() interface{} {
  229. return bytes.NewBuffer([]byte{})
  230. },
  231. }
  232. buf := bfPool.Get().(*bytes.Buffer)
  233. for _, i := range is {
  234. buf.WriteString(i)
  235. buf.WriteString(" AND ")
  236. }
  237. if buf.Len() > 0 {
  238. buf.Truncate(buf.Len() - 4)
  239. }
  240. s := buf.String()
  241. buf.Reset()
  242. bfPool.Put(buf)
  243. return s
  244. }