item.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/job/openplatform/open-sug/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _fetchItemList = "SELECT `items_id`,`name`,`ip_right_id`,`brief`,`img` FROM `items` WHERE `status` = 1 AND `sub_status` <> 13 AND `ip_right_id` <> 0 AND `is_lastest_version` = 1 limit %d,%d"
  12. _fetchItem = "SELECT `items_id`,`name`,`ip_right_id`,`brief`,`img` FROM `items` WHERE `status` = 1 AND `sub_status` <> 13 AND `is_lastest_version` = 1 and `items_id` = ?"
  13. _fetchIPRight = "SELECT `name`,`chs_name`,`alias`,`parent_id` FROM `ip_right` WHERE `ip_right_id` = ?"
  14. _fetchParentIPRight = "SELECT `name`,`chs_name`,`alias` FROM `ip_right` WHERE `ip_right_id` = ?"
  15. _getWishCount = "SELECT COUNT(*) FROM ugc_subject_vote_%d WHERE `subject_id` = ? AND `subject_type` = 1 AND DATE(`ctime`) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)"
  16. _getCommentCount = "SELECT COUNT(*) FROM ugc_info_%d WHERE `subject_id` = ? AND `subject_type` = 1 AND DATE(`ctime`) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)"
  17. _getSaleCount = "select sum(sku_num) from (select order_id,payment_id,payment_time from order_basic where status in (3, 4, 5, 7) and parent_order_id = 0) as t1 join (select order_id,items_id,sku_id, sku_num from order_sku) as t2 on t1.order_id = t2.order_id where items_id = ? AND DATE(`ctime`) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) group by items_id "
  18. _fetchMatch = "SELECT `season_id`,`items_id`,`sort` FROM `sug_filter` where type = 1 and sort > 1000000000 limit %d,%d"
  19. _insertMatch = "INSERT INTO sug_filter (`items_id`,`season_id`,`season_name`,`sort`,`type`,`items_name`,`head_pic`,`sug_pic`) VALUES(?, ?, ?,?,'1',?,?,?)ON DUPLICATE KEY UPDATE items_name = IF(type=0, items_name,?) ,season_name = IF(type=0, season_name,?) ,sort = IF(sort>1000000000, sort,?),mtime = IF(type=0, mtime,NOW()),head_pic = IF(type=0, head_pic,?),sug_pic = IF(type=0, sug_pic,?)"
  20. _updatePic = "update sug_filter set sug_pic = ?,items_name = ?,head_pic =? where items_id = ? and season_id = ?"
  21. )
  22. //FetchItem query project list from mysql
  23. func (d *Dao) FetchItem(c context.Context) (itemList []*model.Item, err error) {
  24. var (
  25. rows *sql.Rows
  26. start = 0
  27. offset = 10
  28. )
  29. for {
  30. sqlStr := fmt.Sprintf(_fetchItemList, start, offset)
  31. start += offset
  32. if rows, err = d.mallDB.Query(c, sqlStr); err != nil {
  33. log.Error("d._fetchProjectListSQL.Query error(%v)", err)
  34. return
  35. }
  36. i := 0
  37. for rows.Next() {
  38. i++
  39. item := new(model.Item)
  40. var imgList string
  41. if err = rows.Scan(&item.ID, &item.Name, &item.IPRightID, &item.Brief, &imgList); err != nil {
  42. log.Error("row.Scan() error(%v)", err)
  43. continue
  44. }
  45. if item.Brief == "" {
  46. item.Brief = item.Name
  47. item.Name = ""
  48. }
  49. var imgListArr []string
  50. if err = json.Unmarshal([]byte(imgList), &imgListArr); err != nil {
  51. log.Error("get first img err[%s] (%v)", imgList, err)
  52. err = nil
  53. continue
  54. }
  55. item.HeadImg = imgListArr[0]
  56. row := d.mallDB.QueryRow(c, _fetchIPRight, item.IPRightID)
  57. var name, chs_name, alias, parentID, parentName, parentChsName, parentAlias string
  58. if err = row.Scan(&name, &chs_name, &alias, &parentID); err != nil {
  59. err = nil
  60. continue
  61. }
  62. if parentID != "0" {
  63. row := d.mallDB.QueryRow(c, _fetchParentIPRight, parentID)
  64. if err = row.Scan(&parentName, &parentChsName, &parentAlias); err != nil {
  65. log.Error("get parent ip_right_info err (%v)", err)
  66. err = nil
  67. continue
  68. }
  69. }
  70. item.Keywords = name + " " + chs_name + " " + alias + " " + parentName + " " + parentChsName + " " + parentAlias
  71. itemList = append(itemList, item)
  72. }
  73. if i < 10 {
  74. break
  75. }
  76. }
  77. defer rows.Close()
  78. err = rows.Err()
  79. return
  80. }
  81. // GetBind select sug from db
  82. func (d *Dao) GetBind(c context.Context) (sugList []*model.Sug, err error) {
  83. var (
  84. rows *sql.Rows
  85. start = 0
  86. offset = 50
  87. )
  88. for {
  89. sqlStr := fmt.Sprintf(_fetchMatch, start, offset)
  90. start += offset
  91. if rows, err = d.ticketDB.Query(c, sqlStr); err != nil {
  92. log.Error("d._fetchSugListSQL.Query error(%v)", err)
  93. return
  94. }
  95. i := 0
  96. for rows.Next() {
  97. i++
  98. sug := &model.Sug{}
  99. sug.Item = &model.Item{}
  100. if err = rows.Scan(&sug.SeasonID, &sug.Item.ID, &sug.Score); err != nil {
  101. log.Error("row.Scan() error(%v)", err)
  102. continue
  103. }
  104. sugList = append(sugList, sug)
  105. }
  106. if i < 10 {
  107. break
  108. }
  109. }
  110. defer rows.Close()
  111. err = rows.Err()
  112. return
  113. }
  114. // GetItem get item info
  115. func (d *Dao) GetItem(c context.Context, sug *model.Sug) (err error) {
  116. var row *sql.Row
  117. if row = d.mallDB.QueryRow(c, _fetchItem, sug.Item.ID); err != nil {
  118. log.Error("d._fetchProjectListSQL.Query error(%v)", err)
  119. return
  120. }
  121. var imgList string
  122. if err = row.Scan(&sug.Item.ID, &sug.Item.Name, &sug.Item.IPRightID, &sug.Item.Brief, &imgList); err != nil {
  123. log.Error("row.Scan() error(%v)", err)
  124. return
  125. }
  126. if sug.Item.Brief == "" {
  127. sug.Item.Brief = sug.Item.Name
  128. sug.Item.Name = ""
  129. }
  130. var imgListArr []string
  131. if err = json.Unmarshal([]byte(imgList), &imgListArr); err != nil {
  132. log.Error("get first img err[%s] (%v)", imgList, err)
  133. err = nil
  134. return
  135. }
  136. sug.Item.HeadImg = imgListArr[0]
  137. row = d.mallDB.QueryRow(c, _fetchIPRight, sug.Item.IPRightID)
  138. var name, chs_name, alias, parentID, parentName, parentChsName, parentAlias string
  139. if err = row.Scan(&name, &chs_name, &alias, &parentID); err != nil {
  140. err = nil
  141. return
  142. }
  143. if parentID != "0" {
  144. row := d.mallDB.QueryRow(c, _fetchParentIPRight, parentID)
  145. if err = row.Scan(&parentName, &parentChsName, &parentAlias); err != nil {
  146. log.Error("get parent ip_right_info err (%v)", err)
  147. err = nil
  148. return
  149. }
  150. }
  151. sug.Item.Keywords = name + " " + chs_name + " " + alias + " " + parentName + " " + parentChsName + " " + parentAlias
  152. return
  153. }