card.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package card
  2. import (
  3. "context"
  4. "strconv"
  5. "time"
  6. "go-common/app/interface/main/app-card/model/card/operate"
  7. "go-common/app/interface/main/app-show/conf"
  8. "go-common/app/interface/main/app-show/model"
  9. "go-common/app/interface/main/app-show/model/card"
  10. "go-common/library/cache/memcache"
  11. "go-common/library/database/sql"
  12. "go-common/library/log"
  13. )
  14. const (
  15. // daily_selection
  16. _appColumnSQL = "SELECT id,tab,resource_id,tpl,name,plat_ver FROM app_column WHERE state=1"
  17. _appPosRecSQL = "SELECT p.id,p.tab,p.resource_id,p.type,p.title,p.cover,p.re_type,p.re_value,p.plat_ver,p.desc,p.tag_id FROM app_pos_rec AS p WHERE p.stime<? AND p.etime>? AND p.state=1 ORDER BY p.weight ASC"
  18. _appContentRSQL = "SELECT c.id,c.module,c.rec_id,c.ctype,c.cvalue,c.ctitle,c.tag_id FROM app_content AS c, app_pos_rec AS r WHERE c.rec_id=r.id AND r.state=1 AND r.stime<? AND r.etime>? AND c.module=1"
  19. _appColumnNperSQL = "SELECT n.id,n.column_id,n.name,n.desc,n.nper,n.nper_time,n.cover,n.plat_ver,n.title,n.re_type,n.re_value FROM app_column_nper AS n WHERE n.cron_time<? AND n.state=1 ORDER BY n.nper DESC"
  20. _appContentNSQL = "SELECT c.id,c.module,c.rec_id,c.ctype,c.cvalue,c.ctitle,c.tag_id FROM app_content AS c, app_column_nper AS n WHERE c.rec_id=n.id AND n.state=1 AND n.cron_time<? AND c.module=2"
  21. _appColumnList = "SELECT c.id,c.name,cn.id,cn.title,cn.plat_ver FROM app_column AS c,app_column_nper AS cn WHERE c.id=cn.column_id AND c.state=1 AND cn.state=1 AND cn.cron_time<? ORDER BY cn.nper DESC"
  22. // hot card
  23. _cardSQL = `SELECT c.id,c.title,c.card_type,c.card_value,c.recommand_reason,c.recommand_state,c.priority FROM popular_card AS c
  24. WHERE c.stime<? AND c.etime>? AND c.check=2 AND c.is_delete=0 ORDER BY c.priority ASC`
  25. _cardPlatSQL = `SELECT card_id,plat,conditions,build FROM popular_card_plat WHERE is_delete=0`
  26. _cardSetSQL = `SELECT c.id,c.type,c.value,c.title,c.long_title,c.content FROM card_set AS c WHERE c.deleted=0`
  27. _eventTopicSQL = `SELECT c.id,c.title,c.desc,c.cover,c.re_type,c.re_value,c.corner FROM event_topic AS c WHERE c.deleted=0`
  28. )
  29. // Dao is card dao.
  30. type Dao struct {
  31. db *sql.DB
  32. column *sql.Stmt
  33. posRec *sql.Stmt
  34. recContent *sql.Stmt
  35. nperContent *sql.Stmt
  36. columnNper *sql.Stmt
  37. columnList *sql.Stmt
  38. // memcache
  39. mc *memcache.Pool
  40. expire int32
  41. }
  42. func New(c *conf.Config) *Dao {
  43. d := &Dao{
  44. db: sql.NewMySQL(c.MySQL.Show),
  45. // memcache
  46. mc: memcache.NewPool(c.Memcache.Cards.Config),
  47. expire: int32(time.Duration(c.Memcache.Cards.Expire) / time.Second),
  48. }
  49. d.column = d.db.Prepared(_appColumnSQL)
  50. d.posRec = d.db.Prepared(_appPosRecSQL)
  51. d.recContent = d.db.Prepared(_appContentRSQL)
  52. d.nperContent = d.db.Prepared(_appContentNSQL)
  53. d.columnNper = d.db.Prepared(_appColumnNperSQL)
  54. d.columnList = d.db.Prepared(_appColumnList)
  55. return d
  56. }
  57. // Columns
  58. func (d *Dao) Columns(ctx context.Context) (res map[int8][]*card.Column, err error) {
  59. res = map[int8][]*card.Column{}
  60. rows, err := d.column.Query(ctx)
  61. if err != nil {
  62. log.Error("query error(%v)", err)
  63. return
  64. }
  65. defer rows.Close()
  66. for rows.Next() {
  67. c := &card.Column{}
  68. if err = rows.Scan(&c.ID, &c.Tab, &c.RegionID, &c.Tpl, &c.Name, &c.PlatVer); err != nil {
  69. log.Error("rows.Scan error(%v)", err)
  70. res = nil
  71. return
  72. }
  73. for _, limit := range c.ColumnPlatChange() {
  74. tmpc := &card.Column{}
  75. *tmpc = *c
  76. tmpc.Plat = limit.Plat
  77. tmpc.Build = limit.Build
  78. tmpc.Condition = limit.Condition
  79. tmpc.PlatVer = ""
  80. tmpc.ColumnGotoChannge()
  81. res[tmpc.Plat] = append(res[tmpc.Plat], tmpc)
  82. }
  83. }
  84. return
  85. }
  86. // PosRecs
  87. func (d *Dao) PosRecs(ctx context.Context, now time.Time) (res map[int8]map[int][]*card.Card, err error) {
  88. res = map[int8]map[int][]*card.Card{}
  89. rows, err := d.posRec.Query(ctx, now, now)
  90. if err != nil {
  91. log.Error("query error(%v)", err)
  92. return
  93. }
  94. defer rows.Close()
  95. for rows.Next() {
  96. c := &card.Card{}
  97. if err = rows.Scan(&c.ID, &c.Tab, &c.RegionID, &c.Type, &c.Title, &c.Cover, &c.Rtype, &c.Rvalue, &c.PlatVer, &c.Desc, &c.TagID); err != nil {
  98. log.Error("rows.Scan error(%v)", err)
  99. res = nil
  100. return
  101. }
  102. for _, limit := range c.CardPlatChange() {
  103. tmpc := &card.Card{}
  104. *tmpc = *c
  105. tmpc.Plat = limit.Plat
  106. tmpc.Build = limit.Build
  107. tmpc.Condition = limit.Condition
  108. tmpc.PlatVer = ""
  109. tmpc.CardGotoChannge()
  110. if cards, ok := res[tmpc.Plat]; ok {
  111. cards[tmpc.RegionID] = append(cards[tmpc.RegionID], tmpc)
  112. } else {
  113. res[tmpc.Plat] = map[int][]*card.Card{
  114. tmpc.RegionID: []*card.Card{tmpc},
  115. }
  116. }
  117. }
  118. }
  119. return
  120. }
  121. // RecContents
  122. func (d *Dao) RecContents(ctx context.Context, now time.Time) (res map[int][]*card.Content, aids map[int][]int64, err error) {
  123. res = map[int][]*card.Content{}
  124. aids = map[int][]int64{}
  125. rows, err := d.recContent.Query(ctx, now, now)
  126. if err != nil {
  127. log.Error("query error(%v)", err)
  128. return
  129. }
  130. defer rows.Close()
  131. for rows.Next() {
  132. c := &card.Content{}
  133. if err = rows.Scan(&c.ID, &c.Module, &c.RecID, &c.Type, &c.Value, &c.Title, &c.TagID); err != nil {
  134. log.Error("rows.Scan error(%v)", err)
  135. res = nil
  136. return
  137. }
  138. res[c.RecID] = append(res[c.RecID], c)
  139. if c.Type == model.CardGotoAv {
  140. aidInt, _ := strconv.ParseInt(c.Value, 10, 64)
  141. aids[c.RecID] = append(aids[c.RecID], aidInt)
  142. }
  143. }
  144. return
  145. }
  146. // NperContents
  147. func (d *Dao) NperContents(ctx context.Context, now time.Time) (res map[int][]*card.Content, aids map[int][]int64, err error) {
  148. res = map[int][]*card.Content{}
  149. aids = map[int][]int64{}
  150. rows, err := d.nperContent.Query(ctx, now)
  151. if err != nil {
  152. log.Error("query error(%v)", err)
  153. return
  154. }
  155. defer rows.Close()
  156. for rows.Next() {
  157. c := &card.Content{}
  158. if err = rows.Scan(&c.ID, &c.Module, &c.RecID, &c.Type, &c.Value, &c.Title, &c.TagID); err != nil {
  159. log.Error("rows.Scan error(%v)", err)
  160. res = nil
  161. return
  162. }
  163. res[c.RecID] = append(res[c.RecID], c)
  164. if c.Type == model.CardGotoAv {
  165. aidInt, _ := strconv.ParseInt(c.Value, 10, 64)
  166. aids[c.RecID] = append(aids[c.RecID], aidInt)
  167. }
  168. }
  169. return
  170. }
  171. // ColumnNpers
  172. func (d *Dao) ColumnNpers(ctx context.Context, now time.Time) (res map[int8][]*card.ColumnNper, err error) {
  173. res = map[int8][]*card.ColumnNper{}
  174. rows, err := d.columnNper.Query(ctx, now)
  175. if err != nil {
  176. log.Error("query error(%v)", err)
  177. return
  178. }
  179. defer rows.Close()
  180. for rows.Next() {
  181. c := &card.ColumnNper{}
  182. if err = rows.Scan(&c.ID, &c.ColumnID, &c.Name, &c.Desc, &c.Nper, &c.NperTime, &c.Cover, &c.PlatVer, &c.Title, &c.Rtype, &c.Rvalue); err != nil {
  183. log.Error("rows.Scan error(%v)", err)
  184. res = nil
  185. return
  186. }
  187. for _, limit := range c.ColumnNperPlatChange() {
  188. tmpc := &card.ColumnNper{}
  189. *tmpc = *c
  190. tmpc.Plat = limit.Plat
  191. tmpc.Build = limit.Build
  192. tmpc.Condition = limit.Condition
  193. tmpc.PlatVer = ""
  194. tmpc.ColumnNperGotoChange()
  195. res[tmpc.Plat] = append(res[tmpc.Plat], tmpc)
  196. }
  197. }
  198. return
  199. }
  200. // ColumnList
  201. func (d *Dao) ColumnPlatList(ctx context.Context, now time.Time) (res map[int8][]*card.ColumnList, err error) {
  202. res = map[int8][]*card.ColumnList{}
  203. rows, err := d.columnList.Query(ctx, now)
  204. if err != nil {
  205. log.Error("query error(%v)", err)
  206. return
  207. }
  208. for rows.Next() {
  209. c := &card.ColumnList{}
  210. if err = rows.Scan(&c.Ceid, &c.Cname, &c.Cid, &c.Name, &c.PlatVer); err != nil {
  211. log.Error("rows.Scan error(%v)", err)
  212. res = nil
  213. return
  214. }
  215. for _, limit := range c.ColumnListPlatChange() {
  216. tmpc := &card.ColumnList{}
  217. *tmpc = *c
  218. tmpc.Plat = limit.Plat
  219. tmpc.Build = limit.Build
  220. tmpc.Condition = limit.Condition
  221. tmpc.PlatVer = ""
  222. res[tmpc.Plat] = append(res[tmpc.Plat], tmpc)
  223. }
  224. }
  225. return
  226. }
  227. // ColumnList
  228. func (d *Dao) ColumnList(ctx context.Context, now time.Time) (res []*card.ColumnList, err error) {
  229. res = []*card.ColumnList{}
  230. rows, err := d.columnList.Query(ctx, now)
  231. if err != nil {
  232. log.Error("query error(%v)", err)
  233. return
  234. }
  235. for rows.Next() {
  236. c := &card.ColumnList{}
  237. if err = rows.Scan(&c.Ceid, &c.Cname, &c.Cid, &c.Name, &c.PlatVer); err != nil {
  238. log.Error("rows.Scan error(%v)", err)
  239. res = nil
  240. return
  241. }
  242. res = append(res, c)
  243. }
  244. return
  245. }
  246. // Card channel card
  247. func (d *Dao) Card(ctx context.Context, now time.Time) (res []*card.PopularCard, err error) {
  248. rows, err := d.db.Query(ctx, _cardSQL, now, now)
  249. if err != nil {
  250. return
  251. }
  252. defer rows.Close()
  253. for rows.Next() {
  254. c := &card.PopularCard{}
  255. var valueStr string
  256. if err = rows.Scan(&c.ID, &c.Title, &c.Type, &valueStr, &c.Reason, &c.ReasonType, &c.Pos); err != nil {
  257. return
  258. }
  259. c.Value, _ = strconv.ParseInt(valueStr, 10, 64)
  260. res = append(res, c)
  261. }
  262. return
  263. }
  264. // CardPlat channel card plat
  265. func (d *Dao) CardPlat(ctx context.Context) (res map[int64]map[int8][]*card.PopularCardPlat, err error) {
  266. res = map[int64]map[int8][]*card.PopularCardPlat{}
  267. rows, err := d.db.Query(ctx, _cardPlatSQL)
  268. if err != nil {
  269. return
  270. }
  271. defer rows.Close()
  272. for rows.Next() {
  273. c := &card.PopularCardPlat{}
  274. if err = rows.Scan(&c.CardID, &c.Plat, &c.Condition, &c.Build); err != nil {
  275. return
  276. }
  277. if r, ok := res[c.CardID]; !ok {
  278. res[c.CardID] = map[int8][]*card.PopularCardPlat{
  279. c.Plat: []*card.PopularCardPlat{c},
  280. }
  281. } else {
  282. r[c.Plat] = append(r[c.Plat], c)
  283. }
  284. }
  285. return
  286. }
  287. // CardSet card set
  288. func (d *Dao) CardSet(ctx context.Context) (res map[int64]*operate.CardSet, err error) {
  289. var rows *sql.Rows
  290. if rows, err = d.db.Query(ctx, _cardSetSQL); err != nil {
  291. return
  292. }
  293. defer rows.Close()
  294. res = make(map[int64]*operate.CardSet)
  295. for rows.Next() {
  296. var (
  297. c = &operate.CardSet{}
  298. value string
  299. )
  300. if err = rows.Scan(&c.ID, &c.Type, &value, &c.Title, &c.LongTitle, &c.Content); err != nil {
  301. return
  302. }
  303. c.Value, _ = strconv.ParseInt(value, 10, 64)
  304. res[c.ID] = c
  305. }
  306. return
  307. }
  308. // EventTopic event_topic all
  309. func (d *Dao) EventTopic(ctx context.Context) (res map[int64]*operate.EventTopic, err error) {
  310. var rows *sql.Rows
  311. if rows, err = d.db.Query(ctx, _eventTopicSQL); err != nil {
  312. return
  313. }
  314. defer rows.Close()
  315. res = make(map[int64]*operate.EventTopic)
  316. for rows.Next() {
  317. c := &operate.EventTopic{}
  318. if err = rows.Scan(&c.ID, &c.Title, &c.Desc, &c.Cover, &c.ReType, &c.ReValue, &c.Corner); err != nil {
  319. return
  320. }
  321. res[c.ID] = c
  322. }
  323. return
  324. }