industrial_management.go 16 KB

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