resource_data.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_mfyx/models"
  6. "hongze/hongze_mfyx/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. var yanxuanSpecialIds []int // 研选专栏
  41. var askserieVideoIds []string //问答系列视频
  42. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt "`
  43. for _, v := range list {
  44. if v.Source == "article" {
  45. articleIds = append(articleIds, v.SourceId)
  46. } else if v.Source == "newchart" {
  47. newchartIds = append(newchartIds, v.SourceId)
  48. } else if v.Source == "roadshow" {
  49. roadshowIds = append(roadshowIds, strconv.Itoa(v.SourceId))
  50. } else if v.Source == "activity" {
  51. activityIds = append(activityIds, v.SourceId)
  52. } else if v.Source == "activityvideo" {
  53. activityvideoIds = append(activityvideoIds, strconv.Itoa(v.SourceId))
  54. } else if v.Source == "activityvoice" {
  55. activityvoiceIds = append(activityvoiceIds, strconv.Itoa(v.SourceId))
  56. } else if v.Source == "activityspecial" {
  57. activityspecialIds = append(activityspecialIds, v.SourceId)
  58. } else if v.Source == "researchsummary" {
  59. researchsummaryIds = append(researchsummaryIds, v.SourceId)
  60. } else if v.Source == "minutessummary" {
  61. minutessummaryIds = append(minutessummaryIds, v.SourceId)
  62. } else if v.Source == "meetingreviewchapt" {
  63. meetingreviewchaptIds = append(meetingreviewchaptIds, v.SourceId)
  64. } else if v.Source == "productinterior" {
  65. productinteriorIds = append(productinteriorIds, v.SourceId)
  66. } else if v.Source == "reportselection" {
  67. reportselectionIds = append(reportselectionIds, v.SourceId)
  68. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL {
  69. yanxuanSpecialIds = append(yanxuanSpecialIds, v.SourceId)
  70. } else if v.Source == utils.CYGX_OBJ_ASKSERIEVIDEO {
  71. askserieVideoIds = append(askserieVideoIds, strconv.Itoa(v.SourceId))
  72. }
  73. }
  74. //处理文章
  75. if len(articleIds) > 0 {
  76. pars = make([]interface{}, 0)
  77. condition = ` AND a.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  78. pars = append(pars, articleIds)
  79. listArticle, e := models.GetHomeList(condition, pars, 0, len(articleIds))
  80. if e != nil {
  81. err = errors.New("GetResourceDataList, Err: " + e.Error())
  82. return
  83. }
  84. listArticle, e = HandleArticleCategoryImg(listArticle)
  85. if e != nil {
  86. err = errors.New("HandleArticleCategoryImg, Err: " + e.Error())
  87. return
  88. }
  89. for _, v := range listArticle {
  90. v.Body = ""
  91. v.BodyHtml = ""
  92. mapItems[fmt.Sprint("article", v.ArticleId)].Article = v
  93. }
  94. }
  95. detail, e := models.GetConfigByCode("city_img_url")
  96. if e != nil {
  97. err = errors.New("GetResourceDataList, Err: " + e.Error())
  98. return
  99. }
  100. detailChart, e := models.GetConfigByCode("chart_img_url")
  101. if e != nil {
  102. err = errors.New("GetResourceDataList, Err: " + e.Error())
  103. return
  104. }
  105. addressList := strings.Split(detail.ConfigValue, "{|}")
  106. mapAddress := make(map[string]string)
  107. chartList := strings.Split(detailChart.ConfigValue, "{|}")
  108. mapChart := make(map[string]string)
  109. var cityName string
  110. var chartName string
  111. var imgUrl string
  112. var imgUrlChart string
  113. for _, v := range addressList {
  114. vslice := strings.Split(v, "_")
  115. cityName = vslice[0]
  116. imgUrl = vslice[len(vslice)-1]
  117. mapAddress[cityName] = imgUrl
  118. }
  119. for _, v := range chartList {
  120. vslice := strings.Split(v, "_")
  121. chartName = vslice[0]
  122. imgUrlChart = vslice[len(vslice)-1]
  123. mapChart[chartName] = imgUrlChart
  124. }
  125. var imgUrlResp string
  126. //处理活动
  127. if len(activityIds) > 0 {
  128. for _, vss := range activityIds {
  129. imgUrlResp += strconv.Itoa(vss) + ","
  130. }
  131. pars = make([]interface{}, 0)
  132. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityIds)) + `)`
  133. pars = append(pars, activityIds)
  134. activityList, e := models.GetActivityListNew(condition, pars, uid, 0, len(activityIds), 0, 0, "")
  135. if e != nil {
  136. err = errors.New("GetResourceDataList, Err: " + e.Error())
  137. return
  138. }
  139. //处理不同的报名方式按钮回显
  140. mapActivitySignup, e := GetActivitySignupResp(activityIds, user)
  141. if e != nil {
  142. e = errors.New("GetActivitySignupResp, Err: " + e.Error())
  143. return
  144. }
  145. var activityListRersp []*models.ActivityDetail
  146. for _, v := range activityList {
  147. v.SignupType = mapActivitySignup[v.ActivityId]
  148. //activityListRersp = append(activityListRersp, ActivityButtonShow(v))
  149. }
  150. activityListRersp = ActivityArrButtonShow(activityList)
  151. for _, v := range activityListRersp {
  152. if v == nil {
  153. continue
  154. }
  155. if v.ActivityType == 0 {
  156. if mapAddress[v.City] != "" {
  157. imgUrlResp = mapAddress[v.City]
  158. } else {
  159. imgUrlResp = mapAddress["其它"]
  160. }
  161. } else {
  162. if mapChart[v.ChartPermissionName] != "" {
  163. imgUrlResp = mapChart[v.ChartPermissionName]
  164. }
  165. }
  166. v.SourceType = 1
  167. v.Expert, _ = GetReportContentTextSub(v.Expert)
  168. mapItems[fmt.Sprint("activity", v.ActivityId)].Activity = v
  169. }
  170. }
  171. if len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds)+len(askserieVideoIds) > 0 {
  172. audioIdstr := strings.Join(activityvoiceIds, ",")
  173. activityVideoIdsStr := strings.Join(activityvideoIds, ",")
  174. roadshowIdsStr := strings.Join(roadshowIds, ",")
  175. askserieVideoIdsStr := strings.Join(askserieVideoIds, ",")
  176. list, _, e := GetMicroRoadShowMycollectV12(len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds)+len(askserieVideoIds), 0, audioIdstr, activityVideoIdsStr, roadshowIdsStr, askserieVideoIdsStr, user)
  177. if e != nil {
  178. err = errors.New("GetMicroRoadShowMycollect, Err: " + e.Error())
  179. return
  180. }
  181. // 用户权限
  182. authInfo, permissionArr, e := GetUserRaiPermissionInfo(user.UserId, user.CompanyId)
  183. if e != nil {
  184. err = errors.New("GetUserRaiPermissionInfo, Err: " + e.Error())
  185. return
  186. }
  187. // 获取默认图配置
  188. audioMap, videoMap, audioShareMap, videoShareMap, e := GetMicroRoadShowDefaultImgConfig()
  189. if e != nil {
  190. err = errors.New("GetMicroRoadShowDefaultImgConfig, Err: " + e.Error())
  191. return
  192. }
  193. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial"`
  194. for i := range list {
  195. // 权限
  196. au := new(models.UserPermissionAuthInfo)
  197. au.SellerName = authInfo.SellerName
  198. au.SellerMobile = authInfo.SellerMobile
  199. au.HasPermission = authInfo.HasPermission
  200. au.OperationMode = authInfo.OperationMode
  201. if au.HasPermission == 1 {
  202. // 非宏观权限进一步判断是否有权限
  203. if list[i].ChartPermissionId != utils.HONG_GUAN_ID && !utils.InArrayByStr(permissionArr, list[i].ChartPermissionName) {
  204. au.HasPermission = 2
  205. }
  206. }
  207. // 无权限的弹框提示
  208. if au.HasPermission != 1 {
  209. if au.OperationMode == UserPermissionOperationModeCall {
  210. if list[i].Type == 1 {
  211. au.PopupMsg = UserPermissionPopupMsgCallActivity
  212. } else {
  213. au.PopupMsg = UserPermissionPopupMsgCallMicroVideo
  214. }
  215. } else {
  216. if list[i].Type == 1 {
  217. au.PopupMsg = UserPermissionPopupMsgApplyActivity
  218. } else {
  219. au.PopupMsg = UserPermissionPopupMsgApplyMicroVideo
  220. }
  221. }
  222. }
  223. list[i].AuthInfo = au
  224. list[i].PublishTime = utils.StrTimeToTime(list[i].PublishTime).Format(utils.FormatDate)
  225. // 默认图
  226. if list[i].BackgroundImg == "" {
  227. if list[i].Type == 1 {
  228. list[i].BackgroundImg = audioMap[list[i].ChartPermissionId]
  229. } else {
  230. list[i].BackgroundImg = videoMap[list[i].ChartPermissionId]
  231. }
  232. }
  233. // 分享图
  234. if list[i].ShareImg == "" {
  235. if list[i].Type == 1 {
  236. list[i].ShareImg = audioShareMap[list[i].ChartPermissionId]
  237. } else {
  238. list[i].ShareImg = videoShareMap[list[i].ChartPermissionId]
  239. }
  240. }
  241. }
  242. //Type int `description:"类型: 1-音频; 2-活动视频; 3-产业视频"`
  243. for _, item := range list {
  244. if item.Type == 1 {
  245. mapItems[fmt.Sprint("activityvoice", item.Id)].Activityvoice = item
  246. } else if item.Type == 2 {
  247. mapItems[fmt.Sprint("activityvideo", item.Id)].Activityvideo = item
  248. } else if item.Type == 3 {
  249. mapItems[fmt.Sprint("roadshow", item.Id)].Roadshow = item
  250. } else if item.Type == 4 {
  251. mapItems[fmt.Sprint(utils.CYGX_OBJ_ASKSERIEVIDEO, item.Id)].AskserieVideo = item
  252. }
  253. }
  254. }
  255. //处理研选专栏
  256. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  257. if lenyanxuanSpecialIds > 0 {
  258. pars = make([]interface{}, 0)
  259. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanSpecialIds) + `) `
  260. pars = append(pars, yanxuanSpecialIds)
  261. listyanxuanSpecial, e := models.GetYanxuanSpecialList(user.UserId, condition, pars, 0, 0)
  262. if e != nil {
  263. err = errors.New("GetYanxuanSpecialList, Err: " + e.Error())
  264. return
  265. }
  266. yanxuanSpecialPv := GetYanxuanSpecialRecordByYanxuanSpecialId(yanxuanSpecialIds) // 专栏Pv
  267. for _, v := range listyanxuanSpecial {
  268. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  269. v.Annotation, _ = GetReportContentTextSubNew(v.Content)
  270. // Source PublishDate 字段兼容前端样式
  271. v.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  272. v.PublishDate = v.PublishTime
  273. v.LabelKeyword = "专栏"
  274. v.Pv = yanxuanSpecialPv[v.Id]
  275. v.LabelKeywordImgLink = utils.LABEL_ICO_4
  276. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL, v.Id)].YanxuanSpecial = v
  277. }
  278. }
  279. for _, vList := range list {
  280. for _, v := range mapItems {
  281. //如果这些类型都为空,那么就不合并
  282. if v.Article == nil && v.Roadshow == nil && v.Activity == nil && v.Activityvideo == nil && v.Activityvoice == nil && v.Activityspecial == nil && v.Researchsummary == nil && v.Minutessummary == nil && v.Meetingreviewchapt == nil && v.ProductInterior == nil && v.IndustrialResource == nil && v.ReportSelection == nil && v.YanxuanSpecial == nil && v.AskserieVideo == nil {
  283. continue
  284. }
  285. if v.SourceId == vList.SourceId && v.Source == vList.Source {
  286. items = append(items, v)
  287. }
  288. }
  289. }
  290. return
  291. }
  292. // 同步活动到最新数据表
  293. func UpdateResourceData(sourceId int, source, doType, publishDate string) (err error) {
  294. defer func() {
  295. if err != nil {
  296. go utils.SendAlarmMsg("同步到最新数据表失败,Err:"+err.Error()+"资源ID"+strconv.Itoa(sourceId)+"资源类型"+source+"操作方式"+doType, 3)
  297. }
  298. }()
  299. if doType == "add" {
  300. item := new(models.CygxResourceData)
  301. item.SourceId = sourceId
  302. item.Source = source
  303. item.PublishDate = publishDate
  304. item.CreateTime = time.Now()
  305. _, err = models.AddCygxResourceData(item)
  306. } else if doType == "delete" {
  307. err = models.DeleteResourceData(sourceId, source)
  308. } else if doType == "update" {
  309. err = models.UpdateResourceData(sourceId, source, publishDate)
  310. }
  311. return
  312. }
  313. // 批量删除最新图表数据
  314. func Deletenewchart(chartIdsDelete []int) (err error) {
  315. defer func() {
  316. if err != nil {
  317. fmt.Println(err)
  318. go utils.SendAlarmMsg("批量删除最新图表数据,Err:"+err.Error(), 3)
  319. }
  320. }()
  321. lenchartIdsDelete := len(chartIdsDelete)
  322. if lenchartIdsDelete == 0 {
  323. return
  324. }
  325. var condition string
  326. var pars []interface{}
  327. condition += ` AND source = 'newchart' AND source_id IN (` + utils.GetOrmInReplace(lenchartIdsDelete) + `) `
  328. pars = append(pars, chartIdsDelete)
  329. err = models.DeleteResourceDataList(condition, pars)
  330. return
  331. }
  332. //func init() {
  333. // UpdateArticleResourceData(9050)
  334. //}
  335. // 更新文章
  336. func UpdateArticleResourceData(sourceId int) {
  337. var err error
  338. defer func() {
  339. if err != nil {
  340. fmt.Println("err:", err)
  341. go utils.SendAlarmMsg("更新文章 失败,UpdateArticleResourceData Err:"+err.Error()+"资源ID"+strconv.Itoa(sourceId), 3)
  342. }
  343. }()
  344. var source = utils.CYGX_OBJ_ARTICLE
  345. var condition string
  346. var pars []interface{}
  347. condition = ` AND publish_status = 1 AND article_id = ? `
  348. pars = append(pars, sourceId)
  349. total, e := models.GetCygxArticleCount(condition, pars)
  350. if e != nil {
  351. err = errors.New("GetCygxReportSelection, Err: " + err.Error())
  352. return
  353. }
  354. //如果取消发布了就做删除处理
  355. if total == 0 {
  356. e = models.DeleteResourceData(sourceId, source)
  357. if e != nil {
  358. err = errors.New("DeleteResourceData, Err: " + e.Error())
  359. return
  360. }
  361. //删除 cygx_resource_data 表关联的产业ID,标的ID
  362. e = models.DeleteCygxResourceDataGroup(sourceId, source)
  363. if e != nil {
  364. err = errors.New("DeleteCygxResourceDataGroup, Err: " + e.Error())
  365. return
  366. }
  367. } else {
  368. //判断是否存在,如果不存在就新增,存在就更新
  369. totalData, e := models.GetCygxResourceDataBySourceAndIdCount(sourceId, source)
  370. if e != nil {
  371. err = errors.New("GetCygxReportSelectionBySourceAndId, Err: " + e.Error())
  372. return
  373. }
  374. detail, e := models.GetArticleDetailById(sourceId)
  375. if e != nil {
  376. err = errors.New("GetCygxReportSelectionInfoById, Err: " + e.Error())
  377. return
  378. }
  379. var resourceDataId int
  380. publishDate := time.Now().Format(utils.FormatDateTime)
  381. item := new(models.CygxResourceData)
  382. if detail.ArticleTypeId > 0 {
  383. item.SearchTag = detail.ArticleTypeName // 研选类型名称
  384. } else {
  385. item.SearchTag = detail.MatchTypeName
  386. //获取文章分类详情
  387. detailCategory, _ := models.GetCygxReportMappingCelueMaxDetailByCategoryId(detail.CategoryId)
  388. if detailCategory != nil {
  389. item.ChartPermissionId = detailCategory.ChartPermissionId
  390. }
  391. }
  392. item.SourceId = sourceId
  393. item.Source = source
  394. item.PublishDate = publishDate
  395. item.CreateTime = time.Now()
  396. if totalData == 0 {
  397. newId, e := models.AddCygxResourceData(item)
  398. if e != nil {
  399. err = errors.New("AddCygxResourceData, Err: " + e.Error())
  400. return
  401. }
  402. resourceDataId = int(newId)
  403. } else {
  404. e = models.UpdateResourceDataByItem(item)
  405. if e != nil {
  406. err = errors.New("UpdateResourceData, Err: " + e.Error())
  407. return
  408. }
  409. sourceDetail, e := models.GetCygxResourceDataByIdAndSource(sourceId, source)
  410. if e != nil {
  411. err = errors.New("UpdateResourceData, Err: " + e.Error())
  412. return
  413. }
  414. resourceDataId = sourceDetail.Id
  415. }
  416. //建立首页资源表,与产业的关系
  417. industrialList, e := models.GetIndustrialArticleGroupManagementListByArticleId(sourceId)
  418. if e != nil && e.Error() != utils.ErrNoRow() {
  419. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  420. return
  421. }
  422. var industrialItems []*models.CygxResourceDataIndustrialGroupManagement
  423. for _, v := range industrialList {
  424. var industrialItem = new(models.CygxResourceDataIndustrialGroupManagement)
  425. industrialItem.SourceId = sourceId
  426. industrialItem.Source = source
  427. industrialItem.IndustrialManagementId = v.IndustrialManagementId
  428. industrialItem.ResourceDataId = resourceDataId
  429. industrialItem.CreateTime = time.Now()
  430. industrialItems = append(industrialItems, industrialItem)
  431. }
  432. //建立首页资源表,与标的 的关系
  433. subjectList, e := models.GetSubjectArticleGroupManagementListByArtcileId(sourceId)
  434. if e != nil && e.Error() != utils.ErrNoRow() {
  435. err = errors.New("GetSubjectArticleGroupManagementList, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  436. return
  437. }
  438. var subjectItems []*models.CygxResourceDataIndustrialGroupSubject
  439. for _, v := range subjectList {
  440. var subjectItem = new(models.CygxResourceDataIndustrialGroupSubject)
  441. subjectItem.SourceId = sourceId
  442. subjectItem.Source = source
  443. subjectItem.IndustrialSubjectId = v.IndustrialSubjectId
  444. subjectItem.ResourceDataId = resourceDataId
  445. subjectItem.CreateTime = time.Now()
  446. subjectItems = append(subjectItems, subjectItem)
  447. }
  448. //插入关联信息
  449. e = models.AddCygxResourceDataGroup(sourceId, source, industrialItems, subjectItems)
  450. if e != nil {
  451. err = errors.New("AddCygxResourceDataGroup, Err: " + e.Error())
  452. return
  453. }
  454. }
  455. return
  456. }