material.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package material
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. appMdl "go-common/app/interface/main/creative/model/app"
  7. "go-common/app/interface/main/creative/model/music"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. "strings"
  11. "time"
  12. )
  13. const (
  14. _basicSQL = "SELECT mtime,id,name,rank,extra,platform,build,type FROM material WHERE type in (%s) and state = 0 order by type asc, rank asc"
  15. _FiltersSQL = "SELECT mtime,id,name,rank,extra,platform,build FROM material WHERE type=2 and state = 0 order by rank asc "
  16. _CategoryBindSQL = "SELECT a.new, a.type, a.name as cname,a.rank as crank,b.index as brank,b.material_id as mid, b.category_id as cid from material_category a join material_with_category b where a.id = b.category_id and a.state=0 and b.state=0 and a.type =%d order by a.rank asc, b.index asc"
  17. _VstickersSQL = "SELECT mtime,id,name,rank,extra,platform,build FROM material WHERE type=7 and state = 0 order by rank asc "
  18. _CooperatesSQL = "SELECT mtime,id,name,rank,extra,platform,build FROM material WHERE type=9 and state = 0 order by rank asc "
  19. )
  20. var basicTypes = []int64{
  21. int64(appMdl.TypeSubtitle),
  22. int64(appMdl.TypeFont),
  23. int64(appMdl.TypeHotWord),
  24. int64(appMdl.TypeSticker),
  25. int64(appMdl.TypeIntro),
  26. int64(appMdl.TypeTransition),
  27. int64(appMdl.TypeTheme),
  28. }
  29. // Basic fn
  30. func (d *Dao) Basic(c context.Context) (basicMap map[string]interface{}, err error) {
  31. rows, err := d.db.Query(c, fmt.Sprintf(_basicSQL, xstr.JoinInts(basicTypes)))
  32. if err != nil {
  33. log.Error("mysqlDB.Query error(%v)", err)
  34. return
  35. }
  36. defer rows.Close()
  37. basicMap = make(map[string]interface{})
  38. var (
  39. subs = make([]*music.Subtitle, 0)
  40. fons = make([]*music.Font, 0)
  41. hots = make([]*music.Hotword, 0)
  42. stis = make([]*music.Sticker, 0)
  43. ints = make([]*music.Intro, 0)
  44. trans = make([]*music.Transition, 0)
  45. themes = make([]*music.Theme, 0)
  46. )
  47. basicMap["subs"] = subs
  48. basicMap["fons"] = fons
  49. basicMap["hots"] = hots
  50. basicMap["stis"] = stis
  51. basicMap["ints"] = ints
  52. basicMap["trans"] = trans
  53. basicMap["themes"] = themes
  54. for rows.Next() {
  55. b := &music.Basic{
  56. Material: music.Material{},
  57. }
  58. if err = rows.Scan(&b.MTime, &b.ID, &b.Name, &b.Rank, &b.Extra, &b.Material.Platform, &b.Material.Build, &b.Material.Type); err != nil {
  59. log.Error("row.Scan error(%v)", err)
  60. return
  61. }
  62. var extra *struct {
  63. Cover string `json:"cover"`
  64. DownloadURL string `json:"download_url"`
  65. Max int `json:"max"`
  66. SubType int64 `json:"sub_type"`
  67. Tip string `json:"tip"`
  68. White int8 `json:"white_list"`
  69. TagsStr string `json:"tags"`
  70. }
  71. if err = json.Unmarshal([]byte(b.Extra), &extra); err != nil {
  72. log.Error("json.Unmarshal extra failed error(%v)", err)
  73. continue
  74. }
  75. b.Cover = extra.Cover
  76. b.DownloadURL = extra.DownloadURL
  77. b.Max = extra.Max
  78. b.White = extra.White
  79. if len(b.Material.Build) > 0 {
  80. var buildComps []*music.BuildComp
  81. if err = json.Unmarshal([]byte(b.Build), &buildComps); err != nil {
  82. log.Error("json.Unmarshal buildComps failed error(%v)", err)
  83. continue
  84. }
  85. b.Material.BuildComps = buildComps
  86. }
  87. if b.MTime.Time().AddDate(0, 0, 7).Unix() >= time.Now().Unix() {
  88. b.New = 1
  89. }
  90. if len(extra.TagsStr) > 0 {
  91. b.Tags = append(b.Tags, strings.Split(extra.TagsStr, ",")...)
  92. }
  93. log.Info("b info v(%+v)", b)
  94. switch b.Material.Type {
  95. case appMdl.TypeSubtitle:
  96. subs = append(subs, &music.Subtitle{
  97. ID: b.ID,
  98. Name: b.Name,
  99. Cover: b.Cover,
  100. DownloadURL: b.DownloadURL,
  101. Rank: b.Rank,
  102. Max: b.Max,
  103. Material: b.Material,
  104. New: b.New,
  105. Tags: b.Tags,
  106. MTime: b.MTime,
  107. })
  108. case appMdl.TypeFont:
  109. fons = append(fons, &music.Font{
  110. ID: b.ID,
  111. Name: b.Name,
  112. Cover: b.Cover,
  113. DownloadURL: b.DownloadURL,
  114. Rank: b.Rank,
  115. Material: b.Material,
  116. New: b.New,
  117. Tags: b.Tags,
  118. MTime: b.MTime,
  119. })
  120. case appMdl.TypeHotWord:
  121. hots = append(hots, &music.Hotword{
  122. ID: b.ID,
  123. Name: b.Name,
  124. Cover: b.Cover,
  125. DownloadURL: b.DownloadURL,
  126. Rank: b.Rank,
  127. Material: b.Material,
  128. New: b.New,
  129. Tags: b.Tags,
  130. MTime: b.MTime,
  131. })
  132. case appMdl.TypeSticker:
  133. stis = append(stis, &music.Sticker{
  134. ID: b.ID,
  135. Name: b.Name,
  136. Cover: b.Cover,
  137. DownloadURL: b.DownloadURL,
  138. Rank: b.Rank,
  139. Material: b.Material,
  140. SubType: extra.SubType,
  141. Tip: extra.Tip,
  142. New: b.New,
  143. Tags: b.Tags,
  144. MTime: b.MTime,
  145. })
  146. case appMdl.TypeTheme:
  147. themes = append(themes, &music.Theme{
  148. ID: b.ID,
  149. Name: b.Name,
  150. Cover: b.Cover,
  151. DownloadURL: b.DownloadURL,
  152. Rank: b.Rank,
  153. Material: b.Material,
  154. New: b.New,
  155. Tags: b.Tags,
  156. MTime: b.MTime,
  157. })
  158. case appMdl.TypeIntro:
  159. ints = append(ints, &music.Intro{
  160. ID: b.ID,
  161. Name: b.Name,
  162. Cover: b.Cover,
  163. DownloadURL: b.DownloadURL,
  164. Rank: b.Rank,
  165. Material: b.Material,
  166. New: b.New,
  167. Tags: b.Tags,
  168. MTime: b.MTime,
  169. })
  170. case appMdl.TypeTransition:
  171. trans = append(trans, &music.Transition{
  172. ID: b.ID,
  173. Name: b.Name,
  174. Cover: b.Cover,
  175. DownloadURL: b.DownloadURL,
  176. Rank: b.Rank,
  177. Material: b.Material,
  178. New: b.New,
  179. Tags: b.Tags,
  180. MTime: b.MTime,
  181. })
  182. }
  183. }
  184. basicMap["subs"] = subs
  185. basicMap["fons"] = fons
  186. basicMap["hots"] = hots
  187. basicMap["stis"] = stis
  188. basicMap["ints"] = ints
  189. basicMap["trans"] = trans
  190. basicMap["themes"] = themes
  191. return
  192. }
  193. // CategoryBind fn
  194. func (d *Dao) CategoryBind(c context.Context, tp int8) (res []*music.MaterialBind, err error) {
  195. res = make([]*music.MaterialBind, 0)
  196. rows, err := d.db.Query(c, fmt.Sprintf(_CategoryBindSQL, tp))
  197. if err != nil {
  198. log.Error("mysqlDB.Query error(%v)", err)
  199. return
  200. }
  201. defer rows.Close()
  202. for rows.Next() {
  203. v := &music.MaterialBind{}
  204. if err = rows.Scan(&v.New, &v.Tp, &v.CName, &v.CRank, &v.BRank, &v.MID, &v.CID); err != nil {
  205. log.Error("row.Scan error(%v)", err)
  206. return
  207. }
  208. res = append(res, v)
  209. }
  210. return
  211. }
  212. // Filters fn
  213. func (d *Dao) Filters(c context.Context) (res []*music.Filter, resMap map[int64]*music.Filter, err error) {
  214. res = make([]*music.Filter, 0)
  215. resMap = make(map[int64]*music.Filter)
  216. rows, err := d.db.Query(c, _FiltersSQL)
  217. if err != nil {
  218. log.Error("mysqlDB.Query error(%v)", err)
  219. return
  220. }
  221. defer rows.Close()
  222. for rows.Next() {
  223. v := &music.Filter{
  224. Material: music.Material{},
  225. }
  226. if err = rows.Scan(&v.MTime, &v.ID, &v.Name, &v.Rank, &v.Extra, &v.Material.Platform, &v.Material.Build); err != nil {
  227. log.Error("row.Scan error(%v)", err)
  228. return
  229. }
  230. var extra *struct {
  231. Cover string `json:"cover"`
  232. DownloadURL string `json:"download_url"`
  233. TagsStr string `json:"tags"`
  234. FilterType int8 `json:"filter_type"`
  235. }
  236. if err = json.Unmarshal([]byte(v.Extra), &extra); err != nil {
  237. log.Error("json.Unmarshal failed error(%v)", err)
  238. continue
  239. }
  240. v.Cover = extra.Cover
  241. v.DownloadURL = extra.DownloadURL
  242. v.FilterType = extra.FilterType
  243. if len(v.Material.Build) > 0 {
  244. var buildComps []*music.BuildComp
  245. if err = json.Unmarshal([]byte(v.Build), &buildComps); err != nil {
  246. log.Error("json.Unmarshal buildComps failed error(%v)", err)
  247. continue
  248. }
  249. v.Material.BuildComps = buildComps
  250. }
  251. if v.MTime.Time().AddDate(0, 0, 7).Unix() >= time.Now().Unix() {
  252. v.New = 1
  253. }
  254. if len(extra.TagsStr) > 0 {
  255. v.Tags = append(v.Tags, strings.Split(extra.TagsStr, ",")...)
  256. }
  257. log.Info("v info v(%+v)", v)
  258. res = append(res, v)
  259. resMap[v.ID] = v
  260. }
  261. return
  262. }
  263. // Vstickers fn
  264. func (d *Dao) Vstickers(c context.Context) (res []*music.VSticker, resMap map[int64]*music.VSticker, err error) {
  265. res = make([]*music.VSticker, 0)
  266. resMap = make(map[int64]*music.VSticker)
  267. rows, err := d.db.Query(c, _VstickersSQL)
  268. if err != nil {
  269. log.Error("mysqlDB.Query error(%v)", err)
  270. return
  271. }
  272. defer rows.Close()
  273. for rows.Next() {
  274. v := &music.VSticker{
  275. Material: music.Material{},
  276. }
  277. if err = rows.Scan(&v.MTime, &v.ID, &v.Name, &v.Rank, &v.Extra, &v.Material.Platform, &v.Material.Build); err != nil {
  278. log.Error("row.Scan error(%v)", err)
  279. return
  280. }
  281. var extra *struct {
  282. Cover string `json:"cover"`
  283. DownloadURL string `json:"download_url"`
  284. TagsStr string `json:"tags"`
  285. }
  286. if err = json.Unmarshal([]byte(v.Extra), &extra); err != nil {
  287. log.Error("json.Unmarshal failed error(%v)", err)
  288. continue
  289. }
  290. v.Cover = extra.Cover
  291. v.DownloadURL = extra.DownloadURL
  292. if len(v.Material.Build) > 0 {
  293. var buildComps []*music.BuildComp
  294. if err = json.Unmarshal([]byte(v.Build), &buildComps); err != nil {
  295. log.Error("json.Unmarshal buildComps failed error(%v)", err)
  296. continue
  297. }
  298. v.Material.BuildComps = buildComps
  299. }
  300. if v.MTime.Time().AddDate(0, 0, 7).Unix() >= time.Now().Unix() {
  301. v.New = 1
  302. }
  303. if len(extra.TagsStr) > 0 {
  304. v.Tags = append(v.Tags, strings.Split(extra.TagsStr, ",")...)
  305. }
  306. log.Info("v info v(%+v)", v)
  307. res = append(res, v)
  308. resMap[v.ID] = v
  309. }
  310. return
  311. }
  312. // Cooperates fn
  313. func (d *Dao) Cooperates(c context.Context) (res []*music.Cooperate, daids []int64, err error) {
  314. res = make([]*music.Cooperate, 0)
  315. rows, err := d.db.Query(c, _CooperatesSQL)
  316. if err != nil {
  317. log.Error("mysqlDB.Query error(%v)", err)
  318. return
  319. }
  320. defer rows.Close()
  321. for rows.Next() {
  322. v := &music.Cooperate{
  323. Material: music.Material{},
  324. }
  325. if err = rows.Scan(&v.MTime, &v.ID, &v.Name, &v.Rank, &v.Extra, &v.Material.Platform, &v.Material.Build); err != nil {
  326. log.Error("row.Scan error(%v)", err)
  327. return
  328. }
  329. var extra *struct {
  330. Cover string `json:"cover"`
  331. MaterialAID int64 `json:"material_aid"`
  332. MaterialCID int64 `json:"material_cid"`
  333. DemoAID int64 `json:"demo_aid"`
  334. DemoCID int64 `json:"demo_cid"`
  335. MissionID int64 `json:"mission_id"`
  336. TagsStr string `json:"tags"`
  337. SubType int `json:"sub_type"`
  338. Style int `json:"style"`
  339. DownloadURL string `json:"download_url"`
  340. }
  341. if err = json.Unmarshal([]byte(v.Extra), &extra); err != nil {
  342. log.Error("json.Unmarshal failed error(%v)", err)
  343. continue
  344. }
  345. daids = append(daids, extra.MaterialAID)
  346. v.Cover = extra.Cover
  347. v.MaterialAID = extra.MaterialAID
  348. v.MaterialCID = extra.MaterialCID
  349. v.DemoAID = extra.DemoAID
  350. v.DemoCID = extra.DemoCID
  351. v.MissionID = extra.MissionID
  352. v.SubType = extra.SubType
  353. v.Style = extra.Style
  354. v.DownloadURL = extra.DownloadURL
  355. if len(v.Material.Build) > 0 {
  356. var buildComps []*music.BuildComp
  357. if err = json.Unmarshal([]byte(v.Build), &buildComps); err != nil {
  358. log.Error("json.Unmarshal buildComps failed error(%v)", err)
  359. continue
  360. }
  361. v.Material.BuildComps = buildComps
  362. }
  363. if v.MTime.Time().AddDate(0, 0, 7).Unix() >= time.Now().Unix() {
  364. v.New = 1
  365. }
  366. if len(extra.TagsStr) > 0 {
  367. v.Tags = append(v.Tags, strings.Split(extra.TagsStr, ",")...)
  368. }
  369. log.Info("v info v(%+v)", v)
  370. res = append(res, v)
  371. }
  372. return
  373. }