resource_data.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. func GetResourceDataList(condition string, pars []interface{}, startSize, pageSize int, user *models.WxUserItem) (items []*models.CygxResourceDataResp, err error) {
  12. uid := user.UserId
  13. list, e := models.GetResourceDataList(condition, pars, startSize, pageSize)
  14. if e != nil {
  15. err = errors.New("GetResourceDataList, Err: " + e.Error())
  16. return
  17. }
  18. mapItems := make(map[string]*models.CygxResourceDataResp)
  19. for _, v := range list {
  20. //预处理文章
  21. item := new(models.CygxResourceDataResp)
  22. item.Id = v.Id
  23. item.SourceId = v.SourceId
  24. item.Source = v.Source
  25. item.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  26. mapItems[fmt.Sprint(v.Source, v.SourceId)] = item
  27. }
  28. var articleIds []int
  29. var newchartIds []int
  30. var roadshowIds []string
  31. var activityIds []int
  32. var activityvideoIds []string
  33. var activityvoiceIds []string
  34. var activityspecialIds []int
  35. var researchsummaryIds []int
  36. var minutessummaryIds []int
  37. var meetingreviewchaptIds []int
  38. var productinteriorIds []int
  39. var reportselectionIds []int // 报告精选
  40. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt "`
  41. for _, v := range list {
  42. if v.Source == "article" {
  43. articleIds = append(articleIds, v.SourceId)
  44. } else if v.Source == "newchart" {
  45. newchartIds = append(newchartIds, v.SourceId)
  46. } else if v.Source == "roadshow" {
  47. roadshowIds = append(roadshowIds, strconv.Itoa(v.SourceId))
  48. } else if v.Source == "activity" {
  49. activityIds = append(activityIds, v.SourceId)
  50. } else if v.Source == "activityvideo" {
  51. activityvideoIds = append(activityvideoIds, strconv.Itoa(v.SourceId))
  52. } else if v.Source == "activityvoice" {
  53. activityvoiceIds = append(activityvoiceIds, strconv.Itoa(v.SourceId))
  54. } else if v.Source == "activityspecial" {
  55. activityspecialIds = append(activityspecialIds, v.SourceId)
  56. } else if v.Source == "researchsummary" {
  57. researchsummaryIds = append(researchsummaryIds, v.SourceId)
  58. } else if v.Source == "minutessummary" {
  59. minutessummaryIds = append(minutessummaryIds, v.SourceId)
  60. } else if v.Source == "meetingreviewchapt" {
  61. meetingreviewchaptIds = append(meetingreviewchaptIds, v.SourceId)
  62. } else if v.Source == "productinterior" {
  63. productinteriorIds = append(productinteriorIds, v.SourceId)
  64. } else if v.Source == "reportselection" {
  65. reportselectionIds = append(reportselectionIds, v.SourceId)
  66. }
  67. }
  68. //处理文章
  69. if len(articleIds) > 0 {
  70. pars = make([]interface{}, 0)
  71. condition = ` AND a.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  72. pars = append(pars, articleIds)
  73. listArticle, e := models.GetHomeList(condition, pars, 0, len(articleIds))
  74. if e != nil {
  75. err = errors.New("GetResourceDataList, Err: " + e.Error())
  76. return
  77. }
  78. listArticle, e = HandleArticleCategoryImg(listArticle)
  79. if e != nil {
  80. err = errors.New("HandleArticleCategoryImg, Err: " + e.Error())
  81. return
  82. }
  83. for _, v := range listArticle {
  84. v.Body = ""
  85. v.BodyHtml = ""
  86. mapItems[fmt.Sprint("article", v.ArticleId)].Article = v
  87. }
  88. }
  89. //处理晨会精华
  90. if len(meetingreviewchaptIds) > 0 {
  91. pars = make([]interface{}, 0)
  92. condition = ` AND c.id IN (` + utils.GetOrmInReplace(len(meetingreviewchaptIds)) + `)`
  93. pars = append(pars, meetingreviewchaptIds)
  94. listrMeet, e := models.GetCygxMorningMeetingReviewChapterList(condition, pars)
  95. if e != nil {
  96. err = errors.New("GetResourceDataList, Err: " + e.Error())
  97. return
  98. }
  99. for _, v := range listrMeet {
  100. v.Content = AnnotationHtml(v.Content)
  101. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  102. mapItems[fmt.Sprint("meetingreviewchapt", v.Id)].Meetingreviewchapt = v
  103. }
  104. }
  105. //处理上周纪要汇总
  106. if len(minutessummaryIds) > 0 {
  107. pars = make([]interface{}, 0)
  108. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(minutessummaryIds)) + `)`
  109. pars = append(pars, minutessummaryIds)
  110. listresearchsummary, e := models.GetReportSelectionListHome(condition, "cygx_minutes_summary", pars, 0, len(minutessummaryIds))
  111. if e != nil {
  112. err = errors.New("GetResourceDataList, Err: " + e.Error())
  113. return
  114. }
  115. for _, v := range listresearchsummary {
  116. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  117. mapItems[fmt.Sprint("minutessummary", v.ArticleId)].Minutessummary = v
  118. }
  119. }
  120. //处理本周纪要汇总
  121. if len(researchsummaryIds) > 0 {
  122. pars = make([]interface{}, 0)
  123. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(researchsummaryIds)) + `)`
  124. pars = append(pars, researchsummaryIds)
  125. listresearchsummary, e := models.GetReportSelectionListHome(condition, "cygx_research_summary", pars, 0, len(researchsummaryIds))
  126. if e != nil {
  127. err = errors.New("GetReportSelectionListHome, Err: " + e.Error())
  128. return
  129. }
  130. for _, v := range listresearchsummary {
  131. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  132. mapItems[fmt.Sprint("researchsummary", v.ArticleId)].Researchsummary = v
  133. }
  134. }
  135. //处理产品内测
  136. if len(productinteriorIds) > 0 {
  137. pars = make([]interface{}, 0)
  138. condition = ` AND art.status = 1 AND art.product_interior_id IN (` + utils.GetOrmInReplace(len(productinteriorIds)) + `)`
  139. pars = append(pars, productinteriorIds)
  140. listProductInterior, e := models.GetCygxProductInteriorList(condition, pars, 0, len(productinteriorIds))
  141. if e != nil {
  142. err = errors.New("GetCygxProductInteriorList, Err: " + e.Error())
  143. return
  144. }
  145. for _, v := range listProductInterior {
  146. v.Body = ProductInteriorHtml(v.Body)
  147. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  148. mapItems[fmt.Sprint("productinterior", v.ProductInteriorId)].ProductInterior = v
  149. }
  150. }
  151. detail, e := models.GetConfigByCode("city_img_url")
  152. if e != nil {
  153. err = errors.New("GetResourceDataList, Err: " + e.Error())
  154. return
  155. }
  156. detailChart, e := models.GetConfigByCode("chart_img_url")
  157. if e != nil {
  158. err = errors.New("GetResourceDataList, Err: " + e.Error())
  159. return
  160. }
  161. addressList := strings.Split(detail.ConfigValue, "{|}")
  162. mapAddress := make(map[string]string)
  163. chartList := strings.Split(detailChart.ConfigValue, "{|}")
  164. mapChart := make(map[string]string)
  165. var cityName string
  166. var chartName string
  167. var imgUrl string
  168. var imgUrlChart string
  169. for _, v := range addressList {
  170. vslice := strings.Split(v, "_")
  171. cityName = vslice[0]
  172. imgUrl = vslice[len(vslice)-1]
  173. mapAddress[cityName] = imgUrl
  174. }
  175. for _, v := range chartList {
  176. vslice := strings.Split(v, "_")
  177. chartName = vslice[0]
  178. imgUrlChart = vslice[len(vslice)-1]
  179. mapChart[chartName] = imgUrlChart
  180. }
  181. var imgUrlResp string
  182. //处理活动
  183. if len(activityIds) > 0 {
  184. for _, vss := range activityIds {
  185. imgUrlResp += strconv.Itoa(vss) + ","
  186. }
  187. pars = make([]interface{}, 0)
  188. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityIds)) + `)`
  189. pars = append(pars, activityIds)
  190. activityList, e := models.GetActivityListNew(condition, pars, uid, 0, len(activityIds), 0, 0, "")
  191. if e != nil {
  192. err = errors.New("GetResourceDataList, Err: " + e.Error())
  193. return
  194. }
  195. //处理不同的报名方式按钮回显
  196. mapActivitySignup, e := GetActivitySignupResp(activityIds, user)
  197. if e != nil {
  198. e = errors.New("GetActivitySignupResp, Err: " + e.Error())
  199. return
  200. }
  201. var activityListRersp []*models.ActivityDetail
  202. for _, v := range activityList {
  203. v.SignupType = mapActivitySignup[v.ActivityId]
  204. activityListRersp = append(activityListRersp, ActivityButtonShow(v))
  205. }
  206. for _, v := range activityListRersp {
  207. if v == nil {
  208. continue
  209. }
  210. if v.ActivityType == 0 {
  211. if mapAddress[v.City] != "" {
  212. imgUrlResp = mapAddress[v.City]
  213. } else {
  214. imgUrlResp = mapAddress["其它"]
  215. }
  216. } else {
  217. if mapChart[v.ChartPermissionName] != "" {
  218. imgUrlResp = mapChart[v.ChartPermissionName]
  219. }
  220. }
  221. v.SourceType = 1
  222. v.Expert, _ = GetReportContentTextSub(v.Expert)
  223. mapItems[fmt.Sprint("activity", v.ActivityId)].Activity = v
  224. }
  225. }
  226. //处理图表
  227. if len(newchartIds) > 0 {
  228. pars = make([]interface{}, 0)
  229. condition = ` AND a.chart_id IN (` + utils.GetOrmInReplace(len(newchartIds)) + `)`
  230. pars = append(pars, newchartIds)
  231. chartDateList, e := models.GetChartListCollectionNew(condition, pars, uid, 0, len(newchartIds))
  232. if e != nil {
  233. err = errors.New("GetResourceDataList, Err: " + e.Error())
  234. return
  235. }
  236. for _, v := range chartDateList {
  237. mapItems[fmt.Sprint("newchart", v.ChartId)].Newchart = v
  238. }
  239. }
  240. //处理专项调研
  241. if len(activityspecialIds) > 0 {
  242. pars = make([]interface{}, 0)
  243. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityspecialIds)) + `)`
  244. pars = append(pars, activityspecialIds)
  245. activitySpeciallist, e := models.GetCygxActivitySpecialDetailList(condition, pars, user.UserId, 0, len(activityspecialIds))
  246. if e != nil {
  247. err = errors.New("GetCygxActivitySpecialDetailList, Err: " + e.Error())
  248. return
  249. }
  250. UserMap, e := GetSpecialTripUserMap(activityIds, user.UserId)
  251. if e != nil {
  252. err = errors.New("GetSpecialTripUserMap, Err: " + e.Error())
  253. return
  254. }
  255. for _, v := range activitySpeciallist {
  256. if mapChart[v.ChartPermissionName] != "" {
  257. imgUrlResp = mapChart[v.ChartPermissionName]
  258. }
  259. if _, ok := UserMap[v.ActivityId]; ok {
  260. v.IsTrip = 1
  261. }
  262. if v.Days == 0 {
  263. v.TripStatus = 1
  264. v.TripImgLink = v.TripImgLink
  265. } else {
  266. v.TripStatus = 2
  267. v.TripImgLink = v.TripImgLinkFix
  268. v.ActivityTimeText = v.ActivityTimeTextByDay
  269. }
  270. resultTimeStart := utils.StrTimeToTime(v.ActivityTime) //时间字符串格式转时间格式
  271. resultTimeEnd := utils.StrTimeToTime(v.ActivityTimeEnd) //时间字符串格式转时间格式
  272. if resultTimeStart.After(time.Now()) {
  273. v.ActiveState = 1
  274. } else if time.Now().After(resultTimeEnd) {
  275. v.ActiveState = 3
  276. } else {
  277. v.ActiveState = 2
  278. }
  279. v.ImgUrl = imgUrlResp
  280. mapItems[fmt.Sprint("activityspecial", v.ActivityId)].Activityspecial = v
  281. }
  282. }
  283. if len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds) > 0 {
  284. audioIdstr := strings.Join(activityvoiceIds, ",")
  285. ideoIdsStr := strings.Join(roadshowIds, ",")
  286. activityVideoIdsStr := strings.Join(activityvideoIds, ",")
  287. list, _, e := GetMicroRoadShowMycollect(len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds), 0, audioIdstr, ideoIdsStr, activityVideoIdsStr)
  288. if e != nil {
  289. err = errors.New("GetMicroRoadShowMycollect, Err: " + e.Error())
  290. return
  291. }
  292. for _, item := range list {
  293. if item.Type == 1 {
  294. //音频
  295. count, e := models.GetVoiceCollectCount(user.UserId, item.Id)
  296. if e != nil {
  297. err = errors.New("GetVoiceCollectCount, Err: " + e.Error())
  298. return
  299. }
  300. if count > 0 {
  301. item.IsCollect = true
  302. }
  303. } else if item.Type == 2 {
  304. //活动视频
  305. count, e := models.GetActivityVideoCollectCount(user.UserId, item.Id)
  306. if e != nil {
  307. err = errors.New("GetActivityVideoCollectCount, Err: " + e.Error())
  308. return
  309. }
  310. if count > 0 {
  311. item.IsCollect = true
  312. }
  313. } else if item.Type == 3 {
  314. //微路演视频
  315. count, e := models.GetVideoCollectCount(user.UserId, item.Id)
  316. if e != nil {
  317. err = errors.New("GetVideoCollectCount, Err: " + e.Error())
  318. return
  319. }
  320. if count > 0 {
  321. item.IsCollect = true
  322. }
  323. }
  324. }
  325. // 用户权限
  326. authInfo, permissionArr, e := GetUserRaiPermissionInfo(user.UserId, user.CompanyId)
  327. if e != nil {
  328. err = errors.New("GetUserRaiPermissionInfo, Err: " + e.Error())
  329. return
  330. }
  331. // 获取默认图配置
  332. audioMap, videoMap, audioShareMap, videoShareMap, e := GetMicroRoadShowDefaultImgConfig()
  333. if e != nil {
  334. err = errors.New("GetMicroRoadShowDefaultImgConfig, Err: " + e.Error())
  335. return
  336. }
  337. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial"`
  338. for i := range list {
  339. // 权限
  340. au := new(models.UserPermissionAuthInfo)
  341. au.SellerName = authInfo.SellerName
  342. au.SellerMobile = authInfo.SellerMobile
  343. au.HasPermission = authInfo.HasPermission
  344. au.OperationMode = authInfo.OperationMode
  345. if au.HasPermission == 1 {
  346. // 非宏观权限进一步判断是否有权限
  347. if list[i].ChartPermissionId != utils.HONG_GUAN_ID && !utils.InArrayByStr(permissionArr, list[i].ChartPermissionName) {
  348. au.HasPermission = 2
  349. }
  350. }
  351. // 无权限的弹框提示
  352. if au.HasPermission != 1 {
  353. if au.OperationMode == UserPermissionOperationModeCall {
  354. if list[i].Type == 1 {
  355. au.PopupMsg = UserPermissionPopupMsgCallActivity
  356. } else {
  357. au.PopupMsg = UserPermissionPopupMsgCallMicroVideo
  358. }
  359. } else {
  360. if list[i].Type == 1 {
  361. au.PopupMsg = UserPermissionPopupMsgApplyActivity
  362. } else {
  363. au.PopupMsg = UserPermissionPopupMsgApplyMicroVideo
  364. }
  365. }
  366. }
  367. list[i].AuthInfo = au
  368. list[i].PublishTime = utils.StrTimeToTime(list[i].PublishTime).Format(utils.FormatDate)
  369. // 默认图
  370. if list[i].BackgroundImg == "" {
  371. if list[i].Type == 1 {
  372. list[i].BackgroundImg = audioMap[list[i].ChartPermissionId]
  373. } else {
  374. list[i].BackgroundImg = videoMap[list[i].ChartPermissionId]
  375. }
  376. }
  377. // 分享图
  378. if list[i].ShareImg == "" {
  379. if list[i].Type == 1 {
  380. list[i].ShareImg = audioShareMap[list[i].ChartPermissionId]
  381. } else {
  382. list[i].ShareImg = videoShareMap[list[i].ChartPermissionId]
  383. }
  384. }
  385. }
  386. //Type int `description:"类型: 1-音频; 2-活动视频; 3-产业视频"`
  387. for _, item := range list {
  388. if item.Type == 1 {
  389. mapItems[fmt.Sprint("activityvoice", item.Id)].Activityvoice = item
  390. } else if item.Type == 2 {
  391. mapItems[fmt.Sprint("activityvideo", item.Id)].Activityvideo = item
  392. } else if item.Type == 3 {
  393. mapItems[fmt.Sprint("roadshow", item.Id)].Roadshow = item
  394. }
  395. }
  396. }
  397. //处理报告精选
  398. lenreportselectionIds := len(reportselectionIds)
  399. if lenreportselectionIds > 0 {
  400. pars = make([]interface{}, 0)
  401. condition = ` AND article_id IN (` + utils.GetOrmInReplace(lenreportselectionIds) + `)`
  402. pars = append(pars, lenreportselectionIds)
  403. listreportselection, e := models.GetReportSelectionList(condition, pars, startSize, pageSize)
  404. if e != nil {
  405. err = errors.New("GetReportSelectionList, Err: " + e.Error())
  406. return
  407. }
  408. for _, v := range listreportselection {
  409. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  410. mapItems[fmt.Sprint("reportselection", v.ArticleId)].ReportSelection = v
  411. }
  412. }
  413. for _, vList := range list {
  414. for _, v := range mapItems {
  415. if v.SourceId == vList.SourceId {
  416. items = append(items, v)
  417. }
  418. }
  419. }
  420. return
  421. }
  422. // 同步活动到最新数据表
  423. func UpdateResourceData(sourceId int, source, doType, publishDate string) (err error) {
  424. defer func() {
  425. if err != nil {
  426. go utils.SendAlarmMsg("同步到最新数据表失败,Err:"+err.Error()+"资源ID"+strconv.Itoa(sourceId)+"资源类型"+source+"操作方式"+doType, 3)
  427. }
  428. }()
  429. if doType == "add" {
  430. item := new(models.CygxResourceData)
  431. item.SourceId = sourceId
  432. item.Source = source
  433. item.PublishDate = publishDate
  434. item.CreateTime = time.Now()
  435. _, err = models.AddCygxResourceData(item)
  436. } else if doType == "delete" {
  437. err = models.DeleteResourceData(sourceId, source)
  438. } else if doType == "update" {
  439. err = models.UpdateResourceData(sourceId, source, publishDate)
  440. }
  441. return
  442. }
  443. // 批量删除最新图表数据
  444. func Deletenewchart(chartIdsDelete []int) (err error) {
  445. defer func() {
  446. if err != nil {
  447. fmt.Println(err)
  448. go utils.SendAlarmMsg("批量删除最新图表数据,Err:"+err.Error(), 3)
  449. }
  450. }()
  451. lenchartIdsDelete := len(chartIdsDelete)
  452. if lenchartIdsDelete == 0 {
  453. return
  454. }
  455. var condition string
  456. var pars []interface{}
  457. condition += ` AND source = 'newchart' AND source_id IN (` + utils.GetOrmInReplace(lenchartIdsDelete) + `) `
  458. pars = append(pars, chartIdsDelete)
  459. err = models.DeleteResourceDataList(condition, pars)
  460. return
  461. }