mango.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/app/admin/main/tv/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. xtime "go-common/library/time"
  11. )
  12. const (
  13. _mangoKey = "mango_cms_recom"
  14. _delRecom = "UPDATE mango_recom SET deleted = 1 WHERE id = ?"
  15. _maxOrder = "SELECT MAX(rorder) AS ord FROM mango_recom WHERE deleted = 0"
  16. )
  17. //MangoRecom is used to set mango recom cache in MC
  18. func (d *Dao) MangoRecom(c context.Context, ids []int64) (err error) {
  19. conn := d.mc.Get(c)
  20. defer conn.Close()
  21. mcItem := &model.MRecomMC{
  22. RIDs: ids,
  23. Pubtime: xtime.Time(time.Now().Unix()),
  24. }
  25. itemJSON := &memcache.Item{
  26. Key: _mangoKey,
  27. Object: mcItem,
  28. Flags: memcache.FlagJSON,
  29. Expiration: 0,
  30. }
  31. if err = conn.Set(itemJSON); err != nil {
  32. log.Error("MangoRecom Ids %v, Err %v", ids, err)
  33. }
  34. return
  35. }
  36. // GetMRecom get mango recom mc data
  37. func (d *Dao) GetMRecom(c context.Context) (res *model.MRecomMC, err error) {
  38. var item *memcache.Item
  39. conn := d.mc.Get(c)
  40. defer conn.Close()
  41. if item, err = conn.Get(_mangoKey); err != nil {
  42. log.Error("GetMRecom MangoKey, Err %v", _mangoKey, err)
  43. return
  44. }
  45. err = json.Unmarshal(item.Value, &res)
  46. return
  47. }
  48. // DelMRecom deletes an mango recom position
  49. func (d *Dao) DelMRecom(c *bm.Context, id int64) (err error) {
  50. if err = d.DB.Exec(_delRecom, id).Error; err != nil {
  51. log.Error("DelMRecom Error %v", err)
  52. }
  53. return
  54. }
  55. // MaxOrder picks the max rorder from the table
  56. func (d *Dao) MaxOrder(ctx context.Context) int {
  57. var maxR = new(struct {
  58. Ord int
  59. })
  60. if err := d.DB.Raw(_maxOrder).Scan(&maxR).Error; err != nil {
  61. log.Error("MaxOrder Error %v", err)
  62. return 0
  63. }
  64. return maxR.Ord
  65. }