chart.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "hongze/hongze_clpt/models"
  7. "hongze/hongze_clpt/utils"
  8. "io/ioutil"
  9. nhttp "net/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. //get公共请求方法
  15. func PublicGetDate(url, authorization string) (body []byte, err error) {
  16. defer func() {
  17. if err != nil {
  18. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", url+"Get ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  19. }
  20. }()
  21. method := "GET"
  22. client := &nhttp.Client{}
  23. req, err := nhttp.NewRequest(method, url, nil)
  24. if err != nil {
  25. return
  26. }
  27. req.Header.Add("Authorization", authorization)
  28. res, err := client.Do(req)
  29. if err != nil {
  30. return
  31. }
  32. defer res.Body.Close()
  33. body, err = ioutil.ReadAll(res.Body)
  34. if err != nil {
  35. return
  36. }
  37. return
  38. }
  39. //同步图表列表
  40. func GetChartListByApi(cont context.Context) (err error) {
  41. defer func() {
  42. if err != nil {
  43. go utils.SendAlarmMsg("发送邮件:同步用户到策略平台信息失败", 2)
  44. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetArticleListByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  45. }
  46. }()
  47. mapChartid := make(map[int]int)
  48. chartList, err := models.GetChartListAll()
  49. if err != nil {
  50. return
  51. }
  52. for _, v := range chartList {
  53. mapChartid[v.ChartId] = v.ChartId
  54. }
  55. mapAllChartid := make(map[int]int)
  56. chartAllList, err := models.GetChartAllListAll()
  57. if err != nil {
  58. return
  59. }
  60. for _, v := range chartAllList {
  61. mapAllChartid[v.ChartId] = v.ChartId
  62. }
  63. url := utils.ApiUrl + "charts/mp?take=100&skip=0"
  64. authorization := utils.ApiAuthorization
  65. body, err := PublicGetDate(url, authorization)
  66. if err != nil {
  67. return
  68. }
  69. var chartResult models.ChartResultApi
  70. err = json.Unmarshal(body, &chartResult)
  71. if err != nil {
  72. return err
  73. }
  74. for _, v := range chartResult.Data {
  75. item := new(models.CygxChart)
  76. item.ChartId = v.ChartId
  77. item.PtagId = v.PtagId
  78. item.CtagId = v.CtagId
  79. item.Title = v.Title
  80. item.TitleEn = v.TitleEn
  81. item.CreateDateApi = time.Now()
  82. item.CreateDate = v.CreateDate
  83. item.PublishStatus = v.PublishStatus
  84. item.PtagName = v.Ptag.Name
  85. item.CtagName = v.Ctag.Name
  86. item.PtagNameTwo = v.PtagTwo.Name
  87. item.CtagNameTwo = v.CtagTwo.Name
  88. item.PtagIdTwo = v.PtagTwo.Id
  89. item.CtagIdTwo = v.CtagTwo.Id
  90. item.Cover = v.Cover
  91. item.Iframe = v.Iframe
  92. //如果没有就新增 有就更新
  93. if mapChartid[v.ChartId] == 0 {
  94. _, err := models.AddCygxChart(item)
  95. if err != nil {
  96. return err
  97. }
  98. } else {
  99. updateParams := make(map[string]interface{})
  100. updateParams["Title"] = v.Title
  101. updateParams["PtagId"] = v.PtagId
  102. updateParams["CtagId"] = v.CtagId
  103. updateParams["TitleEn"] = v.TitleEn
  104. updateParams["CreateDate"] = v.CreateDate
  105. updateParams["PublishStatus"] = v.PublishStatus
  106. updateParams["PtagName"] = v.Ptag.Name
  107. updateParams["CtagName"] = v.Ctag.Name
  108. updateParams["PtagNameTwo"] = v.PtagTwo.Name
  109. updateParams["CtagNameTwo"] = v.CtagTwo.Name
  110. updateParams["PtagIdTwo"] = v.PtagTwo.Id
  111. updateParams["CtagIdTwo"] = v.CtagTwo.Id
  112. updateParams["Cover"] = v.Cover
  113. updateParams["Iframe"] = v.Iframe
  114. whereParam := map[string]interface{}{"chart_id": v.ChartId}
  115. err = models.UpdateByExpr(models.CygxChart{}, whereParam, updateParams)
  116. if err != nil {
  117. return err
  118. }
  119. }
  120. }
  121. //策略平台图表,记录所有,的显示用户收藏使用
  122. url = utils.ApiUrl + "charts?take=100&skip=0" // 获取所有的图表链接
  123. body, err = PublicGetDate(url, authorization)
  124. if err != nil {
  125. return
  126. }
  127. err = json.Unmarshal(body, &chartResult)
  128. if err != nil {
  129. return err
  130. }
  131. for _, v := range chartResult.Data {
  132. item := new(models.CygxChartAll)
  133. item.ChartId = v.ChartId
  134. item.PtagId = v.PtagId
  135. item.CtagId = v.CtagId
  136. item.Title = v.Title
  137. item.TitleEn = v.TitleEn
  138. item.CreateDateApi = time.Now()
  139. item.CreateDate = v.CreateDate
  140. item.PublishStatus = v.PublishStatus
  141. item.PtagName = v.Ptag.Name
  142. item.CtagName = v.Ctag.Name
  143. item.PtagNameTwo = v.PtagTwo.Name
  144. item.CtagNameTwo = v.CtagTwo.Name
  145. item.PtagIdTwo = v.PtagTwo.Id
  146. item.CtagIdTwo = v.CtagTwo.Id
  147. item.Cover = v.Cover
  148. item.Iframe = v.Iframe
  149. //如果没有就新增 有就更新
  150. if mapAllChartid[v.ChartId] == 0 {
  151. _, err := models.AddCygxChartAll(item)
  152. if err != nil {
  153. return err
  154. }
  155. } else {
  156. updateParams := make(map[string]interface{})
  157. updateParams["Title"] = v.Title
  158. updateParams["PtagId"] = v.PtagId
  159. updateParams["CtagId"] = v.CtagId
  160. updateParams["TitleEn"] = v.TitleEn
  161. updateParams["CreateDate"] = v.CreateDate
  162. updateParams["PublishStatus"] = v.PublishStatus
  163. updateParams["PtagName"] = v.Ptag.Name
  164. updateParams["CtagName"] = v.Ctag.Name
  165. updateParams["PtagNameTwo"] = v.PtagTwo.Name
  166. updateParams["CtagNameTwo"] = v.CtagTwo.Name
  167. updateParams["PtagIdTwo"] = v.PtagTwo.Id
  168. updateParams["CtagIdTwo"] = v.CtagTwo.Id
  169. updateParams["Cover"] = v.Cover
  170. updateParams["Iframe"] = v.Iframe
  171. whereParam := map[string]interface{}{"chart_id": v.ChartId}
  172. err = models.UpdateByExpr(models.CygxChartAll{}, whereParam, updateParams)
  173. if err != nil {
  174. return err
  175. }
  176. }
  177. }
  178. return
  179. }
  180. //获取图表分类
  181. func GetChartPtagByApi() (items []*models.ChartPtagResp, err error) {
  182. defer func() {
  183. if err != nil {
  184. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetChartPtagByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  185. }
  186. }()
  187. url := utils.ApiUrl + "charts/ptag"
  188. authorization := utils.ApiAuthorization
  189. body, err := PublicGetDate(url, authorization)
  190. if err != nil {
  191. return
  192. }
  193. var chartResult models.ChartPtagResultApi
  194. err = json.Unmarshal(body, &chartResult)
  195. if err != nil {
  196. return
  197. }
  198. for _, v := range chartResult.Data {
  199. item := new(models.ChartPtagResp)
  200. item.PermissionName = v.ChartPermissionName
  201. item.ChartPermissionId = v.ChartPermissionId
  202. if len(v.Ctag) > 0 {
  203. for _, v2 := range v.Ctag {
  204. itemCtag := new(models.CtagResp)
  205. itemCtag.CtagId = v2.Id
  206. itemCtag.Name = v2.Name
  207. item.List = append(item.List, itemCtag)
  208. }
  209. }
  210. items = append(items, item)
  211. }
  212. return
  213. }
  214. //获取用户的Token
  215. func GetUserTokenByMobile(mobile string) (token string, err error) {
  216. //缓存校验
  217. cacheKey := fmt.Sprint("xygx_chart:chart_token:add:", "Mobile", mobile)
  218. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  219. if ttlTime > 0 {
  220. token, _ = utils.Rc.RedisString(cacheKey)
  221. }
  222. if token == "" {
  223. url := utils.ApiUrl + "auth/login"
  224. method := "POST"
  225. payload := strings.NewReader(`{
  226. "phone_number":"` + mobile + `",
  227. "password":"hz123456"}`)
  228. client := &nhttp.Client{}
  229. req, errReq := nhttp.NewRequest(method, url, payload)
  230. if errReq != nil {
  231. err = errReq
  232. return
  233. }
  234. req.Header.Add("Content-Type", "application/json")
  235. req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
  236. res, errReq := client.Do(req)
  237. if errReq != nil {
  238. err = errReq
  239. return
  240. }
  241. defer res.Body.Close()
  242. body, errReq := ioutil.ReadAll(res.Body)
  243. if errReq != nil {
  244. err = errReq
  245. return
  246. }
  247. var chartResult models.ChartUserTokenResultApi
  248. errReq = json.Unmarshal(body, &chartResult)
  249. if errReq != nil {
  250. err = errReq
  251. return
  252. }
  253. token = chartResult.Data.AccessToken
  254. utils.Rc.Put(cacheKey, token, time.Hour*24)
  255. }
  256. return
  257. }
  258. //获取图表收藏
  259. func GetChartCollectionByApi(mobile string, take, skip int) (items []*models.HomeChartListResp, err error, total int) {
  260. defer func() {
  261. if err != nil {
  262. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetChartPtagByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  263. }
  264. }()
  265. url := utils.ApiUrl + "charts/favorites?take=" + strconv.Itoa(take) + "&skip=" + strconv.Itoa(skip)
  266. authorization, err := GetUserTokenByMobile(mobile)
  267. if err != nil {
  268. return
  269. }
  270. authorization = "bearer " + authorization
  271. body, err := PublicGetDate(url, authorization)
  272. if err != nil {
  273. return
  274. }
  275. var chartResult models.ChartFavoritesResultApi
  276. err = json.Unmarshal(body, &chartResult)
  277. if err != nil {
  278. return
  279. }
  280. for _, v := range chartResult.Data {
  281. item := new(models.HomeChartListResp)
  282. item.ChartId = v.ChartId
  283. item.Title = v.ChartInfo.Title
  284. item.TitleEn = v.ChartInfo.TitleEn
  285. item.CreateDate = v.CreateDate
  286. item.PtagName = v.ChartInfo.Ptag.Name
  287. item.CtagName = v.ChartInfo.Ctag.Name
  288. item.BodyHtml = v.ChartInfo.Cover
  289. item.HttpUrl = utils.CHART_INFO_HTTP_URL + strconv.Itoa(v.ChartId)
  290. item.IsNeedJump = true
  291. items = append(items, item)
  292. }
  293. total = chartResult.Pagination.Total
  294. return
  295. }
  296. //判断策略平台是否已经添加过收藏
  297. func GetIsCollectionChart(mobile string, chartId int) (isCollection bool, err error) {
  298. //获取所有的收藏列表,进行比对看看是否收藏,调用三方接口详情没有是否收藏的字段
  299. list, err, _ := GetChartCollectionByApi(mobile, 9999, 0)
  300. if err != nil {
  301. return
  302. }
  303. for _, v := range list {
  304. if v.ChartId == chartId {
  305. isCollection = true
  306. }
  307. }
  308. return
  309. }
  310. //添加收藏
  311. func AddCollectionChart(mobile string, chartId int) (err error) {
  312. authorization, err := GetUserTokenByMobile(mobile)
  313. if err != nil {
  314. return
  315. }
  316. authorization = "bearer " + authorization
  317. url := utils.ApiUrl + "charts/favorites"
  318. method := "POST"
  319. payload := strings.NewReader(`{
  320. "chart_id":` + strconv.Itoa(chartId) + `
  321. }`)
  322. client := &nhttp.Client{}
  323. req, err := nhttp.NewRequest(method, url, payload)
  324. if err != nil {
  325. return
  326. }
  327. req.Header.Add("Authorization", authorization)
  328. req.Header.Add("Content-Type", "application/json")
  329. req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
  330. res, err := client.Do(req)
  331. if err != nil {
  332. return
  333. }
  334. defer res.Body.Close()
  335. _, err = ioutil.ReadAll(res.Body)
  336. if err != nil {
  337. return
  338. }
  339. return
  340. }
  341. //移除收藏
  342. func DeleteCollectionChart(mobile string, chartId int) (err error) {
  343. authorization, err := GetUserTokenByMobile(mobile)
  344. if err != nil {
  345. return
  346. }
  347. authorization = "bearer " + authorization
  348. url := utils.ApiUrl + "charts/favorites/" + strconv.Itoa(chartId)
  349. method := "DELETE"
  350. client := &nhttp.Client{}
  351. req, err := nhttp.NewRequest(method, url, nil)
  352. if err != nil {
  353. return
  354. }
  355. req.Header.Add("Authorization", authorization)
  356. req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
  357. res, err := client.Do(req)
  358. if err != nil {
  359. return
  360. }
  361. defer res.Body.Close()
  362. _, err = ioutil.ReadAll(res.Body)
  363. if err != nil {
  364. return
  365. }
  366. return
  367. }
  368. func DoCompany() {
  369. //listCollect, err := models.GetCygxArticleCollectList()
  370. //if err != nil {
  371. // fmt.Println("GetAddCygxArticleCollectList ,Err" + err.Error())
  372. //}
  373. //for _, v := range listCollect {
  374. // user, err := models.GetWxUserItemByUserId(v.UserId)
  375. // if err != nil && err.Error() != utils.ErrNoRow() {
  376. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  377. // }
  378. // if user != nil {
  379. // fmt.Println(user.RealName)
  380. // err = models.UpdateCygxArticleCollect(user)
  381. // if err != nil {
  382. // fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
  383. // }
  384. // }
  385. //}
  386. //listCollect, err := models.GetCygxSearchKeyWordList()
  387. //if err != nil {
  388. // fmt.Println("GetAddCygxArticleCollectList ,Err" + err.Error())
  389. //}
  390. //for _, v := range listCollect {
  391. // user, err := models.GetWxUserItemByUserId(v.UserId)
  392. // if err != nil && err.Error() != utils.ErrNoRow() {
  393. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  394. // }
  395. // if user != nil {
  396. // fmt.Println(user.RealName)
  397. // err = models.UpdateCygxSearchKeyWord(user)
  398. // if err != nil {
  399. // fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
  400. // }
  401. // }
  402. //}
  403. //var condition string
  404. ////updateTime := time.Now().Add(-time.Hour * 25).Format(utils.FormatDateTime)
  405. //condition = ` AND real_name = '' `
  406. //listArticlePv, err := models.GetArticleHistoryRecordAllByMobileList(condition)
  407. //if err != nil {
  408. // fmt.Println("GetArticleHistoryRecordAllByMobileList ,Err" + err.Error())
  409. //}
  410. //fmt.Println("长度", len(listArticlePv))
  411. //for k, v := range listArticlePv {
  412. // if v.Mobile != "" {
  413. // user, err := models.GetWxUserItemByMobile(v.Mobile)
  414. // if err != nil && err.Error() != utils.ErrNoRow() {
  415. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  416. // }
  417. // if user != nil {
  418. // fmt.Println(user.RealName, k)
  419. // err = models.UpdateCygxArticleHistoryRecordAll(user)
  420. // if err != nil {
  421. // fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
  422. // }
  423. // }
  424. // }
  425. //}
  426. //listIndustryFllow, err := models.GetCygxIndustryFllowList("")
  427. //if err != nil {
  428. // fmt.Println("GetArticleDepartmentFollowByMobileList ,Err" + err.Error())
  429. //}
  430. //fmt.Println("长度", len(listIndustryFllow))
  431. //for k, v := range listIndustryFllow {
  432. // if v.Mobile != "" {
  433. // user, err := models.GetWxUserItemByMobile(v.Mobile)
  434. // if err != nil && err.Error() != utils.ErrNoRow() {
  435. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  436. // }
  437. // if user != nil {
  438. // fmt.Println(user.RealName, k)
  439. // err = models.UpdateCygxIndustryFllow(user)
  440. // if err != nil {
  441. // fmt.Println("UpdateCygxIndustryFllow ,Err" + err.Error())
  442. // }
  443. // }
  444. // }
  445. //}
  446. //
  447. //lisDepartmentF, err := models.GetArticleDepartmentFollowByMobileList("")
  448. //if err != nil {
  449. // fmt.Println("GetArticleDepartmentFollowByMobileList ,Err" + err.Error())
  450. //}
  451. //fmt.Println("长度", len(lisDepartmentF))
  452. //for k, v := range lisDepartmentF {
  453. // if v.Mobile != "" {
  454. // user, err := models.GetWxUserItemByMobile(v.Mobile)
  455. // if err != nil && err.Error() != utils.ErrNoRow() {
  456. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  457. // }
  458. // if user != nil {
  459. // fmt.Println(user.RealName, k)
  460. // err = models.UpdateCygxArticleDepartmentFollow(user)
  461. // if err != nil {
  462. // fmt.Println("UpdateCygxArticleDepartmentFollow ,Err" + err.Error())
  463. // }
  464. // }
  465. // }
  466. //}
  467. //listChartCollect, err := models.GetCygxChartCollectByMobileList()
  468. //if err != nil {
  469. // fmt.Println("GetCygxChartCollectByMobileList ,Err" + err.Error())
  470. //}
  471. //fmt.Println("长度", len(listChartCollect))
  472. //for k, v := range listChartCollect {
  473. // if v.Mobile != "" {
  474. // user, err := models.GetWxUserItemByMobile(v.Mobile)
  475. // if err != nil && err.Error() != utils.ErrNoRow() {
  476. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  477. // }
  478. // if user != nil {
  479. // fmt.Println(user.RealName, k)
  480. // err = models.UpdateCygxChartCollect(user)
  481. // if err != nil {
  482. // fmt.Println("UpdateCygxChartCollect ,Err" + err.Error())
  483. // }
  484. // }
  485. // }
  486. //}
  487. fmt.Println("end")
  488. }