resource_data.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. //处理晨会精华
  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 := GetCygxMorningMeetingReviewChapterListByIds(meetingreviewchaptIds)
  101. if e != nil {
  102. err = errors.New("GetCygxMorningMeetingReviewChapterListByIds, 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(activityspecialIds) > 0 {
  241. pars = make([]interface{}, 0)
  242. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityspecialIds)) + `)`
  243. pars = append(pars, activityspecialIds)
  244. activitySpeciallist, e := models.GetCygxActivitySpecialDetailList(condition, pars, user.UserId, 0, len(activityspecialIds))
  245. if e != nil {
  246. err = errors.New("GetCygxActivitySpecialDetailList, Err: " + e.Error())
  247. return
  248. }
  249. for _, v := range activitySpeciallist {
  250. if mapChart[v.ChartPermissionName] != "" {
  251. imgUrlResp = mapChart[v.ChartPermissionName]
  252. }
  253. if v.Days == 0 {
  254. v.TripStatus = 1
  255. v.TripImgLink = v.TripImgLink
  256. } else {
  257. v.TripStatus = 2
  258. v.TripImgLink = v.TripImgLinkFix
  259. v.ActivityTimeText = v.ActivityTimeTextByDay
  260. }
  261. resultTimeStart := utils.StrTimeToTime(v.ActivityTime) //时间字符串格式转时间格式
  262. resultTimeEnd := utils.StrTimeToTime(v.ActivityTimeEnd) //时间字符串格式转时间格式
  263. if resultTimeStart.After(time.Now()) {
  264. v.ActiveState = 1
  265. } else if time.Now().After(resultTimeEnd) {
  266. v.ActiveState = 3
  267. } else {
  268. v.ActiveState = 2
  269. }
  270. v.ImgUrl = imgUrlResp
  271. mapItems[fmt.Sprint("activityspecial", v.ActivityId)].Activityspecial = v
  272. }
  273. }
  274. if len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds)+len(askserieVideoIds) > 0 {
  275. audioIdstr := strings.Join(activityvoiceIds, ",")
  276. activityVideoIdsStr := strings.Join(activityvideoIds, ",")
  277. roadshowIdsStr := strings.Join(roadshowIds, ",")
  278. askserieVideoIdsStr := strings.Join(askserieVideoIds, ",")
  279. list, _, e := GetMicroRoadShowMycollectV12(len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds)+len(askserieVideoIds), 0, audioIdstr, activityVideoIdsStr, roadshowIdsStr, askserieVideoIdsStr, user)
  280. if e != nil {
  281. err = errors.New("GetMicroRoadShowMycollect, Err: " + e.Error())
  282. return
  283. }
  284. // 用户权限
  285. authInfo, permissionArr, e := GetUserRaiPermissionInfo(user.UserId, user.CompanyId)
  286. if e != nil {
  287. err = errors.New("GetUserRaiPermissionInfo, Err: " + e.Error())
  288. return
  289. }
  290. // 获取默认图配置
  291. audioMap, videoMap, audioShareMap, videoShareMap, e := GetMicroRoadShowDefaultImgConfig()
  292. if e != nil {
  293. err = errors.New("GetMicroRoadShowDefaultImgConfig, Err: " + e.Error())
  294. return
  295. }
  296. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial"`
  297. for i := range list {
  298. // 权限
  299. au := new(models.UserPermissionAuthInfo)
  300. au.SellerName = authInfo.SellerName
  301. au.SellerMobile = authInfo.SellerMobile
  302. au.HasPermission = authInfo.HasPermission
  303. au.OperationMode = authInfo.OperationMode
  304. if au.HasPermission == 1 {
  305. // 非宏观权限进一步判断是否有权限
  306. if list[i].ChartPermissionId != utils.HONG_GUAN_ID && !utils.InArrayByStr(permissionArr, list[i].ChartPermissionName) {
  307. au.HasPermission = 2
  308. }
  309. }
  310. // 无权限的弹框提示
  311. if au.HasPermission != 1 {
  312. if au.OperationMode == UserPermissionOperationModeCall {
  313. if list[i].Type == 1 {
  314. au.PopupMsg = UserPermissionPopupMsgCallActivity
  315. } else {
  316. au.PopupMsg = UserPermissionPopupMsgCallMicroVideo
  317. }
  318. } else {
  319. if list[i].Type == 1 {
  320. au.PopupMsg = UserPermissionPopupMsgApplyActivity
  321. } else {
  322. au.PopupMsg = UserPermissionPopupMsgApplyMicroVideo
  323. }
  324. }
  325. }
  326. list[i].AuthInfo = au
  327. list[i].PublishTime = utils.StrTimeToTime(list[i].PublishTime).Format(utils.FormatDate)
  328. // 默认图
  329. if list[i].BackgroundImg == "" {
  330. if list[i].Type == 1 {
  331. list[i].BackgroundImg = audioMap[list[i].ChartPermissionId]
  332. } else {
  333. list[i].BackgroundImg = videoMap[list[i].ChartPermissionId]
  334. }
  335. }
  336. // 分享图
  337. if list[i].ShareImg == "" {
  338. if list[i].Type == 1 {
  339. list[i].ShareImg = audioShareMap[list[i].ChartPermissionId]
  340. } else {
  341. list[i].ShareImg = videoShareMap[list[i].ChartPermissionId]
  342. }
  343. }
  344. }
  345. //Type int `description:"类型: 1-音频; 2-活动视频; 3-产业视频"`
  346. for _, item := range list {
  347. if item.Type == 1 {
  348. mapItems[fmt.Sprint("activityvoice", item.Id)].Activityvoice = item
  349. } else if item.Type == 2 {
  350. mapItems[fmt.Sprint("activityvideo", item.Id)].Activityvideo = item
  351. } else if item.Type == 3 {
  352. mapItems[fmt.Sprint("roadshow", item.Id)].Roadshow = item
  353. } else if item.Type == 4 {
  354. mapItems[fmt.Sprint(utils.CYGX_OBJ_ASKSERIEVIDEO, item.Id)].AskserieVideo = item
  355. }
  356. }
  357. }
  358. //处理报告精选
  359. lenreportselectionIds := len(reportselectionIds)
  360. if lenreportselectionIds > 0 {
  361. pars = make([]interface{}, 0)
  362. condition = ` AND article_id IN (` + utils.GetOrmInReplace(lenreportselectionIds) + `)`
  363. pars = append(pars, reportselectionIds)
  364. listreportselection, e := models.GetReportSelectionList(condition, pars, 0, lenreportselectionIds)
  365. if e != nil {
  366. err = errors.New("GetReportSelectionList, Err: " + e.Error())
  367. return
  368. }
  369. mapPv := GetCygxReportHistoryRecordListMap(reportselectionIds, "bgjx")
  370. for _, v := range listreportselection {
  371. v.Title += "(第" + v.Periods + "期)"
  372. v.MarketStrategy = AnnotationHtml(v.MarketStrategy)
  373. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  374. v.Pv = mapPv[v.ArticleId]
  375. mapItems[fmt.Sprint("reportselection", v.ArticleId)].ReportSelection = v
  376. }
  377. }
  378. //处理研选专栏
  379. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  380. if lenyanxuanSpecialIds > 0 {
  381. pars = make([]interface{}, 0)
  382. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanSpecialIds) + `) `
  383. pars = append(pars, yanxuanSpecialIds)
  384. listyanxuanSpecial, e := models.GetYanxuanSpecialList(user.UserId, condition, pars, 0, 0)
  385. if e != nil {
  386. err = errors.New("GetYanxuanSpecialList, Err: " + e.Error())
  387. return
  388. }
  389. for _, v := range listyanxuanSpecial {
  390. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  391. v.Annotation, _ = GetReportContentTextSubNew(v.Content)
  392. // Source PublishDate 字段兼容前端样式
  393. v.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  394. v.PublishDate = v.PublishTime
  395. v.LabelKeyword = "专栏"
  396. v.LabelKeywordImgLink = utils.LABEL_ICO_4
  397. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL, v.Id)].YanxuanSpecial = v
  398. }
  399. }
  400. for _, vList := range list {
  401. for _, v := range mapItems {
  402. //如果这些类型都为空,那么就不合并
  403. 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 {
  404. continue
  405. }
  406. if v.SourceId == vList.SourceId && v.Source == vList.Source {
  407. items = append(items, v)
  408. }
  409. }
  410. }
  411. return
  412. }
  413. // 同步活动到最新数据表
  414. func UpdateResourceData(sourceId int, source, doType, publishDate string) (err error) {
  415. defer func() {
  416. if err != nil {
  417. go utils.SendAlarmMsg("同步到最新数据表失败,Err:"+err.Error()+"资源ID"+strconv.Itoa(sourceId)+"资源类型"+source+"操作方式"+doType, 3)
  418. }
  419. }()
  420. if doType == "add" {
  421. item := new(models.CygxResourceData)
  422. item.SourceId = sourceId
  423. item.Source = source
  424. item.PublishDate = publishDate
  425. item.CreateTime = time.Now()
  426. _, err = models.AddCygxResourceData(item)
  427. } else if doType == "delete" {
  428. err = models.DeleteResourceData(sourceId, source)
  429. } else if doType == "update" {
  430. err = models.UpdateResourceData(sourceId, source, publishDate)
  431. }
  432. return
  433. }
  434. // 批量删除最新图表数据
  435. func Deletenewchart(chartIdsDelete []int) (err error) {
  436. defer func() {
  437. if err != nil {
  438. fmt.Println(err)
  439. go utils.SendAlarmMsg("批量删除最新图表数据,Err:"+err.Error(), 3)
  440. }
  441. }()
  442. lenchartIdsDelete := len(chartIdsDelete)
  443. if lenchartIdsDelete == 0 {
  444. return
  445. }
  446. var condition string
  447. var pars []interface{}
  448. condition += ` AND source = 'newchart' AND source_id IN (` + utils.GetOrmInReplace(lenchartIdsDelete) + `) `
  449. pars = append(pars, chartIdsDelete)
  450. err = models.DeleteResourceDataList(condition, pars)
  451. return
  452. }
  453. //func init() {
  454. // UpdateArticleResourceData(9050)
  455. //}
  456. // 更新文章
  457. func UpdateArticleResourceData(sourceId int) {
  458. var err error
  459. defer func() {
  460. if err != nil {
  461. fmt.Println("err:", err)
  462. go utils.SendAlarmMsg("更新文章 失败,UpdateArticleResourceData Err:"+err.Error()+"资源ID"+strconv.Itoa(sourceId), 3)
  463. }
  464. }()
  465. var source = utils.CYGX_OBJ_ARTICLE
  466. var condition string
  467. var pars []interface{}
  468. condition = ` AND publish_status = 1 AND article_id = ? `
  469. pars = append(pars, sourceId)
  470. total, e := models.GetCygxArticleCount(condition, pars)
  471. if e != nil {
  472. err = errors.New("GetCygxReportSelection, Err: " + err.Error())
  473. return
  474. }
  475. //如果取消发布了就做删除处理
  476. if total == 0 {
  477. e = models.DeleteResourceData(sourceId, source)
  478. if e != nil {
  479. err = errors.New("DeleteResourceData, Err: " + e.Error())
  480. return
  481. }
  482. //删除 cygx_resource_data 表关联的产业ID,标的ID
  483. e = models.DeleteCygxResourceDataGroup(sourceId, source)
  484. if e != nil {
  485. err = errors.New("DeleteCygxResourceDataGroup, Err: " + e.Error())
  486. return
  487. }
  488. } else {
  489. //判断是否存在,如果不存在就新增,存在就更新
  490. totalData, e := models.GetCygxResourceDataBySourceAndIdCount(sourceId, source)
  491. if e != nil {
  492. err = errors.New("GetCygxReportSelectionBySourceAndId, Err: " + e.Error())
  493. return
  494. }
  495. detail, e := models.GetArticleDetailById(sourceId)
  496. if e != nil {
  497. err = errors.New("GetCygxReportSelectionInfoById, Err: " + e.Error())
  498. return
  499. }
  500. var resourceDataId int
  501. publishDate := time.Now().Format(utils.FormatDateTime)
  502. item := new(models.CygxResourceData)
  503. if detail.ArticleTypeId > 0 {
  504. item.SearchTag = detail.ArticleTypeName // 研选类型名称
  505. } else {
  506. item.SearchTag = detail.MatchTypeName
  507. //获取文章分类详情
  508. detailCategory, _ := models.GetCygxReportMappingCelueMaxDetailByCategoryId(detail.CategoryId)
  509. if detailCategory != nil {
  510. item.ChartPermissionId = detailCategory.ChartPermissionId
  511. }
  512. }
  513. item.SourceId = sourceId
  514. item.Source = source
  515. item.PublishDate = publishDate
  516. item.CreateTime = time.Now()
  517. if totalData == 0 {
  518. newId, e := models.AddCygxResourceData(item)
  519. if e != nil {
  520. err = errors.New("AddCygxResourceData, Err: " + e.Error())
  521. return
  522. }
  523. resourceDataId = int(newId)
  524. } else {
  525. e = models.UpdateResourceDataByItem(item)
  526. if e != nil {
  527. err = errors.New("UpdateResourceData, Err: " + e.Error())
  528. return
  529. }
  530. sourceDetail, e := models.GetCygxResourceDataByIdAndSource(sourceId, source)
  531. if e != nil {
  532. err = errors.New("UpdateResourceData, Err: " + e.Error())
  533. return
  534. }
  535. resourceDataId = sourceDetail.Id
  536. }
  537. //建立首页资源表,与产业的关系
  538. industrialList, e := models.GetIndustrialArticleGroupManagementListByArticleId(sourceId)
  539. if e != nil && e.Error() != utils.ErrNoRow() {
  540. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  541. return
  542. }
  543. var industrialItems []*models.CygxResourceDataIndustrialGroupManagement
  544. for _, v := range industrialList {
  545. var industrialItem = new(models.CygxResourceDataIndustrialGroupManagement)
  546. industrialItem.SourceId = sourceId
  547. industrialItem.Source = source
  548. industrialItem.IndustrialManagementId = v.IndustrialManagementId
  549. industrialItem.ResourceDataId = resourceDataId
  550. industrialItem.CreateTime = time.Now()
  551. industrialItems = append(industrialItems, industrialItem)
  552. }
  553. //建立首页资源表,与标的 的关系
  554. subjectList, e := models.GetSubjectArticleGroupManagementListByArtcileId(sourceId)
  555. if e != nil && e.Error() != utils.ErrNoRow() {
  556. err = errors.New("GetSubjectArticleGroupManagementList, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  557. return
  558. }
  559. var subjectItems []*models.CygxResourceDataIndustrialGroupSubject
  560. for _, v := range subjectList {
  561. var subjectItem = new(models.CygxResourceDataIndustrialGroupSubject)
  562. subjectItem.SourceId = sourceId
  563. subjectItem.Source = source
  564. subjectItem.IndustrialSubjectId = v.IndustrialSubjectId
  565. subjectItem.ResourceDataId = resourceDataId
  566. subjectItem.CreateTime = time.Now()
  567. subjectItems = append(subjectItems, subjectItem)
  568. }
  569. //插入关联信息
  570. e = models.AddCygxResourceDataGroup(sourceId, source, industrialItems, subjectItems)
  571. if e != nil {
  572. err = errors.New("AddCygxResourceDataGroup, Err: " + e.Error())
  573. return
  574. }
  575. }
  576. return
  577. }