sear_inter.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package http
  2. import (
  3. "go-common/library/cache/memcache"
  4. "strings"
  5. "time"
  6. "go-common/app/admin/main/tv/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. func searInterList(c *bm.Context) {
  12. var (
  13. req = c.Request.Form
  14. items []*model.SearInter
  15. total int
  16. pn = atoi(req.Get("pn"))
  17. ps = atoi(req.Get("ps"))
  18. pubs *model.PublishStatus
  19. err error
  20. )
  21. if pn == 0 {
  22. pn = 1
  23. }
  24. if ps == 0 {
  25. ps = 20
  26. }
  27. if items, total, err = tvSrv.GetSearInterList(c, pn, ps); err != nil {
  28. log.Error("tvSrv.searInterList error(%v)", err)
  29. c.JSON(nil, err)
  30. return
  31. }
  32. //rank
  33. for i := 0; i < len(items); i++ {
  34. items[i].Rank = int64(i) + 1
  35. }
  36. if pubs, err = tvSrv.GetPublishState(c); err != nil {
  37. if err == memcache.ErrNotFound {
  38. nowTime := time.Now()
  39. t := nowTime.Format("2006-01-02 15:04:05")
  40. pubs = &model.PublishStatus{
  41. Time: t,
  42. State: 0,
  43. }
  44. } else {
  45. log.Error("tvSrv.searInterList GetHotPubState error(%v)", err)
  46. c.JSON("MC获取发布状态报错", ecode.RequestErr)
  47. return
  48. }
  49. }
  50. pager := &model.SearInterPager{
  51. TotalCount: total,
  52. Pn: pn,
  53. Ps: ps,
  54. Items: items,
  55. PubState: pubs.State,
  56. PubTime: pubs.Time,
  57. }
  58. c.JSON(pager, nil)
  59. }
  60. func searInterAdd(c *bm.Context) {
  61. var (
  62. req = c.Request.PostForm
  63. searchword = req.Get("searchword")
  64. err error
  65. count int64
  66. item model.SearInter
  67. pubs *model.PublishStatus
  68. )
  69. if err = tvSrv.DB.Model(&model.SearInter{}).Where("deleted!=?", _isDeleted).Count(&count).Error; err != nil {
  70. log.Error("tvSrv.searInterAdd err(%v)", err)
  71. c.JSON(nil, err)
  72. return
  73. }
  74. if count >= 20 {
  75. c.JSON("热词数最多只能添加20条数据", ecode.RequestErr)
  76. return
  77. }
  78. if searchword == "" {
  79. c.JSON("searchword can not null", ecode.RequestErr)
  80. return
  81. }
  82. exist := &model.SearInter{}
  83. if err = tvSrv.DB.Where("searchword=?", searchword).Where("deleted!=?", _isDeleted).First(exist).Error; err != nil && err != ecode.NothingFound {
  84. log.Error("tvSrv.searInterAdd error(%v)", err)
  85. c.JSON("查找搜索词Mysql报错", ecode.RequestErr)
  86. return
  87. }
  88. if exist.ID != 0 {
  89. log.Error("searchword is existed, error(%v)", err)
  90. c.JSON("搜索词已经存在", ecode.RequestErr)
  91. return
  92. }
  93. if item, err = tvSrv.GetMaxRank(c); err != nil && err != ecode.NothingFound {
  94. log.Error("tvSrv.searInterAdd GetMaxRank error(%v)", err)
  95. c.JSON("查找最大的排序报错", ecode.RequestErr)
  96. return
  97. }
  98. //default rank is last
  99. rank := item.Rank + 1
  100. searchInter := &model.SearInter{
  101. Searchword: searchword,
  102. Rank: rank,
  103. }
  104. if err = tvSrv.AddSearInter(c, searchInter); err != nil {
  105. log.Error("tvSrv.searInterAdd error(%v)", err)
  106. c.JSON("添加搜索词报错", ecode.RequestErr)
  107. return
  108. }
  109. //get publish state
  110. if pubs, err = tvSrv.GetPublishState(c); err != nil {
  111. if err == memcache.ErrNotFound {
  112. nowTime := time.Now()
  113. t := nowTime.Format("2006-01-02 15:04:05")
  114. pubs = &model.PublishStatus{
  115. Time: t,
  116. State: 0,
  117. }
  118. } else {
  119. log.Error("tvSrv.searInterList GetHotPubState error(%v)", err)
  120. c.JSON("MC获取发布状态报错", ecode.RequestErr)
  121. return
  122. }
  123. }
  124. //set publish state
  125. s := &model.PublishStatus{
  126. Time: pubs.Time,
  127. State: 0,
  128. }
  129. if err = tvSrv.SetPublishState(c, s); err != nil {
  130. log.Error("tvSrv.SetPubStat error(%v)", err)
  131. c.JSON("设置发布状态到MC中报错", ecode.RequestErr)
  132. return
  133. }
  134. c.JSON(nil, nil)
  135. }
  136. func searInterEdit(c *bm.Context) {
  137. var (
  138. req = c.Request.PostForm
  139. id = parseInt(req.Get("id"))
  140. searchword = req.Get("searchword")
  141. err error
  142. pubs *model.PublishStatus
  143. )
  144. if req.Get("id") == "" {
  145. c.JSON("id can no null", ecode.RequestErr)
  146. return
  147. }
  148. if req.Get("searchword") == "" {
  149. c.JSON("searchword can no null", ecode.RequestErr)
  150. return
  151. }
  152. exist := &model.SearInter{}
  153. if err = tvSrv.DB.Where("id=?", id).Where("deleted!=?", _isDeleted).First(exist).Error; err != nil {
  154. log.Error("tvSrv.searInterEdit error(%v)", err)
  155. c.JSON("can not find value", err)
  156. return
  157. }
  158. exist = &model.SearInter{}
  159. if err = tvSrv.DB.Where("searchword=?", searchword).Where("deleted!=?", _isDeleted).First(exist).Error; err != nil && err != ecode.NothingFound {
  160. log.Error("tvSrv.searInterEdit error(%v)", err)
  161. c.JSON(err, ecode.RequestErr)
  162. return
  163. }
  164. if exist.ID != 0 && exist.ID != id {
  165. c.JSON("searchword existed", nil)
  166. return
  167. }
  168. if err = tvSrv.UpdateSearInter(c, id, searchword); err != nil {
  169. log.Error("tvSrv.searInterEdit err(%v)", err)
  170. c.JSON(nil, err)
  171. return
  172. }
  173. //get publish state
  174. if pubs, err = tvSrv.GetPublishState(c); err != nil {
  175. log.Error("tvSrv.searInterEdit GetHotPubState error(%v)", err)
  176. c.JSON(nil, err)
  177. return
  178. }
  179. //set publish state
  180. s := &model.PublishStatus{
  181. Time: pubs.Time,
  182. State: 0,
  183. }
  184. if err = tvSrv.SetPublishState(c, s); err != nil {
  185. log.Error("tvSrv.searInterEdit SetPubStat error(%v)", err)
  186. c.JSON(err, ecode.RequestErr)
  187. return
  188. }
  189. c.JSON(nil, nil)
  190. }
  191. func searInterDel(c *bm.Context) {
  192. var (
  193. req = c.Request.PostForm
  194. id = parseInt(req.Get("id"))
  195. err error
  196. pubs *model.PublishStatus
  197. )
  198. if req.Get("id") == "" {
  199. c.JSON("id can not null", err)
  200. return
  201. }
  202. exist := &model.SearInter{}
  203. if err = tvSrv.DB.Where("id=?", id).Where("deleted!=?", _isDeleted).First(exist).Error; err != nil {
  204. c.JSON("can not find value", err)
  205. return
  206. }
  207. if err = tvSrv.DelSearInter(c, id); err != nil {
  208. log.Error("tvSrv.searInterDel err(%v)", err)
  209. c.JSON(nil, err)
  210. return
  211. }
  212. //get publish state
  213. if pubs, err = tvSrv.GetPublishState(c); err != nil {
  214. log.Error("tvSrv.searInterDel GetHotPubState error(%v)", err)
  215. c.JSON(nil, err)
  216. return
  217. }
  218. //set publish state
  219. s := &model.PublishStatus{
  220. Time: pubs.Time,
  221. State: 0,
  222. }
  223. if err = tvSrv.SetPublishState(c, s); err != nil {
  224. log.Error("tvSrv.searInterDel error(%v)", err)
  225. c.JSON(err, ecode.RequestErr)
  226. return
  227. }
  228. c.JSON(nil, nil)
  229. }
  230. func searInterRank(c *bm.Context) {
  231. var (
  232. req = c.Request.PostForm
  233. ids = req.Get("ids")
  234. err error
  235. pubs *model.PublishStatus
  236. total int
  237. )
  238. if ids == "" {
  239. c.JSON("不能发布空数据", ecode.RequestErr)
  240. return
  241. }
  242. idsArr := strings.Split(ids, ",")
  243. if len(idsArr) <= 0 {
  244. c.JSON("不能发布空数据", ecode.RequestErr)
  245. return
  246. }
  247. if total, err = tvSrv.GetSearInterCount(c); err != nil {
  248. log.Error("tvSrv.GetSearInterCount err ", err)
  249. c.JSON("更新排序失败,GetSearInterCount error ", err)
  250. return
  251. }
  252. if len((idsArr)) != total {
  253. c.JSON("请提交全部数据", ecode.RequestErr)
  254. return
  255. }
  256. if err = tvSrv.RankSearInter(c, idsArr); err != nil {
  257. log.Error("tvSrv.searInterRank err(%v),idsArr(%v)", err, idsArr)
  258. c.JSON("更新排序失败, RankSearIntererror error ", err)
  259. return
  260. }
  261. //get publish state
  262. if pubs, err = tvSrv.GetPublishState(c); err != nil {
  263. log.Error("tvSrv.searInterDel GetHotPubState error(%v)", err)
  264. c.JSON(nil, err)
  265. return
  266. }
  267. //set publish state
  268. s := &model.PublishStatus{
  269. Time: pubs.Time,
  270. State: 0,
  271. }
  272. if err = tvSrv.SetPublishState(c, s); err != nil {
  273. log.Error("tvSrv.searInterDel error(%v)", err)
  274. c.JSON(err, ecode.RequestErr)
  275. return
  276. }
  277. c.JSON(nil, nil)
  278. }
  279. func searInterPublish(c *bm.Context) {
  280. var (
  281. items []*model.SearInter
  282. err error
  283. )
  284. if items, err = tvSrv.GetSearInterPublish(c); err != nil {
  285. log.Error("tvSrv.searInterPublish GetSearInterPublish error(%v)", err)
  286. c.JSON(nil, ecode.RequestErr)
  287. return
  288. }
  289. if len(items) == 0 {
  290. c.JSON("不能发布空数据", ecode.RequestErr)
  291. return
  292. }
  293. var rank []*model.OutSearchInter
  294. for _, v := range items {
  295. out := &model.OutSearchInter{
  296. Keyword: v.Searchword,
  297. Status: v.Tag,
  298. }
  299. rank = append(rank, out)
  300. }
  301. if err = tvSrv.SetSearInterRank(c, rank); err != nil {
  302. log.Error("tvSrv.searInterPublish SearInterRank error(%v)", err)
  303. c.JSON(nil, err)
  304. return
  305. }
  306. t := time.Now().Format("2006-01-02 15:04:05")
  307. s := &model.PublishStatus{
  308. Time: t,
  309. State: 1,
  310. }
  311. if err = tvSrv.SetPublishState(c, s); err != nil {
  312. log.Error("tvSrv.searInterPublish SetPubStat error(%v)", err)
  313. c.JSON(err, ecode.RequestErr)
  314. return
  315. }
  316. c.JSON(nil, nil)
  317. }
  318. func searInterPubList(c *bm.Context) {
  319. var (
  320. items []*model.OutSearchInter
  321. err error
  322. )
  323. if items, err = tvSrv.GetSearInterRank(c); err != nil {
  324. log.Error("tvSrv.searInterListOut error(%v)", err)
  325. c.JSON(nil, err)
  326. return
  327. }
  328. c.JSON(items, nil)
  329. }