resource_data.go 22 KB

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