industrial_management.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_clpt/models"
  6. "hongze/hongze_clpt/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // 根据行业处理所选的全部赛道字段
  12. func DoXzsChooseSend(chartPermissionName string) string {
  13. var allIn string
  14. if chartPermissionName == utils.YI_YAO_NAME {
  15. allIn = "all_in_yi_yao"
  16. } else if chartPermissionName == utils.XIAO_FEI_NAME {
  17. allIn = "all_in_xiao_fei"
  18. } else if chartPermissionName == utils.KE_JI_NAME {
  19. allIn = "all_in_ke_ji"
  20. } else if chartPermissionName == utils.ZHI_ZAO_NAME {
  21. allIn = "all_in_zhi_zao"
  22. } else if chartPermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN {
  23. allIn = "all_in_yan_xuan"
  24. }
  25. return allIn
  26. }
  27. // 行业关注或者取消关注的时候,对于用户全部赛道的影响
  28. func IndustryFllowWithTrack(industrialManagementId, count, uid int) (err error) {
  29. defer func() {
  30. if err != nil {
  31. fmt.Println(err)
  32. go utils.SendAlarmMsg("修改行业关注或者取消关注的时候,对于用户全部赛道的影响信息失败"+err.Error(), 2)
  33. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "修改行业关注或者取消关注的时候,对于用户全部赛道的影响信息失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  34. }
  35. }()
  36. detail, err := models.GetIndustrialManagementDetail(industrialManagementId)
  37. if err != nil {
  38. return err
  39. }
  40. categoryinfo, err := models.GetChartPermissionById(detail.ChartPermissionId)
  41. if err != nil {
  42. return err
  43. }
  44. var allIn string
  45. allIn = DoXzsChooseSend(categoryinfo.PermissionName)
  46. if allIn != "" {
  47. //如果取消关注就把该赛道设置为非全部赛道
  48. if count == 1 {
  49. err = models.UpdateCygxXzsChooseSendIsAllIn(allIn, 0, uid)
  50. if err != nil {
  51. return err
  52. }
  53. } else {
  54. //医药、消费、科技、智造、研选下的产业赛道
  55. mapIndustrial := make(map[int][]*models.IndustrialManagementRep)
  56. industrialList, err := models.GetindustrialManagement()
  57. if err != nil {
  58. return err
  59. }
  60. for _, v := range industrialList {
  61. mapIndustrial[v.ChartPermissionId] = append(mapIndustrial[v.ChartPermissionId], v)
  62. }
  63. fllowNum, err := models.GetCountCygxIndustryFllowByUidAndChartPermissionId(uid, detail.ChartPermissionId)
  64. if err != nil {
  65. return err
  66. }
  67. if fllowNum == len(mapIndustrial[detail.ChartPermissionId]) {
  68. err = models.UpdateCygxXzsChooseSendIsAllIn(allIn, 1, uid)
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. }
  74. }
  75. return err
  76. }
  77. // HandleIndustryList预处理产业列表字段
  78. func HandleIndustryList(list []*models.IndustrialManagement, user *models.WxUserItem, resource string) (items []*models.IndustrialManagement, err error) {
  79. userId := user.UserId
  80. fllowList, err := models.GetUserFllowIndustrialList(userId)
  81. if err != nil {
  82. return
  83. }
  84. fllowMap := make(map[int]int)
  85. if len(fllowList) > 0 {
  86. for _, v := range fllowList {
  87. fllowMap[v.IndustrialManagementId] = v.IndustrialManagementId
  88. }
  89. }
  90. hotMap, e := IndustrialManagementHotMapGropuPermission()
  91. if e != nil {
  92. err = errors.New("获取hot标签失败,IndustrialManagementHotMapGropuPermission " + e.Error())
  93. return
  94. }
  95. var condition string
  96. var pars []interface{}
  97. articleTypeIds, _ := GetYanXuanArticleTypeIds()
  98. //给默认值
  99. if resource != "Yx" {
  100. resource = "Hz"
  101. }
  102. if articleTypeIds != "" {
  103. if resource == "Yx" {
  104. condition = ` AND a.publish_status = 1 AND a.article_type_id IN (` + articleTypeIds + `) `
  105. } else {
  106. condition = ` AND a.article_type_id NOT IN (` + articleTypeIds + `) `
  107. }
  108. }
  109. mapindustrialId, e := GetMicroRoadshowVideoMap()
  110. if e != nil {
  111. err = e
  112. return
  113. }
  114. //合并产业关联的标的
  115. listSubjcet, err := models.GetThemeHeatSubjectList("")
  116. if err != nil {
  117. return
  118. }
  119. industrialIdArr := make([]int, 0)
  120. for k, v := range list {
  121. list[k].Source = 1
  122. if fllowMap[v.IndustrialManagementId] > 0 {
  123. list[k].IsFollow = true
  124. }
  125. v.IsHot = hotMap[v.IndustrialManagementId]
  126. industrialIdArr = append(industrialIdArr, v.IndustrialManagementId)
  127. if _, ok := mapindustrialId[v.IndustrialManagementId]; !ok {
  128. continue
  129. }
  130. videoSimple, au, e := GetindustryVideo(user, v.IndustrialManagementId)
  131. if e != nil {
  132. err = errors.New("获取产业关联的视频失败,GetindustryVideo " + e.Error())
  133. return
  134. }
  135. if videoSimple != nil && videoSimple.Id > 0 {
  136. v.IndustryVideo = videoSimple
  137. }
  138. v.AuthInfo = au
  139. }
  140. if len(industrialIdArr) > 0 {
  141. //合并产业关联的标的
  142. conditionSubject := condition + ` AND m.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialIdArr)) + `)`
  143. pars = append(pars, industrialIdArr)
  144. listSubjcet, err = models.GetIndustrialSubjectAllByIndustrialId(pars, conditionSubject)
  145. if err != nil {
  146. return
  147. }
  148. mapIndustrial := make(map[string]int)
  149. for _, v := range listSubjcet {
  150. for k2, v2 := range list {
  151. if v2.IndustrialManagementId == v.IndustrialManagementId && mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, "Hz", v.SubjectName)] == 0 {
  152. list[k2].IndustrialSubjectList = append(list[k2].IndustrialSubjectList, v)
  153. mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, "Hz", v.SubjectName)] = v2.IndustrialManagementId
  154. }
  155. }
  156. }
  157. }
  158. //获取产业关联的的文章用户是否阅读
  159. mapIndustrialArticleHistory, err := GetUserArticleHistoryIndustrialNewPublishDate(user, industrialIdArr, articleTypeIds, resource)
  160. if err != nil {
  161. err = errors.New("获取产业关联的的文章用户是否阅读失败 " + err.Error())
  162. return
  163. }
  164. mapUPdateTime := make(map[int]string)
  165. articleIdArr := make([]int, 0)
  166. //获取这些产业下最新更新的文章
  167. var conditionindustrial string
  168. var parsindustrial []interface{}
  169. var industrialIds string
  170. if len(industrialIdArr) > 0 {
  171. conditionindustrial = condition + ` AND mg.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialIdArr)) + `)`
  172. parsindustrial = append(parsindustrial, industrialIdArr)
  173. listUpdateTime, e := models.GetNewArticleDetailByIndustrialIds(parsindustrial, conditionindustrial)
  174. if e != nil && e.Error() != utils.ErrNoRow() {
  175. err = errors.New("GetNewArticleDetailByIndustrialIds Err " + e.Error())
  176. return
  177. }
  178. for _, v := range listUpdateTime {
  179. mapUPdateTime[v.IndustrialManagementId] = v.PublishDate
  180. articleIdArr = append(articleIdArr, v.ArticleId)
  181. }
  182. //时间线的更新时间
  183. // maptimelineUPdateTime := make(map[int]string)
  184. listtimelinePublishdate, e := models.GetTimeLineReportIndustrialPublishdateList(industrialIdArr)
  185. if e != nil && e.Error() != utils.ErrNoRow() {
  186. err = e
  187. return
  188. }
  189. for _, v := range listtimelinePublishdate {
  190. if mapUPdateTime[v.IndustrialManagementId] != "" {
  191. if utils.StrTimeToTime(v.PublishDate).After(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) {
  192. mapUPdateTime[v.IndustrialManagementId] = v.PublishDate
  193. }
  194. }
  195. }
  196. //产品内测的更新时间线
  197. listtProductInterior, e := models.GetNewProductInteriorByIndustrialIds(industrialIdArr)
  198. if e != nil && e.Error() != utils.ErrNoRow() {
  199. err = e
  200. return
  201. }
  202. for _, v := range listtProductInterior {
  203. if mapUPdateTime[v.IndustrialManagementId] != "" {
  204. if utils.StrTimeToTime(v.PublishDate).After(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) {
  205. mapUPdateTime[v.IndustrialManagementId] = v.PublishDate
  206. }
  207. }
  208. }
  209. for _, id := range industrialIdArr {
  210. industrialIds += strconv.Itoa(id) + ","
  211. }
  212. industrialIds = strings.TrimRight(industrialIds, ",")
  213. articleList, e := models.GetReportIndustrialReadNumList(userId, industrialIds, user.CreatedTime)
  214. if e != nil && e.Error() != utils.ErrNoRow() {
  215. err = e
  216. return
  217. }
  218. mapHistroyindustrialId := make(map[int]int)
  219. for _, v := range articleList {
  220. mapHistroyindustrialId[v.IndustrialManagementId] = v.Readnum
  221. }
  222. }
  223. nowTime := time.Now().Local()
  224. threeMonBefore := nowTime.AddDate(0, -3, 0)
  225. //查询用户今天是否看过时间线
  226. //haveMorningMeeting := false
  227. //var morningMeetingTime string
  228. recrodList, err := models.GetCygxTimelineLogCount(user.UserId, industrialIdArr)
  229. if err != nil && err.Error() != utils.ErrNoRow() {
  230. return
  231. }
  232. var industrialManagementIdstr string
  233. industrialIdMap := make(map[string]time.Time)
  234. for _, v := range recrodList {
  235. //fmt.Println(v.Router)
  236. //sliceindustrial := strings.Split(v.Router, "=")
  237. //industrialManagementIdstr = strings.TrimLeft(v.Router, utils.TIME_LINE_ROUTE)
  238. industrialManagementIdstr = strconv.Itoa(v.IndustrialManagementId)
  239. industrialIdMap[industrialManagementIdstr] = v.ModifyTime
  240. //fmt.Println(industrialManagementIdstr)
  241. //if createTime, ok := industrialIdMap[industrialManagementIdstr]; ok {
  242. // if createTime.Before(v.CreateTime) {
  243. // industrialIdMap[industrialManagementIdstr] = v.CreateTime
  244. // }
  245. //} else {
  246. // industrialIdMap[industrialManagementIdstr] = v.CreateTime
  247. //}
  248. }
  249. morningMeetingTimeMap := make(map[int]time.Time)
  250. if industrialIds != "" {
  251. mmList, e := models.GetCygxMorningMeetingReviewsListByIndustrialIds(industrialIds)
  252. if e != nil {
  253. err = e
  254. return
  255. }
  256. for _, v := range mmList {
  257. morningMeetingTimeMap[v.IndustryId] = v.CreateTime
  258. }
  259. }
  260. timeLineRedMap := make(map[int]bool, 0)
  261. for _, industrialId := range industrialIdArr {
  262. //if createTime, ok := industrialIdMap[strconv.Itoa(industrialId)]; ok {
  263. // if createTime.Before(morningMeetingTimeMap[industrialId]) {
  264. // timeLineRedMap[industrialId] = true
  265. // }
  266. //}
  267. if industrialIdMap[strconv.Itoa(industrialId)].Before(morningMeetingTimeMap[industrialId]) {
  268. timeLineRedMap[industrialId] = true
  269. }
  270. }
  271. //fmt.Println(industrialIdMap["90"])
  272. //fmt.Println(timeLineRedMap[90])
  273. //fmt.Println(mapIndustrialArticleHistory[90])
  274. for k, v := range list {
  275. list[k].UpdateTime = utils.TimeRemoveHms(mapUPdateTime[v.IndustrialManagementId])
  276. if userId > 0 {
  277. //如果文章没有阅读,而且,文章的发布时间晚于项目的上线时间,而且文章的发布时间晚于用户的注册时间,就进行标红处理
  278. if mapIndustrialArticleHistory[v.IndustrialManagementId] || timeLineRedMap[v.IndustrialManagementId] {
  279. //if mapHistroyindustrialId[v.IndustrialManagementId] == 0 || timeLineRedMap[v.IndustrialManagementId] {
  280. if user.CreatedTime.Before(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) && utils.StrTimeToTime(utils.OnlineTime).Before(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) {
  281. list[k].IsRed = true
  282. }
  283. }
  284. } else {
  285. if utils.StrTimeToTime(utils.OnlineTime).Before(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) {
  286. list[k].IsRed = true
  287. }
  288. list[k].IsFollow = true
  289. }
  290. list[k].UpdateTime = utils.TimeRemoveHms(mapUPdateTime[v.IndustrialManagementId])
  291. if resource == "Yx" {
  292. list[k].IsRed = false
  293. }
  294. // 关联报告发布时间均在3个月内则标记New
  295. if v.MinReportTime != "" {
  296. t, e := time.Parse(utils.FormatDateTime, v.MinReportTime)
  297. if e != nil {
  298. err = errors.New("报告最早发布时间有误,GetindustryVideo " + e.Error())
  299. return
  300. }
  301. if t.After(threeMonBefore) {
  302. list[k].IsNew = true
  303. }
  304. }
  305. }
  306. items = list
  307. return
  308. }
  309. // 通过文章ID获取文章所关联的产业
  310. func GetArticleIndustrialByArticleId(articleIds []int) (itemMap map[int][]*models.IndustrialManagementResp, err error) {
  311. lenarticleIds := len(articleIds)
  312. if lenarticleIds == 0 {
  313. return
  314. }
  315. var condition string
  316. var pars []interface{}
  317. condition = ` AND mg.article_id IN (` + utils.GetOrmInReplace(lenarticleIds) + `)`
  318. pars = append(pars, articleIds)
  319. industrialList, err := models.GetIndustrialListByarticleId(pars, condition)
  320. if err != nil {
  321. return
  322. }
  323. industrialMap := make(map[int][]*models.IndustrialManagementResp)
  324. if len(industrialList) > 0 {
  325. for _, v := range industrialList {
  326. item := new(models.IndustrialManagementResp)
  327. item.IndustrialManagementId = v.IndustrialManagementId
  328. item.IndustryName = v.IndustryName
  329. item.ChartPermissionId = v.ChartPermissionId
  330. industrialMap[v.ArticleId] = append(industrialMap[v.ArticleId], item)
  331. }
  332. }
  333. itemMap = industrialMap
  334. return
  335. }
  336. // 获取研选文章类型的配置信息
  337. func GetArticleTypeMap() (nameMapResp map[int]string, buttonStyleMapResp map[int]string, err error) {
  338. condition := " AND is_show_yanx = 1 "
  339. list, e := models.GetCygxArticleTypeListCondition(condition)
  340. if e != nil {
  341. err = errors.New("报告最早发布时间有误,GetindustryVideo " + e.Error())
  342. return
  343. }
  344. nameMap := make(map[int]string)
  345. buttonStyleMap := make(map[int]string)
  346. if len(list) > 0 {
  347. for _, v := range list {
  348. nameMap[v.ArticleTypeId] = v.ArticleTypeName
  349. buttonStyleMap[v.ArticleTypeId] = v.ButtonStyle
  350. }
  351. }
  352. nameMapResp = nameMap
  353. buttonStyleMapResp = buttonStyleMap
  354. return
  355. }
  356. // GetUserFollowIndustrialMap获取用户关注的产业
  357. func GetUserFollowIndustrialMap(user *models.WxUserItem) (mapResp map[int]int, err error) {
  358. list, e := models.GetUserFllowIndustrialList(user.UserId)
  359. if e != nil {
  360. err = errors.New("GetUserFllowIndustrialList " + e.Error())
  361. return
  362. }
  363. fllowMap := make(map[int]int)
  364. if len(list) > 0 {
  365. for _, v := range list {
  366. fllowMap[v.IndustrialManagementId] = v.IndustrialManagementId
  367. }
  368. }
  369. mapResp = fllowMap
  370. return
  371. }
  372. // IndustrialManagementHotMapGropuPermission获取近一个月报告阅读数量最多的产业信息 根据行业分组
  373. func IndustrialManagementHotMapGropuPermission() (mapResp map[int]bool, err error) {
  374. var topCond string
  375. var topPars []interface{}
  376. toplist, err := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  377. if err != nil {
  378. return
  379. }
  380. mapPermission := make(map[int][]*models.IndustrialManagement)
  381. mapPermissionMax := make(map[int]int)
  382. for _, v := range toplist {
  383. item := new(models.IndustrialManagement)
  384. item.ChartPermissionId = v.ChartPermissionId
  385. item.IndustrialManagementId = v.IndustrialManagementId
  386. item.ArticleReadNum = v.ArticleReadNum
  387. mapPermission[v.ChartPermissionId] = append(mapPermission[v.ChartPermissionId], item)
  388. }
  389. for k, v := range mapPermission {
  390. for _, item := range v {
  391. //mapPermissionMax[k] = item.ArticleReadNum
  392. if item.ArticleReadNum > mapPermissionMax[k] {
  393. mapPermissionMax[k] = item.ArticleReadNum
  394. }
  395. }
  396. }
  397. mapResp = make(map[int]bool, 0)
  398. for _, v := range toplist {
  399. if v.ArticleReadNum == mapPermissionMax[v.ChartPermissionId] {
  400. mapResp[v.IndustrialManagementId] = true
  401. }
  402. }
  403. return
  404. }