chart.go 15 KB

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