subject.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/activity/model"
  6. articlemodel "go-common/app/interface/openplatform/article/model"
  7. "go-common/library/log"
  8. "go-common/library/net/metadata"
  9. "github.com/jinzhu/gorm"
  10. )
  11. // GetArticleMetas from rpc .
  12. func (s *Service) GetArticleMetas(c context.Context, aids []int64) (res map[int64]*articlemodel.Meta, err error) {
  13. if res, err = s.artRPC.ArticleMetas(c, &articlemodel.ArgAids{Aids: aids}); err != nil {
  14. log.Error("s.ArticleMetas(%v) error(%v)", aids, err)
  15. }
  16. return
  17. }
  18. // SubjectList get subject list .
  19. func (s *Service) SubjectList(c context.Context, listParams *model.ListSub) (listRes *model.SubListRes, err error) {
  20. var (
  21. count int64
  22. list []*model.ActSubject
  23. )
  24. db := s.DB
  25. if listParams.Keyword != "" {
  26. names := listParams.Keyword + "%"
  27. db = db.Where("`id` = ? or `name` like ? or `author` like ?", listParams.Keyword, names, names)
  28. }
  29. if listParams.Sctime != 0 {
  30. parseScime := time.Unix(listParams.Sctime, 0)
  31. db = db.Where("ctime >= ?", parseScime.Format("2006-01-02 15:04:05"))
  32. }
  33. if listParams.Ectime != 0 {
  34. parseEcime := time.Unix(listParams.Ectime, 0)
  35. db = db.Where("etime <= ?", parseEcime.Format("2006-01-02 15:04:05"))
  36. }
  37. if len(listParams.States) > 0 {
  38. db = db.Where("state in (?)", listParams.States)
  39. }
  40. if len(listParams.Types) > 0 {
  41. db = db.Where("type in (?)", listParams.Types)
  42. }
  43. if err = db.Offset((listParams.Page - 1) * listParams.PageSize).Limit(listParams.PageSize).Order("id desc").Find(&list).Error; err != nil && err != gorm.ErrRecordNotFound {
  44. log.Error(" db.Model(&model.ActSubject{}).Find() args(%v) error(%v)", listParams, err)
  45. return
  46. }
  47. if err = db.Model(&model.ActSubject{}).Count(&count).Error; err != nil {
  48. log.Error("db.Model(&model.ActSubject{}).Count() args(%v) error(%v)", listParams, err)
  49. return
  50. }
  51. listRes = &model.SubListRes{
  52. List: list,
  53. Page: &model.PageRes{
  54. Num: listParams.Page,
  55. Size: listParams.PageSize,
  56. Total: count,
  57. },
  58. }
  59. return
  60. }
  61. // VideoList .
  62. func (s *Service) VideoList(c context.Context) (res []*model.ActSubjectResult, err error) {
  63. var (
  64. types = []int{1, 4}
  65. list []*model.ActSubject
  66. likeList []*model.Like
  67. )
  68. db := s.DB
  69. if err = db.Where("state = ?", 1).Where("type in (?)", types).Find(&list).Error; err != nil && err != gorm.ErrRecordNotFound {
  70. log.Error("db.Model(&model.ActSubject{}).Where(state = ?, 1).Where(type in (?), %v).Find() error(%v)", types, err)
  71. return
  72. }
  73. listCount := len(list)
  74. if listCount == 0 {
  75. return
  76. }
  77. sids := make([]int64, 0, listCount)
  78. for _, value := range list {
  79. sids = append(sids, value.ID)
  80. }
  81. if err = db.Where("sid in (?)", sids).Where("wid > ?", 0).Find(&likeList).Error; err != nil && err != gorm.ErrRecordNotFound {
  82. log.Error("db.Model(&model.Like{}).Where(sid in (?), %v).Find() error(%v)", sids, err)
  83. return
  84. }
  85. hashList := make(map[int64][]int64)
  86. for _, value := range likeList {
  87. hashList[value.Sid] = append(hashList[value.Sid], value.Wid)
  88. }
  89. res = make([]*model.ActSubjectResult, 0, len(list))
  90. for _, value := range list {
  91. rs := &model.ActSubjectResult{
  92. ActSubject: value,
  93. }
  94. if v, ok := hashList[value.ID]; ok {
  95. rs.Aids = v
  96. }
  97. res = append(res, rs)
  98. }
  99. return
  100. }
  101. // AddActSubject .
  102. func (s *Service) AddActSubject(c context.Context, params *model.AddList) (res int64, err error) {
  103. if params.ScreenSet != 2 {
  104. params.ScreenSet = 1
  105. }
  106. protect := &model.ActSubjectProtocol{
  107. Protocol: params.Protocol,
  108. Types: params.Types,
  109. Pubtime: params.Pubtime,
  110. Deltime: params.Deltime,
  111. Editime: params.Editime,
  112. Tags: params.Tags,
  113. Hot: params.Hot,
  114. BgmID: params.BgmID,
  115. Oids: params.Oids,
  116. ScreenSet: params.ScreenSet,
  117. PasterID: params.PasterID,
  118. }
  119. actTime := &model.ActTimeConfig{
  120. Interval: params.Interval,
  121. Tlimit: params.Tlimit,
  122. Ltime: params.Ltime,
  123. }
  124. if params.Tags != "" {
  125. if err = s.dao.AddTags(c, params.Tags, metadata.String(c, metadata.RemoteIP)); err != nil {
  126. log.Error("s.AddTags(%s,) error(%v)", params.Tags, err)
  127. return
  128. }
  129. }
  130. actSub := &model.ActSubject{
  131. Oid: params.ActSubject.Oid,
  132. Type: params.ActSubject.Type,
  133. State: params.ActSubject.State,
  134. Level: params.ActSubject.Level,
  135. Flag: params.ActSubject.Flag,
  136. Rank: params.ActSubject.Rank,
  137. Stime: params.ActSubject.Stime,
  138. Etime: params.ActSubject.Etime,
  139. Lstime: params.ActSubject.Lstime,
  140. Letime: params.ActSubject.Letime,
  141. Uetime: params.ActSubject.Uetime,
  142. Ustime: params.ActSubject.Ustime,
  143. Name: params.ActSubject.Name,
  144. Author: params.ActSubject.Author,
  145. ActURL: params.ActSubject.ActURL,
  146. Cover: params.ActSubject.Cover,
  147. Dic: params.ActSubject.Dic,
  148. H5Cover: params.ActSubject.H5Cover,
  149. LikeLimit: params.ActSubject.LikeLimit,
  150. AndroidURL: params.ActSubject.AndroidURL,
  151. IosURL: params.ActSubject.IosURL,
  152. }
  153. if err = s.DB.Create(actSub).Error; err != nil {
  154. log.Error("s.DB.Create(%v) error(%v)", actSub, err)
  155. return
  156. }
  157. protect.Sid = actSub.ID
  158. if err = s.DB.Create(protect).Error; err != nil {
  159. log.Error("s.DB.Create(%v) error(%v)", protect, err)
  160. return
  161. }
  162. if params.Type == model.ONLINEVOTE {
  163. actTime.Sid = actSub.ID
  164. if err = s.DB.Create(actTime).Error; err != nil {
  165. log.Error("s.DB.Create(%v) error(%v)", actTime, err)
  166. return
  167. }
  168. }
  169. res = actSub.ID
  170. return
  171. }
  172. // UpActSubject .
  173. func (s *Service) UpActSubject(c context.Context, params *model.AddList, sid int64) (res int64, err error) {
  174. if params.ScreenSet != 2 {
  175. params.ScreenSet = 1
  176. }
  177. onlineData := &model.ActTimeConfig{
  178. Interval: params.Interval,
  179. Tlimit: params.Tlimit,
  180. Ltime: params.Ltime,
  181. }
  182. actSubject := new(model.ActSubject)
  183. if err = s.DB.Where("id = ?", sid).Last(actSubject).Error; err != nil {
  184. log.Error("s.DB.Where(id = ?, %d).Last(%v) error(%v)", sid, actSubject, err)
  185. return
  186. }
  187. data := map[string]interface{}{
  188. "Oid": params.Oid,
  189. "Type": params.Type,
  190. "State": params.State,
  191. "Level": params.Level,
  192. "Flag": params.Flag,
  193. "Rank": params.Rank,
  194. "Stime": params.Stime,
  195. "Etime": params.Etime,
  196. "Lstime": params.Lstime,
  197. "Uetime": params.Uetime,
  198. "Ustime": params.Ustime,
  199. "Name": params.Name,
  200. "Author": params.Author,
  201. "ActURL": params.ActURL,
  202. "Cover": params.Cover,
  203. "Dic": params.Dic,
  204. "H5Cover": params.H5Cover,
  205. "LikeLimit": params.LikeLimit,
  206. "AndroidURL": params.AndroidURL,
  207. "IosURL": params.IosURL,
  208. }
  209. if err = s.DB.Model(&model.ActSubject{}).Where("id = ?", sid).Update(data).Error; err != nil {
  210. log.Error("s.DB.Model(&model.ActSubject{}).Where(id = ?, %d).Update(%v) error(%v)", sid, data, err)
  211. return
  212. }
  213. item := new(model.ActSubjectProtocol)
  214. if err = s.DB.Where("sid = ? ", sid).Last(item).Error; err != nil && err != gorm.ErrRecordNotFound {
  215. log.Error("s.DB.Where(sid = ? , %d).Last(%v) error(%v)", sid, item, err)
  216. return
  217. }
  218. //item有值
  219. if item.ID > 0 {
  220. if params.Tags != "" {
  221. if item.Tags != params.Tags {
  222. if err = s.dao.AddTags(c, params.Tags, metadata.String(c, metadata.RemoteIP)); err != nil {
  223. log.Error("s.AddTags(%s) error(%v)", params.Tags, err)
  224. return
  225. }
  226. }
  227. }
  228. upProtectData := map[string]interface{}{
  229. "Protocol": params.Protocol,
  230. "Types": params.Types,
  231. "Pubtime": params.Pubtime,
  232. "Deltime": params.Deltime,
  233. "Editime": params.Editime,
  234. "Hot": params.Hot,
  235. "BgmID": params.BgmID,
  236. "Oids": params.Oids,
  237. "ScreenSet": params.ScreenSet,
  238. "PasterID": params.PasterID,
  239. "Tags": params.Tags,
  240. }
  241. if err = s.DB.Model(&model.ActSubjectProtocol{}).Where("id = ?", item.ID).Update(upProtectData).Error; err != nil {
  242. log.Error("s.DB.Model(&model.ActSubjectProtocol{}).Where(id = ?, %d).Update(%v) error(%v)", item.ID, upProtectData, err)
  243. return
  244. }
  245. } else {
  246. protectDtata := &model.ActSubjectProtocol{
  247. Protocol: params.Protocol,
  248. Types: params.Types,
  249. Pubtime: params.Pubtime,
  250. Deltime: params.Deltime,
  251. Editime: params.Editime,
  252. Hot: params.Hot,
  253. BgmID: params.BgmID,
  254. Oids: params.Oids,
  255. ScreenSet: params.ScreenSet,
  256. PasterID: params.PasterID,
  257. Sid: sid,
  258. }
  259. if err = s.DB.Create(protectDtata).Error; err != nil {
  260. log.Error("s.DB.Create(%v) error(%v)", protectDtata, err)
  261. return
  262. }
  263. }
  264. if actSubject.Type == model.ONLINEVOTE {
  265. onlineData.Sid = sid
  266. output := new(model.ActTimeConfig)
  267. if err = s.DB.Where("sid = ?", sid).Last(output).Error; err != nil && err != gorm.ErrRecordNotFound {
  268. log.Error("s.DB.Where(sid = ?, %d).Last(%v) error(%v)", sid, output, err)
  269. return
  270. }
  271. if output.ID > 0 {
  272. if err = s.DB.Model(&model.ActTimeConfig{}).Where("id = ?", output.ID).Update(onlineData).Error; err != nil {
  273. log.Error("s.DB.Model(&model.ActTimeConfig{}).Where(id = ?, %d).Update(%v) error(%v)", output.ID, onlineData, err)
  274. return
  275. }
  276. }
  277. }
  278. res = sid
  279. return
  280. }
  281. // SubProtocol .
  282. func (s *Service) SubProtocol(c context.Context, sid int64) (res *model.ActSubjectProtocol, err error) {
  283. res = &model.ActSubjectProtocol{}
  284. if err = s.DB.Where("sid = ?", sid).First(res).Error; err != nil && err != gorm.ErrRecordNotFound {
  285. log.Error("s.DB.Where(sid = %d ) error(%v)", sid, err)
  286. }
  287. return
  288. }
  289. // TimeConf .
  290. func (s *Service) TimeConf(c context.Context, sid int64) (res *model.ActTimeConfig, err error) {
  291. res = new(model.ActTimeConfig)
  292. if err = s.DB.Where("sid = ?", sid).First(res).Error; err != nil && err != gorm.ErrRecordNotFound {
  293. log.Error("actSrv.DB.Where(sid = ?, %d) error(%v)", sid, err)
  294. }
  295. return
  296. }