industrial_management.go 17 KB

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