123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- package http
- import (
- "go-common/library/cache/memcache"
- "strings"
- "time"
- "go-common/app/admin/main/tv/model"
- "go-common/library/ecode"
- "go-common/library/log"
- bm "go-common/library/net/http/blademaster"
- )
- func searInterList(c *bm.Context) {
- var (
- req = c.Request.Form
- items []*model.SearInter
- total int
- pn = atoi(req.Get("pn"))
- ps = atoi(req.Get("ps"))
- pubs *model.PublishStatus
- err error
- )
- if pn == 0 {
- pn = 1
- }
- if ps == 0 {
- ps = 20
- }
- if items, total, err = tvSrv.GetSearInterList(c, pn, ps); err != nil {
- log.Error("tvSrv.searInterList error(%v)", err)
- c.JSON(nil, err)
- return
- }
- //rank
- for i := 0; i < len(items); i++ {
- items[i].Rank = int64(i) + 1
- }
- if pubs, err = tvSrv.GetPublishState(c); err != nil {
- if err == memcache.ErrNotFound {
- nowTime := time.Now()
- t := nowTime.Format("2006-01-02 15:04:05")
- pubs = &model.PublishStatus{
- Time: t,
- State: 0,
- }
- } else {
- log.Error("tvSrv.searInterList GetHotPubState error(%v)", err)
- c.JSON("MC获取发布状态报错", ecode.RequestErr)
- return
- }
- }
- pager := &model.SearInterPager{
- TotalCount: total,
- Pn: pn,
- Ps: ps,
- Items: items,
- PubState: pubs.State,
- PubTime: pubs.Time,
- }
- c.JSON(pager, nil)
- }
- func searInterAdd(c *bm.Context) {
- var (
- req = c.Request.PostForm
- searchword = req.Get("searchword")
- err error
- count int64
- item model.SearInter
- pubs *model.PublishStatus
- )
- if err = tvSrv.DB.Model(&model.SearInter{}).Where("deleted!=?", _isDeleted).Count(&count).Error; err != nil {
- log.Error("tvSrv.searInterAdd err(%v)", err)
- c.JSON(nil, err)
- return
- }
- if count >= 20 {
- c.JSON("热词数最多只能添加20条数据", ecode.RequestErr)
- return
- }
- if searchword == "" {
- c.JSON("searchword can not null", ecode.RequestErr)
- return
- }
- exist := &model.SearInter{}
- if err = tvSrv.DB.Where("searchword=?", searchword).Where("deleted!=?", _isDeleted).First(exist).Error; err != nil && err != ecode.NothingFound {
- log.Error("tvSrv.searInterAdd error(%v)", err)
- c.JSON("查找搜索词Mysql报错", ecode.RequestErr)
- return
- }
- if exist.ID != 0 {
- log.Error("searchword is existed, error(%v)", err)
- c.JSON("搜索词已经存在", ecode.RequestErr)
- return
- }
- if item, err = tvSrv.GetMaxRank(c); err != nil && err != ecode.NothingFound {
- log.Error("tvSrv.searInterAdd GetMaxRank error(%v)", err)
- c.JSON("查找最大的排序报错", ecode.RequestErr)
- return
- }
- //default rank is last
- rank := item.Rank + 1
- searchInter := &model.SearInter{
- Searchword: searchword,
- Rank: rank,
- }
- if err = tvSrv.AddSearInter(c, searchInter); err != nil {
- log.Error("tvSrv.searInterAdd error(%v)", err)
- c.JSON("添加搜索词报错", ecode.RequestErr)
- return
- }
- //get publish state
- if pubs, err = tvSrv.GetPublishState(c); err != nil {
- if err == memcache.ErrNotFound {
- nowTime := time.Now()
- t := nowTime.Format("2006-01-02 15:04:05")
- pubs = &model.PublishStatus{
- Time: t,
- State: 0,
- }
- } else {
- log.Error("tvSrv.searInterList GetHotPubState error(%v)", err)
- c.JSON("MC获取发布状态报错", ecode.RequestErr)
- return
- }
- }
- //set publish state
- s := &model.PublishStatus{
- Time: pubs.Time,
- State: 0,
- }
- if err = tvSrv.SetPublishState(c, s); err != nil {
- log.Error("tvSrv.SetPubStat error(%v)", err)
- c.JSON("设置发布状态到MC中报错", ecode.RequestErr)
- return
- }
- c.JSON(nil, nil)
- }
- func searInterEdit(c *bm.Context) {
- var (
- req = c.Request.PostForm
- id = parseInt(req.Get("id"))
- searchword = req.Get("searchword")
- err error
- pubs *model.PublishStatus
- )
- if req.Get("id") == "" {
- c.JSON("id can no null", ecode.RequestErr)
- return
- }
- if req.Get("searchword") == "" {
- c.JSON("searchword can no null", ecode.RequestErr)
- return
- }
- exist := &model.SearInter{}
- if err = tvSrv.DB.Where("id=?", id).Where("deleted!=?", _isDeleted).First(exist).Error; err != nil {
- log.Error("tvSrv.searInterEdit error(%v)", err)
- c.JSON("can not find value", err)
- return
- }
- exist = &model.SearInter{}
- if err = tvSrv.DB.Where("searchword=?", searchword).Where("deleted!=?", _isDeleted).First(exist).Error; err != nil && err != ecode.NothingFound {
- log.Error("tvSrv.searInterEdit error(%v)", err)
- c.JSON(err, ecode.RequestErr)
- return
- }
- if exist.ID != 0 && exist.ID != id {
- c.JSON("searchword existed", nil)
- return
- }
- if err = tvSrv.UpdateSearInter(c, id, searchword); err != nil {
- log.Error("tvSrv.searInterEdit err(%v)", err)
- c.JSON(nil, err)
- return
- }
- //get publish state
- if pubs, err = tvSrv.GetPublishState(c); err != nil {
- log.Error("tvSrv.searInterEdit GetHotPubState error(%v)", err)
- c.JSON(nil, err)
- return
- }
- //set publish state
- s := &model.PublishStatus{
- Time: pubs.Time,
- State: 0,
- }
- if err = tvSrv.SetPublishState(c, s); err != nil {
- log.Error("tvSrv.searInterEdit SetPubStat error(%v)", err)
- c.JSON(err, ecode.RequestErr)
- return
- }
- c.JSON(nil, nil)
- }
- func searInterDel(c *bm.Context) {
- var (
- req = c.Request.PostForm
- id = parseInt(req.Get("id"))
- err error
- pubs *model.PublishStatus
- )
- if req.Get("id") == "" {
- c.JSON("id can not null", err)
- return
- }
- exist := &model.SearInter{}
- if err = tvSrv.DB.Where("id=?", id).Where("deleted!=?", _isDeleted).First(exist).Error; err != nil {
- c.JSON("can not find value", err)
- return
- }
- if err = tvSrv.DelSearInter(c, id); err != nil {
- log.Error("tvSrv.searInterDel err(%v)", err)
- c.JSON(nil, err)
- return
- }
- //get publish state
- if pubs, err = tvSrv.GetPublishState(c); err != nil {
- log.Error("tvSrv.searInterDel GetHotPubState error(%v)", err)
- c.JSON(nil, err)
- return
- }
- //set publish state
- s := &model.PublishStatus{
- Time: pubs.Time,
- State: 0,
- }
- if err = tvSrv.SetPublishState(c, s); err != nil {
- log.Error("tvSrv.searInterDel error(%v)", err)
- c.JSON(err, ecode.RequestErr)
- return
- }
- c.JSON(nil, nil)
- }
- func searInterRank(c *bm.Context) {
- var (
- req = c.Request.PostForm
- ids = req.Get("ids")
- err error
- pubs *model.PublishStatus
- total int
- )
- if ids == "" {
- c.JSON("不能发布空数据", ecode.RequestErr)
- return
- }
- idsArr := strings.Split(ids, ",")
- if len(idsArr) <= 0 {
- c.JSON("不能发布空数据", ecode.RequestErr)
- return
- }
- if total, err = tvSrv.GetSearInterCount(c); err != nil {
- log.Error("tvSrv.GetSearInterCount err ", err)
- c.JSON("更新排序失败,GetSearInterCount error ", err)
- return
- }
- if len((idsArr)) != total {
- c.JSON("请提交全部数据", ecode.RequestErr)
- return
- }
- if err = tvSrv.RankSearInter(c, idsArr); err != nil {
- log.Error("tvSrv.searInterRank err(%v),idsArr(%v)", err, idsArr)
- c.JSON("更新排序失败, RankSearIntererror error ", err)
- return
- }
- //get publish state
- if pubs, err = tvSrv.GetPublishState(c); err != nil {
- log.Error("tvSrv.searInterDel GetHotPubState error(%v)", err)
- c.JSON(nil, err)
- return
- }
- //set publish state
- s := &model.PublishStatus{
- Time: pubs.Time,
- State: 0,
- }
- if err = tvSrv.SetPublishState(c, s); err != nil {
- log.Error("tvSrv.searInterDel error(%v)", err)
- c.JSON(err, ecode.RequestErr)
- return
- }
- c.JSON(nil, nil)
- }
- func searInterPublish(c *bm.Context) {
- var (
- items []*model.SearInter
- err error
- )
- if items, err = tvSrv.GetSearInterPublish(c); err != nil {
- log.Error("tvSrv.searInterPublish GetSearInterPublish error(%v)", err)
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if len(items) == 0 {
- c.JSON("不能发布空数据", ecode.RequestErr)
- return
- }
- var rank []*model.OutSearchInter
- for _, v := range items {
- out := &model.OutSearchInter{
- Keyword: v.Searchword,
- Status: v.Tag,
- }
- rank = append(rank, out)
- }
- if err = tvSrv.SetSearInterRank(c, rank); err != nil {
- log.Error("tvSrv.searInterPublish SearInterRank error(%v)", err)
- c.JSON(nil, err)
- return
- }
- t := time.Now().Format("2006-01-02 15:04:05")
- s := &model.PublishStatus{
- Time: t,
- State: 1,
- }
- if err = tvSrv.SetPublishState(c, s); err != nil {
- log.Error("tvSrv.searInterPublish SetPubStat error(%v)", err)
- c.JSON(err, ecode.RequestErr)
- return
- }
- c.JSON(nil, nil)
- }
- func searInterPubList(c *bm.Context) {
- var (
- items []*model.OutSearchInter
- err error
- )
- if items, err = tvSrv.GetSearInterRank(c); err != nil {
- log.Error("tvSrv.searInterListOut error(%v)", err)
- c.JSON(nil, err)
- return
- }
- c.JSON(items, nil)
- }
|