report.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/common"
  6. "hongze/hongze_open_api/logic"
  7. "hongze/hongze_open_api/models/request/article"
  8. "hongze/hongze_open_api/models/response/report"
  9. celuePushTable "hongze/hongze_open_api/models/tables/cygx/article"
  10. "hongze/hongze_open_api/models/tables/rddp/classify"
  11. tables "hongze/hongze_open_api/models/tables/report"
  12. "hongze/hongze_open_api/models/tables/wx_user"
  13. "hongze/hongze_open_api/services/alarm_msg"
  14. "hongze/hongze_open_api/utils"
  15. "net/url"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. // 报告模块
  21. type ReportController struct {
  22. BaseAuth
  23. }
  24. // 报告模块
  25. type ReportControllerCommon struct {
  26. BaseCommon
  27. }
  28. // ListReport
  29. // @Title 获取报告列表接口
  30. // @Description 获取报告列表
  31. // @Param _page_size query int true "每页数据条数"
  32. // @Param _page query int true "当前页页码,从1开始"
  33. // @Param report_type query string true "类型 day:晨报 、week :周报、two_week:双周报 、month:月报、other :点评 (默认为day:晨报) "
  34. // @Param keyword query string true "搜索关键词"
  35. // @Param mobile query string true "用户手机号(加密后的)"
  36. // @Success 200 {object} report.ReportListResp
  37. // @router /list [get]
  38. func (c *ReportController) ListReport() {
  39. pageSize, _ := c.GetInt("_page_size")
  40. currentIndex, _ := c.GetInt("_page")
  41. keyWord := c.GetString("keyword")
  42. reportType := c.GetString("report_type")
  43. //mobile := c.GetString("mobile")
  44. var startSize int
  45. if pageSize <= 0 {
  46. pageSize = utils.PageSize20
  47. }
  48. if currentIndex <= 0 {
  49. currentIndex = 1
  50. }
  51. startSize = utils.StartIndex(currentIndex, pageSize)
  52. //通过url获取mobile中的参数
  53. var mobile string
  54. URL, err := url.Parse(c.Ctx.Input.URI())
  55. if err != nil {
  56. c.FailWithMessage("获取报告失败")
  57. return
  58. }
  59. urlParameter := URL.RawQuery
  60. sliceUrl := strings.Split(urlParameter, "&")
  61. if len(sliceUrl) > 0 {
  62. for _, v := range sliceUrl {
  63. if strings.Contains(v, "mobile=") {
  64. mobile = strings.Replace(v, "mobile=", "", -1)
  65. }
  66. }
  67. }
  68. if mobile == "" {
  69. c.FailWithMessage("mobile 必传")
  70. return
  71. }
  72. mobile, err = url.QueryUnescape(mobile)
  73. if err != nil {
  74. c.FailWithMessage("mobile 必传")
  75. return
  76. }
  77. var dateTxt = []byte(mobile)
  78. resultDe := utils.DesBase64Decrypt(dateTxt)
  79. deMobile := string(resultDe)
  80. if deMobile == "" {
  81. c.FailWithMessage("手机号加密格式错误")
  82. return
  83. }
  84. totalFicc, err := tables.GetUserReportFiccCount(deMobile)
  85. if err != nil {
  86. c.FailWithMessage("获取失败")
  87. return
  88. }
  89. if totalFicc < 1 {
  90. c.FailWithMessage("用户不存在")
  91. return
  92. }
  93. var condition string
  94. var pars []interface{}
  95. condition = ` AND enabled = 1 `
  96. if keyWord != "" {
  97. condition += ` AND research_report_name LIKE '%` + keyWord + `%'`
  98. }
  99. //day:晨报 、week :周报、two_week:双周报 、month:月报、other :点评
  100. if reportType != "week" && reportType != "two_week" && reportType != "month" && reportType != "other" {
  101. reportType = "day"
  102. }
  103. if reportType != "" {
  104. condition += ` AND type = ? AND status = 'report' `
  105. pars = append(pars, reportType)
  106. }
  107. total, err := tables.GetReportListCount(condition, pars)
  108. if err != nil {
  109. c.FailWithMessage("获取失败")
  110. return
  111. }
  112. list, err := tables.GetReportList(condition, pars, startSize, pageSize)
  113. if err != nil {
  114. c.FailWithMessage("获取失败")
  115. return
  116. }
  117. mobile = url.QueryEscape(mobile) //转义 +
  118. nonceStr := common.GetRandString(10)
  119. timeUnix := strconv.FormatInt(time.Now().Unix(), 10)
  120. if len(list) > 0 {
  121. for k, v := range list {
  122. postData := make(map[string]string)
  123. reportId := strconv.Itoa(v.ResearchReportId)
  124. parameter := "mobile=" + mobile + "&research_report_id=" + reportId + "&nonce_str=" + nonceStr + "&timestamp=" + timeUnix
  125. postData["mobile"] = mobile
  126. postData["research_report_id"] = reportId
  127. postData["appid"] = utils.ReportAppid
  128. postData["nonce_str"] = nonceStr
  129. postData["timestamp"] = timeUnix
  130. sign := utils.GetSign(postData)
  131. list[k].HttpUrl = utils.ResearchReportUrl + "hzsl/report/detail?" + parameter + "&sign=" + sign
  132. }
  133. }
  134. page := utils.GetPaging(currentIndex, pageSize, total)
  135. resp := tables.ReportListResp{
  136. List: list,
  137. Paging: page,
  138. }
  139. c.OkDetailed(resp, "获取成功")
  140. }
  141. // GetReportInfo
  142. // @Title 获取报告详情
  143. // @Description 获取报告详情
  144. // @Param research_report_id query int true "报告ID"
  145. // @Param mobile query string true "用户手机号(加密后的)"
  146. // @Success 200 {object} report.ResearchReportInfo
  147. // @router /getReportInfo [get]
  148. func (c *ReportControllerCommon) GetReportInfo() {
  149. researchReportId, _ := c.GetInt("research_report_id")
  150. //mobile := c.GetString("mobile")
  151. if researchReportId < 1 {
  152. c.FailWithMessage("请传入报告id")
  153. return
  154. }
  155. mobile := c.GetString("mobile")
  156. if mobile == "" {
  157. c.FailWithMessage("mobile 必传")
  158. return
  159. }
  160. var dateTxt = []byte(mobile)
  161. resultDe := utils.DesBase64Decrypt(dateTxt)
  162. deMobile := string(resultDe)
  163. if deMobile == "" {
  164. c.FailWithMessage("手机号加密格式错误")
  165. return
  166. }
  167. totalFicc, err := tables.GetUserReportFiccCount(deMobile)
  168. if err != nil {
  169. c.FailWithMessage("获取失败")
  170. return
  171. }
  172. if totalFicc < 1 {
  173. c.FailWithMessage("用户不存在")
  174. return
  175. }
  176. userInfo, err := wx_user.GetWxUserByMobileStr(deMobile)
  177. if err != nil {
  178. c.FailWithMessage("找不到该用户")
  179. return
  180. }
  181. reportInfo, hasPermission, err := tables.GetResearchReportInfo(researchReportId, userInfo.UserId)
  182. if err != nil {
  183. c.FailWithMessage("获取报告失败")
  184. return
  185. }
  186. if !hasPermission {
  187. c.FailWithMessage("无权限")
  188. return
  189. }
  190. mobile = url.QueryEscape(mobile) //转义 +
  191. nonceStr := common.GetRandString(10)
  192. timeUnix := strconv.FormatInt(time.Now().Unix(), 10)
  193. if len(reportInfo.ResearchReportTypeList) > 1 {
  194. for k, v := range reportInfo.ResearchReportTypeList {
  195. postData := make(map[string]string)
  196. reportId := strconv.Itoa(int(v.ResearchReportTypeId))
  197. parameter := "mobile=" + mobile + "&ResearchReportTypeId=" + reportId + "&appid=" + utils.ReportAppid + "&nonce_str=" + nonceStr + "&timestamp=" + timeUnix
  198. postData["mobile"] = mobile
  199. postData["ResearchReportTypeId"] = reportId
  200. postData["appid"] = utils.ReportAppid
  201. postData["nonce_str"] = nonceStr
  202. postData["timestamp"] = timeUnix
  203. sign := utils.GetSign(postData)
  204. reportInfo.ResearchReportTypeList[k].HttpUrl = utils.ResearchReportUrl + "report/?" + parameter + "&sign=" + sign
  205. }
  206. }
  207. c.OkDetailed(reportInfo, "获取成功")
  208. }
  209. // GetResearchReportChapter
  210. // @Title 获取章节详情接口
  211. // @Description 获取章节详情
  212. // @Param ResearchReportTypeId query int true "章节ID"
  213. // @Param mobile query string false "用户手机号(加密后的)"
  214. // @Success 200 {object} report.ResearchReportTypeContentInfo
  215. // @router /getReportChapterInfo [get]
  216. func (c *ReportControllerCommon) GetResearchReportChapter() {
  217. researchReportTypeId, _ := c.GetInt("ResearchReportTypeId")
  218. //mobile := c.GetString("mobile")
  219. if researchReportTypeId < 1 {
  220. c.FailWithMessage("请传入章节id")
  221. return
  222. }
  223. mobile := c.GetString("mobile")
  224. if mobile == "" {
  225. c.FailWithMessage("mobile 必传")
  226. return
  227. }
  228. var dateTxt = []byte(mobile)
  229. resultDe := utils.DesBase64Decrypt(dateTxt)
  230. deMobile := string(resultDe)
  231. if deMobile == "" {
  232. c.FailWithMessage("手机号加密格式错误")
  233. return
  234. }
  235. totalFicc, err := tables.GetUserReportFiccCount(deMobile)
  236. if err != nil {
  237. c.FailWithMessage("获取失败")
  238. return
  239. }
  240. if totalFicc < 1 {
  241. c.FailWithMessage("用户不存在")
  242. return
  243. }
  244. userInfo, err := wx_user.GetWxUserByMobileStr(deMobile)
  245. if err != nil {
  246. c.FailWithMessage("找不到该用户")
  247. return
  248. }
  249. reportInfo, hasPermission, err := tables.GetResearchReportTypeContentInfo(uint64(researchReportTypeId), uint64(userInfo.UserId))
  250. if err != nil {
  251. c.FailWithMessage("获取报告失败")
  252. return
  253. }
  254. if !hasPermission {
  255. c.FailWithMessage("无权限")
  256. return
  257. }
  258. c.OkDetailed(reportInfo, "获取成功")
  259. }
  260. // ArticleChange
  261. // @Title 报告变更通知的插入点接口
  262. // @Description 报告变更通知的插入点接口
  263. // @Param request body article.CreatArticleCeluePushReq true "type json string"
  264. // @Success 200 创建成功
  265. // @router /article/change [post]
  266. func (c *ReportController) ArticleChange() {
  267. var req article.CreatArticleCeluePushReq
  268. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  269. if err != nil {
  270. c.FailWithMessage("参数解析异常")
  271. return
  272. }
  273. //appid权限校验
  274. appid := req.Appid
  275. if utils.RunMode == "release" && appid != "XVuGlcyEEVNYVWx5" {
  276. c.FailWithMessage("无权限")
  277. return
  278. }
  279. articleId := req.ArticleId
  280. action := req.Action
  281. if articleId < 0 {
  282. c.FailWithMessage("缺少 article_id 参数")
  283. return
  284. }
  285. if action != "add" && action != "edit" && action != "move" {
  286. c.FailWithMessage("action参数类型错误")
  287. return
  288. }
  289. item := new(celuePushTable.CygxArticleCeluePush)
  290. item.ArticleId = articleId
  291. item.Action = action
  292. item.CreateTime = time.Now()
  293. item.ModifyTime = time.Now()
  294. err = celuePushTable.AddCygxArticleCeluePush(item)
  295. if err != nil {
  296. c.OkWithMessage("创建失败")
  297. return
  298. }
  299. if action == "edit" || action == "move" {
  300. log := &celuePushTable.CygxArticleCeluePushRedis{ArticleId: articleId, Action: action, CreateTime: time.Now()}
  301. if utils.Re == nil {
  302. err := utils.Rc.LPush(utils.CYGX_ARTICLE_UPDATE_KEY, log)
  303. if err != nil {
  304. go alarm_msg.SendAlarmMsg(fmt.Sprint("CygxArticleCeluePushRedis LPush Err:", err.Error(), "文章ID", articleId), 3)
  305. }
  306. }
  307. }
  308. c.OkWithMessage("创建成功")
  309. }
  310. //func init() {
  311. // articleId := 10187
  312. // action := "edit"
  313. // log := &celuePushTable.CygxArticleCeluePushRedis{ArticleId: articleId, Action: action, CreateTime: time.Now()}
  314. // if utils.Re == nil {
  315. // err := utils.Rc.LPush(utils.CYGX_ARTICLE_UPDATE_KEY, log)
  316. // if err != nil {
  317. // go alarm_msg.SendAlarmMsg(fmt.Sprint("CygxArticleCeluePushRedis LPush Err:", err.Error(), "文章ID", articleId), 3)
  318. // }
  319. // }
  320. //}
  321. // ClassifyList
  322. // @Title 获取报告分类列表接口
  323. // @Description 获取报告分类列表接口
  324. // @Param _page_size query int true "每页数据条数"
  325. // @Param _page query int true "当前页页码,从1开始"
  326. // @Param report_type query string true "类型 day:晨报 、week :周报、two_week:双周报 、month:月报、other :点评 (默认为day:晨报) "
  327. // @Param keyword query string true "搜索关键词"
  328. // @Param mobile query string true "用户手机号(加密后的)"
  329. // @Success 200 {object} classify.ClassifyList
  330. // @router /classify/list [get]
  331. func (c *ReportController) ClassifyList() {
  332. list, err := classify.GetClassifyList()
  333. if err != nil {
  334. c.FailWithMessage("获取分类失败")
  335. return
  336. }
  337. for _, v := range list {
  338. childList, err := classify.GetClassifyListByParentId(v.Id)
  339. if err != nil {
  340. c.FailWithMessage("获取下级分类失败")
  341. return
  342. }
  343. for _, childV := range childList {
  344. childV.Child = make([]*classify.ClassifyList, 0)
  345. }
  346. if len(childList) == 0 {
  347. tmpClassifyList := *v
  348. tmpClassifyList.Child = make([]*classify.ClassifyList, 0)
  349. childList = []*classify.ClassifyList{&tmpClassifyList}
  350. }
  351. v.Child = childList
  352. }
  353. c.OkDetailed(list, "获取成功")
  354. }
  355. // ListReportV2
  356. // @Title 获取报告列表接口
  357. // @Description 获取报告列表
  358. // @Param _page_size query int true "每页数据条数"
  359. // @Param _page query int true "当前页页码,从1开始"
  360. // @Param classify_id query int true "分类id"
  361. // @Param keyword query string true "搜索关键词"
  362. // @Param mobile query string true "用户手机号(加密后的)"
  363. // @Success 200 {object} report.ReportListResp
  364. // @router /list/v2 [get]
  365. func (c *ReportController) ListReportV2() {
  366. pageSize, _ := c.GetInt("_page_size")
  367. currentIndex, _ := c.GetInt("_page")
  368. keyword := c.GetString("keyword")
  369. classifyId, _ := c.GetInt("classify_id")
  370. //mobile := c.GetString("mobile")
  371. var startSize int
  372. if pageSize <= 0 {
  373. pageSize = utils.PageSize20
  374. }
  375. if currentIndex <= 0 {
  376. currentIndex = 1
  377. }
  378. startSize = utils.StartIndex(currentIndex, pageSize)
  379. //通过url获取mobile中的参数
  380. var mobile string
  381. URL, err := url.Parse(c.Ctx.Input.URI())
  382. if err != nil {
  383. c.FailWithMessage("获取报告失败")
  384. return
  385. }
  386. urlParameter := URL.RawQuery
  387. sliceUrl := strings.Split(urlParameter, "&")
  388. if len(sliceUrl) > 0 {
  389. for _, v := range sliceUrl {
  390. if strings.Contains(v, "mobile=") {
  391. mobile = strings.Replace(v, "mobile=", "", -1)
  392. }
  393. }
  394. }
  395. if mobile == "" {
  396. c.FailWithMessage("mobile 必传")
  397. return
  398. }
  399. mobile, err = url.QueryUnescape(mobile)
  400. if err != nil {
  401. c.FailWithMessage("mobile 必传")
  402. return
  403. }
  404. var dateTxt = []byte(mobile)
  405. resultDe := utils.DesBase64Decrypt(dateTxt)
  406. deMobile := string(resultDe)
  407. if deMobile == "" {
  408. c.FailWithMessage("手机号加密格式错误")
  409. return
  410. }
  411. if classifyId <= 0 {
  412. c.FailWithMessage("分类必传")
  413. return
  414. }
  415. total, list, err, errMsg := logic.GetReportList(classifyId, deMobile, keyword, startSize, pageSize)
  416. if err != nil {
  417. c.FailWithMessage(errMsg)
  418. return
  419. }
  420. page := utils.GetPaging(currentIndex, pageSize, total)
  421. resp := report.ReportListResp{
  422. List: list,
  423. Paging: page,
  424. }
  425. c.OkDetailed(resp, "获取成功")
  426. }
  427. // GetReportInfoV2
  428. // @Title 获取报告详情
  429. // @Description 获取报告详情
  430. // @Param report_id query int true "报告ID"
  431. // @Param mobile query string true "用户手机号(加密后的)"
  432. // @Success 200 {object} report.ReportDetail
  433. // @router /getReportInfo/v2 [get]
  434. func (c *ReportControllerCommon) GetReportInfoV2() {
  435. reportId, _ := c.GetInt("report_id")
  436. //mobile := c.GetString("mobile")
  437. if reportId < 1 {
  438. c.FailWithMessage("请传入报告id")
  439. return
  440. }
  441. mobile := c.GetString("mobile")
  442. if mobile == "" {
  443. c.FailWithMessage("mobile 必传")
  444. return
  445. }
  446. var dateTxt = []byte(mobile)
  447. resultDe := utils.DesBase64Decrypt(dateTxt)
  448. deMobile := string(resultDe)
  449. if deMobile == "" {
  450. c.FailWithMessage("手机号加密格式错误")
  451. return
  452. }
  453. reportDetail, err, errMsg := logic.GetReportDetail(reportId, deMobile)
  454. if err != nil {
  455. //c.FailWithMessage(errMsg)
  456. c.FailWithMessageErr(errMsg, err.Error())
  457. return
  458. }
  459. c.OkDetailed(reportDetail, "获取成功")
  460. }
  461. // GetResearchReportChapterV2
  462. // @Title 获取章节详情接口
  463. // @Description 获取章节详情
  464. // @Param report_chapter_id query int true "章节ID"
  465. // @Param mobile query string false "用户手机号(加密后的)"
  466. // @Success 200 {object} report.ReportChapterDetail
  467. // @router /getReportChapterInfo/v2 [get]
  468. func (c *ReportControllerCommon) GetResearchReportChapterV2() {
  469. reportChapterId, _ := c.GetInt("report_chapter_id")
  470. if reportChapterId < 1 {
  471. c.FailWithMessage("请传入章节id")
  472. return
  473. }
  474. mobile := c.GetString("mobile")
  475. if mobile == "" {
  476. c.FailWithMessage("mobile 必传")
  477. return
  478. }
  479. var dateTxt = []byte(mobile)
  480. resultDe := utils.DesBase64Decrypt(dateTxt)
  481. deMobile := string(resultDe)
  482. if deMobile == "" {
  483. c.FailWithMessage("手机号加密格式错误")
  484. return
  485. }
  486. chapterDetail, err, errMsg := logic.GetChapterDetail(deMobile, reportChapterId)
  487. if err != nil {
  488. //c.FailWithMessage(errMsg)
  489. c.FailWithMessageErr(errMsg, err.Error())
  490. return
  491. }
  492. c.OkDetailed(chapterDetail, "获取成功")
  493. }
  494. // GetTickerData
  495. // @Title 获取报告详情获取指标数据接口
  496. // @Description 获取报告详情获取指标数据接口
  497. // @Param report_chapter_id query int true "章节ID"
  498. // @Success 200 {object} report.TickerData
  499. // @router /getTickerData [get]
  500. func (c *ReportControllerCommon) GetTickerData() {
  501. reportChapterId, _ := c.GetInt("report_chapter_id")
  502. if reportChapterId < 1 {
  503. c.FailWithMessage("请传入章节id")
  504. return
  505. }
  506. tickerData, err, errMsg := logic.GetTickerData(reportChapterId)
  507. if err != nil {
  508. //c.FailWithMessage(errMsg)
  509. c.FailWithMessageErr(errMsg, err.Error())
  510. return
  511. }
  512. c.OkDetailed(tickerData, "获取成功")
  513. }