123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- package service
- import (
- "context"
- "go-common/app/admin/main/tv/model"
- accmdl "go-common/app/service/main/account/api"
- account "go-common/app/service/main/account/model"
- "go-common/library/ecode"
- "go-common/library/log"
- "github.com/jinzhu/gorm"
- )
- const (
- _removeArcs = 2
- _deleted = 1
- _toinit = 1
- _requestedInit = 2
- _onlineAct = "1"
- _offlineAct = "0"
- _validOnline = 1
- _validOffline = 0
- )
- // AddMids CheckMids checks the mids, whether all the uppers exist
- func (s *Service) AddMids(mids []int64) (res *model.AddResp, err error) {
- var (
- accsReply *accmdl.InfosReply
- accsInfo map[int64]*account.Info
- midExist bool
- )
- // init the response
- res = &model.AddResp{
- Succ: []int64{},
- Exist: []int64{},
- Invalids: []int64{},
- }
- // rpc get all the mids' info
- if accsReply, err = s.accClient.Infos3(ctx, &accmdl.MidsReq{
- Mids: mids,
- }); err != nil {
- log.Error("CheckMids Mids: %v, Error: %v", mids, err)
- return
- }
- accsInfo = accsReply.Infos
- for _, v := range mids {
- // if invalid account
- if _, ok := accsInfo[v]; !ok {
- res.Invalids = append(res.Invalids, v)
- continue
- }
- if midExist, err = s.existMid(v); err != nil {
- log.Error("AddMid %d, Error %v", v, err)
- return
- }
- // if the account is existing in our DB
- if midExist {
- res.Exist = append(res.Exist, v)
- continue
- }
- // add the upper
- if err = s.dao.UpAdd(v); err != nil {
- log.Error("AddMid %d, Error %v", v, err)
- return
- }
- res.Succ = append(res.Succ, v)
- }
- return
- }
- // existMid checks whether the mid is existing in our DB
- func (s *Service) existMid(mid int64) (exist bool, err error) {
- if err = s.DB.Where("deleted = 0").Where("mid = ?", mid).First(&model.Upper{}).Error; err != nil {
- if err == gorm.ErrRecordNotFound {
- return false, nil
- }
- log.Error("existMid %d, Error %v", mid, err)
- return
- }
- return true, nil
- }
- // ImportMids is for updating the data to tell the tv-job to import the uppers' all videos
- func (s *Service) ImportMids(mids []int64) (res *model.ImportResp, err error) {
- var midExist bool
- res = &model.ImportResp{
- NotExist: []int64{},
- Succ: []int64{},
- }
- for _, v := range mids {
- if midExist, err = s.existMid(v); err != nil {
- log.Error("ImportMids %d, Error %v", v, err)
- return
- }
- if !midExist {
- res.NotExist = append(res.NotExist, v)
- continue
- }
- if err = s.DB.Model(&model.Upper{}).Where("mid = ?", v).Update(map[string]int{"toinit": _toinit, "state": _requestedInit}).Error; err != nil {
- log.Error("ImportMids %d, Error %v", v, err)
- return
- }
- res.Succ = append(res.Succ, v)
- }
- return
- }
- // DelMid is for updating remove one upper from the list
- func (s *Service) DelMid(mid int64) (err error) {
- var exist bool
- if exist, err = s.existMid(mid); err != nil {
- return
- }
- if !exist {
- return ecode.TvUpperNotInList
- }
- if err = s.DB.Model(&model.Upper{}).Where("mid = ?", mid).Update(map[string]int{"deleted": _deleted, "toinit": _removeArcs}).Error; err != nil {
- log.Error("DelMid %d, Error %v", mid, err)
- }
- return
- }
- // UpList shows the upper list
- func (s *Service) UpList(order int, page int, name string, id int) (pager *model.UpperPager, err error) {
- var (
- source []*model.Upper
- mids []int64
- match map[int64]*account.Info
- info *account.Info
- ok bool
- namesReply, infosReply *accmdl.InfosReply
- nameRes map[int64]*account.Info
- ids []int64
- )
- pager = &model.UpperPager{}
- // id treatmnet
- if id != 0 {
- ids = append(ids, int64(id))
- }
- // name treatment
- if name != "" {
- if namesReply, err = s.accClient.InfosByName3(ctx, &accmdl.NamesReq{
- Names: []string{name},
- }); err != nil {
- log.Error("accRPC InfosByName3 %s, Err %v", name, err)
- return
- }
- nameRes = namesReply.Infos
- for k := range nameRes {
- ids = append(ids, k)
- }
- }
- if source, pager.Page, err = s.dao.UpList(order, page, ids); err != nil {
- return
- }
- // pick upper's name from AccRPC
- for _, v := range source {
- mids = append(mids, v.MID)
- }
- if infosReply, err = s.accClient.Infos3(ctx, &accmdl.MidsReq{
- Mids: mids,
- }); err != nil {
- log.Error("accRPC Infos3, %v, Error %v", mids, err)
- return
- }
- match = infosReply.Infos
- for _, v := range source { // arrange the data to output
- if info, ok = match[v.MID]; !ok {
- log.Error("Mid %v, AccRPC info is nil", v.MID)
- continue
- }
- pager.Items = append(pager.Items, &model.UpperR{
- MID: v.MID,
- State: v.State,
- Name: info.Name,
- Ctime: s.TimeFormat(v.Ctime),
- Mtime: s.TimeFormat(v.Mtime),
- })
- }
- return
- }
- // CmsList is the upper cms list service function
- func (s *Service) CmsList(ctx context.Context, req *model.ReqUpCms) (pager *model.CmsUpperPager, err error) {
- ups, page, errList := s.dao.UpCmsList(req)
- if errList != nil {
- return nil, errList
- }
- for _, v := range ups {
- v.MtimeStr = v.Mtime.Time().Format("2006-01-02 15:04:05")
- }
- pager = &model.CmsUpperPager{
- Items: ups,
- Page: page,
- }
- return
- }
- // CmsAudit updates the mids' valid status
- func (s *Service) CmsAudit(ctx context.Context, mids []int64, action string) (resp *model.RespUpAudit, err error) {
- var (
- okMids map[int64]*model.UpMC
- validAct int
- )
- resp = &model.RespUpAudit{
- Succ: mids,
- }
- if okMids, err = s.dao.VerifyIds(mids); err != nil {
- return
- }
- if len(okMids) != len(mids) {
- succ := []int64{}
- for _, v := range mids {
- if _, ok := okMids[v]; !ok {
- resp.Invalid = append(resp.Invalid, v)
- } else {
- succ = append(succ, v)
- }
- }
- resp.Succ = succ
- }
- if action == _onlineAct {
- validAct = _validOnline
- } else if action == _offlineAct {
- validAct = _validOffline
- }
- if err = s.dao.AuditIds(resp.Succ, validAct); err != nil {
- return
- }
- for _, v := range resp.Succ {
- s.dao.DelCache(ctx, v) // delete cache from MC because their status has been updated
- }
- return
- }
- // CmsEdit updates the upper's info in both DB and cache
- func (s *Service) CmsEdit(ctx context.Context, req *model.ReqUpEdit) (err error) {
- var (
- okMids map[int64]*model.UpMC
- upInfo *model.UpMC
- ok bool
- )
- if okMids, err = s.dao.VerifyIds([]int64{req.MID}); err != nil {
- return
- }
- if len(okMids) == 0 {
- return ecode.TvUpperNotInList
- }
- if upInfo, ok = okMids[req.MID]; !ok {
- log.Error("okMids %v, Mid %d, not found", okMids, req.MID)
- return ecode.NothingFound
- }
- if err = s.dao.SetUpper(req); err != nil { // update upper cms info in DB
- return
- }
- upInfo.CMSFace = req.Face
- upInfo.CMSName = req.Name
- err = s.dao.SetUpMetaCache(ctx, upInfo) // update Cache
- return
- }
|