search_web.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package search
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. "go-common/app/admin/main/feed/model/common"
  9. "go-common/app/admin/main/feed/model/show"
  10. showModel "go-common/app/admin/main/feed/model/show"
  11. "go-common/app/admin/main/feed/util"
  12. "go-common/library/log"
  13. "github.com/jinzhu/gorm"
  14. )
  15. const (
  16. //_ActAddSearchWebCard log action
  17. _ActAddSearchWebCard = "ActAddSearchWebCard"
  18. //_ActUpSearchWebCard log action
  19. _ActUpSearchWebCard = "ActUpSearchWebCard"
  20. //_ActDelSearchWebCard log action
  21. _ActDelSearchWebCard = "ActDelSearchWebCard"
  22. //_ActAddSearchWeb log action
  23. _ActAddSearchWeb = "ActAddSearchWeb"
  24. //_ActUpSearchWeb log action
  25. _ActUpSearchWeb = "ActUpSearchWeb"
  26. //_ActDelSearchWeb log action
  27. _ActDelSearchWeb = "ActDelSearchWeb"
  28. //_ActOptSearchWeb log action
  29. _ActOptSearchWeb = "ActOptSearchWeb"
  30. )
  31. var (
  32. _emptyWebQuery = make([]*show.SearchWebQuery, 0)
  33. )
  34. //SearchWebCardList channel SearchWebCard list
  35. func (s *Service) SearchWebCardList(lp *show.SearchWebCardLP) (pager *show.SearchWebCardPager, err error) {
  36. pager = &show.SearchWebCardPager{
  37. Page: common.Page{
  38. Num: lp.Pn,
  39. Size: lp.Ps,
  40. },
  41. }
  42. w := map[string]interface{}{
  43. "deleted": common.NotDeleted,
  44. }
  45. query := s.showDao.DB.Model(&show.SearchWebCard{})
  46. if lp.ID > 0 {
  47. w["id"] = lp.ID
  48. }
  49. if lp.Person != "" {
  50. query = query.Where("person like ?", "%"+lp.Person+"%")
  51. }
  52. if lp.Title != "" {
  53. query = query.Where("title like ?", "%"+lp.Title+"%")
  54. }
  55. if lp.STime != "" {
  56. query = query.Where("ctime >= ?", lp.STime)
  57. }
  58. if lp.ETime != "" {
  59. query = query.Where("ctime <= ?", lp.ETime)
  60. }
  61. if err = query.Where(w).Count(&pager.Page.Total).Error; err != nil {
  62. log.Error("searchWebSvc.SearchWebCardList count error(%v)", err)
  63. return
  64. }
  65. SearchWebCards := make([]*show.SearchWebCard, 0)
  66. if err = query.Where(w).Order("`id` DESC").Offset((lp.Pn - 1) * lp.Ps).Limit(lp.Ps).Find(&SearchWebCards).Error; err != nil {
  67. log.Error("searchWebSvc.SearchWebCardList Find error(%v)", err)
  68. return
  69. }
  70. pager.Item = SearchWebCards
  71. return
  72. }
  73. //AddSearchWebCard add channel SearchWebCard
  74. func (s *Service) AddSearchWebCard(c context.Context, param *show.SearchWebCardAP, name string, uid int64) (err error) {
  75. if err = s.showDao.SearchWebCardAdd(param); err != nil {
  76. return
  77. }
  78. if err = util.AddLogs(common.LogSWEBCard, name, uid, 0, _ActAddSearchWebCard, param); err != nil {
  79. log.Error("searchWebSvc.AddSearchWebCard AddLog error(%v)", err)
  80. return
  81. }
  82. return
  83. }
  84. //UpdateSearchWebCard update channel SearchWebCard
  85. func (s *Service) UpdateSearchWebCard(c context.Context, param *show.SearchWebCardUP, name string, uid int64) (err error) {
  86. if err = s.showDao.SearchWebCardUpdate(param); err != nil {
  87. return
  88. }
  89. if err = util.AddLogs(common.LogSWEBCard, name, uid, 0, _ActUpSearchWebCard, param); err != nil {
  90. log.Error("searchWebSvc.UpdateSearchWebCard AddLog error(%v)", err)
  91. return
  92. }
  93. return
  94. }
  95. //DeleteSearchWebCard delete channel SearchWebCard
  96. func (s *Service) DeleteSearchWebCard(id int64, name string, uid int64) (err error) {
  97. if err = s.showDao.SearchWebCardDelete(id); err != nil {
  98. return
  99. }
  100. if err = util.AddLogs(common.LogSWEBCard, name, uid, id, _ActDelSearchWebCard, id); err != nil {
  101. log.Error("searchWebSvc.DeleteSearchWebCard AddLog error(%v)", err)
  102. return
  103. }
  104. return
  105. }
  106. //SearchWebList SearchWeb list
  107. func (s *Service) SearchWebList(lp *show.SearchWebLP) (pager *show.SearchWebPager, err error) {
  108. pager = &show.SearchWebPager{
  109. Page: common.Page{
  110. Num: lp.Pn,
  111. Size: lp.Ps,
  112. },
  113. }
  114. w := map[string]interface{}{
  115. "deleted": common.NotDeleted,
  116. }
  117. query := s.showDao.DB.Model(&show.SearchWeb{})
  118. if lp.ID > 0 {
  119. w["id"] = lp.ID
  120. }
  121. if lp.Person != "" {
  122. query = query.Where("person like ?", "%"+lp.Person+"%")
  123. }
  124. if lp.STime != "" {
  125. query = query.Where("stime >= ?", lp.STime)
  126. }
  127. if lp.ETime != "" {
  128. query = query.Where("etime <= ?", lp.ETime)
  129. }
  130. cTimeStr := util.CTimeStr()
  131. if lp.Check != 0 {
  132. if lp.Check == common.Pass {
  133. //已通过 未生效
  134. query = query.Where("`check` = ?", common.Pass)
  135. query = query.Where("stime > ?", cTimeStr)
  136. } else if lp.Check == common.Valid {
  137. //已通过 已生效
  138. query = query.Where("`check` = ?", common.Pass)
  139. query = query.Where("stime <= ?", cTimeStr).Where("etime >= ?", cTimeStr)
  140. } else if lp.Check == common.InValid {
  141. //已通过 已失效
  142. query = query.Where("(`check` = ? AND etime <= ?) OR (`check` = ?)", common.Pass, cTimeStr, common.InValid)
  143. } else {
  144. query = query.Where("`check` = ? ", lp.Check)
  145. }
  146. }
  147. if err = query.Where(w).Count(&pager.Page.Total).Error; err != nil {
  148. log.Error("searchSvc.SearchWebList count error(%v)", err)
  149. return
  150. }
  151. SearchWebs := make([]*show.SearchWeb, 0)
  152. if err = query.Where(w).Order("`id` DESC").Offset((lp.Pn - 1) * lp.Ps).Limit(lp.Ps).Find(&SearchWebs).Error; err != nil {
  153. log.Error("searchSvc.SearchWebList Find error(%v)", err)
  154. return
  155. }
  156. if len(SearchWebs) > 0 {
  157. var (
  158. ids []int64
  159. queryMap map[int64][]*show.SearchWebQuery
  160. )
  161. for _, v := range SearchWebs {
  162. if v.Check == common.Pass {
  163. c := time.Now().Unix()
  164. if (c >= v.Stime.Time().Unix()) && (c <= v.Etime.Time().Unix()) {
  165. v.Check = common.Valid
  166. } else if c > v.Etime.Time().Unix() && v.Check != common.InValid {
  167. v.Check = common.InValid
  168. v.Status = common.StatusDownline
  169. }
  170. }
  171. webCard := &show.SearchWebCard{}
  172. cardWhere := map[string]interface{}{
  173. "deleted": common.NotDeleted,
  174. "id": v.CardValue,
  175. }
  176. if err = s.showDao.DB.Model(&show.SearchWebCard{}).Where(cardWhere).First(webCard).Error; err != nil {
  177. if err == gorm.ErrRecordNotFound {
  178. err = nil
  179. } else {
  180. log.Error("searchSvc.SearchWebCard Find error(%v)", err)
  181. return
  182. }
  183. }
  184. v.Card = webCard
  185. ids = append(ids, v.ID)
  186. }
  187. where := map[string]interface{}{
  188. "deleted": common.NotDeleted,
  189. }
  190. SearchWebQuery := make([]*show.SearchWebQuery, 0)
  191. if err = s.showDao.DB.Model(&show.SearchWebQuery{}).Where(where).Where("sid in (?)", ids).Find(&SearchWebQuery).Error; err != nil {
  192. log.Error("searchSvc.SearchWebList Find error(%v)", err)
  193. return
  194. }
  195. queryMap = make(map[int64][]*show.SearchWebQuery, len(SearchWebQuery))
  196. for _, v := range SearchWebQuery {
  197. queryMap[v.SID] = append(queryMap[v.SID], v)
  198. }
  199. for _, v := range SearchWebs {
  200. if value, ok := queryMap[v.ID]; ok {
  201. v.Query = value
  202. } else {
  203. v.Query = _emptyWebQuery
  204. }
  205. }
  206. }
  207. pager.Item = SearchWebs
  208. return
  209. }
  210. //OpenSearchWebList SearchWeb list
  211. func (s *Service) OpenSearchWebList() (SearchWebs []*show.SearchWeb, err error) {
  212. cTimeStr := util.CTimeStr()
  213. SearchWebs = make([]*show.SearchWeb, 0)
  214. w := map[string]interface{}{
  215. "deleted": common.NotDeleted,
  216. "check": common.Pass,
  217. }
  218. query := s.showDao.DB.Model(&show.SearchWeb{})
  219. //已通过 已生效
  220. query = query.Where("stime <= ?", cTimeStr).Where("etime >= ?", cTimeStr)
  221. if err = query.Where(w).Order("`id` DESC").Find(&SearchWebs).Error; err != nil {
  222. log.Error("searchSvc.OpenSearchWebList Find error(%v)", err)
  223. return
  224. }
  225. if len(SearchWebs) > 0 {
  226. var (
  227. ids []int64
  228. queryMap map[int64][]*show.SearchWebQuery
  229. )
  230. for _, v := range SearchWebs {
  231. webCard := &show.SearchWebCard{}
  232. cardWhere := map[string]interface{}{
  233. "deleted": common.NotDeleted,
  234. "id": v.CardValue,
  235. }
  236. if err = s.showDao.DB.Model(&show.SearchWebCard{}).Where(cardWhere).First(webCard).Error; err != nil {
  237. if err == gorm.ErrRecordNotFound {
  238. err = nil
  239. webCard = nil
  240. } else {
  241. log.Error("searchSvc.OpenSearchWebList Find error(%v)", err)
  242. }
  243. }
  244. if webCard != nil {
  245. v.Card = webCard
  246. } else {
  247. v.Card = struct{}{}
  248. }
  249. ids = append(ids, v.ID)
  250. }
  251. where := map[string]interface{}{
  252. "deleted": common.NotDeleted,
  253. }
  254. SearchWebQuery := make([]*show.SearchWebQuery, 0)
  255. if err = s.showDao.DB.Model(&show.SearchWebQuery{}).Where(where).Where("sid in (?)", ids).Find(&SearchWebQuery).Error; err != nil {
  256. log.Error("searchSvc.OpenSearchWebList Find error(%v)", err)
  257. return
  258. }
  259. queryMap = make(map[int64][]*show.SearchWebQuery, len(SearchWebQuery))
  260. for _, v := range SearchWebQuery {
  261. queryMap[v.SID] = append(queryMap[v.SID], v)
  262. }
  263. for _, v := range SearchWebs {
  264. if value, ok := queryMap[v.ID]; ok {
  265. v.Query = value
  266. } else {
  267. v.Query = _emptyWebQuery
  268. }
  269. }
  270. }
  271. return
  272. }
  273. //Validate validate search web card
  274. func (s *Service) Validate(p *show.SWTimeValid) (err error) {
  275. var (
  276. querys []*show.SearchWebQuery
  277. webCard *showModel.SearchWebCard
  278. id int64
  279. )
  280. if id, err = strconv.ParseInt(p.CardValue, 10, 64); err != nil {
  281. return
  282. }
  283. if webCard, err = s.showDao.SWBFindByID(id); err != nil {
  284. return err
  285. }
  286. if webCard == nil {
  287. return fmt.Errorf("无效web卡片ID(%d)", id)
  288. }
  289. if err = json.Unmarshal([]byte(p.Query), &querys); err != nil {
  290. log.Error("searchSvc.Validate json.Unmarshal(%v) error(%v)", p, err)
  291. return
  292. }
  293. if len(querys) == 0 {
  294. err = fmt.Errorf("query不能为空")
  295. return
  296. }
  297. for _, v := range querys {
  298. count := 0
  299. p.Query = v.Value
  300. if count, err = s.showDao.SWTimeValid(p); err != nil {
  301. return
  302. }
  303. if count > 0 {
  304. err = fmt.Errorf("相同query(%s)该位置已有运营卡片", v.Value)
  305. }
  306. }
  307. return
  308. }
  309. //AddSearchWeb add SearchWeb
  310. func (s *Service) AddSearchWeb(c context.Context, param *show.SearchWebAP, name string, uid int64) (err error) {
  311. p := &show.SWTimeValid{
  312. Priority: param.Priority,
  313. STime: param.Stime,
  314. ETime: param.Etime,
  315. Query: param.Query,
  316. CardValue: param.CardValue,
  317. }
  318. if err = s.Validate(p); err != nil {
  319. return
  320. }
  321. if err = s.showDao.SearchWebAdd(param); err != nil {
  322. return
  323. }
  324. if err = util.AddLogs(common.LogSWEB, name, uid, 0, _ActAddSearchWeb, param); err != nil {
  325. log.Error("searchSvc.AddSearchWeb AddLog error(%v)", err)
  326. return
  327. }
  328. return
  329. }
  330. //UpdateSearchWeb update SearchWeb
  331. func (s *Service) UpdateSearchWeb(c context.Context, param *show.SearchWebUP, name string, uid int64) (err error) {
  332. var (
  333. swValue *show.SearchWeb
  334. )
  335. p := &show.SWTimeValid{
  336. ID: param.ID,
  337. Priority: param.Priority,
  338. STime: param.Stime,
  339. ETime: param.Etime,
  340. Query: param.Query,
  341. CardValue: param.CardValue,
  342. }
  343. if err = s.Validate(p); err != nil {
  344. return
  345. }
  346. if swValue, err = s.showDao.SWFindByID(param.ID); err != nil {
  347. log.Error("searchSvc.UpdateSearchWeb AddLog error(%v)", err)
  348. return
  349. }
  350. //待审核&已通过&已生效-》编辑-》状态不变;其它-》编辑-》审待核
  351. cTime := time.Now().Unix()
  352. if (swValue.Check == common.Verify) ||
  353. (swValue.Check == common.Pass && swValue.Stime.Time().Unix() > cTime ||
  354. (swValue.Check == common.Pass && (cTime > swValue.Stime.Time().Unix() && cTime <= swValue.Stime.Time().Unix()))) {
  355. param.Check = swValue.Check
  356. param.Status = swValue.Status
  357. } else {
  358. param.Check = common.Verify
  359. param.Status = common.StatusDownline
  360. }
  361. if err = s.showDao.SearchWebUpdate(param); err != nil {
  362. return
  363. }
  364. if err = util.AddLogs(common.LogSWEB, name, uid, 0, _ActUpSearchWeb, param); err != nil {
  365. log.Error("searchSvc.UpdateSearchWeb AddLog error(%v)", err)
  366. return
  367. }
  368. return
  369. }
  370. //DeleteSearchWeb delete SearchWeb
  371. func (s *Service) DeleteSearchWeb(id int64, name string, uid int64) (err error) {
  372. if err = s.showDao.SearchWebDelete(id); err != nil {
  373. return
  374. }
  375. if err = util.AddLogs(common.LogSWEB, name, uid, id, _ActDelSearchWeb, id); err != nil {
  376. log.Error("searchSvc.DeleteSearchWeb AddLog error(%v)", err)
  377. return
  378. }
  379. return
  380. }
  381. //OptionSearchWeb option SearchWeb
  382. func (s *Service) OptionSearchWeb(id int64, opt string, name string, uid int64) (err error) {
  383. up := &show.SearchWebOption{}
  384. if opt == common.OptionOnline {
  385. up.Status = common.StatusOnline
  386. up.Check = common.Pass
  387. } else if opt == common.OptionHidden {
  388. up.Status = common.StatusDownline
  389. up.Check = common.InValid
  390. } else if opt == common.OptionPass {
  391. up.Status = common.StatusOnline
  392. up.Check = common.Pass
  393. } else if opt == common.OptionReject {
  394. up.Status = common.StatusDownline
  395. up.Check = common.Rejecte
  396. } else {
  397. err = fmt.Errorf("参数不合法")
  398. return
  399. }
  400. up.ID = id
  401. if err = s.showDao.SearchWebOption(up); err != nil {
  402. return
  403. }
  404. logParam := map[string]interface{}{
  405. "id": id,
  406. "opt": opt,
  407. "up": up,
  408. }
  409. if err = util.AddLogs(common.LogSWEB, name, uid, id, _ActOptSearchWeb, logParam); err != nil {
  410. log.Error("searchSvc.OptionSearchWeb AddLog error(%v)", err)
  411. return
  412. }
  413. return
  414. }