123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- package material
- import (
- "context"
- "encoding/json"
- "fmt"
- appMdl "go-common/app/interface/main/creative/model/app"
- "go-common/app/interface/main/creative/model/music"
- "go-common/library/log"
- "go-common/library/xstr"
- "strings"
- "time"
- )
- const (
- _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"
- _FiltersSQL = "SELECT mtime,id,name,rank,extra,platform,build FROM material WHERE type=2 and state = 0 order by rank asc "
- _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"
- _VstickersSQL = "SELECT mtime,id,name,rank,extra,platform,build FROM material WHERE type=7 and state = 0 order by rank asc "
- _CooperatesSQL = "SELECT mtime,id,name,rank,extra,platform,build FROM material WHERE type=9 and state = 0 order by rank asc "
- )
- var basicTypes = []int64{
- int64(appMdl.TypeSubtitle),
- int64(appMdl.TypeFont),
- int64(appMdl.TypeHotWord),
- int64(appMdl.TypeSticker),
- int64(appMdl.TypeIntro),
- int64(appMdl.TypeTransition),
- int64(appMdl.TypeTheme),
- }
- // Basic fn
- func (d *Dao) Basic(c context.Context) (basicMap map[string]interface{}, err error) {
- rows, err := d.db.Query(c, fmt.Sprintf(_basicSQL, xstr.JoinInts(basicTypes)))
- if err != nil {
- log.Error("mysqlDB.Query error(%v)", err)
- return
- }
- defer rows.Close()
- basicMap = make(map[string]interface{})
- var (
- subs = make([]*music.Subtitle, 0)
- fons = make([]*music.Font, 0)
- hots = make([]*music.Hotword, 0)
- stis = make([]*music.Sticker, 0)
- ints = make([]*music.Intro, 0)
- trans = make([]*music.Transition, 0)
- themes = make([]*music.Theme, 0)
- )
- basicMap["subs"] = subs
- basicMap["fons"] = fons
- basicMap["hots"] = hots
- basicMap["stis"] = stis
- basicMap["ints"] = ints
- basicMap["trans"] = trans
- basicMap["themes"] = themes
- for rows.Next() {
- b := &music.Basic{
- Material: music.Material{},
- }
- 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 {
- log.Error("row.Scan error(%v)", err)
- return
- }
- var extra *struct {
- Cover string `json:"cover"`
- DownloadURL string `json:"download_url"`
- Max int `json:"max"`
- SubType int64 `json:"sub_type"`
- Tip string `json:"tip"`
- White int8 `json:"white_list"`
- TagsStr string `json:"tags"`
- }
- if err = json.Unmarshal([]byte(b.Extra), &extra); err != nil {
- log.Error("json.Unmarshal extra failed error(%v)", err)
- continue
- }
- b.Cover = extra.Cover
- b.DownloadURL = extra.DownloadURL
- b.Max = extra.Max
- b.White = extra.White
- if len(b.Material.Build) > 0 {
- var buildComps []*music.BuildComp
- if err = json.Unmarshal([]byte(b.Build), &buildComps); err != nil {
- log.Error("json.Unmarshal buildComps failed error(%v)", err)
- continue
- }
- b.Material.BuildComps = buildComps
- }
- if b.MTime.Time().AddDate(0, 0, 7).Unix() >= time.Now().Unix() {
- b.New = 1
- }
- if len(extra.TagsStr) > 0 {
- b.Tags = append(b.Tags, strings.Split(extra.TagsStr, ",")...)
- }
- log.Info("b info v(%+v)", b)
- switch b.Material.Type {
- case appMdl.TypeSubtitle:
- subs = append(subs, &music.Subtitle{
- ID: b.ID,
- Name: b.Name,
- Cover: b.Cover,
- DownloadURL: b.DownloadURL,
- Rank: b.Rank,
- Max: b.Max,
- Material: b.Material,
- New: b.New,
- Tags: b.Tags,
- MTime: b.MTime,
- })
- case appMdl.TypeFont:
- fons = append(fons, &music.Font{
- ID: b.ID,
- Name: b.Name,
- Cover: b.Cover,
- DownloadURL: b.DownloadURL,
- Rank: b.Rank,
- Material: b.Material,
- New: b.New,
- Tags: b.Tags,
- MTime: b.MTime,
- })
- case appMdl.TypeHotWord:
- hots = append(hots, &music.Hotword{
- ID: b.ID,
- Name: b.Name,
- Cover: b.Cover,
- DownloadURL: b.DownloadURL,
- Rank: b.Rank,
- Material: b.Material,
- New: b.New,
- Tags: b.Tags,
- MTime: b.MTime,
- })
- case appMdl.TypeSticker:
- stis = append(stis, &music.Sticker{
- ID: b.ID,
- Name: b.Name,
- Cover: b.Cover,
- DownloadURL: b.DownloadURL,
- Rank: b.Rank,
- Material: b.Material,
- SubType: extra.SubType,
- Tip: extra.Tip,
- New: b.New,
- Tags: b.Tags,
- MTime: b.MTime,
- })
- case appMdl.TypeTheme:
- themes = append(themes, &music.Theme{
- ID: b.ID,
- Name: b.Name,
- Cover: b.Cover,
- DownloadURL: b.DownloadURL,
- Rank: b.Rank,
- Material: b.Material,
- New: b.New,
- Tags: b.Tags,
- MTime: b.MTime,
- })
- case appMdl.TypeIntro:
- ints = append(ints, &music.Intro{
- ID: b.ID,
- Name: b.Name,
- Cover: b.Cover,
- DownloadURL: b.DownloadURL,
- Rank: b.Rank,
- Material: b.Material,
- New: b.New,
- Tags: b.Tags,
- MTime: b.MTime,
- })
- case appMdl.TypeTransition:
- trans = append(trans, &music.Transition{
- ID: b.ID,
- Name: b.Name,
- Cover: b.Cover,
- DownloadURL: b.DownloadURL,
- Rank: b.Rank,
- Material: b.Material,
- New: b.New,
- Tags: b.Tags,
- MTime: b.MTime,
- })
- }
- }
- basicMap["subs"] = subs
- basicMap["fons"] = fons
- basicMap["hots"] = hots
- basicMap["stis"] = stis
- basicMap["ints"] = ints
- basicMap["trans"] = trans
- basicMap["themes"] = themes
- return
- }
- // CategoryBind fn
- func (d *Dao) CategoryBind(c context.Context, tp int8) (res []*music.MaterialBind, err error) {
- res = make([]*music.MaterialBind, 0)
- rows, err := d.db.Query(c, fmt.Sprintf(_CategoryBindSQL, tp))
- if err != nil {
- log.Error("mysqlDB.Query error(%v)", err)
- return
- }
- defer rows.Close()
- for rows.Next() {
- v := &music.MaterialBind{}
- if err = rows.Scan(&v.New, &v.Tp, &v.CName, &v.CRank, &v.BRank, &v.MID, &v.CID); err != nil {
- log.Error("row.Scan error(%v)", err)
- return
- }
- res = append(res, v)
- }
- return
- }
- // Filters fn
- func (d *Dao) Filters(c context.Context) (res []*music.Filter, resMap map[int64]*music.Filter, err error) {
- res = make([]*music.Filter, 0)
- resMap = make(map[int64]*music.Filter)
- rows, err := d.db.Query(c, _FiltersSQL)
- if err != nil {
- log.Error("mysqlDB.Query error(%v)", err)
- return
- }
- defer rows.Close()
- for rows.Next() {
- v := &music.Filter{
- Material: music.Material{},
- }
- if err = rows.Scan(&v.MTime, &v.ID, &v.Name, &v.Rank, &v.Extra, &v.Material.Platform, &v.Material.Build); err != nil {
- log.Error("row.Scan error(%v)", err)
- return
- }
- var extra *struct {
- Cover string `json:"cover"`
- DownloadURL string `json:"download_url"`
- TagsStr string `json:"tags"`
- FilterType int8 `json:"filter_type"`
- }
- if err = json.Unmarshal([]byte(v.Extra), &extra); err != nil {
- log.Error("json.Unmarshal failed error(%v)", err)
- continue
- }
- v.Cover = extra.Cover
- v.DownloadURL = extra.DownloadURL
- v.FilterType = extra.FilterType
- if len(v.Material.Build) > 0 {
- var buildComps []*music.BuildComp
- if err = json.Unmarshal([]byte(v.Build), &buildComps); err != nil {
- log.Error("json.Unmarshal buildComps failed error(%v)", err)
- continue
- }
- v.Material.BuildComps = buildComps
- }
- if v.MTime.Time().AddDate(0, 0, 7).Unix() >= time.Now().Unix() {
- v.New = 1
- }
- if len(extra.TagsStr) > 0 {
- v.Tags = append(v.Tags, strings.Split(extra.TagsStr, ",")...)
- }
- log.Info("v info v(%+v)", v)
- res = append(res, v)
- resMap[v.ID] = v
- }
- return
- }
- // Vstickers fn
- func (d *Dao) Vstickers(c context.Context) (res []*music.VSticker, resMap map[int64]*music.VSticker, err error) {
- res = make([]*music.VSticker, 0)
- resMap = make(map[int64]*music.VSticker)
- rows, err := d.db.Query(c, _VstickersSQL)
- if err != nil {
- log.Error("mysqlDB.Query error(%v)", err)
- return
- }
- defer rows.Close()
- for rows.Next() {
- v := &music.VSticker{
- Material: music.Material{},
- }
- if err = rows.Scan(&v.MTime, &v.ID, &v.Name, &v.Rank, &v.Extra, &v.Material.Platform, &v.Material.Build); err != nil {
- log.Error("row.Scan error(%v)", err)
- return
- }
- var extra *struct {
- Cover string `json:"cover"`
- DownloadURL string `json:"download_url"`
- TagsStr string `json:"tags"`
- }
- if err = json.Unmarshal([]byte(v.Extra), &extra); err != nil {
- log.Error("json.Unmarshal failed error(%v)", err)
- continue
- }
- v.Cover = extra.Cover
- v.DownloadURL = extra.DownloadURL
- if len(v.Material.Build) > 0 {
- var buildComps []*music.BuildComp
- if err = json.Unmarshal([]byte(v.Build), &buildComps); err != nil {
- log.Error("json.Unmarshal buildComps failed error(%v)", err)
- continue
- }
- v.Material.BuildComps = buildComps
- }
- if v.MTime.Time().AddDate(0, 0, 7).Unix() >= time.Now().Unix() {
- v.New = 1
- }
- if len(extra.TagsStr) > 0 {
- v.Tags = append(v.Tags, strings.Split(extra.TagsStr, ",")...)
- }
- log.Info("v info v(%+v)", v)
- res = append(res, v)
- resMap[v.ID] = v
- }
- return
- }
- // Cooperates fn
- func (d *Dao) Cooperates(c context.Context) (res []*music.Cooperate, daids []int64, err error) {
- res = make([]*music.Cooperate, 0)
- rows, err := d.db.Query(c, _CooperatesSQL)
- if err != nil {
- log.Error("mysqlDB.Query error(%v)", err)
- return
- }
- defer rows.Close()
- for rows.Next() {
- v := &music.Cooperate{
- Material: music.Material{},
- }
- if err = rows.Scan(&v.MTime, &v.ID, &v.Name, &v.Rank, &v.Extra, &v.Material.Platform, &v.Material.Build); err != nil {
- log.Error("row.Scan error(%v)", err)
- return
- }
- var extra *struct {
- Cover string `json:"cover"`
- MaterialAID int64 `json:"material_aid"`
- MaterialCID int64 `json:"material_cid"`
- DemoAID int64 `json:"demo_aid"`
- DemoCID int64 `json:"demo_cid"`
- MissionID int64 `json:"mission_id"`
- TagsStr string `json:"tags"`
- SubType int `json:"sub_type"`
- Style int `json:"style"`
- DownloadURL string `json:"download_url"`
- }
- if err = json.Unmarshal([]byte(v.Extra), &extra); err != nil {
- log.Error("json.Unmarshal failed error(%v)", err)
- continue
- }
- daids = append(daids, extra.MaterialAID)
- v.Cover = extra.Cover
- v.MaterialAID = extra.MaterialAID
- v.MaterialCID = extra.MaterialCID
- v.DemoAID = extra.DemoAID
- v.DemoCID = extra.DemoCID
- v.MissionID = extra.MissionID
- v.SubType = extra.SubType
- v.Style = extra.Style
- v.DownloadURL = extra.DownloadURL
- if len(v.Material.Build) > 0 {
- var buildComps []*music.BuildComp
- if err = json.Unmarshal([]byte(v.Build), &buildComps); err != nil {
- log.Error("json.Unmarshal buildComps failed error(%v)", err)
- continue
- }
- v.Material.BuildComps = buildComps
- }
- if v.MTime.Time().AddDate(0, 0, 7).Unix() >= time.Now().Unix() {
- v.New = 1
- }
- if len(extra.TagsStr) > 0 {
- v.Tags = append(v.Tags, strings.Split(extra.TagsStr, ",")...)
- }
- log.Info("v info v(%+v)", v)
- res = append(res, v)
- }
- return
- }
|