mysql.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "encoding/json"
  6. "go-common/app/admin/openplatform/sug/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "time"
  10. )
  11. const (
  12. _getItem = "SELECT `items_id`,`name`,`brief`,`img` FROM `items` WHERE `items_id` = ? AND `is_lastest_version` = 1"
  13. _insertMatch = "INSERT INTO `sug_filter` (`season_id`,`items_id`,`type`,`sort`,`season_name`,`items_name`,`head_pic`,`sug_pic`) VALUE (?,?,?,?,?,?,?,?)"
  14. _updateMatch = "UPDATE `sug_filter` SET `type` = ?,`season_name`=?,`items_name`=?,`sort`=?,`sug_pic`=? WHERE `season_id` = ? AND `items_id` = ?"
  15. _matchExist = "SELECT `type` FROM `sug_filter` WHERE `season_id` = ? AND `items_id` = ?"
  16. )
  17. var _selectMatch = "SELECT `season_id`,`items_id`,`sort`,`season_name`,`items_name`,`head_pic`,`sug_pic` FROM `sug_filter` where type = 1"
  18. // GetItem get mall items from db.
  19. func (d *Dao) GetItem(c context.Context, itemsID int64) (item model.Item, err error) {
  20. row := d.dbMall.QueryRow(c, _getItem, itemsID)
  21. if err = row.Scan(&item.ItemsID, &item.Name, &item.Brief, &item.Img); err != nil {
  22. log.Error("Get item %d error,err(%v)", itemsID, err)
  23. return
  24. }
  25. if item.Brief == "" {
  26. item.Brief = item.Name
  27. item.Name = ""
  28. }
  29. var imgListArr []string
  30. if err = json.Unmarshal([]byte(item.Img), &imgListArr); err != nil {
  31. log.Error("get first img err[%s] (%v)", item.Img, err)
  32. return
  33. }
  34. item.Img = imgListArr[0]
  35. return
  36. }
  37. // UpdateMatch update match.
  38. func (d *Dao) UpdateMatch(c context.Context, season model.Season, item model.Item, typeInt int8, sugPic string) (affect int64, err error) {
  39. res, err := d.dbTicket.Exec(c, _updateMatch, typeInt, season.Title, item.Name, time.Now().Unix(), sugPic, season.ID, item.ItemsID)
  40. if err != nil {
  41. log.Error("row.exec error(%v)", err)
  42. return
  43. }
  44. affect, err = res.RowsAffected()
  45. return
  46. }
  47. // InsertMatch insert match.
  48. func (d *Dao) InsertMatch(c context.Context, season model.Season, item model.Item, typeInt int8, sort int64, location string) (affect int64, err error) {
  49. res, err := d.dbTicket.Exec(c, _insertMatch, season.ID, item.ItemsID, typeInt, sort, season.Title, item.Name, item.Img, location)
  50. if err != nil {
  51. log.Error("row.exec error(%v)", err)
  52. return
  53. }
  54. affect, err = res.RowsAffected()
  55. return
  56. }
  57. // GetMatchType get match type.
  58. func (d *Dao) GetMatchType(c context.Context, seasonID, itemsID int64) (matchType int8, err error) {
  59. row := d.dbTicket.QueryRow(c, _matchExist, seasonID, itemsID)
  60. if err = row.Scan(&matchType); err != nil {
  61. if err == sql.ErrNoRows {
  62. return -1, nil
  63. }
  64. return
  65. }
  66. return
  67. }
  68. // SearchV2 search match from db
  69. func (d *Dao) SearchV2(c context.Context, params *model.Search) (list []model.SugList, err error) {
  70. var rows *sql.Rows
  71. sqlStr := _selectMatch
  72. if params.ItemsID > 0 {
  73. sqlStr += fmt.Sprintf(" and items_id = %d", params.ItemsID)
  74. }
  75. if params.SeasonID > 0 {
  76. sqlStr += fmt.Sprintf(" and season_id = %d", params.SeasonID)
  77. }
  78. if rows, err = d.dbTicket.Query(c, sqlStr); err != nil {
  79. log.Error("d._selectMatchSQL.Query error(%v)", err)
  80. return
  81. }
  82. for rows.Next() {
  83. sug := new(model.SugList)
  84. if err = rows.Scan(&sug.SeasonId, &sug.ItemsID, &sug.Score, &sug.SeasonName, &sug.ItemsName, &sug.PicURL, &sug.SugURL); err != nil {
  85. log.Error("row.Scan() error(%v)", err)
  86. continue
  87. }
  88. list = append(list, *sug)
  89. }
  90. return
  91. }