activity.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package activity
  2. import (
  3. "errors"
  4. "fmt"
  5. "gorm.io/gorm"
  6. "hongze/hongze_yb/global"
  7. "hongze/hongze_yb/models/response"
  8. "hongze/hongze_yb/models/response/purchase"
  9. "hongze/hongze_yb/models/tables/company_product"
  10. "hongze/hongze_yb/models/tables/rddp/report"
  11. "hongze/hongze_yb/models/tables/research_report"
  12. "hongze/hongze_yb/models/tables/yb_activity"
  13. "hongze/hongze_yb/models/tables/yb_activity_permission"
  14. "hongze/hongze_yb/models/tables/yb_activity_register"
  15. "hongze/hongze_yb/models/tables/yb_activity_remind"
  16. "hongze/hongze_yb/models/tables/yb_activity_voice"
  17. "hongze/hongze_yb/models/tables/yb_config"
  18. "hongze/hongze_yb/services/company"
  19. "hongze/hongze_yb/services/user"
  20. "hongze/hongze_yb/utils"
  21. "log"
  22. "strings"
  23. "time"
  24. )
  25. // ActivityList 活动列表
  26. type ActivityList struct {
  27. yb_activity.ActivityItem // 活动信息
  28. VoiceList []*yb_activity_voice.YbActivityVoice // 音频列表
  29. }
  30. // PageList 活动分页列表
  31. func PageList(condition string, pars []interface{}, page, limit int, order string, userInfo user.UserInfo) (list []ActivityList, err error) {
  32. activities, err := yb_activity.GetPageListByWhere(condition, pars, page, limit, order, int(userInfo.UserID))
  33. if err != nil {
  34. log.Print(err.Error())
  35. return
  36. }
  37. if activities != nil {
  38. var defaultSpeakerHeadPic string
  39. // 获取默认演讲人头像
  40. {
  41. configCond := `config_code = ?`
  42. configPars := make([]interface{}, 0)
  43. configPars = append(configPars, yb_config.DefaultSpeakerHeadPic)
  44. confOB := new(yb_config.YbConfig)
  45. conf, e := confOB.Fetch(configCond, configPars)
  46. if e != nil {
  47. err = e
  48. return
  49. }
  50. defaultSpeakerHeadPic = conf.ConfigValue
  51. }
  52. var activityIds []int
  53. for _, item := range activities {
  54. activityIds = append(activityIds, item.ActivityID)
  55. }
  56. // 录音
  57. voices, queryErr := yb_activity_voice.GetVoicesByActivityIds(activityIds)
  58. if queryErr != nil {
  59. log.Print(queryErr.Error())
  60. return
  61. }
  62. // 提醒
  63. reminds, queryErr := yb_activity_remind.GetRemindsByUserIdAndActivityIds(int(userInfo.UserID), activityIds)
  64. if queryErr != nil {
  65. log.Print(queryErr.Error())
  66. return
  67. }
  68. remindMap := make(map[int]int, 0)
  69. for _, remind := range reminds {
  70. remindMap[remind.ActivityId] = 1
  71. }
  72. // 报名
  73. registers, queryErr := yb_activity_register.GetRegistersByUserIdAndActivityIds(int(userInfo.UserID), activityIds)
  74. if queryErr != nil {
  75. log.Print(queryErr.Error())
  76. return
  77. }
  78. registerMap := make(map[int]int, 0)
  79. for _, register := range registers {
  80. if register.RegisterState == 1 {
  81. registerMap[register.ActivityId] = 1
  82. }
  83. }
  84. var temp ActivityList
  85. for _, activity := range activities {
  86. temp.ActivityItem = activity
  87. temp.WeekString = utils.StrDateTimeToWeek(activity.StartTime.Weekday().String())
  88. temp.ActivityState = getActivityStateByTime(activity.StartTime, activity.EndTime)
  89. // 音频列表
  90. var voiceMap []*yb_activity_voice.YbActivityVoice
  91. for _, voice := range voices {
  92. if temp.ActivityID == int(voice.ActivityID) {
  93. voiceMap = append(voiceMap, voice)
  94. temp.HasPlayBack = 1 // 有回放
  95. }
  96. }
  97. temp.VoiceList = voiceMap
  98. // 是否有提醒
  99. _, ok := remindMap[temp.ActivityID]
  100. if ok {
  101. temp.HasRemind = 1
  102. }
  103. // 是否已报名
  104. _, ok = registerMap[temp.ActivityID]
  105. if ok {
  106. temp.RegisterState = 1
  107. }
  108. // 如果是多图,那么是默认头像
  109. if strings.Count(temp.SpeakerHeadPic, ",") > 0 {
  110. temp.SpeakerHeadPic = defaultSpeakerHeadPic
  111. }
  112. list = append(list, temp)
  113. }
  114. }
  115. return
  116. }
  117. // getActivityStateByTime 根据时间生成活动枚举值
  118. func getActivityStateByTime(startTime, endTime time.Time) (state int) {
  119. nowTime := time.Now().Format(utils.FormatDateTime)
  120. timeNow, _ := time.ParseInLocation(utils.FormatDateTime, nowTime, time.Local)
  121. if timeNow.Before(endTime) && timeNow.After(startTime) {
  122. // 进行中
  123. state = 2
  124. } else if timeNow.After(endTime) {
  125. // 已结束
  126. state = 3
  127. } else if timeNow.Before(startTime) {
  128. // 未开始
  129. state = 1
  130. }
  131. return
  132. }
  133. // GetActivityDetail 活动详情
  134. func GetActivityDetail(activityId, userId int) (detail *yb_activity.ActivityDetail, err error) {
  135. detail, err = yb_activity.GetDetailById(activityId)
  136. if err != nil {
  137. if err == gorm.ErrRecordNotFound {
  138. return
  139. }
  140. }
  141. if detail == nil {
  142. return
  143. }
  144. registeredNum, err := yb_activity_register.CountRegistedNumByActivityId(activityId)
  145. if err != nil {
  146. log.Print(err.Error())
  147. return
  148. }
  149. detail.RegisteredNum = int(registeredNum)
  150. detail.WeekString = utils.StrDateTimeToWeek(detail.StartTime.Weekday().String())
  151. detail.ActivityState = getActivityStateByTime(detail.StartTime, detail.EndTime)
  152. _, remindErr := yb_activity_remind.RemindExist(userId, activityId)
  153. if remindErr != gorm.ErrRecordNotFound {
  154. detail.HasRemind = 1
  155. }
  156. _, registerErr := yb_activity_register.RegisterExist(userId, activityId)
  157. if registerErr != gorm.ErrRecordNotFound {
  158. detail.RegisterState = 1
  159. }
  160. // 判断报告链接对应的报告发布状态,若为未发布则报告链接置空
  161. if detail.ReportId > 0 && detail.ReportLink != "" {
  162. if detail.IsNewReport == 1 {
  163. // 去新报告表中查询
  164. newReport, e := report.GetByReportId(detail.ReportId)
  165. if e != nil {
  166. err = errors.New("获取新报告详情失败, Err: " + e.Error())
  167. return
  168. }
  169. if newReport.State != 2 && newReport.State != 6 {
  170. detail.ReportLink = ""
  171. }
  172. } else {
  173. // 查询老报告表
  174. oldReport, e := research_report.GetByResearchReportId(uint64(detail.ReportId))
  175. if e != nil {
  176. err = errors.New("获取老报告详情失败, Err: " + e.Error())
  177. return
  178. }
  179. if oldReport.Status != "report" {
  180. detail.ReportLink = ""
  181. }
  182. }
  183. }
  184. return
  185. }
  186. // CheckActivityPermission 验证用户的活动参与权限
  187. func CheckActivityPermission(userInfo user.UserInfo, activityId int) (ok bool, permissionCheckInfo company.PermissionCheckInfo, err error) {
  188. companyId := userInfo.CompanyID
  189. activity, err := yb_activity.GetOneById(activityId)
  190. if err != nil {
  191. if err == gorm.ErrRecordNotFound {
  192. return
  193. }
  194. }
  195. if activity == nil {
  196. return
  197. }
  198. // 查询活动权限集合
  199. permissions, err := yb_activity_permission.GetPermissionsByActivityId(activityId)
  200. if err != nil {
  201. return
  202. }
  203. var permissionIds []int
  204. for _, item := range permissions {
  205. pid := int(item.PermissionID)
  206. permissionIds = append(permissionIds, pid)
  207. }
  208. ok, permissionCheckInfo, err = company.CheckPermissionByPermissionIdList2Ficc(companyId, int(userInfo.UserID), permissionIds)
  209. return
  210. }
  211. // GetUserAuthActivityIds 获取用户有权限参与的活动Ids
  212. func GetUserAuthActivityIds(userInfo user.UserInfo) (acrivityIds []int, err error) {
  213. productId := 1
  214. // 查询用户产品状态是否正常
  215. companyProduct, err := company_product.GetByCompany2ProductId(userInfo.CompanyID, int64(productId))
  216. if err != nil {
  217. return
  218. }
  219. statusMatch := "正式,试用,永续"
  220. if !strings.Contains(statusMatch, companyProduct.Status) || companyProduct.IsSuspend == 1 {
  221. acrivityIds = append(acrivityIds, 0)
  222. return
  223. }
  224. // 获取用户有效权限
  225. validPermissionIdList, err := company.GetValidPermissionIdListByCompany2ProductId(userInfo.CompanyID, int64(productId))
  226. if err != nil {
  227. return
  228. }
  229. // 获取用户权限可参与的活动ID
  230. permissions, err := yb_activity_permission.GetPermissionsByPermissionIds(validPermissionIdList)
  231. if err != nil {
  232. return
  233. }
  234. for _, v := range permissions {
  235. acrivityIds = append(acrivityIds, int(v.ActivityID))
  236. }
  237. return
  238. }
  239. // GetLatestActivity 从用户有权限参与的活动中,筛选出最新的活动
  240. func GetLatestActivity(permissionIds []int, userId uint64) (purchaseItem *purchase.PurchaseListItem, err error) {
  241. var errMsg string
  242. defer func() {
  243. if sysErr := recover(); sysErr != nil {
  244. err = errors.New("系统错误")
  245. global.LOG.Critical(fmt.Sprintf("GetLatestClassReport: userId=%d, err:%s,sysErr:%s", userId, err.Error(), sysErr))
  246. }
  247. if err != nil {
  248. global.LOG.Critical(fmt.Sprintf("GetLatestClassReport: userId=%d, err:%s, errMsg:%s", userId, err.Error(), errMsg))
  249. }
  250. }()
  251. // 获取用户权限可参与的活动ID
  252. var activityIds []int
  253. permissions, err := yb_activity_permission.GetPermissionsByPermissionIds(permissionIds)
  254. if err != nil {
  255. return
  256. }
  257. for _, v := range permissions {
  258. activityIds = append(activityIds, int(v.ActivityID))
  259. }
  260. activityItem, err := yb_activity.GetLatestByIds(activityIds)
  261. if err != nil {
  262. errMsg = err.Error()
  263. err = errors.New("查询活动错误")
  264. return
  265. }
  266. //统计未读数
  267. unRead, err := GetUnRead(activityIds, userId)
  268. if err != nil {
  269. errMsg = err.Error()
  270. err = errors.New("查询活动未读数错误")
  271. return
  272. }
  273. purchaseItem = new(purchase.PurchaseListItem)
  274. if activityItem.ActivityID > 0 {
  275. purchaseItem.ActivityId = activityItem.ActivityID
  276. purchaseItem.Title = activityItem.ActivityName
  277. purchaseItem.Content = fmt.Sprintf("主讲:%s", activityItem.Speaker)
  278. purchaseItem.Time = activityItem.ModifyTime
  279. purchaseItem.Unread = unRead
  280. // 添加活动icon
  281. purchaseItem.ImgUrl = utils.ALIYUN_YBIMG_HOST + "purchase_icon_xj_two_week.png"
  282. }
  283. return
  284. }
  285. // GetList 从用户有权限参与的活动中,筛选出最新的活动
  286. func GetPurchaseDetail(permissionIds []int, userId uint64, pageIndex, pageSize int) (ret *purchase.DetailResp, err error) {
  287. // 获取用户权限可参与的活动ID
  288. var activityIds []int
  289. permissions, err := yb_activity_permission.GetPermissionsByPermissionIds(permissionIds)
  290. if err != nil {
  291. return
  292. }
  293. for _, v := range permissions {
  294. activityIds = append(activityIds, int(v.ActivityID))
  295. }
  296. offset := (pageIndex - 1) * pageSize
  297. activityList, err := yb_activity.GetListByIds(activityIds, offset, pageSize)
  298. if err != nil {
  299. return
  300. }
  301. total, err := yb_activity.GetListCountByIds(activityIds)
  302. if err != nil {
  303. return
  304. }
  305. var WeekDayMap = map[string]string{
  306. "Monday": "周一",
  307. "Tuesday": "周二",
  308. "Wednesday": "周三",
  309. "Thursday": "周四",
  310. "Friday": "周五",
  311. "Saturday": "周六",
  312. "Sunday": "周日",
  313. }
  314. ret = new(purchase.DetailResp)
  315. var list []*purchase.Detail
  316. for _, item := range activityList {
  317. temp := new(purchase.Detail)
  318. temp.ActivityId = item.ActivityID
  319. temp.Title = fmt.Sprintf("主讲:%s", item.Speaker)
  320. // 时间处理
  321. activityDate := item.StartTime.Format("2006-01-02")
  322. activityStart := item.StartTime.Format("15:04")
  323. activityEnd := item.EndTime.Format("15:04")
  324. activityWeek := item.StartTime.Weekday().String()
  325. weekStr := WeekDayMap[activityWeek]
  326. timeStr := activityDate + " " + activityStart + "-" + activityEnd + " " + weekStr
  327. temp.Content = fmt.Sprintf("活动时间:%s", timeStr)
  328. temp.Time = item.ModifyTime
  329. temp.TopName = item.ActivityName
  330. temp.ImgUrl = utils.ALIYUN_YBIMG_HOST + "purchase_bg_activity.png"
  331. list = append(list, temp)
  332. }
  333. ret.List = list
  334. ret.Paging = response.GetPaging(pageIndex, pageSize, int(total))
  335. num, tErr := BatchInsertActivityView(activityIds, userId)
  336. log.Printf("新增活动浏览记录:%d", num)
  337. if tErr != nil {
  338. err = tErr
  339. return
  340. }
  341. return
  342. }