industrial_management.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. fllowTypeMap := make(map[int]int)
  86. if len(fllowList) > 0 {
  87. for _, v := range fllowList {
  88. fllowMap[v.IndustrialManagementId] = v.IndustrialManagementId
  89. fllowTypeMap[v.IndustrialManagementId] = v.FollowType
  90. }
  91. }
  92. hotMap, e := IndustrialManagementHotMapGropuPermission()
  93. if e != nil {
  94. err = errors.New("获取hot标签失败,IndustrialManagementHotMapGropuPermission " + e.Error())
  95. return
  96. }
  97. var condition string
  98. var pars []interface{}
  99. articleTypeIds, _ := GetYanXuanArticleTypeIds()
  100. //给默认值
  101. if resource != "Yx" {
  102. resource = "Hz"
  103. }
  104. if articleTypeIds != "" {
  105. if resource == "Yx" {
  106. condition = ` AND a.publish_status = 1 AND a.article_type_id IN (` + articleTypeIds + `) `
  107. } else {
  108. condition = ` AND a.article_type_id NOT IN (` + articleTypeIds + `) `
  109. }
  110. }
  111. mapindustrialId, e := GetMicroRoadshowVideoMap()
  112. if e != nil {
  113. err = e
  114. return
  115. }
  116. //合并产业关联的标的
  117. listSubjcet, err := models.GetThemeHeatSubjectList("")
  118. if err != nil {
  119. return
  120. }
  121. industrialIdArr := make([]int, 0)
  122. for k, v := range list {
  123. list[k].Source = 1
  124. if fllowMap[v.IndustrialManagementId] > 0 {
  125. list[k].IsFollow = true
  126. }
  127. v.IsHot = hotMap[v.IndustrialManagementId]
  128. industrialIdArr = append(industrialIdArr, v.IndustrialManagementId)
  129. v.FollowType = fllowTypeMap[v.IndustrialManagementId]
  130. if _, ok := mapindustrialId[v.IndustrialManagementId]; !ok {
  131. continue
  132. }
  133. videoSimple, au, e := GetindustryVideo(user, v.IndustrialManagementId)
  134. if e != nil {
  135. err = errors.New("获取产业关联的视频失败,GetindustryVideo " + e.Error())
  136. return
  137. }
  138. if videoSimple != nil && videoSimple.Id > 0 {
  139. v.IndustryVideo = videoSimple
  140. }
  141. v.AuthInfo = au
  142. }
  143. if len(industrialIdArr) > 0 {
  144. //合并产业关联的标的
  145. conditionSubject := condition + ` AND m.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialIdArr)) + `)`
  146. pars = append(pars, industrialIdArr)
  147. listSubjcet, err = models.GetIndustrialSubjectAllByIndustrialId(pars, conditionSubject)
  148. if err != nil {
  149. return
  150. }
  151. mapIndustrial := make(map[string]int)
  152. for _, v := range listSubjcet {
  153. for k2, v2 := range list {
  154. if v2.IndustrialManagementId == v.IndustrialManagementId && mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, "Hz", v.SubjectName)] == 0 {
  155. list[k2].IndustrialSubjectList = append(list[k2].IndustrialSubjectList, v)
  156. mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, "Hz", v.SubjectName)] = v2.IndustrialManagementId
  157. }
  158. }
  159. }
  160. }
  161. //获取产业关联的的文章用户是否阅读
  162. mapIndustrialArticleHistory, err := GetUserArticleHistoryIndustrialNewPublishDate(user, industrialIdArr, articleTypeIds, resource)
  163. if err != nil {
  164. err = errors.New("获取产业关联的的文章用户是否阅读失败 " + err.Error())
  165. return
  166. }
  167. mapUPdateTime := make(map[int]string)
  168. articleIdArr := make([]int, 0)
  169. //获取这些产业下最新更新的文章
  170. var conditionindustrial string
  171. var parsindustrial []interface{}
  172. var industrialIds string
  173. if len(industrialIdArr) > 0 {
  174. conditionindustrial = condition + ` AND mg.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialIdArr)) + `)`
  175. parsindustrial = append(parsindustrial, industrialIdArr)
  176. listUpdateTime, e := models.GetNewArticleDetailByIndustrialIds(parsindustrial, conditionindustrial)
  177. if e != nil && e.Error() != utils.ErrNoRow() {
  178. err = errors.New("GetNewArticleDetailByIndustrialIds Err " + e.Error())
  179. return
  180. }
  181. for _, v := range listUpdateTime {
  182. mapUPdateTime[v.IndustrialManagementId] = v.PublishDate
  183. articleIdArr = append(articleIdArr, v.ArticleId)
  184. }
  185. //时间线的更新时间
  186. // maptimelineUPdateTime := make(map[int]string)
  187. listtimelinePublishdate, e := models.GetTimeLineReportIndustrialPublishdateList(industrialIdArr)
  188. if e != nil && e.Error() != utils.ErrNoRow() {
  189. err = e
  190. return
  191. }
  192. for _, v := range listtimelinePublishdate {
  193. if mapUPdateTime[v.IndustrialManagementId] != "" {
  194. if utils.StrTimeToTime(v.PublishDate).After(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) {
  195. mapUPdateTime[v.IndustrialManagementId] = v.PublishDate
  196. }
  197. }
  198. }
  199. //产品内测的更新时间线
  200. listtProductInterior, e := models.GetNewProductInteriorByIndustrialIds(industrialIdArr)
  201. if e != nil && e.Error() != utils.ErrNoRow() {
  202. err = e
  203. return
  204. }
  205. for _, v := range listtProductInterior {
  206. if mapUPdateTime[v.IndustrialManagementId] != "" {
  207. if utils.StrTimeToTime(v.PublishDate).After(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) {
  208. mapUPdateTime[v.IndustrialManagementId] = v.PublishDate
  209. }
  210. }
  211. }
  212. for _, id := range industrialIdArr {
  213. industrialIds += strconv.Itoa(id) + ","
  214. }
  215. industrialIds = strings.TrimRight(industrialIds, ",")
  216. articleList, e := models.GetReportIndustrialReadNumList(userId, industrialIds, user.CreatedTime)
  217. if e != nil && e.Error() != utils.ErrNoRow() {
  218. err = e
  219. return
  220. }
  221. mapHistroyindustrialId := make(map[int]int)
  222. for _, v := range articleList {
  223. mapHistroyindustrialId[v.IndustrialManagementId] = v.Readnum
  224. }
  225. }
  226. nowTime := time.Now().Local()
  227. threeMonBefore := nowTime.AddDate(0, -3, 0)
  228. //查询用户今天是否看过时间线
  229. //haveMorningMeeting := false
  230. //var morningMeetingTime string
  231. recrodList, err := models.GetCygxTimelineLogCount(user.UserId, industrialIdArr)
  232. if err != nil && err.Error() != utils.ErrNoRow() {
  233. return
  234. }
  235. var industrialManagementIdstr string
  236. industrialIdMap := make(map[string]time.Time)
  237. for _, v := range recrodList {
  238. //fmt.Println(v.Router)
  239. //sliceindustrial := strings.Split(v.Router, "=")
  240. //industrialManagementIdstr = strings.TrimLeft(v.Router, utils.TIME_LINE_ROUTE)
  241. industrialManagementIdstr = strconv.Itoa(v.IndustrialManagementId)
  242. industrialIdMap[industrialManagementIdstr] = v.ModifyTime
  243. //fmt.Println(industrialManagementIdstr)
  244. //if createTime, ok := industrialIdMap[industrialManagementIdstr]; ok {
  245. // if createTime.Before(v.CreateTime) {
  246. // industrialIdMap[industrialManagementIdstr] = v.CreateTime
  247. // }
  248. //} else {
  249. // industrialIdMap[industrialManagementIdstr] = v.CreateTime
  250. //}
  251. }
  252. morningMeetingTimeMap := make(map[int]time.Time)
  253. if industrialIds != "" {
  254. mmList, e := models.GetCygxMorningMeetingReviewsListByIndustrialIds(industrialIds)
  255. if e != nil {
  256. err = e
  257. return
  258. }
  259. for _, v := range mmList {
  260. morningMeetingTimeMap[v.IndustryId] = v.CreateTime
  261. }
  262. }
  263. timeLineRedMap := make(map[int]bool, 0)
  264. for _, industrialId := range industrialIdArr {
  265. //if createTime, ok := industrialIdMap[strconv.Itoa(industrialId)]; ok {
  266. // if createTime.Before(morningMeetingTimeMap[industrialId]) {
  267. // timeLineRedMap[industrialId] = true
  268. // }
  269. //}
  270. if industrialIdMap[strconv.Itoa(industrialId)].Before(morningMeetingTimeMap[industrialId]) {
  271. timeLineRedMap[industrialId] = true
  272. }
  273. }
  274. //fmt.Println(industrialIdMap["90"])
  275. //fmt.Println(timeLineRedMap[90])
  276. //fmt.Println(mapIndustrialArticleHistory[90])
  277. for k, v := range list {
  278. list[k].UpdateTime = utils.TimeRemoveHms(mapUPdateTime[v.IndustrialManagementId])
  279. if userId > 0 {
  280. //如果文章没有阅读,而且,文章的发布时间晚于项目的上线时间,而且文章的发布时间晚于用户的注册时间,就进行标红处理
  281. if mapIndustrialArticleHistory[v.IndustrialManagementId] || timeLineRedMap[v.IndustrialManagementId] {
  282. //if mapHistroyindustrialId[v.IndustrialManagementId] == 0 || timeLineRedMap[v.IndustrialManagementId] {
  283. if user.CreatedTime.Before(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) && utils.StrTimeToTime(utils.OnlineTime).Before(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) {
  284. list[k].IsRed = true
  285. }
  286. }
  287. } else {
  288. if utils.StrTimeToTime(utils.OnlineTime).Before(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) {
  289. list[k].IsRed = true
  290. }
  291. list[k].IsFollow = true
  292. }
  293. list[k].UpdateTime = utils.TimeRemoveHms(mapUPdateTime[v.IndustrialManagementId])
  294. if resource == "Yx" {
  295. list[k].IsRed = false
  296. }
  297. // 关联报告发布时间均在3个月内则标记New
  298. if v.MinReportTime != "" {
  299. t, e := time.Parse(utils.FormatDateTime, v.MinReportTime)
  300. if e != nil {
  301. err = errors.New("报告最早发布时间有误,GetindustryVideo " + e.Error())
  302. return
  303. }
  304. if t.After(threeMonBefore) {
  305. list[k].IsNew = true
  306. }
  307. }
  308. }
  309. items = list
  310. return
  311. }
  312. // 通过文章ID获取文章所关联的产业
  313. func GetArticleIndustrialByArticleId(articleIds []int) (itemMap map[int][]*models.IndustrialManagementResp, err error) {
  314. lenarticleIds := len(articleIds)
  315. if lenarticleIds == 0 {
  316. return
  317. }
  318. var condition string
  319. var pars []interface{}
  320. condition = ` AND mg.article_id IN (` + utils.GetOrmInReplace(lenarticleIds) + `)`
  321. pars = append(pars, articleIds)
  322. industrialList, err := models.GetIndustrialListByarticleId(pars, condition)
  323. if err != nil {
  324. return
  325. }
  326. industrialMap := make(map[int][]*models.IndustrialManagementResp)
  327. if len(industrialList) > 0 {
  328. for _, v := range industrialList {
  329. item := new(models.IndustrialManagementResp)
  330. item.IndustrialManagementId = v.IndustrialManagementId
  331. item.IndustryName = v.IndustryName
  332. item.ChartPermissionId = v.ChartPermissionId
  333. industrialMap[v.ArticleId] = append(industrialMap[v.ArticleId], item)
  334. }
  335. }
  336. itemMap = industrialMap
  337. return
  338. }
  339. // 获取研选文章类型的配置信息
  340. func GetArticleTypeMap() (nameMapResp map[int]string, buttonStyleMapResp map[int]string, err error) {
  341. condition := " AND is_show_yanx = 1 "
  342. list, e := models.GetCygxArticleTypeListCondition(condition)
  343. if e != nil {
  344. err = errors.New("报告最早发布时间有误,GetindustryVideo " + e.Error())
  345. return
  346. }
  347. nameMap := make(map[int]string)
  348. buttonStyleMap := make(map[int]string)
  349. if len(list) > 0 {
  350. for _, v := range list {
  351. nameMap[v.ArticleTypeId] = v.ArticleTypeName
  352. buttonStyleMap[v.ArticleTypeId] = v.ButtonStyle
  353. }
  354. }
  355. nameMapResp = nameMap
  356. buttonStyleMapResp = buttonStyleMap
  357. return
  358. }
  359. // GetUserFollowIndustrialMap获取用户关注的产业
  360. func GetUserFollowIndustrialMap(user *models.WxUserItem) (mapResp map[int]int, err error) {
  361. list, e := models.GetUserFllowIndustrialList(user.UserId)
  362. if e != nil {
  363. err = errors.New("GetUserFllowIndustrialList " + e.Error())
  364. return
  365. }
  366. fllowMap := make(map[int]int)
  367. if len(list) > 0 {
  368. for _, v := range list {
  369. fllowMap[v.IndustrialManagementId] = v.IndustrialManagementId
  370. }
  371. }
  372. mapResp = fllowMap
  373. return
  374. }
  375. // IndustrialManagementHotMapGropuPermission获取近一个月报告阅读数量最多的产业信息 根据行业分组
  376. func IndustrialManagementHotMapGropuPermission() (mapResp map[int]bool, err error) {
  377. var topCond string
  378. var topPars []interface{}
  379. toplist, err := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  380. if err != nil {
  381. return
  382. }
  383. mapPermission := make(map[int][]*models.IndustrialManagement)
  384. mapPermissionMax := make(map[int]int)
  385. for _, v := range toplist {
  386. item := new(models.IndustrialManagement)
  387. item.ChartPermissionId = v.ChartPermissionId
  388. item.IndustrialManagementId = v.IndustrialManagementId
  389. item.ArticleReadNum = v.ArticleReadNum
  390. mapPermission[v.ChartPermissionId] = append(mapPermission[v.ChartPermissionId], item)
  391. }
  392. for k, v := range mapPermission {
  393. for _, item := range v {
  394. //mapPermissionMax[k] = item.ArticleReadNum
  395. if item.ArticleReadNum > mapPermissionMax[k] {
  396. mapPermissionMax[k] = item.ArticleReadNum
  397. }
  398. }
  399. }
  400. mapResp = make(map[int]bool, 0)
  401. for _, v := range toplist {
  402. if v.ArticleReadNum == mapPermissionMax[v.ChartPermissionId] {
  403. mapResp[v.IndustrialManagementId] = true
  404. }
  405. }
  406. return
  407. }
  408. // GetCygxIndustryFllowListTypeMapByMobile 根据手机号获取产业关注的信息
  409. func GetCygxIndustryFllowListTypeMapByMobile(mobile string) (mapFollowTypeResp map[int]int) {
  410. if mobile == "" {
  411. return
  412. }
  413. var err error
  414. defer func() {
  415. if err != nil {
  416. fmt.Println("err:", err)
  417. go utils.SendAlarmMsg(fmt.Sprint("根据手机号获取产业关注的信息 失败,GetCygxIndustryFllowListTypeMapByMobile Err:"+err.Error()+"mobile", mobile), 3)
  418. }
  419. }()
  420. mapFollowTypeResp = make(map[int]int, 0)
  421. fllowList, e := models.GetCygxIndustryFllowList(mobile)
  422. //策略的处理
  423. if e != nil {
  424. err = errors.New("GetUserFllowIndustrialList, Err: " + err.Error())
  425. return
  426. }
  427. for _, v := range fllowList {
  428. mapFollowTypeResp[v.IndustrialManagementId] = v.FollowType
  429. }
  430. return
  431. }