search.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. package search
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/feed/conf"
  7. "go-common/app/admin/main/feed/dao/search"
  8. "go-common/app/admin/main/feed/dao/show"
  9. searchModel "go-common/app/admin/main/feed/model/search"
  10. Log "go-common/app/admin/main/feed/util"
  11. "go-common/library/cache/memcache"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "github.com/jinzhu/gorm"
  15. "github.com/robfig/cron"
  16. )
  17. var (
  18. ctx = context.TODO()
  19. )
  20. // Service is search service
  21. type Service struct {
  22. dao *search.Dao
  23. showDao *show.Dao
  24. cronHot *cron.Cron
  25. HotFre string
  26. cronDark *cron.Cron
  27. DarkFre string
  28. }
  29. const (
  30. _HotPubState = "tianma_search_hot_state"
  31. _HotPubValue = "tianma_search_hot_value"
  32. _HotPubSearchState = "tianma_search_hot_search_state"
  33. _DarkPubState = "tianma_search_dark_state"
  34. _DarkPubValue = "tianma_search_dark_value"
  35. _DarkPubSearchState = "tianma_search_dark_search_state"
  36. _HotAutoPubState = "tianma_search_auto_hot_state"
  37. _DarkAutoPubState = "tianma_search_auto_dark_state"
  38. _HotShowUnpub = 0
  39. _HotShowPub = 1
  40. _HotShowUnUp = 2
  41. _DarkShowUnpub = 0
  42. _DarkShowPub = 1
  43. _DarkShowUnUp = 2
  44. )
  45. // New new a search service
  46. func New(c *conf.Config) (s *Service) {
  47. s = &Service{
  48. dao: search.New(c),
  49. showDao: show.New(c),
  50. cronHot: cron.New(),
  51. cronDark: cron.New(),
  52. HotFre: c.Cfg.HotCroFre,
  53. DarkFre: c.Cfg.DarkCroFre,
  54. }
  55. go s.CrontLoad()
  56. s.cronHot.Start()
  57. s.cronDark.Start()
  58. return
  59. }
  60. //CrontLoad search box history
  61. func (s *Service) CrontLoad() (err error) {
  62. if err = s.cronHot.AddFunc(s.HotFre, s.LoadHot); err != nil {
  63. log.Error("searchSrv.CrontLoaHot AddFunc LoadHot error(%v)", err)
  64. panic(err)
  65. }
  66. if err = s.cronDark.AddFunc(s.DarkFre, s.LoadDark); err != nil {
  67. log.Error("searchSrv.CrontLoaHot AddFunc LoadDark error(%v)", err)
  68. panic(err)
  69. }
  70. return
  71. }
  72. //LoadHot crontab auto load hot word
  73. func (s *Service) LoadHot() {
  74. var (
  75. err error
  76. status bool
  77. )
  78. timeTwelve := time.Now().Format("2006-01-02 ") + "12:00:00"
  79. timeTwelveStr, _ := s.parseTime(timeTwelve, "2006-01-02 15:04:05")
  80. timeZero := time.Now().Format("2006-01-02 ") + "00:00:00"
  81. timeZeroStr, _ := s.parseTime(timeZero, "2006-01-02 15:04:05")
  82. log.Info("searchSrv.LoadHot Auto LoadHot Start!")
  83. if time.Now().Unix() == timeZeroStr.Unix() {
  84. //0点会自动发布一次数据
  85. if err = s.SetHotPub(ctx, "crontabLoadHot", 0); err != nil {
  86. log.Error("searchSrv.LoadHot SetHotPub error(%v)", err)
  87. return
  88. }
  89. log.Info("searchSrv.LoadHot Auto LoadHot Success! 00:00 clock")
  90. } else if time.Now().Unix() >= timeTwelveStr.Unix() {
  91. log.Info("searchSrv.LoadHot Auto LoadHot Time > (%v)", timeTwelveStr)
  92. if status, err = s.isTodayAutoPubHot(ctx); err != nil {
  93. log.Error("searchSrv.LoadHot isTodayAutoPubHot error(%v)", err)
  94. return
  95. }
  96. log.Info("searchSrv.LoadHot Auto LoadHot Publish Status = (%v)", status)
  97. if status {
  98. return
  99. }
  100. if err = s.SetHotPub(ctx, "crontabLoadHot", 0); err != nil {
  101. log.Error("searchSrv.LoadHot SetHotPub error(%v)", err)
  102. return
  103. }
  104. log.Info("searchSrv.LoadHot Auto LoadHot Success! more than 12:00 clock")
  105. }
  106. }
  107. //LoadDark crontab auto load dark word
  108. func (s *Service) LoadDark() {
  109. var (
  110. status bool
  111. err error
  112. )
  113. log.Info("searchSrv.LoadDark Auto LoadDark Start!")
  114. timeTwelve := time.Now().Format("2006-01-02 ") + "12:00:00"
  115. timeTwelveStr, _ := s.parseTime(timeTwelve, "2006-01-02 15:04:05")
  116. timeZero := time.Now().Format("2006-01-02 ") + "00:00:00"
  117. timeZeroStr, _ := s.parseTime(timeZero, "2006-01-02 15:04:05")
  118. log.Info("searchSrv.LoadDark Auto LoadDark Start!")
  119. if time.Now().Unix() == timeZeroStr.Unix() {
  120. //0点会自动发布一次数据
  121. if err = s.SetDarkPub(ctx, "crontabLoadDark", 0); err != nil {
  122. log.Error("searchSrv.LoadDark SetDarkPub error(%v)", err)
  123. return
  124. }
  125. log.Info("searchSrv.LoadDark Auto LoadDark Success! 00:00 clock")
  126. } else if time.Now().Unix() >= timeTwelveStr.Unix() {
  127. log.Info("searchSrv.LoadDark Auto LoadDark Time > (%v)", timeTwelveStr)
  128. if status, err = s.isTodayAutoPubDark(ctx); err != nil {
  129. log.Error("searchSrv.LoadDark isTodayAutoPubDark error(%v)", err)
  130. return
  131. }
  132. log.Info("searchSrv.LoadDark Auto LoadDark Publish Status = (%v)", status)
  133. if status {
  134. return
  135. }
  136. if err = s.SetDarkPub(ctx, "crontabLoadDark", 0); err != nil {
  137. log.Error("searchSrv.LoadDark SetDarkPub error(%v)", err)
  138. return
  139. }
  140. log.Info("searchSrv.LoadDark Auto LoadDark Success!")
  141. }
  142. }
  143. //isTodayAutoPubHot is today publish hot word
  144. func (s *Service) isTodayAutoPubHot(c context.Context) (status bool, err error) {
  145. var (
  146. flag bool
  147. date string
  148. )
  149. if flag, date, err = s.dao.GetSearchAuditStat(c, _HotAutoPubState); err != nil {
  150. log.Error("searchSrv.isTodayAutoPubHot GetPubState error(%v)", err)
  151. return
  152. }
  153. //已发布 且是今天发布的数据 则证明今天发布过
  154. if flag && date == time.Now().Format("2006-01-02") {
  155. return true, nil
  156. }
  157. return
  158. }
  159. //isTodayAutoPubHot is today publish hot word
  160. func (s *Service) isTodayAutoPubDark(c context.Context) (status bool, err error) {
  161. var (
  162. flag bool
  163. date string
  164. )
  165. if flag, date, err = s.dao.GetSearchAuditStat(c, _DarkAutoPubState); err != nil {
  166. log.Error("searchSrv.isTodayAutoPubDark GetPubState error(%v)", err)
  167. return
  168. }
  169. //已发布 且是今天发布的数据 则证明今天发布过
  170. if flag && date == time.Now().Format("2006-01-02") {
  171. return true, nil
  172. }
  173. return
  174. }
  175. //parseTime parse string to unix timestamp
  176. func (s *Service) parseTime(t string, timeLayout string) (theTime time.Time, err error) {
  177. //timeLayout := "2006-01-02 15:04:05"
  178. //timeLayout := "2006-01-02" //转化所需模板
  179. loc, _ := time.LoadLocation("Local") //重要:获取时区
  180. //使用模板在对应时区转化为time.time类型
  181. if theTime, err = time.ParseInLocation(timeLayout, t, loc); err != nil {
  182. log.Error("searchSrv.parseTime ParseInLocation(%v) error(%v)", t, err)
  183. return
  184. }
  185. return
  186. }
  187. //GetSearchValue 获取搜索的数据
  188. func (s *Service) GetSearchValue(date string, blackSlice []string) (his []searchModel.History, err error) {
  189. if err = s.dao.DB.Model(&searchModel.History{}).
  190. Where("atime = ?", date).Where("searchword not in (?)", blackSlice).
  191. Where("deleted = ?", searchModel.NotDelete).
  192. Find(&his).Error; err != nil && err != gorm.ErrRecordNotFound {
  193. log.Error("searchSrv.GetSearchValue error(%v)", err)
  194. return
  195. }
  196. return
  197. }
  198. //GetSearHisValue 获取搜索热词的数据
  199. func (s *Service) GetSearHisValue(blackSlice []string) (his []searchModel.History, err error) {
  200. var hisTmp searchModel.History
  201. if err = s.dao.DB.Model(&searchModel.History{}).
  202. Where("deleted = ?", searchModel.NotDelete).Order("atime desc").Limit(1).
  203. First(&hisTmp).Error; err != nil {
  204. log.Error("searchSrv.GetSearchHisValue Last Day error(%v)", err)
  205. return
  206. }
  207. dao := s.dao.DB.Model(&searchModel.History{}).
  208. Where("atime = ?", hisTmp.Atime)
  209. if len(blackSlice) > 0 {
  210. dao = dao.Where("searchword not in (?)", blackSlice)
  211. }
  212. if err = dao.Where("deleted = ?", searchModel.NotDelete).Find(&his).Error; err != nil && err != gorm.ErrRecordNotFound {
  213. log.Error("searchSrv.GetSearchHisValue error(%v)", err)
  214. return
  215. }
  216. return
  217. }
  218. //HotwordFromDB 从DB中取有效数据
  219. func (s *Service) HotwordFromDB(date string) (hot []searchModel.Intervene, searchCount int, err error) {
  220. var (
  221. black []searchModel.Black
  222. blackSlice []string
  223. his []searchModel.History
  224. )
  225. if black, err = s.BlackList(); err != nil {
  226. log.Error("searchSrv.HotList Black error(%v)", err)
  227. return
  228. }
  229. for _, v := range black {
  230. blackSlice = append(blackSlice, v.Searchword)
  231. }
  232. his, err = s.GetSearchValue(date, blackSlice)
  233. if err != nil {
  234. log.Error("searchSrv.HotwordFromDB error(%v)", err)
  235. return
  236. }
  237. searchCount = len(his)
  238. //如果是取今天发布的数据 且没有取到 则以昨天的为准
  239. if time.Now().Format("2006-01-02") == date && len(his) == 0 {
  240. //如果 当天的搜索热词暂未同步过来 则取昨天的搜索热词
  241. if his, err = s.GetSearHisValue(blackSlice); err != nil {
  242. log.Error("searchSrv.HotList GetHotPubLog error(%v)", err)
  243. return
  244. }
  245. }
  246. //未结束的运营干预词
  247. if err = s.dao.DB.Model(&searchModel.Intervene{}).Where("etime >= ?", date).Where("searchword not in (?)", blackSlice).
  248. Where("deleted = ?", searchModel.NotDelete).
  249. Find(&hot).Error; err != nil && err != gorm.ErrRecordNotFound {
  250. log.Error("searchSrv.HotList Intervene error(%v)", err)
  251. return
  252. }
  253. //搜索历史词 默认position为-1
  254. for _, v := range his {
  255. var (
  256. inter = searchModel.Intervene{}
  257. )
  258. //判断搜索词是否在干预中
  259. if err = s.dao.DB.Model(&searchModel.Intervene{}).Where("etime >= ?", date).Where("searchword = ?", v.Searchword).
  260. Where("deleted = ?", searchModel.NotDelete).
  261. First(&inter).Error; err != nil && err != gorm.ErrRecordNotFound {
  262. log.Error("searchSrv.HotwordFromDB Intervene First error(%v)", err)
  263. return
  264. }
  265. //没有找到 则直接添加搜索的热词数据
  266. if err == gorm.ErrRecordNotFound {
  267. i := searchModel.Intervene{
  268. ID: v.ID,
  269. Searchword: v.Searchword,
  270. Rank: -1,
  271. Tag: v.Tag,
  272. Pv: v.Pv,
  273. }
  274. hot = append(hot, i)
  275. } else {
  276. //找到 则直接将PV值复制到运营词的数据
  277. for j, k := range hot {
  278. if k.Searchword == v.Searchword {
  279. hot[j].Pv = v.Pv
  280. }
  281. }
  282. }
  283. }
  284. return hot, searchCount, nil
  285. }
  286. //GetDarkValue 获取搜索热词的数据
  287. func (s *Service) GetDarkValue(blackSlice []string) (his []searchModel.Dark, err error) {
  288. var darkTmp searchModel.Dark
  289. if err = s.dao.DB.Model(&searchModel.Dark{}).
  290. Where("deleted = ?", searchModel.NotDelete).Order("atime desc").Limit(1).
  291. First(&darkTmp).Error; err != nil {
  292. log.Error("searchSrv.GetDarkValue Last Day error(%v)", err)
  293. return
  294. }
  295. dao := s.dao.DB.Model(&searchModel.Dark{}).
  296. Where("atime = ?", darkTmp.Atime)
  297. if len(blackSlice) > 0 {
  298. dao = dao.Where("searchword not in (?)", blackSlice)
  299. }
  300. if err = dao.Where("deleted = ?", searchModel.NotDelete).Find(&his).Error; err != nil && err != gorm.ErrRecordNotFound {
  301. log.Error("searchSrv.GetDarkValue error(%v)", err)
  302. return
  303. }
  304. return
  305. }
  306. //DarkwordFromDB 从DB中取有效数据
  307. func (s *Service) DarkwordFromDB(date string) (darkValue []searchModel.Dark, searchCount int, err error) {
  308. var (
  309. black []searchModel.Black
  310. blackSlice []string
  311. dark []searchModel.Dark
  312. )
  313. if black, err = s.BlackList(); err != nil {
  314. log.Error("searchSrv.DarkwordFromDB BlackList error(%v)", err)
  315. }
  316. for _, v := range black {
  317. blackSlice = append(blackSlice, v.Searchword)
  318. }
  319. if err = s.dao.DB.Model(&searchModel.Dark{}).Where("deleted = ?", searchModel.NotDelete).
  320. Where("atime = ?", date).Where("deleted = ?", searchModel.NotDelete).Find(&dark).Error; err != nil && err != gorm.ErrRecordNotFound {
  321. log.Error("searchSrv.DarkwordFromDB Find error(%v)", err)
  322. return
  323. }
  324. searchCount = len(dark)
  325. if err = s.dao.DB.Model(&searchModel.Dark{}).Where("deleted = ?", searchModel.NotDelete).
  326. Where("atime = ?", date).Where("searchword not in (?)", blackSlice).
  327. Where("deleted = ?", searchModel.NotDelete).Find(&dark).Error; err != nil && err != gorm.ErrRecordNotFound {
  328. log.Error("searchSrv.DarkwordFromDB Find error(%v)", err)
  329. return
  330. }
  331. //若搜索没有推黑马词过来 则以昨天的数据为准
  332. if time.Now().Format("2006-01-02") == date && len(dark) == 0 {
  333. if dark, err = s.GetDarkValue(blackSlice); err != nil {
  334. log.Error("searchSrv.DarkwordFromDB GetDarkPubLog error(%v)", err)
  335. return
  336. }
  337. }
  338. m := make(map[string]bool)
  339. for _, val := range dark {
  340. if _, ok := m[val.Searchword]; !ok {
  341. m[val.Searchword] = true
  342. darkValue = append(darkValue, val)
  343. }
  344. }
  345. return
  346. }
  347. //OpenHotList open hotword list
  348. func (s *Service) OpenHotList(c *bm.Context) (hotout []searchModel.Intervene, err error) {
  349. var (
  350. hot []searchModel.Intervene
  351. )
  352. if hot, err = s.GetHotPub(c); err != nil {
  353. log.Error("searchSrv.OpenHotList GetHotPub error(%v)", err)
  354. return
  355. }
  356. cTime := time.Now().Unix()
  357. inter := map[string]bool{}
  358. for _, v := range hot {
  359. if v.Rank != -1 && cTime >= v.Stime.Time().Unix() && cTime <= v.Etime.Time().Unix() {
  360. //运营词
  361. inter[v.Searchword] = true
  362. }
  363. }
  364. for _, v := range hot {
  365. if v.Rank == -1 {
  366. //如果运营词已存在 则以运营词为准
  367. if _, flag := inter[v.Searchword]; flag {
  368. continue
  369. }
  370. //-1 是ai的数据 直接添加
  371. hotout = append(hotout, v)
  372. } else if cTime >= v.Stime.Time().Unix() && cTime <= v.Etime.Time().Unix() {
  373. hotout = append(hotout, v)
  374. }
  375. }
  376. return
  377. }
  378. //HotList hotword list
  379. func (s *Service) HotList(c *bm.Context, t string) (hotout searchModel.HotwordOut, err error) {
  380. var (
  381. dateStamp time.Time
  382. todayStamp time.Time
  383. flag bool
  384. hot []searchModel.Intervene
  385. date string
  386. )
  387. if dateStamp, err = s.parseTime(t, "2006-01-02"); err != nil {
  388. return
  389. }
  390. today := time.Unix(time.Now().Unix(), 0).Format("2006-01-02")
  391. if todayStamp, err = s.parseTime(today, "2006-01-02"); err != nil {
  392. return
  393. }
  394. if flag, date, err = s.dao.GetSearchAuditStat(c, _HotPubState); err != nil {
  395. log.Error("searchSrv.HotList GetPublishCache error(%v)", err)
  396. return
  397. }
  398. //过去的时间 则直接从日志中取数据
  399. if dateStamp.Unix() < todayStamp.Unix() {
  400. var logFlag bool
  401. if hotout.Hotword, logFlag, err = s.GetHotPubLog(t); err != nil {
  402. log.Error("searchSrv.HotList GetHotPubLog error(%v)", err)
  403. return
  404. }
  405. if logFlag {
  406. hotout.State = _HotShowPub
  407. } else {
  408. hotout.State = _HotShowUnpub
  409. }
  410. return
  411. }
  412. //公共的逻辑
  413. if hot, _, err = s.HotwordFromDB(t); err != nil {
  414. log.Error("searchSrv.HotList HotwordFromDB error(%v)", err)
  415. return
  416. }
  417. hotout.Hotword = hot
  418. //今天的数据
  419. if dateStamp.Unix() == todayStamp.Unix() {
  420. //已发布 且是今天发布的数据
  421. if flag && date == time.Now().Format("2006-01-02") {
  422. //2.判断发布的时候 是否有搜索数据过来
  423. var pubStatus bool
  424. if pubStatus, _, err = s.dao.GetSearchAuditStat(c, _HotPubSearchState); err != nil {
  425. log.Error("searchSrv.SetHotPub SetSearchPubStat error(%v)", err)
  426. return
  427. }
  428. if pubStatus {
  429. //发布的时候 有搜索的数据 提示上线
  430. hotout.State = _HotShowPub
  431. } else {
  432. //发布的是 没有搜索的数据 提示 未更新
  433. hotout.State = _HotShowUnUp
  434. }
  435. return
  436. }
  437. //未发布
  438. hotout.State = _HotShowUnpub
  439. return
  440. }
  441. //未来的数据 都是未发布的
  442. hotout.State = _HotShowUnpub
  443. return
  444. }
  445. //DarkList darkword list
  446. func (s *Service) DarkList(c *bm.Context, t string) (darkout searchModel.DarkwordOut, err error) {
  447. var (
  448. dateStamp time.Time
  449. todayStamp time.Time
  450. flag bool
  451. //flagAuto bool
  452. dark []searchModel.Dark
  453. date string
  454. )
  455. if dateStamp, err = s.parseTime(t, "2006-01-02"); err != nil {
  456. return
  457. }
  458. today := time.Unix(time.Now().Unix(), 0).Format("2006-01-02")
  459. if todayStamp, err = s.parseTime(today, "2006-01-02"); err != nil {
  460. return
  461. }
  462. if flag, date, err = s.dao.GetSearchAuditStat(c, _DarkPubState); err != nil {
  463. log.Error("searchSrv.DarkList GetPublishCache error(%v)", err)
  464. return
  465. }
  466. //过去的时间 则直接从日志中取数据
  467. if dateStamp.Unix() < todayStamp.Unix() {
  468. var logFlag bool
  469. if darkout.Darkword, logFlag, err = s.GetDarkPubLog(t); err != nil {
  470. log.Error("searchSrv.HotList GetHotPubLog error(%v)", err)
  471. return
  472. }
  473. if logFlag {
  474. darkout.State = _DarkShowPub
  475. } else {
  476. darkout.State = _DarkShowUnpub
  477. }
  478. return
  479. }
  480. //公共的逻辑
  481. if dark, _, err = s.DarkwordFromDB(t); err != nil {
  482. log.Error("searchSrv.DarkList HotwordFromDB error(%v)", err)
  483. return
  484. }
  485. darkout.Darkword = dark
  486. //今天的数据
  487. if dateStamp.Unix() == todayStamp.Unix() {
  488. //已发布 且是今天发布的数据 则直接取缓存数据
  489. if flag && date == time.Now().Format("2006-01-02") {
  490. //判断发布的时候 是否有搜索数据过来
  491. var pubStatus bool
  492. if pubStatus, _, err = s.dao.GetSearchAuditStat(c, _DarkPubSearchState); err != nil {
  493. log.Error("searchSrv.DarkList GetPubState error(%v)", err)
  494. return
  495. }
  496. if pubStatus {
  497. //发布的时候 有搜索的数据 提示上线
  498. darkout.State = _DarkShowPub
  499. } else {
  500. //发布的是 没有搜索的数据 提示 未更新
  501. darkout.State = _DarkShowUnUp
  502. }
  503. return
  504. }
  505. //未更新
  506. darkout.State = _DarkShowUnpub
  507. return
  508. }
  509. //未来的数据 都是未发布的
  510. darkout.State = _HotShowUnpub
  511. return
  512. }
  513. //BlackList black list
  514. func (s *Service) BlackList() (black []searchModel.Black, err error) {
  515. if err = s.dao.DB.Model(&searchModel.Black{}).
  516. Where("deleted = ?", searchModel.NotDelete).Find(&black).Error; err != nil {
  517. log.Error("searchSrv.History Index error(%v)", err)
  518. return
  519. }
  520. return
  521. }
  522. //DelBlack add black
  523. func (s *Service) DelBlack(c *bm.Context, id int, person string, uid int64) (err error) {
  524. var (
  525. black searchModel.Black
  526. )
  527. //根据id查找热词
  528. if err = s.dao.DB.Model(&searchModel.Black{}).
  529. Where("id = ?", id).First(&black).Error; err != nil {
  530. log.Error("searchSrv.DelBlack Black First error(%v)", err)
  531. return
  532. }
  533. if err = s.dao.DB.Model(&searchModel.Black{}).
  534. Where("id = ?", id).Update("deleted", searchModel.Delete).Error; err != nil {
  535. log.Error("searchSrv.DelBlack Update error(%v)", err)
  536. return
  537. }
  538. //更新AI热词为删除状态
  539. if err = s.dao.DB.Model(&searchModel.History{}).
  540. Where("searchword = ?", black.Searchword).Update("deleted", searchModel.Delete).Error; err != nil {
  541. log.Error("searchSrv.DelBlack Update History error(%v)", err)
  542. return
  543. }
  544. //更新运营热词为删除状态
  545. if err = s.dao.DB.Model(&searchModel.Intervene{}).
  546. Where("searchword = ?", black.Searchword).Update("deleted", searchModel.Delete).Error; err != nil {
  547. log.Error("searchSrv.DelBlack Update error(%v)", err)
  548. return
  549. }
  550. //更新黑马词为删除状态
  551. if err = s.dao.DB.Model(&searchModel.Dark{}).
  552. Where("searchword = ?", black.Searchword).Update("deleted", searchModel.Delete).Error; err != nil {
  553. log.Error("searchSrv.DelBlack Update error(%v)", err)
  554. return
  555. }
  556. //设置黑名单之后 立即发布新数据
  557. if err = s.SetHotPub(c, person, uid); err != nil {
  558. return
  559. }
  560. if err = s.SetDarkPub(c, person, uid); err != nil {
  561. return
  562. }
  563. obj := map[string]interface{}{
  564. "id": id,
  565. }
  566. if err = Log.AddLog(searchModel.Business, person, uid, int64(id), searchModel.ActionDelBlack, obj); err != nil {
  567. log.Error("searchSrv.DelBlack AddLog error(%v)", err)
  568. return
  569. }
  570. return
  571. }
  572. //AddBlack add black
  573. func (s *Service) AddBlack(c *bm.Context, black string, person string, uid int64) (err error) {
  574. var (
  575. word searchModel.Black
  576. )
  577. if err = s.dao.DB.Model(&searchModel.Black{}).
  578. Where("deleted = ?", searchModel.NotDelete).Where("searchword = ?", black).
  579. First(&word).Error; err != nil && err != gorm.ErrRecordNotFound {
  580. log.Error("searchSrv.AddBlack get First error(%v)", err)
  581. return
  582. }
  583. if err != gorm.ErrRecordNotFound {
  584. err = fmt.Errorf("黑名单已存在")
  585. return
  586. }
  587. w := searchModel.AddBlack{
  588. Searchword: black,
  589. }
  590. if err = s.dao.DB.Model(&searchModel.Black{}).
  591. Create(w).Error; err != nil && err != gorm.ErrRecordNotFound {
  592. log.Error("searchSrv.AddBlack Create error(%v)", err)
  593. return
  594. }
  595. //设置黑名单之后 立即发布新数据
  596. if err = s.SetHotPub(c, person, uid); err != nil {
  597. return
  598. }
  599. if err = s.SetDarkPub(c, person, uid); err != nil {
  600. return
  601. }
  602. obj := map[string]interface{}{
  603. "blackword": word,
  604. }
  605. if err = Log.AddLog(searchModel.Business, person, uid, 0, searchModel.ActionAddBlack, obj); err != nil {
  606. log.Error("searchSrv.AddBlack AddLog error(%v)", err)
  607. return
  608. }
  609. return
  610. }
  611. //checkBlack checkout blacklist
  612. func (s *Service) checkBlack(word string) (state bool, err error) {
  613. var (
  614. black searchModel.Black
  615. )
  616. if err = s.dao.DB.Model(&searchModel.Black{}).
  617. Where("deleted = ?", searchModel.NotDelete).Where("searchword = ?", word).
  618. First(&black).Error; err != nil && err != gorm.ErrRecordNotFound {
  619. log.Error("searchSrv.checkBlack get First error(%v)", err)
  620. return
  621. }
  622. if err == gorm.ErrRecordNotFound {
  623. return false, nil
  624. }
  625. return true, nil
  626. }
  627. //checkInter checkout intervene
  628. func (s *Service) checkInter(word string, id int) (state bool, err error) {
  629. var (
  630. intervene searchModel.Intervene
  631. )
  632. dataStr := time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05")
  633. query := s.dao.DB.Model(&searchModel.Intervene{}).
  634. Where("searchword = ?", word)
  635. if id != 0 {
  636. query = query.Where("id != ?", id)
  637. }
  638. //取未删除且结束时间大于当前时间的词
  639. query = query.Where("deleted = ?", searchModel.NotDelete).Where("etime > ?", dataStr)
  640. if err = query.First(&intervene).Error; err != nil && err != gorm.ErrRecordNotFound {
  641. log.Error("searchSrv.checkInter get First error(%v)", err)
  642. return
  643. }
  644. if err == gorm.ErrRecordNotFound {
  645. return false, nil
  646. }
  647. return true, nil
  648. }
  649. //checkTimeConflict checkout intervene time conflict
  650. func (s *Service) checkTimeConflict(i searchModel.InterveneAdd, id int) (state bool, err error) {
  651. var (
  652. c int
  653. black []searchModel.Black
  654. blackSlice []string
  655. )
  656. if black, err = s.BlackList(); err != nil {
  657. log.Error("searchSrv.HotList Black error(%v)", err)
  658. }
  659. for _, v := range black {
  660. blackSlice = append(blackSlice, v.Searchword)
  661. }
  662. query := s.dao.DB.Model(&searchModel.Intervene{}).
  663. Where("rank = ?", i.Rank).
  664. Where("stime < ?", i.Etime).
  665. Where("etime > ?", i.Stime).
  666. Where("searchword not in (?)", blackSlice).
  667. Where("deleted = ?", searchModel.NotDelete)
  668. if id != 0 {
  669. query = query.Where("id != ?", id)
  670. }
  671. if err = query.Count(&c).Error; err != nil {
  672. log.Error("searchSrv.checkTimeConflict Count error(%v)", err)
  673. return
  674. }
  675. if c > 0 {
  676. return true, nil
  677. }
  678. return false, nil
  679. }
  680. //AddInter add intervene word
  681. func (s *Service) AddInter(c *bm.Context, v searchModel.InterveneAdd, person string, uid int64) (err error) {
  682. var (
  683. state bool
  684. )
  685. if state, err = s.checkBlack(v.Searchword); err != nil {
  686. log.Error("searchSrv.addInter checkBlack error(%v)", err)
  687. return
  688. }
  689. if state {
  690. err = fmt.Errorf("所添加的词在黑名单中已存在")
  691. return
  692. }
  693. if state, err = s.checkInter(v.Searchword, 0); err != nil {
  694. log.Error("searchSrv.addInter checkBlack error(%v)", err)
  695. return
  696. }
  697. if state {
  698. err = fmt.Errorf("干预词已存在")
  699. return
  700. }
  701. if state, err = s.checkTimeConflict(v, 0); err != nil {
  702. log.Error("searchSrv.addInter checkTimeConflict error(%v)", err)
  703. return
  704. }
  705. if state {
  706. err = fmt.Errorf("相同时间内,该位置已存在搜索词")
  707. return
  708. }
  709. if err = s.dao.DB.Model(&searchModel.InterveneAdd{}).Create(&v).Error; err != nil {
  710. log.Error("searchSrv.AddIntervene Create error(%v)", err)
  711. return
  712. }
  713. s.dao.SetSearchAuditStat(c, _HotPubState, false)
  714. if err = s.dao.SetSearchAuditStat(c, _HotPubState, false); err != nil {
  715. log.Error("searchSrv.DelBlack SetPubStat error(%v)", err)
  716. return
  717. }
  718. obj := map[string]interface{}{
  719. "value": v,
  720. }
  721. if err = Log.AddLog(searchModel.Business, person, uid, 0, searchModel.ActionAddInter, obj); err != nil {
  722. log.Error("searchSrv.addInter AddLog error(%v)", err)
  723. return
  724. }
  725. return
  726. }
  727. //UpdateInter update intervene word
  728. func (s *Service) UpdateInter(c *bm.Context, v searchModel.InterveneAdd, id int, person string, uid int64) (err error) {
  729. var (
  730. state bool
  731. )
  732. if state, err = s.checkBlack(v.Searchword); err != nil {
  733. log.Error("searchSrv.UpdateInter checkBlack error(%v)", err)
  734. return
  735. }
  736. if state {
  737. err = fmt.Errorf("所添加的词在黑名单中已存在")
  738. return
  739. }
  740. if state, err = s.checkInter(v.Searchword, id); err != nil {
  741. log.Error("searchSrv.UpdateInter checkInter error(%v)", err)
  742. return
  743. }
  744. if state {
  745. err = fmt.Errorf("干预词已存在")
  746. return
  747. }
  748. if state, err = s.checkTimeConflict(v, id); err != nil {
  749. log.Error("searchSrv.UpdateInter checkTimeConflict error(%v)", err)
  750. return
  751. }
  752. if state {
  753. err = fmt.Errorf("相同时间内,该位置已存在搜索词")
  754. return
  755. }
  756. if v.Tag == "" {
  757. v2 := map[string]interface{}{
  758. "searchword": v.Searchword,
  759. "rank": v.Rank,
  760. "tag": v.Tag,
  761. "stime": v.Stime,
  762. "etime": v.Etime,
  763. }
  764. if err = s.dao.DB.Model(&searchModel.InterveneAdd{}).
  765. Where("id = ?", id).Updates(v2).Error; err != nil {
  766. log.Error("searchSrv.UpdateInter Update error(%v)", err)
  767. return
  768. }
  769. } else {
  770. if err = s.dao.DB.Model(&searchModel.InterveneAdd{}).
  771. Where("id = ?", id).Updates(&v).Error; err != nil {
  772. log.Error("searchSrv.UpdateInter Update error(%v)", err)
  773. return
  774. }
  775. }
  776. if err = s.dao.SetSearchAuditStat(c, _HotPubState, false); err != nil {
  777. log.Error("searchSrv.DelBlack SetPubStat error(%v)", err)
  778. return
  779. }
  780. obj := map[string]interface{}{
  781. "value": v,
  782. "id": id,
  783. }
  784. if err = Log.AddLog(searchModel.Business, person, uid, int64(id), searchModel.ActionUpdateInter, obj); err != nil {
  785. log.Error("searchSrv.addInter AddLog error(%v)", err)
  786. return
  787. }
  788. return
  789. }
  790. //UpdateSearch update search hot tag
  791. func (s *Service) UpdateSearch(c *bm.Context, tag string, id int, person string, uid int64) (err error) {
  792. if err = s.dao.DB.Model(&searchModel.History{}).
  793. Where("id = ?", id).Update("tag", tag).Error; err != nil {
  794. log.Error("searchSrv.UpdateSearch Update error(%v)", err)
  795. return
  796. }
  797. if err = s.dao.SetSearchAuditStat(c, _HotPubState, false); err != nil {
  798. log.Error("searchSrv.DelBlack SetPubStat error(%v)", err)
  799. return
  800. }
  801. obj := map[string]interface{}{
  802. "value": tag,
  803. "id": id,
  804. }
  805. if err = Log.AddLog(searchModel.Business, person, uid, int64(id), searchModel.ActionUpdateSearch, obj); err != nil {
  806. log.Error("searchSrv.UpdateSearch AddLog error(%v)", err)
  807. return
  808. }
  809. return
  810. }
  811. //DeleteHot delete hot word
  812. func (s *Service) DeleteHot(c context.Context, id int, t uint8, person string, uid int64) (err error) {
  813. if t == searchModel.HotAI {
  814. //删除AI热词
  815. if err = s.dao.DB.Model(&searchModel.History{}).
  816. Where("id = ?", id).Update("deleted", searchModel.Delete).Error; err != nil {
  817. log.Error("searchSrv.DeleteHot Update AI error(%v)", err)
  818. return
  819. }
  820. } else if t == searchModel.HotOpe {
  821. //删除运营热词
  822. if err = s.dao.DB.Model(&searchModel.Intervene{}).
  823. Where("id = ?", id).Update("deleted", searchModel.Delete).Error; err != nil {
  824. log.Error("searchSrv.DeleteHot Update Operate error(%v)", err)
  825. return
  826. }
  827. }
  828. //删除热词之后 立即发布新数据
  829. if err = s.SetHotPub(c, person, uid); err != nil {
  830. return
  831. }
  832. obj := map[string]interface{}{
  833. "type": t,
  834. "id": id,
  835. }
  836. if err = Log.AddLog(searchModel.Business, person, uid, int64(id), searchModel.ActionDeleteHot, obj); err != nil {
  837. log.Error("searchSrv.DeleteHot AddLog error(%v)", err)
  838. return
  839. }
  840. return
  841. }
  842. //DeleteDark delete dark word
  843. func (s *Service) DeleteDark(c context.Context, id int, person string, uid int64) (err error) {
  844. if err = s.dao.DB.Model(&searchModel.Dark{}).
  845. Where("id = ?", id).Update("deleted", searchModel.Delete).Error; err != nil {
  846. log.Error("searchSrv.DeleteDark Update error(%v)", err)
  847. return
  848. }
  849. if err = s.SetDarkPub(c, person, uid); err != nil {
  850. return
  851. }
  852. obj := map[string]interface{}{
  853. "id": id,
  854. }
  855. if err = Log.AddLog(searchModel.Business, person, uid, int64(id), searchModel.ActionDeleteDark, obj); err != nil {
  856. log.Error("searchSrv.DeleteDark AddLog error(%v)", err)
  857. return
  858. }
  859. return
  860. }
  861. //OpenAddDarkword open api for search add dark word
  862. func (s *Service) OpenAddDarkword(c context.Context, values searchModel.OpenDark) (err error) {
  863. if err = s.dao.DB.Model(&searchModel.Dark{}).
  864. Where("atime = ?", values.Date).Update("deleted", searchModel.Delete).Error; err != nil {
  865. log.Error("searchSrv.OpenAddDarkword Update error(%v)", err)
  866. return
  867. }
  868. for _, v := range values.Values {
  869. dark := searchModel.Dark{
  870. Searchword: v.Searchword,
  871. PV: v.PV,
  872. Atime: values.Date,
  873. }
  874. if err = s.dao.DB.Model(&searchModel.Dark{}).Create(&dark).Error; err != nil {
  875. log.Error("searchSrv.OpenAddDarkword Create error(%v)", err)
  876. return
  877. }
  878. }
  879. //如果有黑马词同步过来 则更新发布状态为false
  880. if err = s.dao.SetSearchAuditStat(c, _DarkAutoPubState, false); err != nil {
  881. log.Error("searchSrv.DelBlack SetPubStat error(%v)", err)
  882. return
  883. }
  884. obj := map[string]interface{}{
  885. "value": values,
  886. }
  887. if err = Log.AddLog(searchModel.Business, "SEARCH", 0, 0, searchModel.ActionOpenAddDark, obj); err != nil {
  888. log.Error("searchSrv.OpenAddDarkword AddLog error(%v)", err)
  889. return
  890. }
  891. return
  892. }
  893. //OpenAddHotword open api for search add hotword
  894. func (s *Service) OpenAddHotword(c *bm.Context, values searchModel.OpenHot) (err error) {
  895. if err = s.dao.DB.Model(&searchModel.Hot{}).Where("atime = ?", values.Date).Delete(&searchModel.Hot{}).Error; err != nil {
  896. log.Error("searchSrv.OpenAddHotword Delete error(%v)", err)
  897. return
  898. }
  899. for _, v := range values.Values {
  900. hot := searchModel.Hot{
  901. Searchword: v.Searchword,
  902. PV: v.PV,
  903. Atime: values.Date,
  904. }
  905. if err = s.dao.DB.Model(&searchModel.Hot{}).Create(&hot).Error; err != nil {
  906. log.Error("searchSrv.OpenAddHotword Create error(%v)", err)
  907. return
  908. }
  909. }
  910. //如果有搜索热词同步过来 则更新自动发布状态为false
  911. if err = s.dao.SetSearchAuditStat(c, _HotAutoPubState, false); err != nil {
  912. log.Error("searchSrv.DelBlack SetPubStat error(%v)", err)
  913. return
  914. }
  915. obj := map[string]interface{}{
  916. "value": values,
  917. }
  918. if err = Log.AddLog(searchModel.Business, "SEARCH", 0, 0, searchModel.ActionOpenAddHot, obj); err != nil {
  919. log.Error("searchSrv.OpenAddHotword AddLog error(%v)", err)
  920. return
  921. }
  922. return
  923. }
  924. //GetHotPub get hotword publish from mc
  925. func (s *Service) GetHotPub(c *bm.Context) (hot []searchModel.Intervene, err error) {
  926. var (
  927. conn memcache.Conn
  928. item *memcache.Item
  929. )
  930. conn = s.dao.MC.Get(c)
  931. defer conn.Close()
  932. if item, err = conn.Get(_HotPubValue); err != nil {
  933. if err == memcache.ErrNotFound {
  934. return nil, nil
  935. }
  936. return
  937. }
  938. if err = conn.Scan(item, &hot); err != nil {
  939. return
  940. }
  941. return
  942. }
  943. //GetDarkPub get darkword publish from mc
  944. func (s *Service) GetDarkPub(c *bm.Context) (dark []searchModel.Dark, err error) {
  945. var (
  946. conn memcache.Conn
  947. item *memcache.Item
  948. )
  949. conn = s.dao.MC.Get(c)
  950. defer conn.Close()
  951. if item, err = conn.Get(_DarkPubValue); err != nil {
  952. if err == memcache.ErrNotFound {
  953. return nil, nil
  954. }
  955. return
  956. }
  957. if err = conn.Scan(item, &dark); err != nil {
  958. return
  959. }
  960. return
  961. }
  962. //SetHotPub set hotword publish to mc
  963. func (s *Service) SetHotPub(c context.Context, person string, uid int64) (err error) {
  964. var (
  965. conn memcache.Conn
  966. hot []searchModel.Intervene
  967. searchCount int
  968. )
  969. conn = s.dao.MC.Get(c)
  970. defer conn.Close()
  971. //只能发布当天的数据
  972. //从DB中取今天的数据
  973. if hot, searchCount, err = s.HotwordFromDB(time.Now().Format("2006-01-02")); err != nil {
  974. log.Error("searchSrv.SetHoGetSearHisValuetPub HotwordFromDB error(%v)", err)
  975. return
  976. }
  977. itemJSON := &memcache.Item{
  978. Key: _HotPubValue,
  979. Flags: memcache.FlagJSON,
  980. Object: hot,
  981. Expiration: 0,
  982. }
  983. if err = conn.Set(itemJSON); err != nil {
  984. log.Error("searchSrv.SetHotPub conn.Set error(%v)", err)
  985. return
  986. }
  987. if searchCount == 0 {
  988. //证明搜索没有推数据过来 设置搜索的数据为假
  989. if err = s.dao.SetSearchAuditStat(c, _HotPubSearchState, false); err != nil {
  990. log.Error("searchSrv.SetHotPub SetSearchPubStat error(%v)", err)
  991. return
  992. }
  993. } else {
  994. //证明搜索有推数据过来 设置搜索的数据为真
  995. if err = s.dao.SetSearchAuditStat(c, _HotPubSearchState, true); err != nil {
  996. log.Error("searchSrv.SetHotPub SetSearchPubStat error(%v)", err)
  997. return
  998. }
  999. }
  1000. //设置自动发布状态为true
  1001. if err = s.dao.SetSearchAuditStat(c, _HotAutoPubState, true); err != nil {
  1002. log.Error("searchSrv.SetHotPub SetPubStat error(%v)", err)
  1003. return
  1004. }
  1005. //设置运营发布状态为true
  1006. if err = s.dao.SetSearchAuditStat(c, _HotPubState, true); err != nil {
  1007. log.Error("searchSrv.SetHotPub SetPubStat error(%v)", err)
  1008. return
  1009. }
  1010. obj := map[string]interface{}{
  1011. "value": hot,
  1012. }
  1013. if err = Log.AddLog(searchModel.Business, person, uid, 0, searchModel.ActionPublishHot, obj); err != nil {
  1014. log.Error("searchSrv.SetHotPub AddLog error(%v)", err)
  1015. return
  1016. }
  1017. if err = s.HotPubLog(hot); err != nil {
  1018. log.Error("searchSrv.SetHotPub HotPubLog error(%v)", err)
  1019. return
  1020. }
  1021. return
  1022. }
  1023. //HotPubLog hotword publish log
  1024. func (s *Service) HotPubLog(hot []searchModel.Intervene) (err error) {
  1025. t := time.Now().Unix()
  1026. for _, v := range hot {
  1027. w := searchModel.HotPubLog{
  1028. Searchword: v.Searchword,
  1029. Position: v.Rank,
  1030. Pv: v.Pv,
  1031. Tag: v.Tag,
  1032. Stime: v.Stime,
  1033. Etime: v.Etime,
  1034. Atime: time.Now().Format("2006-01-02"),
  1035. Groupid: t,
  1036. }
  1037. if err = s.dao.DB.Model(&searchModel.HotPubLog{}).Create(&w).Error; err != nil {
  1038. log.Error("searchSrv.DarkPubLog Create error(%v)", err)
  1039. return
  1040. }
  1041. }
  1042. return
  1043. }
  1044. //GetHotPubLog get hotword publish log
  1045. func (s *Service) GetHotPubLog(date string) (hotout []searchModel.Intervene, pub bool, err error) {
  1046. var (
  1047. //hotout searchModel.HotwordOut
  1048. logs []searchModel.HotPubLog
  1049. )
  1050. l := searchModel.HotPubLog{}
  1051. if err = s.dao.DB.Model(&searchModel.HotPubLog{}).Where("atime = ?", date).Order("groupid desc").
  1052. First(&l).Error; err != nil && err != gorm.ErrRecordNotFound {
  1053. log.Error("searchSrv.GetHotPubLog First error(%v)", err)
  1054. return
  1055. }
  1056. //证明没有发布过
  1057. if err == gorm.ErrRecordNotFound {
  1058. return nil, false, nil
  1059. }
  1060. //取最大的groupid的值
  1061. if err = s.dao.DB.Model(&searchModel.HotPubLog{}).Where("groupid = ?", l.Groupid).
  1062. Find(&logs).Error; err != nil && err != gorm.ErrRecordNotFound {
  1063. log.Error("searchSrv.GetHotPubLog Find error(%v)", err)
  1064. return
  1065. }
  1066. for _, v := range logs {
  1067. a := searchModel.Intervene{
  1068. Searchword: v.Searchword,
  1069. Rank: v.Position,
  1070. Pv: v.Pv,
  1071. Tag: v.Tag,
  1072. Stime: v.Stime,
  1073. Etime: v.Etime,
  1074. }
  1075. hotout = append(hotout, a)
  1076. }
  1077. return hotout, true, nil
  1078. }
  1079. //GetDarkPubLog get darkword publish log
  1080. func (s *Service) GetDarkPubLog(date string) (darkout []searchModel.Dark, pub bool, err error) {
  1081. var (
  1082. //hotout searchModel.HotwordOut
  1083. logs []searchModel.DarkPubLog
  1084. )
  1085. l := searchModel.DarkPubLog{}
  1086. if err = s.dao.DB.Model(&searchModel.DarkPubLog{}).Where("atime = ?", date).Order("groupid desc").
  1087. First(&l).Error; err != nil && err != gorm.ErrRecordNotFound {
  1088. log.Error("searchSrv.GetDarkPubLog First error(%v)", err)
  1089. return
  1090. }
  1091. //证明没有发布过
  1092. if err == gorm.ErrRecordNotFound {
  1093. return nil, false, nil
  1094. }
  1095. //取最大的groupid的值
  1096. if err = s.dao.DB.Model(&searchModel.DarkPubLog{}).Where("groupid = ?", l.Groupid).
  1097. Find(&logs).Error; err != nil && err != gorm.ErrRecordNotFound {
  1098. log.Error("searchSrv.GetDarkPubLog Find error(%v)", err)
  1099. return
  1100. }
  1101. for _, v := range logs {
  1102. a := searchModel.Dark{
  1103. Searchword: v.Searchword,
  1104. PV: v.Pv,
  1105. }
  1106. darkout = append(darkout, a)
  1107. }
  1108. return darkout, true, nil
  1109. }
  1110. //SetDarkPub set darkword to mc
  1111. func (s *Service) SetDarkPub(c context.Context, person string, uid int64) (err error) {
  1112. var (
  1113. conn memcache.Conn
  1114. dark []searchModel.Dark
  1115. searchCount int
  1116. )
  1117. conn = s.dao.MC.Get(c)
  1118. defer conn.Close()
  1119. //只能发布当天的数据
  1120. //从DB中取今天的数据
  1121. if dark, searchCount, err = s.DarkwordFromDB(time.Now().Format("2006-01-02")); err != nil {
  1122. log.Error("searchSrv.SetHotPub HotwordFromDB error(%v)", err)
  1123. return
  1124. }
  1125. itemJSON := &memcache.Item{
  1126. Key: _DarkPubValue,
  1127. Flags: memcache.FlagJSON,
  1128. Object: dark,
  1129. Expiration: 0,
  1130. }
  1131. if err = conn.Set(itemJSON); err != nil {
  1132. log.Error("searchSrv.SetHotPub conn.Set error(%v)", err)
  1133. return
  1134. }
  1135. if searchCount == 0 {
  1136. //证明搜索没有推数据过来 设置搜索的数据为假
  1137. if err = s.dao.SetSearchAuditStat(c, _DarkPubSearchState, false); err != nil {
  1138. log.Error("searchSrv.SetDarkPub SetSearchPubStat error(%v)", err)
  1139. return
  1140. }
  1141. } else {
  1142. //证明搜索有推数据过来 设置搜索的数据为真
  1143. if err = s.dao.SetSearchAuditStat(c, _DarkPubSearchState, true); err != nil {
  1144. log.Error("searchSrv.SetDarkPub SetSearchPubStat error(%v)", err)
  1145. return
  1146. }
  1147. }
  1148. if err = s.dao.SetSearchAuditStat(c, _DarkPubState, true); err != nil {
  1149. log.Error("searchSrv.SetHotPub SetPubStat error(%v)", err)
  1150. return
  1151. }
  1152. if err = s.dao.SetSearchAuditStat(c, _DarkAutoPubState, true); err != nil {
  1153. log.Error("searchSrv.SetHotPub SetPubStat error(%v)", err)
  1154. return
  1155. }
  1156. obj := map[string]interface{}{
  1157. "value": dark,
  1158. }
  1159. if err = Log.AddLog(searchModel.Business, person, uid, 0, searchModel.ActionPublishDark, obj); err != nil {
  1160. log.Error("searchSrv.SetDarkPub AddLog error(%v)", err)
  1161. return
  1162. }
  1163. if err = s.DarkPubLog(dark); err != nil {
  1164. log.Error("searchSrv.SetDarkPub DarkPubLog error(%v)", err)
  1165. return
  1166. }
  1167. return
  1168. }
  1169. //DarkPubLog get darkword publish log
  1170. func (s *Service) DarkPubLog(dark []searchModel.Dark) (err error) {
  1171. t := time.Now().Unix()
  1172. for _, v := range dark {
  1173. w := searchModel.DarkPubLog{
  1174. Searchword: v.Searchword,
  1175. Pv: v.PV,
  1176. Atime: time.Now().Format("2006-01-02"),
  1177. Groupid: t,
  1178. }
  1179. if err = s.dao.DB.Model(&searchModel.DarkPubLog{}).Create(&w).Error; err != nil {
  1180. log.Error("searchSrv.DarkPubLog Create error(%v)", err)
  1181. return
  1182. }
  1183. }
  1184. return
  1185. }