likes.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "time"
  8. "go-common/app/admin/main/activity/model"
  9. tagmdl "go-common/app/interface/main/tag/model"
  10. artmdl "go-common/app/interface/openplatform/article/model"
  11. acccli "go-common/app/service/main/account/api"
  12. arcmdl "go-common/app/service/main/archive/api"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. "go-common/library/net/metadata"
  16. "go-common/library/sync/errgroup"
  17. xtime "go-common/library/time"
  18. "go-common/library/xstr"
  19. "github.com/jinzhu/gorm"
  20. )
  21. const (
  22. _approved = 1
  23. _pending = 0
  24. _tagArcType = 3
  25. )
  26. // LikesList .
  27. func (s *Service) LikesList(c context.Context, arg *model.LikesParam) (outRes *model.LikesRes, err error) {
  28. var (
  29. likeSubject *model.ActSubject
  30. list []*model.Like
  31. likeList map[int64]*model.Like
  32. ids, wids, mids []int64
  33. count int64
  34. offset int
  35. )
  36. if likeSubject, err = s.dao.ActSubject(c, arg.Sid); err != nil {
  37. return
  38. }
  39. db := s.DB
  40. db = db.Where("sid = ?", likeSubject.ID)
  41. if len(arg.States) > 0 {
  42. db = db.Where("state in (?)", arg.States)
  43. }
  44. if arg.Mid > 0 {
  45. db = db.Where("mid = ?", arg.Mid)
  46. }
  47. if arg.Wid > 0 {
  48. db = db.Where("wid = ?", arg.Wid)
  49. }
  50. if err = db.Model(model.Like{}).Count(&count).Error; err != nil {
  51. log.Error("db.Model(model.Like{}).Count() arg(%v) error(%v) ", arg, err)
  52. return
  53. }
  54. offset = (arg.Page - 1) * arg.PageSize
  55. if err = db.Offset(offset).Limit(arg.PageSize).Order("id asc").Find(&list).Error; err != nil && err != gorm.ErrRecordNotFound {
  56. log.Error("db.Model(model.Like{}).Find() arg(%v) error(%v)", arg, err)
  57. return
  58. }
  59. likeList = make(map[int64]*model.Like, len(list))
  60. ids = make([]int64, 0, len(list))
  61. wids = make([]int64, 0, len(list))
  62. mids = make([]int64, 0, len(list))
  63. for _, val := range list {
  64. ids = append(ids, val.ID)
  65. wids = append(wids, val.Wid)
  66. mids = append(mids, val.Mid)
  67. likeList[val.ID] = val
  68. //like 根据最新的逻辑获取,现在无法获取 todo
  69. likeList[val.ID].Like = 0
  70. }
  71. if len(list) > 0 {
  72. if err = s.GetContent(c, likeSubject.Type, likeList, ids, wids, mids); err != nil {
  73. log.Error("s.GetContent(%d,%v,%v,%v,%v) error(%v)", likeSubject.Type, likeList, ids, mids, wids, err)
  74. return
  75. }
  76. }
  77. outRes = &model.LikesRes{
  78. Likes: likeList,
  79. }
  80. outRes.Size = arg.PageSize
  81. outRes.Num = arg.Page
  82. outRes.Total = count
  83. return
  84. }
  85. // Likes .
  86. func (s *Service) Likes(c context.Context, Sid int64, lids []int64) (likeList map[int64]*model.Like, err error) {
  87. var (
  88. likeSubject *model.ActSubject
  89. like []*model.Like
  90. ids, wids, mids []int64
  91. )
  92. if likeSubject, err = s.dao.ActSubject(c, Sid); err != nil {
  93. return
  94. }
  95. if err = s.DB.Where("id in (?)", lids).Find(&like).Error; err != nil {
  96. log.Error("s.DB.Where(id in (%v)).Find() error(%v)", lids, err)
  97. return
  98. }
  99. likeList = make(map[int64]*model.Like, len(like))
  100. ids = make([]int64, 0, len(like))
  101. wids = make([]int64, 0, len(like))
  102. mids = make([]int64, 0, len(like))
  103. for _, val := range like {
  104. ids = append(ids, val.ID)
  105. wids = append(wids, val.Wid)
  106. mids = append(mids, val.Mid)
  107. likeList[val.ID] = val
  108. //like 根据最新的逻辑获取,现在无法获取 todo
  109. likeList[val.ID].Like = 0
  110. }
  111. if len(like) > 0 {
  112. if err = s.GetContent(c, likeSubject.Type, likeList, ids, wids, mids); err != nil {
  113. log.Error("s.GetContent( %d, %v, %v, %v, %v) error(%v)", likeSubject.Type, likeList, ids, mids, wids, err)
  114. }
  115. }
  116. return
  117. }
  118. // archiveWithTag get archives and tags.
  119. func (s *Service) archiveWithTag(c context.Context, aids []int64, likes map[int64]*model.Like) (err error) {
  120. var (
  121. archives *arcmdl.ArcsReply
  122. arcErr, tagErr error
  123. tags map[int64][]*tagmdl.Tag
  124. ip = metadata.String(c, metadata.RemoteIP)
  125. )
  126. group, errCtx := errgroup.WithContext(c)
  127. group.Go(func() error {
  128. if archives, arcErr = s.arcClient.Arcs(errCtx, &arcmdl.ArcsRequest{Aids: aids}); err != nil {
  129. log.Error("s.arcClient.Arcs(%v, %s) error(%v)", aids, ip, err)
  130. return arcErr
  131. }
  132. return nil
  133. })
  134. group.Go(func() error {
  135. arg := &tagmdl.ArgResTags{Oids: aids, Type: _tagArcType, RealIP: ip}
  136. if tags, tagErr = s.tagRPC.ResTags(errCtx, arg); tagErr != nil {
  137. log.Error("ResTags接口错误 s.tag.ResTag(%+v) error(%v)", arg, tagErr)
  138. return tagErr
  139. }
  140. return nil
  141. })
  142. if err = group.Wait(); err != nil {
  143. return
  144. }
  145. for _, val := range likes {
  146. if val.Wid != 0 {
  147. tem := make(map[string]interface{}, 2)
  148. if arch, ok := archives.Arcs[val.Wid]; ok && arch.IsNormal() {
  149. tem["archives"] = arch
  150. }
  151. if tag, ok := tags[val.Wid]; ok {
  152. temps := make([]string, 0, len(tag))
  153. for _, val := range tag {
  154. temps = append(temps, val.Name)
  155. }
  156. tem["tags"] = temps
  157. }
  158. val.Object = tem
  159. }
  160. }
  161. return
  162. }
  163. // accountAndContent get likecontent and accountinfo .
  164. func (s *Service) accountAndContent(c context.Context, ids []int64, mids []int64, likes map[int64]*model.Like) (err error) {
  165. var (
  166. contents map[int64]*model.LikeContent
  167. accRly *acccli.CardsReply
  168. )
  169. if contents, err = s.dao.GetLikeContent(c, ids); err != nil {
  170. log.Error(" s.dao.GetLikeContent(%v) error(%v)", ids, err)
  171. return
  172. }
  173. if accRly, err = s.accClient.Cards3(c, &acccli.MidsReq{Mids: mids}); err != nil {
  174. log.Error("s.AccountsInfo(%v) error(%v)", mids, err)
  175. return
  176. }
  177. for _, val := range likes {
  178. temp := make(map[string]interface{}, 2)
  179. if cont, ok := contents[val.ID]; ok {
  180. temp["content"] = cont
  181. }
  182. if val.Mid != 0 && accRly != nil {
  183. if acct, ok := accRly.Cards[val.Mid]; ok {
  184. temp["owner"] = map[string]interface{}{
  185. "mid": acct.Mid,
  186. "name": acct.Name,
  187. "face": acct.Face,
  188. "sex": acct.Sex,
  189. "level": acct.Level,
  190. }
  191. }
  192. }
  193. val.Object = temp
  194. }
  195. return
  196. }
  197. // articles .
  198. func (s *Service) articles(c context.Context, wids []int64, likes map[int64]*model.Like) (err error) {
  199. var artiRes map[int64]*artmdl.Meta
  200. if artiRes, err = s.artRPC.ArticleMetas(c, &artmdl.ArgAids{Aids: wids}); err != nil {
  201. log.Error("s.ArticleMetas(%v) error(%v)", wids, err)
  202. return
  203. }
  204. for _, val := range likes {
  205. if val.Wid != 0 {
  206. if v, ok := artiRes[val.Wid]; ok {
  207. val.Object = map[string]interface{}{
  208. "article": v,
  209. }
  210. }
  211. }
  212. }
  213. return
  214. }
  215. // musicsAndAct .
  216. func (s *Service) musicsAndAct(c context.Context, wids, mids []int64, likes map[int64]*model.Like) (err error) {
  217. var (
  218. musics *model.MusicRes
  219. accRly *acccli.CardsReply
  220. ip = metadata.String(c, metadata.RemoteIP)
  221. )
  222. if musics, err = s.dao.Musics(c, wids, ip); err != nil {
  223. log.Error("s.dao.Musics(%v) error(%+v)", wids, err)
  224. return
  225. }
  226. if accRly, err = s.accClient.Cards3(c, &acccli.MidsReq{Mids: mids}); err != nil {
  227. log.Error("s.AccountsInfo(%v) error(%v)", mids, err)
  228. return
  229. }
  230. for _, val := range likes {
  231. temp := make(map[string]interface{}, 2)
  232. if v, ok := musics.Data[val.Wid]; ok {
  233. temp["music"] = v
  234. }
  235. if val.Mid != 0 && accRly != nil {
  236. if acct, ok := accRly.Cards[val.Mid]; ok {
  237. temp["owner"] = map[string]interface{}{
  238. "mid": acct.Mid,
  239. "name": acct.Name,
  240. "face": acct.Face,
  241. "sex": acct.Sex,
  242. "level": acct.Level,
  243. }
  244. }
  245. }
  246. val.Object = temp
  247. }
  248. return
  249. }
  250. // GetContent get act_subjet extensions .
  251. func (s *Service) GetContent(c context.Context, likeSubType int, likes map[int64]*model.Like, ids []int64, wids []int64, mids []int64) (err error) {
  252. switch likeSubType {
  253. case model.PICTURE, model.PICTURELIKE, model.DRAWYOO, model.DRAWYOOLIKE, model.TEXT, model.TEXTLIKE, model.QUESTION:
  254. err = s.accountAndContent(c, ids, mids, likes)
  255. case model.VIDEO, model.VIDEOLIKE, model.ONLINEVOTE, model.VIDEO2, model.PHONEVIDEO, model.SMALLVIDEO:
  256. err = s.archiveWithTag(c, wids, likes)
  257. case model.ARTICLE:
  258. err = s.articles(c, wids, likes)
  259. case model.MUSIC:
  260. err = s.musicsAndAct(c, wids, mids, likes)
  261. default:
  262. err = ecode.RequestErr
  263. }
  264. return
  265. }
  266. // ItemUp .
  267. func (s *Service) ItemUp(c context.Context, sid, wid int64, state int) (likeList *model.Like, err error) {
  268. var likeSubject *model.ActSubject
  269. if likeSubject, err = s.dao.ActSubject(c, sid); err != nil {
  270. log.Error("s.GetLikesSubjectByID(%d) error(%v)", sid, err)
  271. return
  272. }
  273. likeList = new(model.Like)
  274. if err = s.DB.Where("sid =?", likeSubject.ID).Where("wid = ?", wid).Last(likeList).Error; err != nil && err != gorm.ErrRecordNotFound {
  275. log.Error("s.DB.Model(model.Like{}).Where(sid =? %d).Where(wid = ?, %d).Last(),error(%v)", likeSubject.ID, wid, err)
  276. return
  277. }
  278. if likeList.ID == 0 {
  279. err = nil
  280. return
  281. }
  282. likeList.State = state
  283. likeList.Mtime = xtime.Time(time.Now().Unix())
  284. if err = s.DB.Model(&model.Like{}).Where("id =?", likeList.ID).Update(map[string]interface{}{"state": state, "mtime": likeList.Mtime.Time().Format("2006-01-02 15:04:05")}).Error; err != nil {
  285. log.Error("s.DB.Model(&model.Like{}).Where(id =?, %d).Update() error(%v) ", likeList.ID, err)
  286. }
  287. return
  288. }
  289. // ItemAdd .
  290. func (s *Service) ItemAdd(c context.Context, args *model.AddLikes) (likeList *model.Like, err error) {
  291. var (
  292. likeSubject *model.ActSubject
  293. ip = metadata.String(c, metadata.RemoteIP)
  294. )
  295. if likeSubject, err = s.dao.ActSubject(c, args.Sid); err != nil {
  296. log.Error(" s.GetLikesSubjectByID(%d) error(%v) ", args.Sid, err)
  297. return
  298. }
  299. //怀疑原代码这里是个bug?? todo
  300. if args.Type != likeSubject.Type {
  301. err = errors.New("ItemAdd params liketype error")
  302. return
  303. }
  304. likeList = new(model.Like)
  305. if err = s.DB.Where("sid =?", likeSubject.ID).Where("wid = ?", args.Wid).Last(likeList).Error; err != nil && err != gorm.ErrRecordNotFound {
  306. log.Error("s.DB.Model(model.Like{}).Where(sid =?, %d).Where(wid = ?, %d).Last() error(%v)", likeSubject.ID, args.Wid, err)
  307. return
  308. }
  309. if err == gorm.ErrRecordNotFound {
  310. addLike := &model.Like{
  311. Type: args.Type,
  312. Wid: args.Wid,
  313. Sid: likeSubject.ID,
  314. Mid: args.Mid,
  315. State: args.State,
  316. }
  317. ipv6 := net.ParseIP(ip)
  318. if ipv6 == nil {
  319. ipv6 = []byte{}
  320. }
  321. addLikeContent := &model.LikeContent{
  322. Plat: args.Plat,
  323. Device: args.Device,
  324. Message: "no",
  325. Image: "no",
  326. IPv6: ipv6,
  327. }
  328. tx := s.DB.Begin()
  329. if err = tx.Create(addLike).Error; err != nil {
  330. log.Error("db.Model(&model.Like{}).Create(%v) error(%v)", addLike, err)
  331. tx.Rollback()
  332. return
  333. }
  334. addLikeContent.ID = addLike.ID
  335. if err = tx.Create(addLikeContent).Error; err != nil {
  336. log.Error("db.Model(&model.LikeContent{}).Create(%v) error(%v)\n", addLikeContent, err)
  337. tx.Rollback()
  338. return
  339. }
  340. tx.Commit()
  341. likeList = addLike
  342. } else {
  343. likeList.State = args.State
  344. likeList.Mtime = xtime.Time(time.Now().Unix())
  345. if err = s.DB.Model(&model.Like{}).Where("id =?", likeList.ID).Update(map[string]interface{}{"state": args.State, "mtime": likeList.Mtime.Time().Format("2006-01-02 15:04:05")}).Error; err != nil {
  346. log.Error("s.DB.Model(&model.Like{}).Where(id =?, %d).Update() error(%v) ", likeList.ID, err)
  347. }
  348. }
  349. return
  350. }
  351. // AddPicContent .
  352. func (s *Service) AddPicContent(c context.Context, args *model.AddPic) (likeID int64, err error) {
  353. var (
  354. likeSubject *model.ActSubject
  355. ip = metadata.String(c, metadata.RemoteIP)
  356. )
  357. if likeSubject, err = s.dao.ActSubject(c, args.Sid); err != nil {
  358. log.Error(" s.GetLikesSubjectByID(%d) error(%v) ", args.Sid, err)
  359. return
  360. }
  361. likes := &model.Like{
  362. Sid: likeSubject.ID,
  363. Wid: args.Wid,
  364. Type: args.Type,
  365. Mid: args.Mid,
  366. State: _pending,
  367. }
  368. ipv6 := net.ParseIP(ip)
  369. if ipv6 == nil {
  370. ipv6 = []byte{}
  371. }
  372. likeContent := &model.LikeContent{
  373. IPv6: ipv6,
  374. Plat: args.Plat,
  375. Device: args.Device,
  376. Message: args.Message,
  377. Link: args.Link,
  378. Image: args.Image, //只支持传image串(需要支持传文件)todo
  379. }
  380. tx := s.DB.Begin()
  381. if err = tx.Create(likes).Error; err != nil {
  382. log.Error("db.Model(&model.Like{}).Create(%v) error(%v)", likes, err)
  383. tx.Rollback()
  384. return
  385. }
  386. likeContent.ID = likes.ID
  387. if err = tx.Create(likeContent).Error; err != nil {
  388. log.Error("db.Model(&model.LikeContent{}).Create(%v) error(%v)\n", likeContent, err)
  389. tx.Rollback()
  390. return
  391. }
  392. tx.Commit()
  393. likeID = likes.ID // need to update todo
  394. return
  395. }
  396. // BatchLikes .
  397. func (s *Service) BatchLikes(c context.Context, args *model.BatchLike) (err error) {
  398. var (
  399. likes []*model.Like
  400. likeMap map[int64]*model.Like
  401. addWid []int64
  402. ipv6 = net.ParseIP(metadata.String(c, metadata.RemoteIP))
  403. )
  404. if ipv6 == nil {
  405. ipv6 = []byte{}
  406. }
  407. if err = s.DB.Where(fmt.Sprintf("sid = ? and wid in (%s)", xstr.JoinInts(args.Wid)), args.Sid).Find(&likes).Error; err != nil && err != gorm.ErrRecordNotFound {
  408. log.Error("s.DB.Where(%d,%v) error(%v)", args.Sid, args.Wid, err)
  409. return
  410. }
  411. if err == gorm.ErrRecordNotFound {
  412. addWid = args.Wid
  413. } else {
  414. likeMap = make(map[int64]*model.Like, len(likes))
  415. for _, v := range likes {
  416. likeMap[v.Wid] = v
  417. }
  418. addWid = make([]int64, 0, len(args.Wid))
  419. for _, v := range args.Wid {
  420. if _, ok := likeMap[v]; !ok {
  421. addWid = append(addWid, v)
  422. }
  423. }
  424. }
  425. if len(addWid) == 0 {
  426. return
  427. }
  428. item := &model.Like{
  429. Sid: args.Sid,
  430. Mid: args.Mid,
  431. Type: args.Type,
  432. State: _pending,
  433. }
  434. if err = s.dao.BatchLike(c, item, addWid, ipv6); err != nil {
  435. log.Error("s.dao.BatchLike(%v) error(%+v)", addWid, err)
  436. }
  437. return
  438. }
  439. // VideoAdd .
  440. func (s *Service) VideoAdd(c context.Context, args *model.AddLikes) (likeList *model.Like, err error) {
  441. var (
  442. likeSubject *model.ActSubject
  443. ip = metadata.String(c, metadata.RemoteIP)
  444. )
  445. if likeSubject, err = s.dao.ActSubject(c, args.Sid); err != nil {
  446. log.Error("s.dao.ActSubject(%d) error(%v) ", args.Sid, err)
  447. return
  448. }
  449. if likeSubject.Type != model.VIDEO && likeSubject.Type != model.VIDEOLIKE && likeSubject.Type != model.VIDEO2 {
  450. err = errors.New("VideoAdd params liketype error")
  451. return
  452. }
  453. likeList = new(model.Like)
  454. if err = s.DB.Where("sid =?", likeSubject.ID).Where("wid = ?", args.Wid).Last(likeList).Error; err != nil && err != gorm.ErrRecordNotFound {
  455. log.Error("s.DB.Model(&model.Like{}).Where(sid =?, %d).Where(wid = ?, %d).Last() error(%v)", likeSubject.ID, args.Wid, err)
  456. return
  457. }
  458. if err == gorm.ErrRecordNotFound {
  459. addLike := &model.Like{
  460. Type: args.Type,
  461. Wid: args.Wid,
  462. Sid: likeSubject.ID,
  463. Mid: args.Mid,
  464. State: _approved,
  465. }
  466. ipv6 := net.ParseIP(ip)
  467. if ipv6 == nil {
  468. ipv6 = []byte{}
  469. }
  470. addLikeContent := &model.LikeContent{
  471. IPv6: ipv6,
  472. Plat: args.Plat,
  473. Device: args.Device,
  474. Message: "no",
  475. Image: "no",
  476. }
  477. tx := s.DB.Begin()
  478. if err = tx.Create(addLike).Error; err != nil {
  479. log.Error("db.Model(&model.Like{}).Create(%v) error(%v)", addLike, err)
  480. tx.Rollback()
  481. return
  482. }
  483. addLikeContent.ID = addLike.ID
  484. if err = tx.Create(addLikeContent).Error; err != nil {
  485. log.Error("tx.Model(&model.LikeContent{}).Create(%v) error(%v)", addLikeContent, err)
  486. tx.Rollback()
  487. return
  488. }
  489. tx.Commit()
  490. likeList = addLike
  491. } else {
  492. likeList.State = _approved
  493. likeList.Mtime = xtime.Time(time.Now().Unix())
  494. if err = s.DB.Model(&model.Like{}).Where("id =?", likeList.ID).Update(map[string]interface{}{"state": _approved, "mtime": likeList.Mtime.Time().Format("2006-01-02 15:04:05")}).Error; err != nil {
  495. log.Error("ls.DB.Model(&model.Like{}).Where(id =?, %d).Update() error(%v) ", likeList.ID, err)
  496. }
  497. }
  498. return
  499. }
  500. // UpLikesState .
  501. func (s *Service) UpLikesState(c context.Context, IDs []int64, state int, reply string, username string) (err error) {
  502. if err = s.DB.Model(&model.Like{}).Where("id in (?)", xstr.JoinInts(IDs)).Update(map[string]interface{}{"state": state}).Error; err != nil {
  503. log.Info("s.DB.Model(&model.Like{}).Where(id = ?,%d).Update() error(%v)", IDs, err)
  504. return
  505. }
  506. if reply != "" {
  507. s.DB.Model(&model.LikeContent{}).Where("id in (?)", xstr.JoinInts(IDs)).Update(map[string]interface{}{"reply": reply})
  508. }
  509. if state != 0 {
  510. for _, v := range IDs {
  511. likeLog := &model.ActLikeLog{
  512. Lid: v,
  513. User: username,
  514. State: int64(state),
  515. }
  516. s.DB.Create(likeLog)
  517. }
  518. }
  519. return
  520. }
  521. // UpLike .
  522. func (s *Service) UpLike(c context.Context, args *model.UpLike, username string) (res int64, err error) {
  523. likes := map[string]interface{}{
  524. "Type": args.Type,
  525. "Mid": args.Mid,
  526. "Wid": args.Wid,
  527. "State": args.State,
  528. "StickTop": args.StickTop,
  529. }
  530. if err = s.DB.Model(model.Like{}).Where("id = ?", args.Lid).Update(likes).Error; err != nil {
  531. log.Error("s.DB.Model(model.Like{}).Where(id = ?, %d).Update() error(%v)", args.Lid, err)
  532. return
  533. }
  534. likeContent := map[string]interface{}{
  535. "Message": args.Message,
  536. "Reply": args.Reply,
  537. "Link": args.Link,
  538. "Image": args.Image,
  539. }
  540. if err = s.DB.Model(model.LikeContent{}).Where("id = ?", args.Lid).Update(likeContent).Error; err != nil {
  541. log.Error("s.DB.Model(model.LikeContent{}).Where(id = ?,%d).Update() error(%v)", args.Lid, err)
  542. return
  543. }
  544. if args.State != 0 {
  545. likeLog := &model.ActLikeLog{
  546. Lid: args.Lid,
  547. User: username,
  548. State: int64(args.State),
  549. }
  550. s.DB.Create(likeLog)
  551. }
  552. res = args.Lid
  553. return
  554. }
  555. // AddLike .
  556. func (s *Service) AddLike(c context.Context, args *model.AddLikes) (likesRes *model.Like, err error) {
  557. switch args.DealType {
  558. case "itemUp":
  559. likesRes, err = s.ItemUp(c, args.Sid, args.Wid, args.State)
  560. case "itemAdd":
  561. likesRes, err = s.ItemAdd(c, args)
  562. case "videoAdd":
  563. likesRes, err = s.VideoAdd(c, args)
  564. default:
  565. err = errors.New("type error")
  566. }
  567. return
  568. }
  569. // UpWid .
  570. func (s *Service) UpWid(c context.Context, args *model.UpWid) (err error) {
  571. var (
  572. subject *model.ActSubject
  573. )
  574. if subject, err = s.dao.ActSubject(c, args.Sid); err != nil {
  575. return
  576. }
  577. like := map[string]interface{}{
  578. "state": args.State,
  579. "mtime": xtime.Time(time.Now().Unix()),
  580. }
  581. if err := s.DB.Model(&model.Like{}).Where("sid = ? and wid = ?", subject.ID, args.Wid).Update(like).Limit(1).Error; err != nil {
  582. log.Error("actSrv.DB.Where(sid = %d and wid = %d).Update().Limit(1) error(%v)", subject.ID, args.Wid, err)
  583. }
  584. return
  585. }