relate.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package show
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/app/service/main/resource/model"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _relateSQL = "SELECT `id`,`param`,`title`,`rec_reason`,`position`,`plat_ver`,`stime`,`etime`,`pgc_ids` FROM app_rcmd_pos WHERE `state`=1 AND `goto`='special' AND `pgc_relation`=1 AND `stime`<? AND `etime`>?"
  11. )
  12. // Relate get all relate rec.
  13. func (d *Dao) Relate(c context.Context, now time.Time) (relates []*model.Relate, err error) {
  14. rows, err := d.db.Query(c, _relateSQL, now, now)
  15. if err != nil {
  16. log.Error("d.Relate.Query error(%v)", err)
  17. return
  18. }
  19. defer rows.Close()
  20. for rows.Next() {
  21. r := &model.Relate{}
  22. var verStr string
  23. if err = rows.Scan(&r.ID, &r.Param, &r.Title, &r.RecReason, &r.Position, &verStr, &r.STime, &r.ETime, &r.PgcIDs); err != nil {
  24. log.Error("d.Relate.rows.Scan error(%v)", err)
  25. return
  26. }
  27. if verStr != "" {
  28. var verStruct []*model.Version
  29. if err = json.Unmarshal([]byte(verStr), &verStruct); err != nil {
  30. log.Error("json.Unmarshal(%s) error(%v)", verStr, err)
  31. return
  32. }
  33. vm := make(map[int8][]*model.Version, len(verStruct))
  34. for _, v := range verStruct {
  35. vm[v.Plat] = append(vm[v.Plat], v)
  36. }
  37. r.Versions = vm
  38. }
  39. relates = append(relates, r)
  40. }
  41. err = rows.Err()
  42. return
  43. }