resource_data.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. var yanxuanSpecialIds []int // 研选专栏
  41. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt "`
  42. for _, v := range list {
  43. if v.Source == "article" {
  44. articleIds = append(articleIds, v.SourceId)
  45. } else if v.Source == "newchart" {
  46. newchartIds = append(newchartIds, v.SourceId)
  47. } else if v.Source == "roadshow" {
  48. roadshowIds = append(roadshowIds, strconv.Itoa(v.SourceId))
  49. } else if v.Source == "activity" {
  50. activityIds = append(activityIds, v.SourceId)
  51. } else if v.Source == "activityvideo" {
  52. activityvideoIds = append(activityvideoIds, strconv.Itoa(v.SourceId))
  53. } else if v.Source == "activityvoice" {
  54. activityvoiceIds = append(activityvoiceIds, strconv.Itoa(v.SourceId))
  55. } else if v.Source == "activityspecial" {
  56. activityspecialIds = append(activityspecialIds, v.SourceId)
  57. } else if v.Source == "researchsummary" {
  58. researchsummaryIds = append(researchsummaryIds, v.SourceId)
  59. } else if v.Source == "minutessummary" {
  60. minutessummaryIds = append(minutessummaryIds, v.SourceId)
  61. } else if v.Source == "meetingreviewchapt" {
  62. meetingreviewchaptIds = append(meetingreviewchaptIds, v.SourceId)
  63. } else if v.Source == "productinterior" {
  64. productinteriorIds = append(productinteriorIds, v.SourceId)
  65. } else if v.Source == "reportselection" {
  66. reportselectionIds = append(reportselectionIds, v.SourceId)
  67. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL {
  68. yanxuanSpecialIds = append(yanxuanSpecialIds, v.SourceId)
  69. }
  70. }
  71. //处理文章
  72. if len(articleIds) > 0 {
  73. pars = make([]interface{}, 0)
  74. condition = ` AND a.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  75. pars = append(pars, articleIds)
  76. listArticle, e := models.GetHomeList(condition, pars, 0, len(articleIds))
  77. if e != nil {
  78. err = errors.New("GetResourceDataList, Err: " + e.Error())
  79. return
  80. }
  81. listArticle, e = HandleArticleCategoryImg(listArticle)
  82. if e != nil {
  83. err = errors.New("HandleArticleCategoryImg, Err: " + e.Error())
  84. return
  85. }
  86. for _, v := range listArticle {
  87. v.Body = ""
  88. v.BodyHtml = ""
  89. mapItems[fmt.Sprint("article", v.ArticleId)].Article = v
  90. }
  91. }
  92. //处理晨会精华
  93. if len(meetingreviewchaptIds) > 0 {
  94. pars = make([]interface{}, 0)
  95. condition = ` AND c.id IN (` + utils.GetOrmInReplace(len(meetingreviewchaptIds)) + `)`
  96. pars = append(pars, meetingreviewchaptIds)
  97. listrMeet, e := models.GetCygxMorningMeetingReviewChapterList(condition, pars)
  98. if e != nil {
  99. err = errors.New("GetResourceDataList, Err: " + e.Error())
  100. return
  101. }
  102. for _, v := range listrMeet {
  103. v.Content = AnnotationHtml(v.Content)
  104. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  105. mapItems[fmt.Sprint("meetingreviewchapt", v.Id)].Meetingreviewchapt = v
  106. }
  107. }
  108. //处理上周纪要汇总
  109. if len(minutessummaryIds) > 0 {
  110. pars = make([]interface{}, 0)
  111. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(minutessummaryIds)) + `)`
  112. pars = append(pars, minutessummaryIds)
  113. listresearchsummary, e := models.GetReportSelectionListHome(condition, "cygx_minutes_summary", pars, 0, len(minutessummaryIds))
  114. if e != nil {
  115. err = errors.New("GetResourceDataList, Err: " + e.Error())
  116. return
  117. }
  118. mapPv := GetCygxReportHistoryRecordListMap(minutessummaryIds, "szjyhz")
  119. for _, v := range listresearchsummary {
  120. v.Pv = mapPv[v.ArticleId]
  121. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  122. mapItems[fmt.Sprint("minutessummary", v.ArticleId)].Minutessummary = v
  123. }
  124. }
  125. //处理本周纪要汇总
  126. if len(researchsummaryIds) > 0 {
  127. pars = make([]interface{}, 0)
  128. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(researchsummaryIds)) + `)`
  129. pars = append(pars, researchsummaryIds)
  130. listresearchsummary, e := models.GetReportSelectionListHome(condition, "cygx_research_summary", pars, 0, len(researchsummaryIds))
  131. if e != nil {
  132. err = errors.New("GetReportSelectionListHome, Err: " + e.Error())
  133. return
  134. }
  135. mapPv := GetCygxReportHistoryRecordListMap(minutessummaryIds, "bzyjhz")
  136. for _, v := range listresearchsummary {
  137. v.Pv = mapPv[v.ArticleId]
  138. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  139. mapItems[fmt.Sprint("researchsummary", v.ArticleId)].Researchsummary = v
  140. }
  141. }
  142. //处理产品内测
  143. if len(productinteriorIds) > 0 {
  144. pars = make([]interface{}, 0)
  145. condition = ` AND art.status = 1 AND art.product_interior_id IN (` + utils.GetOrmInReplace(len(productinteriorIds)) + `)`
  146. pars = append(pars, productinteriorIds)
  147. listProductInterior, e := models.GetCygxProductInteriorList(condition, pars, 0, len(productinteriorIds))
  148. if e != nil {
  149. err = errors.New("GetCygxProductInteriorList, Err: " + e.Error())
  150. return
  151. }
  152. ProductInteriorHistoryMap := GetCygxProductInteriorHistoryListPvMap(articleIds)
  153. for _, v := range listProductInterior {
  154. v.Body = ProductInteriorHtml(v.Body)
  155. v.Pv = ProductInteriorHistoryMap[v.ProductInteriorId]
  156. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  157. mapItems[fmt.Sprint("productinterior", v.ProductInteriorId)].ProductInterior = v
  158. }
  159. }
  160. detail, e := models.GetConfigByCode("city_img_url")
  161. if e != nil {
  162. err = errors.New("GetResourceDataList, Err: " + e.Error())
  163. return
  164. }
  165. detailChart, e := models.GetConfigByCode("chart_img_url")
  166. if e != nil {
  167. err = errors.New("GetResourceDataList, Err: " + e.Error())
  168. return
  169. }
  170. addressList := strings.Split(detail.ConfigValue, "{|}")
  171. mapAddress := make(map[string]string)
  172. chartList := strings.Split(detailChart.ConfigValue, "{|}")
  173. mapChart := make(map[string]string)
  174. var cityName string
  175. var chartName string
  176. var imgUrl string
  177. var imgUrlChart string
  178. for _, v := range addressList {
  179. vslice := strings.Split(v, "_")
  180. cityName = vslice[0]
  181. imgUrl = vslice[len(vslice)-1]
  182. mapAddress[cityName] = imgUrl
  183. }
  184. for _, v := range chartList {
  185. vslice := strings.Split(v, "_")
  186. chartName = vslice[0]
  187. imgUrlChart = vslice[len(vslice)-1]
  188. mapChart[chartName] = imgUrlChart
  189. }
  190. var imgUrlResp string
  191. //处理活动
  192. if len(activityIds) > 0 {
  193. for _, vss := range activityIds {
  194. imgUrlResp += strconv.Itoa(vss) + ","
  195. }
  196. pars = make([]interface{}, 0)
  197. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityIds)) + `)`
  198. pars = append(pars, activityIds)
  199. activityList, e := models.GetActivityListNew(condition, pars, uid, 0, len(activityIds), 0, 0, "")
  200. if e != nil {
  201. err = errors.New("GetResourceDataList, Err: " + e.Error())
  202. return
  203. }
  204. //处理不同的报名方式按钮回显
  205. mapActivitySignup, e := GetActivitySignupResp(activityIds, user)
  206. if e != nil {
  207. e = errors.New("GetActivitySignupResp, Err: " + e.Error())
  208. return
  209. }
  210. var activityListRersp []*models.ActivityDetail
  211. for _, v := range activityList {
  212. v.SignupType = mapActivitySignup[v.ActivityId]
  213. //activityListRersp = append(activityListRersp, ActivityButtonShow(v))
  214. }
  215. activityListRersp = ActivityArrButtonShow(activityList)
  216. for _, v := range activityListRersp {
  217. if v == nil {
  218. continue
  219. }
  220. if v.ActivityType == 0 {
  221. if mapAddress[v.City] != "" {
  222. imgUrlResp = mapAddress[v.City]
  223. } else {
  224. imgUrlResp = mapAddress["其它"]
  225. }
  226. } else {
  227. if mapChart[v.ChartPermissionName] != "" {
  228. imgUrlResp = mapChart[v.ChartPermissionName]
  229. }
  230. }
  231. v.SourceType = 1
  232. v.Expert, _ = GetReportContentTextSub(v.Expert)
  233. mapItems[fmt.Sprint("activity", v.ActivityId)].Activity = v
  234. }
  235. }
  236. //处理图表
  237. if len(newchartIds) > 0 {
  238. pars = make([]interface{}, 0)
  239. condition = ` AND a.chart_id IN (` + utils.GetOrmInReplace(len(newchartIds)) + `)`
  240. pars = append(pars, newchartIds)
  241. chartDateList, e := models.GetChartListCollectionNew(condition, pars, uid, 0, len(newchartIds))
  242. if e != nil {
  243. err = errors.New("GetResourceDataList, Err: " + e.Error())
  244. return
  245. }
  246. for _, v := range chartDateList {
  247. mapItems[fmt.Sprint("newchart", v.ChartId)].Newchart = v
  248. }
  249. }
  250. //处理专项调研
  251. if len(activityspecialIds) > 0 {
  252. pars = make([]interface{}, 0)
  253. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityspecialIds)) + `)`
  254. pars = append(pars, activityspecialIds)
  255. activitySpeciallist, e := models.GetCygxActivitySpecialDetailList(condition, pars, user.UserId, 0, len(activityspecialIds))
  256. if e != nil {
  257. err = errors.New("GetCygxActivitySpecialDetailList, Err: " + e.Error())
  258. return
  259. }
  260. UserMap, e := GetSpecialTripUserMap(activityIds, user.UserId)
  261. if e != nil {
  262. err = errors.New("GetSpecialTripUserMap, Err: " + e.Error())
  263. return
  264. }
  265. for _, v := range activitySpeciallist {
  266. if mapChart[v.ChartPermissionName] != "" {
  267. imgUrlResp = mapChart[v.ChartPermissionName]
  268. }
  269. if _, ok := UserMap[v.ActivityId]; ok {
  270. v.IsTrip = 1
  271. }
  272. if v.Days == 0 {
  273. v.TripStatus = 1
  274. v.TripImgLink = v.TripImgLink
  275. } else {
  276. v.TripStatus = 2
  277. v.TripImgLink = v.TripImgLinkFix
  278. v.ActivityTimeText = v.ActivityTimeTextByDay
  279. }
  280. resultTimeStart := utils.StrTimeToTime(v.ActivityTime) //时间字符串格式转时间格式
  281. resultTimeEnd := utils.StrTimeToTime(v.ActivityTimeEnd) //时间字符串格式转时间格式
  282. if resultTimeStart.After(time.Now()) {
  283. v.ActiveState = 1
  284. } else if time.Now().After(resultTimeEnd) {
  285. v.ActiveState = 3
  286. } else {
  287. v.ActiveState = 2
  288. }
  289. v.ImgUrl = imgUrlResp
  290. mapItems[fmt.Sprint("activityspecial", v.ActivityId)].Activityspecial = v
  291. }
  292. }
  293. if len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds) > 0 {
  294. audioIdstr := strings.Join(activityvoiceIds, ",")
  295. ideoIdsStr := strings.Join(roadshowIds, ",")
  296. activityVideoIdsStr := strings.Join(activityvideoIds, ",")
  297. list, _, e := GetMicroRoadShowMycollect(len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds), 0, audioIdstr, ideoIdsStr, activityVideoIdsStr)
  298. if e != nil {
  299. err = errors.New("GetMicroRoadShowMycollect, Err: " + e.Error())
  300. return
  301. }
  302. for _, item := range list {
  303. if item.Type == 1 {
  304. //音频
  305. count, e := models.GetVoiceCollectCount(user.UserId, item.Id)
  306. if e != nil {
  307. err = errors.New("GetVoiceCollectCount, Err: " + e.Error())
  308. return
  309. }
  310. if count > 0 {
  311. item.IsCollect = true
  312. }
  313. } else if item.Type == 2 {
  314. //活动视频
  315. count, e := models.GetActivityVideoCollectCount(user.UserId, item.Id)
  316. if e != nil {
  317. err = errors.New("GetActivityVideoCollectCount, Err: " + e.Error())
  318. return
  319. }
  320. if count > 0 {
  321. item.IsCollect = true
  322. }
  323. } else if item.Type == 3 {
  324. //微路演视频
  325. count, e := models.GetVideoCollectCount(user.UserId, item.Id)
  326. if e != nil {
  327. err = errors.New("GetVideoCollectCount, Err: " + e.Error())
  328. return
  329. }
  330. if count > 0 {
  331. item.IsCollect = true
  332. }
  333. }
  334. }
  335. // 用户权限
  336. authInfo, permissionArr, e := GetUserRaiPermissionInfo(user.UserId, user.CompanyId)
  337. if e != nil {
  338. err = errors.New("GetUserRaiPermissionInfo, Err: " + e.Error())
  339. return
  340. }
  341. // 获取默认图配置
  342. audioMap, videoMap, audioShareMap, videoShareMap, e := GetMicroRoadShowDefaultImgConfig()
  343. if e != nil {
  344. err = errors.New("GetMicroRoadShowDefaultImgConfig, Err: " + e.Error())
  345. return
  346. }
  347. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial"`
  348. for i := range list {
  349. // 权限
  350. au := new(models.UserPermissionAuthInfo)
  351. au.SellerName = authInfo.SellerName
  352. au.SellerMobile = authInfo.SellerMobile
  353. au.HasPermission = authInfo.HasPermission
  354. au.OperationMode = authInfo.OperationMode
  355. if au.HasPermission == 1 {
  356. // 非宏观权限进一步判断是否有权限
  357. if list[i].ChartPermissionId != utils.HONG_GUAN_ID && !utils.InArrayByStr(permissionArr, list[i].ChartPermissionName) {
  358. au.HasPermission = 2
  359. }
  360. }
  361. // 无权限的弹框提示
  362. if au.HasPermission != 1 {
  363. if au.OperationMode == UserPermissionOperationModeCall {
  364. if list[i].Type == 1 {
  365. au.PopupMsg = UserPermissionPopupMsgCallActivity
  366. } else {
  367. au.PopupMsg = UserPermissionPopupMsgCallMicroVideo
  368. }
  369. } else {
  370. if list[i].Type == 1 {
  371. au.PopupMsg = UserPermissionPopupMsgApplyActivity
  372. } else {
  373. au.PopupMsg = UserPermissionPopupMsgApplyMicroVideo
  374. }
  375. }
  376. }
  377. list[i].AuthInfo = au
  378. list[i].PublishTime = utils.StrTimeToTime(list[i].PublishTime).Format(utils.FormatDate)
  379. // 默认图
  380. if list[i].BackgroundImg == "" {
  381. if list[i].Type == 1 {
  382. list[i].BackgroundImg = audioMap[list[i].ChartPermissionId]
  383. } else {
  384. list[i].BackgroundImg = videoMap[list[i].ChartPermissionId]
  385. }
  386. }
  387. // 分享图
  388. if list[i].ShareImg == "" {
  389. if list[i].Type == 1 {
  390. list[i].ShareImg = audioShareMap[list[i].ChartPermissionId]
  391. } else {
  392. list[i].ShareImg = videoShareMap[list[i].ChartPermissionId]
  393. }
  394. }
  395. }
  396. //Type int `description:"类型: 1-音频; 2-活动视频; 3-产业视频"`
  397. for _, item := range list {
  398. if item.Type == 1 {
  399. mapItems[fmt.Sprint("activityvoice", item.Id)].Activityvoice = item
  400. } else if item.Type == 2 {
  401. mapItems[fmt.Sprint("activityvideo", item.Id)].Activityvideo = item
  402. } else if item.Type == 3 {
  403. mapItems[fmt.Sprint("roadshow", item.Id)].Roadshow = item
  404. }
  405. }
  406. }
  407. //处理报告精选
  408. lenreportselectionIds := len(reportselectionIds)
  409. if lenreportselectionIds > 0 {
  410. pars = make([]interface{}, 0)
  411. condition = ` AND article_id IN (` + utils.GetOrmInReplace(lenreportselectionIds) + `)`
  412. pars = append(pars, reportselectionIds)
  413. listreportselection, e := models.GetReportSelectionList(condition, pars, 0, lenreportselectionIds)
  414. if e != nil {
  415. err = errors.New("GetReportSelectionList, Err: " + e.Error())
  416. return
  417. }
  418. mapPv := GetCygxReportHistoryRecordListMap(reportselectionIds, "bgjx")
  419. for _, v := range listreportselection {
  420. v.Title += "(第" + v.Periods + "期)"
  421. v.MarketStrategy = AnnotationHtml(v.MarketStrategy)
  422. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  423. v.Pv = mapPv[v.ArticleId]
  424. mapItems[fmt.Sprint("reportselection", v.ArticleId)].ReportSelection = v
  425. }
  426. }
  427. //处理研选专栏
  428. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  429. if lenyanxuanSpecialIds > 0 {
  430. pars = make([]interface{}, 0)
  431. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanSpecialIds) + `) `
  432. pars = append(pars, yanxuanSpecialIds)
  433. listyanxuanSpecial, e := models.GetYanxuanSpecialList(user.UserId, condition, pars)
  434. if e != nil {
  435. err = errors.New("GetYanxuanSpecialList, Err: " + e.Error())
  436. return
  437. }
  438. for _, v := range listyanxuanSpecial {
  439. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  440. v.Annotation, _ = GetReportContentTextSubNew(v.Content)
  441. // Source PublishDate 字段兼容前端样式
  442. v.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  443. v.PublishDate = v.PublishTime
  444. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL, v.Id)].YanxuanSpecial = v
  445. }
  446. }
  447. for _, vList := range list {
  448. for _, v := range mapItems {
  449. //如果这些类型都为空,那么就不合并
  450. if v.Article == nil && v.Newchart == 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 {
  451. continue
  452. }
  453. if v.SourceId == vList.SourceId {
  454. items = append(items, v)
  455. }
  456. }
  457. }
  458. return
  459. }
  460. // 同步活动到最新数据表
  461. func UpdateResourceData(sourceId int, source, doType, publishDate string) (err error) {
  462. defer func() {
  463. if err != nil {
  464. go utils.SendAlarmMsg("同步到最新数据表失败,Err:"+err.Error()+"资源ID"+strconv.Itoa(sourceId)+"资源类型"+source+"操作方式"+doType, 3)
  465. }
  466. }()
  467. if doType == "add" {
  468. item := new(models.CygxResourceData)
  469. item.SourceId = sourceId
  470. item.Source = source
  471. item.PublishDate = publishDate
  472. item.CreateTime = time.Now()
  473. _, err = models.AddCygxResourceData(item)
  474. } else if doType == "delete" {
  475. err = models.DeleteResourceData(sourceId, source)
  476. } else if doType == "update" {
  477. err = models.UpdateResourceData(sourceId, source, publishDate)
  478. }
  479. return
  480. }
  481. // 批量删除最新图表数据
  482. func Deletenewchart(chartIdsDelete []int) (err error) {
  483. defer func() {
  484. if err != nil {
  485. fmt.Println(err)
  486. go utils.SendAlarmMsg("批量删除最新图表数据,Err:"+err.Error(), 3)
  487. }
  488. }()
  489. lenchartIdsDelete := len(chartIdsDelete)
  490. if lenchartIdsDelete == 0 {
  491. return
  492. }
  493. var condition string
  494. var pars []interface{}
  495. condition += ` AND source = 'newchart' AND source_id IN (` + utils.GetOrmInReplace(lenchartIdsDelete) + `) `
  496. pars = append(pars, chartIdsDelete)
  497. err = models.DeleteResourceDataList(condition, pars)
  498. return
  499. }
  500. //func init() {
  501. // UpdateArticleResourceData(9050)
  502. //}
  503. // 更新文章
  504. func UpdateArticleResourceData(sourceId int) {
  505. var err error
  506. defer func() {
  507. if err != nil {
  508. fmt.Println("err:", err)
  509. go utils.SendAlarmMsg("更新文章 失败,UpdateArticleResourceData Err:"+err.Error()+"资源ID"+strconv.Itoa(sourceId), 3)
  510. }
  511. }()
  512. var source = utils.CYGX_OBJ_ARTICLE
  513. var condition string
  514. var pars []interface{}
  515. condition = ` AND publish_status = 1 AND article_id = ? `
  516. pars = append(pars, sourceId)
  517. total, e := models.GetCygxArticleCount(condition, pars)
  518. if e != nil {
  519. err = errors.New("GetCygxReportSelection, Err: " + err.Error())
  520. return
  521. }
  522. //如果取消发布了就做删除处理
  523. if total == 0 {
  524. e = models.DeleteResourceData(sourceId, source)
  525. if e != nil {
  526. err = errors.New("DeleteResourceData, Err: " + e.Error())
  527. return
  528. }
  529. //删除 cygx_resource_data 表关联的产业ID,标的ID
  530. e = models.DeleteCygxResourceDataGroup(sourceId, source)
  531. if e != nil {
  532. err = errors.New("DeleteCygxResourceDataGroup, Err: " + e.Error())
  533. return
  534. }
  535. } else {
  536. //判断是否存在,如果不存在就新增,存在就更新
  537. totalData, e := models.GetCygxResourceDataBySourceAndIdCount(sourceId, source)
  538. if e != nil {
  539. err = errors.New("GetCygxReportSelectionBySourceAndId, Err: " + e.Error())
  540. return
  541. }
  542. detail, e := models.GetArticleDetailById(sourceId)
  543. if e != nil {
  544. err = errors.New("GetCygxReportSelectionInfoById, Err: " + e.Error())
  545. return
  546. }
  547. var resourceDataId int
  548. publishDate := time.Now().Format(utils.FormatDateTime)
  549. item := new(models.CygxResourceData)
  550. if detail.ArticleTypeId > 0 {
  551. item.SearchTag = detail.ArticleTypeName // 研选类型名称
  552. } else {
  553. item.SearchTag = detail.MatchTypeName
  554. }
  555. item.SourceId = sourceId
  556. item.Source = source
  557. item.PublishDate = publishDate
  558. item.CreateTime = time.Now()
  559. if totalData == 0 {
  560. newId, e := models.AddCygxResourceData(item)
  561. if e != nil {
  562. err = errors.New("AddCygxResourceData, Err: " + e.Error())
  563. return
  564. }
  565. resourceDataId = int(newId)
  566. } else {
  567. e = models.UpdateResourceDataByItem(item)
  568. if e != nil {
  569. err = errors.New("UpdateResourceData, Err: " + e.Error())
  570. return
  571. }
  572. sourceDetail, e := models.GetCygxResourceDataByIdAndSource(sourceId, source)
  573. if e != nil {
  574. err = errors.New("UpdateResourceData, Err: " + e.Error())
  575. return
  576. }
  577. resourceDataId = sourceDetail.Id
  578. }
  579. //建立首页资源表,与产业的关系
  580. industrialList, e := models.GetIndustrialArticleGroupManagementListByArticleId(sourceId)
  581. if e != nil && e.Error() != utils.ErrNoRow() {
  582. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  583. return
  584. }
  585. var industrialItems []*models.CygxResourceDataIndustrialGroupManagement
  586. for _, v := range industrialList {
  587. var industrialItem = new(models.CygxResourceDataIndustrialGroupManagement)
  588. industrialItem.SourceId = sourceId
  589. industrialItem.Source = source
  590. industrialItem.IndustrialManagementId = v.IndustrialManagementId
  591. industrialItem.ResourceDataId = resourceDataId
  592. industrialItem.CreateTime = time.Now()
  593. industrialItems = append(industrialItems, industrialItem)
  594. }
  595. //建立首页资源表,与标的 的关系
  596. subjectList, e := models.GetSubjectArticleGroupManagementListByArtcileId(sourceId)
  597. if e != nil && e.Error() != utils.ErrNoRow() {
  598. err = errors.New("GetSubjectArticleGroupManagementList, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  599. return
  600. }
  601. var subjectItems []*models.CygxResourceDataIndustrialGroupSubject
  602. for _, v := range subjectList {
  603. var subjectItem = new(models.CygxResourceDataIndustrialGroupSubject)
  604. subjectItem.SourceId = sourceId
  605. subjectItem.Source = source
  606. subjectItem.IndustrialSubjectId = v.IndustrialSubjectId
  607. subjectItem.ResourceDataId = resourceDataId
  608. subjectItem.CreateTime = time.Now()
  609. subjectItems = append(subjectItems, subjectItem)
  610. }
  611. //插入关联信息
  612. e = models.AddCygxResourceDataGroup(sourceId, source, industrialItems, subjectItems)
  613. if e != nil {
  614. err = errors.New("AddCygxResourceDataGroup, Err: " + e.Error())
  615. return
  616. }
  617. }
  618. return
  619. }