123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- package service
- import (
- "context"
- "database/sql"
- "fmt"
- "time"
- "go-common/app/admin/main/tv/model"
- "go-common/library/ecode"
- "go-common/library/log"
- "go-common/library/xstr"
- "github.com/jinzhu/gorm"
- )
- // ugcLabels refreshes ugc labels
- func (s *Service) ugcLabels() (err error) {
- var firstTps = s.firstTps()
- if err = s.ugcTpLabel(firstTps); err != nil {
- log.Error("ugcTpLabel Err %v", err)
- return
- }
- if err = s.ugcPubLabel(firstTps); err != nil {
- log.Error("ugcPubLabel Tps %v, Err %v", firstTps, err)
- }
- return
- }
- func (s *Service) ugcPubLabel(firstTps []int32) (err error) {
- var (
- exist bool
- cfg = s.c.Cfg.RefLabel
- )
- for _, v := range firstTps { // check and create pub_time label
- pubtCore := &model.LabelCore{
- CatType: model.UgcLabel,
- Category: v,
- Param: model.ParamUgctime,
- Value: cfg.AllValue,
- Valid: 1,
- ParamName: cfg.UgcTime,
- Name: cfg.AllName,
- }
- if exist, err = s.labelExist(pubtCore); err != nil {
- return
- }
- if !exist {
- if err = s.DB.Create(&model.LabelDB{LabelCore: *pubtCore}).Error; err != nil {
- log.Error("ugcLabels Pubtime All, Create Err %v", err)
- return
- }
- }
- time.Sleep(20 * time.Millisecond)
- }
- return
- }
- func (s *Service) firstTps() (tps []int32) {
- for _, v := range s.ArcTypes {
- if v.Pid == 0 {
- tps = append(tps, v.ID)
- }
- }
- return
- }
- func (s *Service) ugcTpLabel(firstTps []int32) (err error) {
- var (
- exist bool
- cfg = s.c.Cfg.RefLabel
- )
- for _, v := range firstTps {
- extCore := &model.LabelCore{
- CatType: model.UgcLabel,
- Param: model.ParamTypeid,
- Valid: 1,
- Category: v,
- Value: cfg.AllValue,
- ParamName: cfg.UgcType,
- Name: cfg.AllName,
- }
- if exist, err = s.labelExist(extCore); err != nil {
- return
- }
- if !exist {
- if err = s.DB.Create(&model.LabelDB{LabelCore: *extCore}).Error; err != nil {
- log.Error("ugcLabels ArcTypeID %d, Create ALL Label Err %v", v, err)
- return
- }
- }
- }
- for _, v := range s.ArcTypes {
- if v.Pid == 0 { // if first level type, we check the "ALL" label
- continue
- }
- extCore := &model.LabelCore{
- CatType: model.UgcLabel,
- Param: model.ParamTypeid,
- Valid: 1,
- Category: v.Pid,
- Value: fmt.Sprintf("%d", v.ID),
- }
- if exist, err = s.labelExist(extCore); err != nil {
- return
- }
- if !exist {
- label := model.LabelDB{}
- label.FromArcTp(v, s.c.Cfg.RefLabel.UgcType)
- if err = s.DB.Create(&label).Error; err != nil {
- log.Error("ugcLabels ArcTypeID %d, Create Err %v", v.ID, err)
- return
- }
- time.Sleep(20 * time.Millisecond)
- }
- }
- return
- }
- // labelsExist distinguishes whether the ids are existing
- func (s *Service) labelsExist(ids []int64) (err error) {
- var (
- labels []*model.LabelDB
- idmap = make(map[int64]int)
- )
- if err = s.DB.Where(fmt.Sprintf("id IN (%s)", xstr.JoinInts(ids))).Where("deleted = 0").Find(&labels).Error; err != nil {
- log.Error("labelsExist Ids %v, Err %v", ids, err)
- return
- }
- if len(labels) >= len(ids) {
- return
- }
- for _, v := range labels {
- idmap[v.ID] = 1
- }
- for _, v := range ids {
- if _, ok := idmap[v]; !ok {
- log.Warn("labelsExist ids %v, not exist %d", ids, v)
- return ecode.RequestErr
- }
- }
- return
- }
- func (s *Service) labelExist(req *model.LabelCore) (exist bool, err error) {
- var (
- label = model.LabelDB{}
- db = s.DB.Model(label).Where("deleted = 0")
- )
- if req.ID != 0 {
- db = db.Where("id = ?", req.ID)
- }
- if req.Category != 0 {
- db = db.Where("category = ?", req.Category)
- }
- if req.CatType != 0 {
- db = db.Where("cat_type = ?", req.CatType)
- }
- if req.Param != "" {
- db = db.Where("param = ?", req.Param)
- }
- if req.Value != "" {
- db = db.Where("value = ?", req.Value)
- }
- if req.Name != "" {
- db = db.Where("name = ?", req.Name)
- }
- if err = db.First(&label).Error; err != nil {
- if err == gorm.ErrRecordNotFound {
- err = nil
- return
- }
- log.Error("labelExist V %v, Err %v", req, err)
- return
- }
- if label.ID > 0 {
- exist = true
- }
- return
- }
- func (s *Service) pgcLabels() (err error) {
- var (
- result *model.PgcCond
- exist bool
- )
- for _, cat := range s.c.Cfg.SupportCat.PGCTypes {
- if result, err = s.dao.PgcCond(context.Background(), cat); err != nil {
- log.Error("PgcCond Cat %d, Err %v", cat, err)
- return
- }
- for _, cond := range result.Filter {
- if len(cond.Value) == 0 {
- continue
- }
- for _, v := range cond.Value {
- if exist, err = s.labelExist(&model.LabelCore{
- CatType: model.PgcLabel,
- Category: cat,
- Param: cond.ID,
- Value: v.ID,
- }); err != nil {
- return
- }
- if exist {
- continue
- }
- label := model.LabelDB{}
- label.FromPgcCond(v, cond, cat)
- if err = s.DB.Create(&label).Error; err != nil {
- log.Error("pgcLabels Param %s, Cond %v Create Err %v", cond.ID, v, err)
- return
- }
- time.Sleep(20 * time.Millisecond)
- }
- }
- }
- return
- }
- // AddUgcTm adds time label for ugc
- func (s *Service) AddUgcTm(tm *model.UgcTime) (err error) {
- var (
- exist bool
- timeV = tm.TimeV()
- )
- if exist, err = s.labelExist(&model.LabelCore{
- Category: tm.Category,
- Param: model.ParamUgctime,
- Name: tm.Name,
- }); err != nil {
- return
- }
- if exist {
- err = ecode.TvLabelExist
- return
- }
- label := model.LabelDB{}
- label.FromUgcTime(tm, s.c.Cfg.RefLabel.UgcTime)
- if err = s.DB.Create(&label).Error; err != nil {
- log.Error("ugcTimeLabel Time %s, Create Err %v", timeV, err)
- }
- return
- }
- // EditUgcTm edits a time label by name
- func (s *Service) EditUgcTm(tm *model.EditUgcTime) (err error) {
- var exist bool
- if exist, err = s.labelExist(&model.LabelCore{
- ID: tm.ID,
- }); err != nil {
- return
- }
- if !exist {
- err = ecode.NothingFound
- return
- }
- if err = s.DB.Model(&model.LabelDB{}).Where("id = ?", tm.ID).Update(map[string]string{
- "name": tm.Name,
- "value": tm.TimeV(),
- }).Error; err != nil {
- log.Error("ugcTimeLabel LabelID %d, Update Err %v", tm.ID, err)
- }
- return
- }
- // ActLabels act on labels
- func (s *Service) ActLabels(ids []int64, act int) (err error) {
- if err = s.labelsExist(ids); err != nil {
- return
- }
- if err = s.DB.Model(&model.LabelDB{}).Where(fmt.Sprintf("id IN (%s)", xstr.JoinInts(ids))).Update(map[string]int{
- "valid": act,
- }).Error; err != nil {
- log.Error("ActLabels LabelID %s, Update Err %v", ids, err)
- }
- return
- }
- // DelLabels deletes labels
- func (s *Service) DelLabels(ids []int64) (err error) {
- var exist bool
- for _, v := range ids {
- if exist, err = s.labelExist(&model.LabelCore{
- ID: v,
- CatType: model.UgcLabel,
- Param: model.ParamUgctime,
- }); err != nil {
- return
- }
- if !exist {
- log.Warn("DelLabels IDs %v, ID not exist %d", ids, v)
- return ecode.RequestErr
- }
- }
- if err = s.DB.Exec(fmt.Sprintf("UPDATE tv_label SET deleted = 1 WHERE id IN (%s)", xstr.JoinInts(ids))).Error; err != nil {
- log.Error("DelLabels LabelID %s, Update Err %v", ids, err)
- }
- return
- }
- // DynamicLabels picks the defined pgc label types
- func (s *Service) labelTypes() (tps []*model.TpLabel, err error) {
- var rows *sql.Rows
- // select category, param , param_name from tv_label where deleted = 0 and cat_type = 1 group by category,param
- if rows, err = s.DB.Model(&model.LabelDB{}).Where("deleted = 0").
- Where("cat_type = ?", model.PgcLabel).Select("category, param, param_name").Group("category,param").Rows(); err != nil {
- log.Error("labelTypes rows Err %v", err)
- return
- }
- defer rows.Close()
- for rows.Next() {
- var cont = &model.TpLabel{}
- if err = rows.Scan(&cont.Category, &cont.Param, &cont.ParamName); err != nil {
- log.Error("rows.Scan error(%v)", err)
- return
- }
- tps = append(tps, cont)
- }
- if err = rows.Err(); err != nil {
- log.Error("labelTypes rows Err %v", err)
- }
- return
- }
- func (s *Service) loadLabel() (err error) {
- var (
- newTps = make(map[int][]*model.TpLabel)
- labels []*model.TpLabel
- )
- if labels, err = s.labelTypes(); err != nil {
- log.Error("labelTypes err %v", err)
- time.Sleep(time.Duration(10 * time.Second))
- return
- }
- for _, v := range labels {
- if lbs, ok := newTps[v.Category]; ok {
- newTps[v.Category] = append(lbs, v)
- } else {
- newTps[v.Category] = append([]*model.TpLabel{}, v)
- }
- }
- if len(newTps) > 0 {
- s.labelTps = newTps
- }
- return
- }
- // LabelTp returns the category's label types
- func (s *Service) LabelTp(category int) (lbs []*model.TpLabel, err error) {
- var ok bool
- if lbs, ok = s.labelTps[category]; !ok {
- err = ecode.RequestErr
- }
- return
- }
- // PickLabels picks ugc labels
- func (s *Service) PickLabels(req *model.ReqLabel, catType int) (data []*model.LabelList, err error) {
- var (
- db = s.DB.Model(&model.LabelDB{}).
- Where("param = ?", req.Param).
- Where("category = ?", req.Category).
- Where("deleted = 0").
- Where("cat_type = ?", catType)
- labels []*model.LabelDB
- )
- if req.Title != "" {
- db = db.Where("name LIKE ?", "%"+req.Title+"%")
- }
- if req.ID != 0 {
- db = db.Where("id = ?", req.ID)
- }
- if err = db.Order("position ASC").Find(&labels).Error; err != nil {
- log.Error("PickLabels Req %v, Err %v", req, err)
- }
- for _, v := range labels {
- data = append(data, v.ToList())
- }
- return
- }
- // EditLabel edits a pgc label
- func (s *Service) EditLabel(id int64, name string) (err error) {
- var exist bool
- if exist, err = s.labelExist(&model.LabelCore{
- ID: id,
- }); err != nil {
- return
- }
- if !exist {
- err = ecode.NothingFound
- return
- }
- if err = s.DB.Model(&model.LabelDB{}).Where("id = ?", id).Update(map[string]string{
- "name": name,
- }).Error; err != nil {
- log.Error("EditLabel LabelID %d, Update Err %v", id, err)
- }
- return
- }
- // PubLabel publish label's order
- func (s *Service) PubLabel(ids []int64) (err error) {
- if len(ids) == 0 {
- return
- }
- var (
- labels []*model.LabelDB
- position = 1
- tx = s.DB.Begin()
- labelMap = make(map[int64]*model.LabelDB, len(ids))
- )
- if err = s.DB.Where(fmt.Sprintf("id IN (%s)", xstr.JoinInts(ids))).Find(&labels).Error; err != nil {
- log.Error("PubLabel Ids %v, Err %v", ids, err)
- return
- }
- if len(labels) == 0 {
- err = ecode.NothingFound
- return
- }
- for _, v := range labels {
- labelMap[v.ID] = v
- }
- for _, id := range ids {
- lbl, ok := labelMap[id]
- if !ok {
- err = ecode.RequestErr
- log.Warn("PubLabel Id %d Not found", id)
- return
- }
- if !lbl.SameType(labels[0]) {
- log.Error("PubLabel Id %d FirstLabel ID %d, Not same type", lbl.ID, labels[0].ID)
- err = ecode.RequestErr
- tx.Rollback()
- return
- }
- if err = tx.Model(&model.LabelDB{}).Where("id = ?", lbl.ID).Update(map[string]int{"position": position}).Error; err != nil {
- log.Error("PubLabel ID %d, Err %v", lbl.ID, err)
- tx.Rollback()
- return
- }
- position = position + 1
- }
- tx.Commit()
- return
- }
|