resource_data.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. list[i].LabelKeywordImgLink = utils.LABEL_ICO_10
  242. }
  243. //Type int `description:"类型: 1-音频; 2-活动视频; 3-产业视频"`
  244. for _, item := range list {
  245. if item.Type == 1 {
  246. mapItems[fmt.Sprint("activityvoice", item.Id)].Activityvoice = item
  247. } else if item.Type == 2 {
  248. mapItems[fmt.Sprint("activityvideo", item.Id)].Activityvideo = item
  249. } else if item.Type == 3 {
  250. mapItems[fmt.Sprint("roadshow", item.Id)].Roadshow = item
  251. } else if item.Type == 4 {
  252. mapItems[fmt.Sprint(utils.CYGX_OBJ_ASKSERIEVIDEO, item.Id)].AskserieVideo = item
  253. }
  254. }
  255. }
  256. //处理研选专栏
  257. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  258. if lenyanxuanSpecialIds > 0 {
  259. pars = make([]interface{}, 0)
  260. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanSpecialIds) + `) `
  261. pars = append(pars, yanxuanSpecialIds)
  262. listyanxuanSpecial, e := models.GetYanxuanSpecialList(user.UserId, condition, pars, 0, 0)
  263. if e != nil {
  264. err = errors.New("GetYanxuanSpecialList, Err: " + e.Error())
  265. return
  266. }
  267. yanxuanSpecialPv := GetYanxuanSpecialRecordByYanxuanSpecialId(yanxuanSpecialIds) // 专栏Pv
  268. for _, v := range listyanxuanSpecial {
  269. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  270. v.Annotation, _ = GetReportContentTextSubNew(v.Content)
  271. // Source PublishDate 字段兼容前端样式
  272. v.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  273. v.PublishDate = v.PublishTime
  274. v.LabelKeyword = "专栏"
  275. v.Pv = yanxuanSpecialPv[v.Id]
  276. v.LabelKeywordImgLink = utils.LABEL_ICO_4
  277. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL, v.Id)].YanxuanSpecial = v
  278. }
  279. }
  280. for _, vList := range list {
  281. for _, v := range mapItems {
  282. //如果这些类型都为空,那么就不合并
  283. 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 {
  284. continue
  285. }
  286. if v.SourceId == vList.SourceId && v.Source == vList.Source {
  287. items = append(items, v)
  288. }
  289. }
  290. }
  291. return
  292. }
  293. // 同步活动到最新数据表
  294. func UpdateResourceData(sourceId int, source, doType, publishDate string) (err error) {
  295. defer func() {
  296. if err != nil {
  297. go utils.SendAlarmMsg("同步到最新数据表失败,Err:"+err.Error()+"资源ID"+strconv.Itoa(sourceId)+"资源类型"+source+"操作方式"+doType, 3)
  298. }
  299. }()
  300. if doType == "add" {
  301. item := new(models.CygxResourceData)
  302. item.SourceId = sourceId
  303. item.Source = source
  304. item.PublishDate = publishDate
  305. item.CreateTime = time.Now()
  306. _, err = models.AddCygxResourceData(item)
  307. } else if doType == "delete" {
  308. err = models.DeleteResourceData(sourceId, source)
  309. } else if doType == "update" {
  310. err = models.UpdateResourceData(sourceId, source, publishDate)
  311. }
  312. return
  313. }
  314. // 批量删除最新图表数据
  315. func Deletenewchart(chartIdsDelete []int) (err error) {
  316. defer func() {
  317. if err != nil {
  318. fmt.Println(err)
  319. go utils.SendAlarmMsg("批量删除最新图表数据,Err:"+err.Error(), 3)
  320. }
  321. }()
  322. lenchartIdsDelete := len(chartIdsDelete)
  323. if lenchartIdsDelete == 0 {
  324. return
  325. }
  326. var condition string
  327. var pars []interface{}
  328. condition += ` AND source = 'newchart' AND source_id IN (` + utils.GetOrmInReplace(lenchartIdsDelete) + `) `
  329. pars = append(pars, chartIdsDelete)
  330. err = models.DeleteResourceDataList(condition, pars)
  331. return
  332. }
  333. //func init() {
  334. // UpdateArticleResourceData(9050)
  335. //}
  336. // 更新文章
  337. func UpdateArticleResourceData(sourceId int) {
  338. var err error
  339. defer func() {
  340. if err != nil {
  341. fmt.Println("err:", err)
  342. go utils.SendAlarmMsg("更新文章 失败,UpdateArticleResourceData Err:"+err.Error()+"资源ID"+strconv.Itoa(sourceId), 3)
  343. }
  344. }()
  345. var source = utils.CYGX_OBJ_ARTICLE
  346. var condition string
  347. var pars []interface{}
  348. condition = ` AND publish_status = 1 AND article_id = ? `
  349. pars = append(pars, sourceId)
  350. total, e := models.GetCygxArticleCount(condition, pars)
  351. if e != nil {
  352. err = errors.New("GetCygxReportSelection, Err: " + err.Error())
  353. return
  354. }
  355. //如果取消发布了就做删除处理
  356. if total == 0 {
  357. e = models.DeleteResourceData(sourceId, source)
  358. if e != nil {
  359. err = errors.New("DeleteResourceData, Err: " + e.Error())
  360. return
  361. }
  362. //删除 cygx_resource_data 表关联的产业ID,标的ID
  363. e = models.DeleteCygxResourceDataGroup(sourceId, source)
  364. if e != nil {
  365. err = errors.New("DeleteCygxResourceDataGroup, Err: " + e.Error())
  366. return
  367. }
  368. } else {
  369. //判断是否存在,如果不存在就新增,存在就更新
  370. totalData, e := models.GetCygxResourceDataBySourceAndIdCount(sourceId, source)
  371. if e != nil {
  372. err = errors.New("GetCygxReportSelectionBySourceAndId, Err: " + e.Error())
  373. return
  374. }
  375. detail, e := models.GetArticleDetailById(sourceId)
  376. if e != nil {
  377. err = errors.New("GetCygxReportSelectionInfoById, Err: " + e.Error())
  378. return
  379. }
  380. var resourceDataId int
  381. publishDate := time.Now().Format(utils.FormatDateTime)
  382. item := new(models.CygxResourceData)
  383. if detail.ArticleTypeId > 0 {
  384. item.SearchTag = detail.ArticleTypeName // 研选类型名称
  385. } else {
  386. item.SearchTag = detail.MatchTypeName
  387. //获取文章分类详情
  388. detailCategory, _ := models.GetCygxReportMappingCelueMaxDetailByCategoryId(detail.CategoryId)
  389. if detailCategory != nil {
  390. item.ChartPermissionId = detailCategory.ChartPermissionId
  391. }
  392. }
  393. item.SourceId = sourceId
  394. item.Source = source
  395. item.PublishDate = publishDate
  396. item.CreateTime = time.Now()
  397. if totalData == 0 {
  398. newId, e := models.AddCygxResourceData(item)
  399. if e != nil {
  400. err = errors.New("AddCygxResourceData, Err: " + e.Error())
  401. return
  402. }
  403. resourceDataId = int(newId)
  404. } else {
  405. e = models.UpdateResourceDataByItem(item)
  406. if e != nil {
  407. err = errors.New("UpdateResourceData, Err: " + e.Error())
  408. return
  409. }
  410. sourceDetail, e := models.GetCygxResourceDataByIdAndSource(sourceId, source)
  411. if e != nil {
  412. err = errors.New("UpdateResourceData, Err: " + e.Error())
  413. return
  414. }
  415. resourceDataId = sourceDetail.Id
  416. }
  417. //建立首页资源表,与产业的关系
  418. industrialList, e := models.GetIndustrialArticleGroupManagementListByArticleId(sourceId)
  419. if e != nil && e.Error() != utils.ErrNoRow() {
  420. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  421. return
  422. }
  423. var industrialItems []*models.CygxResourceDataIndustrialGroupManagement
  424. for _, v := range industrialList {
  425. var industrialItem = new(models.CygxResourceDataIndustrialGroupManagement)
  426. industrialItem.SourceId = sourceId
  427. industrialItem.Source = source
  428. industrialItem.IndustrialManagementId = v.IndustrialManagementId
  429. industrialItem.ResourceDataId = resourceDataId
  430. industrialItem.CreateTime = time.Now()
  431. industrialItems = append(industrialItems, industrialItem)
  432. }
  433. //建立首页资源表,与标的 的关系
  434. subjectList, e := models.GetSubjectArticleGroupManagementListByArtcileId(sourceId)
  435. if e != nil && e.Error() != utils.ErrNoRow() {
  436. err = errors.New("GetSubjectArticleGroupManagementList, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  437. return
  438. }
  439. var subjectItems []*models.CygxResourceDataIndustrialGroupSubject
  440. for _, v := range subjectList {
  441. var subjectItem = new(models.CygxResourceDataIndustrialGroupSubject)
  442. subjectItem.SourceId = sourceId
  443. subjectItem.Source = source
  444. subjectItem.IndustrialSubjectId = v.IndustrialSubjectId
  445. subjectItem.ResourceDataId = resourceDataId
  446. subjectItem.CreateTime = time.Now()
  447. subjectItems = append(subjectItems, subjectItem)
  448. }
  449. //插入关联信息
  450. e = models.AddCygxResourceDataGroup(sourceId, source, industrialItems, subjectItems)
  451. if e != nil {
  452. err = errors.New("AddCygxResourceDataGroup, Err: " + e.Error())
  453. return
  454. }
  455. }
  456. return
  457. }