card.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package card
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/up/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _UpInfoBaseColumn = `mid, name_cn, name_en, name_alias, signature, content, nationality,
  12. nation, gender, blood_type, constellation, height, weight, birth_place, birth_date, occupation,
  13. tags, masterpieces, school, location, interests, platform, platform_account`
  14. _countUpSQL = "SELECT count(distinct mid) FROM card_up"
  15. _listUpMidSQL = "SELECT mid FROM card_up order BY mtime DESC"
  16. _listUpInfoSQL = "SELECT " + _UpInfoBaseColumn + " FROM card_up limit ? offset ?"
  17. _getUpInfoByMidSQL = "SELECT " + _UpInfoBaseColumn + " FROM card_up WHERE mid=?"
  18. _listUpVideoIDSQL = "SELECT avid FROM card_up_video WHERE mid=? ORDER BY id DESC"
  19. _listUpImageSQL = "SELECT url, height, width FROM card_up_image WHERE mid=? ORDER BY id DESC"
  20. _listUpAccountSQL = "SELECT url, title, picture, abstract FROM card_up_account WHERE mid = ?"
  21. _listUpInfoByMidsSQL = "SELECT " + _UpInfoBaseColumn + " FROM card_up WHERE mid IN (%s)"
  22. _listUpVideoIDByMidsSQL = "SELECT mid, avid FROM card_up_video WHERE mid IN (%s) ORDER BY id DESC"
  23. _listUpImageByMidsSQL = "SELECT mid, url, height, width FROM card_up_image WHERE mid IN (%s) ORDER BY id DESC"
  24. _listUpAccountByMidsSQL = "SELECT mid, url, title, picture, abstract FROM card_up_account WHERE mid IN (%s)"
  25. )
  26. // CountUpCard count up num
  27. func (d *Dao) CountUpCard(ctx context.Context) (total int, err error) {
  28. row := d.db.QueryRow(ctx, _countUpSQL)
  29. err = row.Scan(&total)
  30. if err != nil {
  31. log.Error("CountUpCard row.Scan error(%v)", err)
  32. }
  33. return
  34. }
  35. // ListUpInfo page list up mids
  36. func (d *Dao) ListUpInfo(ctx context.Context, offset uint, size uint) (infos []*model.UpCardInfo, err error) {
  37. rows, err := d.db.Query(ctx, _listUpInfoSQL, size, offset)
  38. if err != nil {
  39. log.Error("ListUpInfo d.db.Query error(%v)", err)
  40. return
  41. }
  42. defer rows.Close()
  43. for rows.Next() {
  44. info := &model.UpCardInfo{}
  45. err = rows.Scan(&info.MID,
  46. &info.NameCN, &info.NameEN, &info.NameAlias,
  47. &info.Signature,
  48. &info.Content, &info.Nationality, &info.Nation,
  49. &info.Gender, &info.BloodType, &info.Constellation,
  50. &info.Height, &info.Weight, &info.BirthPlace,
  51. &info.BirthDate, &info.Occupation, &info.Tags,
  52. &info.Masterpieces, &info.School, &info.Location,
  53. &info.Interests, &info.Platform, &info.PlatformAccount)
  54. if err != nil {
  55. log.Error("ListUpInfo rows.Scan error(%v)", err)
  56. return
  57. }
  58. infos = append(infos, info)
  59. }
  60. return
  61. }
  62. // MidUpInfoMap get <mid, UpInfo> map by mids
  63. func (d *Dao) MidUpInfoMap(ctx context.Context, mids []int64) (midUpInfoMap map[int64]*model.UpCardInfo, err error) {
  64. midUpInfoMap = make(map[int64]*model.UpCardInfo)
  65. rows, err := d.db.Query(ctx, fmt.Sprintf(_listUpInfoByMidsSQL, xstr.JoinInts(mids)))
  66. if err != nil {
  67. log.Error("MidUpInfoMap d.db.Query error(%v)", err)
  68. return
  69. }
  70. defer rows.Close()
  71. for rows.Next() {
  72. info := &model.UpCardInfo{}
  73. err = rows.Scan(&info.MID,
  74. &info.NameCN, &info.NameEN, &info.NameAlias,
  75. &info.Signature,
  76. &info.Content, &info.Nationality, &info.Nation,
  77. &info.Gender, &info.BloodType, &info.Constellation,
  78. &info.Height, &info.Weight, &info.BirthPlace,
  79. &info.BirthDate, &info.Occupation, &info.Tags,
  80. &info.Masterpieces, &info.School, &info.Location,
  81. &info.Interests, &info.Platform, &info.PlatformAccount)
  82. if err != nil {
  83. log.Error("MidUpInfoMap rows.Scan error(%v)", err)
  84. return
  85. }
  86. midUpInfoMap[info.MID] = info
  87. }
  88. return
  89. }
  90. // MidAccountsMap get <mid, Accounts> map by mids
  91. func (d *Dao) MidAccountsMap(ctx context.Context, mids []int64) (midAccountsMap map[int64][]*model.UpCardAccount, err error) {
  92. midAccountsMap = make(map[int64][]*model.UpCardAccount)
  93. rows, err := d.db.Query(ctx, fmt.Sprintf(_listUpAccountByMidsSQL, xstr.JoinInts(mids)))
  94. if err != nil {
  95. log.Error("MidAccountsMap d.db.Query error(%v)", err)
  96. return
  97. }
  98. defer rows.Close()
  99. for rows.Next() {
  100. var mid int64
  101. account := new(model.UpCardAccount)
  102. err = rows.Scan(&mid, &account.URL, &account.Title, &account.Picture, &account.Desc)
  103. if err != nil {
  104. log.Error("MidAccountsMap row.Scan error(%v)", err)
  105. return
  106. }
  107. midAccountsMap[mid] = append(midAccountsMap[mid], account)
  108. }
  109. return
  110. }
  111. // MidImagesMap get <mid, Images> map by mids
  112. func (d *Dao) MidImagesMap(ctx context.Context, mids []int64) (midImagesMap map[int64][]*model.UpCardImage, err error) {
  113. midImagesMap = make(map[int64][]*model.UpCardImage)
  114. rows, err := d.db.Query(ctx, fmt.Sprintf(_listUpImageByMidsSQL, xstr.JoinInts(mids)))
  115. if err != nil {
  116. log.Error("MidImagesMap d.db.Query error(%v)", err)
  117. return
  118. }
  119. defer rows.Close()
  120. for rows.Next() {
  121. var mid int64
  122. image := new(model.UpCardImage)
  123. err = rows.Scan(&mid, &image.URL, &image.Height, &image.Width)
  124. if err != nil {
  125. log.Error("MidImagesMap row.Scan error(%v)", err)
  126. return
  127. }
  128. midImagesMap[mid] = append(midImagesMap[mid], image)
  129. }
  130. return
  131. }
  132. // MidAvidsMap get <mid, Avids> map by mids
  133. func (d *Dao) MidAvidsMap(ctx context.Context, mids []int64) (midAvidsMap map[int64][]int64, err error) {
  134. midAvidsMap = make(map[int64][]int64)
  135. rows, err := d.db.Query(ctx, fmt.Sprintf(_listUpVideoIDByMidsSQL, xstr.JoinInts(mids)))
  136. if err != nil {
  137. log.Error("MidAvidsMap d.db.Query error(%v)", err)
  138. return
  139. }
  140. defer rows.Close()
  141. for rows.Next() {
  142. var mid int64
  143. var avid int64
  144. err = rows.Scan(&mid, &avid)
  145. if err != nil {
  146. log.Error("MidAvidsMap row.Scan error(%v)", err)
  147. return
  148. }
  149. midAvidsMap[mid] = append(midAvidsMap[mid], avid)
  150. }
  151. return
  152. }
  153. // ListUpMID list up mids
  154. func (d *Dao) ListUpMID(ctx context.Context) (mids []int64, err error) {
  155. rows, err := d.db.Query(ctx, _listUpMidSQL)
  156. if err != nil {
  157. log.Error("ListCardBase d.db.Query error(%v)", err)
  158. return
  159. }
  160. defer rows.Close()
  161. for rows.Next() {
  162. var mid int64
  163. err = rows.Scan(&mid)
  164. if err != nil {
  165. log.Error("ListCardBase row.Scan error(%v)", err)
  166. return
  167. }
  168. mids = append(mids, mid)
  169. }
  170. return
  171. }
  172. // GetUpInfo get up info by mid
  173. func (d *Dao) GetUpInfo(ctx context.Context, mid int64) (card *model.UpCardInfo, err error) {
  174. row := d.db.QueryRow(ctx, _getUpInfoByMidSQL, mid)
  175. card = &model.UpCardInfo{}
  176. if err = row.Scan(&card.MID,
  177. &card.NameCN, &card.NameEN, &card.NameAlias,
  178. &card.Signature,
  179. &card.Content, &card.Nationality, &card.Nation,
  180. &card.Gender, &card.BloodType, &card.Constellation,
  181. &card.Height, &card.Weight, &card.BirthPlace,
  182. &card.BirthDate, &card.Occupation, &card.Tags,
  183. &card.Masterpieces, &card.School, &card.Location,
  184. &card.Interests, &card.Platform, &card.PlatformAccount); err != nil {
  185. if err == sql.ErrNoRows {
  186. card = nil
  187. err = nil
  188. } else {
  189. log.Error("GetUpCard row.Scan error(%v)", err)
  190. return
  191. }
  192. }
  193. return
  194. }
  195. // ListUpAccount list up accounts by mid
  196. func (d *Dao) ListUpAccount(ctx context.Context, mid int64) (accounts []*model.UpCardAccount, err error) {
  197. rows, err := d.db.Query(ctx, _listUpAccountSQL, mid)
  198. if err != nil {
  199. log.Error("listUpAccount d.db.Query error(%v)", err)
  200. return
  201. }
  202. defer rows.Close()
  203. for rows.Next() {
  204. account := new(model.UpCardAccount)
  205. err = rows.Scan(&account.URL, &account.Title, &account.Picture, &account.Desc)
  206. if err != nil {
  207. log.Error("listUpAccount row.Scan error(%v)", err)
  208. return
  209. }
  210. accounts = append(accounts, account)
  211. }
  212. return
  213. }
  214. // ListUpImage list up images by mid
  215. func (d *Dao) ListUpImage(ctx context.Context, mid int64) (images []*model.UpCardImage, err error) {
  216. rows, err := d.db.Query(ctx, _listUpImageSQL, mid)
  217. if err != nil {
  218. log.Error("listUpImage d.db.Query error(%v)", err)
  219. return
  220. }
  221. defer rows.Close()
  222. for rows.Next() {
  223. image := new(model.UpCardImage)
  224. err = rows.Scan(&image.URL, &image.Height, &image.Width)
  225. if err != nil {
  226. log.Error("listUpImage row.Scan error(%v)", err)
  227. return
  228. }
  229. images = append(images, image)
  230. }
  231. return
  232. }
  233. // ListAVID list avids by mid
  234. func (d *Dao) ListAVID(ctx context.Context, mid int64) (avids []int64, err error) {
  235. rows, err := d.db.Query(ctx, _listUpVideoIDSQL, mid)
  236. if err != nil {
  237. log.Error("listUpVideo d.db.Query error(%v)", err)
  238. return
  239. }
  240. defer rows.Close()
  241. for rows.Next() {
  242. var avid int64
  243. err = rows.Scan(&avid)
  244. if err != nil {
  245. log.Error("listUpVideo row.Scan error(%v)", err)
  246. return
  247. }
  248. avids = append(avids, avid)
  249. }
  250. return
  251. }