message.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/models/request"
  6. "eta/eta_mini_crm_ht/models/response"
  7. "eta/eta_mini_crm_ht/services"
  8. "eta/eta_mini_crm_ht/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type MessageController struct {
  16. BaseAuthController
  17. }
  18. // ReportList
  19. // @Title 研报列表
  20. // @Description pdf研报列表
  21. // @Param PageSize query int true "每页数据条数"
  22. // @Param CurrentIndex query int true "当前页页码,从1开始"
  23. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  24. // @Param KeyWord query string true "报告标题/创建人"
  25. // @Param SortType query string true "排序方式"
  26. // @Success 200 {object} models.ReportAuthorResp
  27. // @router /reportList [get]
  28. func (this *MessageController) ReportList() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. pageSize, _ := this.GetInt("PageSize")
  35. currentIndex, _ := this.GetInt("CurrentIndex")
  36. permissionIds := this.GetString("PermissionIds")
  37. analystNames := this.GetString("AnalystNames")
  38. sortType := this.GetString("SortType")
  39. KeyWord := this.GetString("KeyWord")
  40. var permissionCondition string
  41. var condition string
  42. var pars []interface{}
  43. if pageSize <= 0 {
  44. pageSize = utils.PageSize20
  45. }
  46. if currentIndex <= 0 {
  47. currentIndex = 1
  48. }
  49. if KeyWord != "" {
  50. condition += " AND title like '%" + KeyWord + "%'"
  51. }
  52. var permissionPars []interface{}
  53. var classifyPars []interface{}
  54. var htPermissions []int
  55. if permissionIds != "" {
  56. permissionArr := strings.Split(permissionIds, ",")
  57. for _, permissionId := range permissionArr {
  58. perId, _ := strconv.Atoi(permissionId)
  59. htPermissions = append(htPermissions, perId)
  60. permissionPars = append(permissionPars, perId)
  61. }
  62. permissionCondition += " AND chart_permission_id in (" + utils.GetOrmReplaceHolder(len(permissionPars)) + ")"
  63. classifyIds, err := models.GetClassifyIdsByPermissionIds(permissionCondition, permissionPars)
  64. if err != nil {
  65. br.Msg = "获取报告列表失败"
  66. br.ErrMsg = "eta获取品种信息失败,Err:" + err.Error()
  67. return
  68. }
  69. if len(classifyIds) == 0 {
  70. br.Ret = 200
  71. br.Success = true
  72. page := paging.GetPaging(currentIndex, pageSize, 0)
  73. resp := new(response.ReportListResp)
  74. resp.List = []*models.ReportView{}
  75. resp.Paging = page
  76. br.Msg = "获取成功"
  77. br.Data = resp
  78. return
  79. }
  80. classifyCondition := " AND COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) in (" + utils.GetOrmReplaceHolder(len(classifyIds)) + ")"
  81. classifyPars = append(classifyPars, classifyIds)
  82. var orgIds []int
  83. // eta
  84. orgIds, _ = models.GetETAReportIdsByCondition(classifyCondition, classifyPars)
  85. if err != nil {
  86. br.Msg = "获取报告列表失败"
  87. br.ErrMsg = "获取报告表ids失败,Err:" + err.Error()
  88. return
  89. }
  90. //海通
  91. names, err := models.GetPermissionNames(htPermissions)
  92. if err != nil {
  93. br.Msg = "获取报告列表失败"
  94. br.ErrMsg = "获取品种名称失败,Err:" + err.Error()
  95. return
  96. }
  97. if len(orgIds) == 0 && len(names) == 0 {
  98. br.Ret = 200
  99. br.Success = true
  100. br.Msg = "获取成功"
  101. page := paging.GetPaging(currentIndex, pageSize, 0)
  102. resp := new(response.ReportListResp)
  103. resp.List = []*models.ReportView{}
  104. resp.Paging = page
  105. br.Data = resp
  106. return
  107. } else if len(orgIds) == 0 {
  108. condition += "And (plate_name in (" + utils.GetOrmReplaceHolder(len(names)) + ") And source ='HT')"
  109. pars = append(pars, names)
  110. } else if len(names) == 0 {
  111. condition += "AND ( org_id in (" + utils.GetOrmReplaceHolder(len(orgIds)) + ") And source ='ETA') "
  112. pars = append(pars, orgIds)
  113. } else {
  114. condition += "AND ( (org_id in (" + utils.GetOrmReplaceHolder(len(orgIds)) + ") And source ='ETA') or (plate_name in (" + utils.GetOrmReplaceHolder(len(names)) + ") And source ='HT'))"
  115. pars = append(pars, orgIds)
  116. pars = append(pars, names)
  117. }
  118. }
  119. if analystNames != "" {
  120. condition += " AND ("
  121. analystNameArr := strings.Split(analystNames, ",")
  122. for i := 0; i < len(analystNameArr); i++ {
  123. condition += "(author like '%" + analystNameArr[i] + "%')"
  124. if i != len(analystNameArr)-1 {
  125. condition += " OR "
  126. }
  127. }
  128. condition += " )"
  129. //pars = append(pars, analystNameArr)
  130. }
  131. sortCondition := " ORDER BY published_time "
  132. if sortType == "" {
  133. sortType = "DESC"
  134. }
  135. sortCondition = sortCondition + sortType
  136. total, err := models.GetReportCountByCondition(condition, pars)
  137. if err != nil {
  138. br.Msg = "获取报告列表失败"
  139. br.ErrMsg = "获取报告列表统计失败,Err:" + err.Error()
  140. return
  141. }
  142. startSize := utils.StartIndex(currentIndex, pageSize)
  143. reportList, err := models.GetReportByCondition(condition, sortCondition, pars, startSize, pageSize)
  144. if err != nil {
  145. br.Msg = "获取报告列表失败"
  146. br.ErrMsg = "获取报告列表失败,Err:" + err.Error()
  147. return
  148. }
  149. var reportViewList []*models.ReportView
  150. for _, report := range reportList {
  151. reportView := report.ToView()
  152. reportViewList = append(reportViewList, reportView)
  153. }
  154. for _, report := range reportViewList {
  155. switch report.Source {
  156. case "ETA":
  157. var classifyId int
  158. classifyId, err = models.GetETAReportById(report.OrgId)
  159. if err != nil {
  160. br.Msg = "获取报告列表失败"
  161. br.ErrMsg = "获取eta报告失败,Err:" + err.Error()
  162. return
  163. }
  164. var list []int
  165. list, err = models.GetByPermissionIdsByClassifyId(classifyId)
  166. if err != nil {
  167. br.Msg = "获取报告列表失败"
  168. br.ErrMsg = "获取eta品种信息失败,Err:" + err.Error()
  169. return
  170. }
  171. if len(list) > 0 {
  172. var names []string
  173. names, err = models.GetPermissionNames(list)
  174. if err != nil {
  175. br.Msg = "获取报告列表失败"
  176. br.ErrMsg = "获取eta品种名称信息失败,Err:" + err.Error()
  177. return
  178. }
  179. report.PermissionNames = strings.Join(names, ",")
  180. }
  181. case "HT":
  182. report.PermissionNames = report.PlateName
  183. }
  184. }
  185. page := paging.GetPaging(currentIndex, pageSize, total)
  186. resp := new(response.ReportListResp)
  187. resp.List = reportViewList
  188. resp.Paging = page
  189. br.Ret = 200
  190. br.Success = true
  191. br.Data = resp
  192. br.Msg = "获取成功"
  193. }
  194. // AudioList
  195. // @Title 研报列表
  196. // @Description pdf研报列表
  197. // @Param PageSize query int true "每页数据条数"
  198. // @Param CurrentIndex query int true "当前页页码,从1开始"
  199. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  200. // @Param KeyWord query string true "报告标题/创建人"
  201. // @Param SortType query string true "排序方式"
  202. // @Success 200 {object} models.ReportAuthorResp
  203. // @router /audioList [get]
  204. func (this *MessageController) AudioList() {
  205. br := new(models.BaseResponse).Init()
  206. defer func() {
  207. this.Data["json"] = br
  208. this.ServeJSON()
  209. }()
  210. pageSize, _ := this.GetInt("PageSize")
  211. currentIndex, _ := this.GetInt("CurrentIndex")
  212. permissionIds := this.GetString("PermissionIds")
  213. analystIds := this.GetString("AnalystIds")
  214. sortType := this.GetString("SortType")
  215. KeyWord := this.GetString("KeyWord")
  216. var permissionCondition string
  217. var condition string
  218. var pars []interface{}
  219. if pageSize <= 0 {
  220. pageSize = utils.PageSize20
  221. }
  222. if currentIndex <= 0 {
  223. currentIndex = 1
  224. }
  225. if KeyWord != "" {
  226. condition += " AND media_name like '%" + KeyWord + "%'"
  227. }
  228. var permissionPars []interface{}
  229. if permissionIds != "" {
  230. permissionArr := strings.Split(permissionIds, ",")
  231. for _, permissionId := range permissionArr {
  232. perId, _ := strconv.Atoi(permissionId)
  233. permissionPars = append(permissionPars, perId)
  234. }
  235. permissionCondition += " AND permission_id in (" + utils.GetOrmReplaceHolder(len(permissionPars)) + ")"
  236. ids, err := models.GetMappingsByCondition(permissionCondition, permissionPars)
  237. if err != nil {
  238. br.Msg = "查询失败"
  239. br.Msg = "筛选品种信息失败,Err:" + err.Error()
  240. return
  241. }
  242. if len(ids) == 0 {
  243. br.Ret = 200
  244. br.Success = true
  245. page := paging.GetPaging(currentIndex, pageSize, 0)
  246. resp := new(response.MessageMediaListResp)
  247. resp.List = []*models.MessageMedia{}
  248. resp.Paging = page
  249. br.Data = resp
  250. br.Msg = "获取成功"
  251. return
  252. }
  253. condition += " AND id in (" + utils.GetOrmReplaceHolder(len(ids)) + ")"
  254. pars = append(pars, ids)
  255. }
  256. if analystIds != "" {
  257. analystIdArr := strings.Split(analystIds, ",")
  258. var authorIds []int
  259. for _, analystId := range analystIdArr {
  260. id, _ := strconv.Atoi(analystId)
  261. authorIds = append(authorIds, id)
  262. }
  263. condition += " AND author_id in (" + utils.GetOrmReplaceHolder(len(authorIds)) + ")"
  264. pars = append(pars, authorIds)
  265. }
  266. sortCondition := " ORDER BY published_time "
  267. if sortType == "" {
  268. sortType = "DESC"
  269. }
  270. sortCondition = sortCondition + sortType
  271. total, err := models.GetMediaCountByCondition(models.Audio, condition, pars)
  272. if err != nil {
  273. br.Msg = "获取研报列表失败"
  274. br.ErrMsg = "获取研报列表统计失败,Err:" + err.Error()
  275. return
  276. }
  277. startSize := utils.StartIndex(currentIndex, pageSize)
  278. reportList, err := models.GetMediaByCondition(models.Audio, condition, sortCondition, pars, startSize, pageSize)
  279. if err != nil {
  280. br.Msg = "获取研报列表失败"
  281. br.ErrMsg = "获取研报列表失败,Err:" + err.Error()
  282. return
  283. }
  284. var reportViewList []*models.MessageMedia
  285. for _, report := range reportList {
  286. reportView := report.ToMessageView()
  287. reportViewList = append(reportViewList, reportView)
  288. }
  289. page := paging.GetPaging(currentIndex, pageSize, total)
  290. resp := new(response.MessageMediaListResp)
  291. resp.List = reportViewList
  292. resp.Paging = page
  293. br.Ret = 200
  294. br.Success = true
  295. br.Data = resp
  296. br.Msg = "获取成功"
  297. }
  298. // VideoList
  299. // @Title 研报列表
  300. // @Description pdf研报列表
  301. // @Param PageSize query int true "每页数据条数"
  302. // @Param CurrentIndex query int true "当前页页码,从1开始"
  303. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  304. // @Param KeyWord query string true "报告标题/创建人"
  305. // @Param SortType query string true "排序方式"
  306. // @Success 200 {object} models.ReportAuthorResp
  307. // @router /videoList [get]
  308. func (this *MessageController) VideoList() {
  309. br := new(models.BaseResponse).Init()
  310. defer func() {
  311. this.Data["json"] = br
  312. this.ServeJSON()
  313. }()
  314. pageSize, _ := this.GetInt("PageSize")
  315. currentIndex, _ := this.GetInt("CurrentIndex")
  316. permissionIds := this.GetString("PermissionIds")
  317. analystIds := this.GetString("AnalystIds")
  318. sortType := this.GetString("SortType")
  319. KeyWord := this.GetString("KeyWord")
  320. var permissionCondition string
  321. var condition string
  322. var pars []interface{}
  323. if pageSize <= 0 {
  324. pageSize = utils.PageSize20
  325. }
  326. if currentIndex <= 0 {
  327. currentIndex = 1
  328. }
  329. if KeyWord != "" {
  330. condition += " AND media_name like '%" + KeyWord + "%'"
  331. }
  332. var permissionPars []interface{}
  333. if permissionIds != "" {
  334. permissionArr := strings.Split(permissionIds, ",")
  335. for _, permissionId := range permissionArr {
  336. perId, _ := strconv.Atoi(permissionId)
  337. permissionPars = append(permissionPars, perId)
  338. }
  339. permissionCondition += " AND permission_id in (" + utils.GetOrmReplaceHolder(len(permissionPars)) + ")"
  340. ids, err := models.GetMappingsByCondition(permissionCondition, permissionPars)
  341. if err != nil {
  342. br.Msg = "查询失败"
  343. br.Msg = "筛选品种信息失败,Err:" + err.Error()
  344. return
  345. }
  346. if len(ids) == 0 {
  347. br.Ret = 200
  348. br.Success = true
  349. page := paging.GetPaging(currentIndex, pageSize, 0)
  350. resp := new(response.MessageMediaListResp)
  351. resp.List = []*models.MessageMedia{}
  352. resp.Paging = page
  353. br.Msg = "获取成功"
  354. return
  355. }
  356. condition += " AND id in (" + utils.GetOrmReplaceHolder(len(ids)) + ")"
  357. pars = append(pars, ids)
  358. }
  359. if analystIds != "" {
  360. analystIdArr := strings.Split(analystIds, ",")
  361. var authorIds []int
  362. for _, analystId := range analystIdArr {
  363. id, _ := strconv.Atoi(analystId)
  364. authorIds = append(authorIds, id)
  365. }
  366. condition += " AND author_id in (" + utils.GetOrmReplaceHolder(len(authorIds)) + ")"
  367. pars = append(pars, authorIds)
  368. }
  369. sortCondition := " ORDER BY published_time "
  370. if sortType == "" {
  371. sortType = "DESC"
  372. }
  373. sortCondition = sortCondition + sortType
  374. total, err := models.GetMediaCountByCondition(models.Video, condition, pars)
  375. if err != nil {
  376. br.Msg = "获取视频列表失败"
  377. br.ErrMsg = "获取视频列表统计失败,Err:" + err.Error()
  378. return
  379. }
  380. startSize := utils.StartIndex(currentIndex, pageSize)
  381. List, err := models.GetMediaByCondition(models.Video, condition, sortCondition, pars, startSize, pageSize)
  382. if err != nil {
  383. br.Msg = "获取视频列表失败"
  384. br.ErrMsg = "获取视频列表失败,Err:" + err.Error()
  385. return
  386. }
  387. var reportViewList []*models.MessageMedia
  388. for _, report := range List {
  389. reportView := report.ToMessageView()
  390. reportViewList = append(reportViewList, reportView)
  391. }
  392. page := paging.GetPaging(currentIndex, pageSize, total)
  393. resp := new(response.MessageMediaListResp)
  394. resp.List = reportViewList
  395. resp.Paging = page
  396. br.Ret = 200
  397. br.Success = true
  398. br.Data = resp
  399. br.Msg = "获取成功"
  400. }
  401. const (
  402. Report string = "report"
  403. Audio string = "audio"
  404. Video string = "video"
  405. )
  406. // SendTemplateMsg
  407. // @Title 研报列表
  408. // @Description pdf研报列表
  409. // @Param PageSize query int true "每页数据条数"
  410. // @Param CurrentIndex query int true "当前页页码,从1开始"
  411. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  412. // @Param KeyWord query string true "报告标题/创建人"
  413. // @Param SortType query string true "排序方式"
  414. // @Success 200 {object} models.ReportAuthorResp
  415. // @router /sendTemplateMsg [post]
  416. func (this *MessageController) SendTemplateMsg() {
  417. br := new(models.BaseResponse).Init()
  418. defer func() {
  419. this.Data["json"] = br
  420. this.ServeJSON()
  421. }()
  422. var req request.WxMessageReq
  423. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  424. if req.DataId <= 0 {
  425. br.Msg = "DataId为空"
  426. return
  427. }
  428. if req.DataType == "" {
  429. br.Msg = "DataType为空"
  430. return
  431. }
  432. var template services.SendWxTemplate
  433. if req.DataType == Report {
  434. var report *models.Report
  435. report, err = models.GetReportById(req.DataId)
  436. if err != nil {
  437. br.Msg = "发送模版消息失败"
  438. br.ErrMsg = "获取报告失败"
  439. return
  440. }
  441. switch report.Source {
  442. case "ETA":
  443. var classifyId int
  444. var ids []int
  445. var names []string
  446. classifyId, err = models.GetETAReportIdByClassifyId(report.OrgId)
  447. ids, err = models.GetByPermissionIdsByClassifyId(classifyId)
  448. if err != nil {
  449. br.Msg = "发送模版消息失败"
  450. br.ErrMsg = fmt.Sprintf("获取eta品种信息失败,Err:%v", err)
  451. return
  452. }
  453. names, err = models.GetPermissionNames(ids)
  454. if err != nil {
  455. br.Msg = "发送模版消息失败"
  456. br.ErrMsg = fmt.Sprintf("获取eta品种信息失败,Err:%v", err)
  457. return
  458. }
  459. template.Keyword1 = strings.Join(names, ",")
  460. case "HT":
  461. template.Keyword1 = "ht"
  462. default:
  463. template.Keyword1 = ""
  464. }
  465. template.Keyword2 = report.Title
  466. template.Keyword3 = report.PublishedTime
  467. template.Keyword4 = report.Abstract
  468. err = report.UpdateSendStatus(models.SEND)
  469. if err != nil {
  470. br.Msg = "发送模版消息失败"
  471. br.ErrMsg = fmt.Sprintf("更新发送状态失败,Err:%v", err)
  472. return
  473. }
  474. }
  475. if req.DataType == Audio {
  476. var media *models.Media
  477. media, err = models.GetMediaById(models.Audio, req.DataId)
  478. if err != nil {
  479. br.Msg = "发送模版消息失败"
  480. br.ErrMsg = "获取音频失败"
  481. return
  482. }
  483. template.Keyword1 = "线上音频"
  484. template.Keyword2 = media.MediaName
  485. template.Keyword3 = media.CreatedTime.Format(time.DateTime)
  486. err = media.UpdateSendStatus(models.SEND)
  487. if err != nil {
  488. br.Msg = "发送模版消息失败"
  489. br.ErrMsg = fmt.Sprintf("更新发送状态失败,Err:%v", err)
  490. return
  491. }
  492. }
  493. if req.DataType == Video {
  494. var media *models.Media
  495. media, err = models.GetMediaById(models.Video, req.DataId)
  496. if err != nil {
  497. br.Msg = "发送模版消息失败"
  498. br.ErrMsg = "获取音频失败"
  499. return
  500. }
  501. template.Keyword1 = "线上视频"
  502. template.Keyword2 = media.MediaName
  503. template.Keyword3 = media.CreatedTime.Format(time.DateTime)
  504. err = media.UpdateSendStatus(models.SEND)
  505. if err != nil {
  506. br.Msg = "发送模版消息失败"
  507. br.ErrMsg = fmt.Sprintf("更新发送状态失败,Err:%v", err)
  508. return
  509. }
  510. }
  511. users, err := models.GetTemplateUserList()
  512. if err != nil {
  513. br.Msg = "发送模版消息失败"
  514. br.ErrMsg = "获取用户OPENID失败,Err:" + err.Error()
  515. return
  516. }
  517. var openIds []*services.OpenIdList
  518. for _, user := range users {
  519. openIdItem := &services.OpenIdList{
  520. OpenId: user.GzhOpenId,
  521. UserId: user.Id,
  522. FollowingGzh: user.FollowingGzh,
  523. }
  524. if openIdItem.OpenId != "" && openIdItem.FollowingGzh {
  525. openIds = append(openIds, openIdItem)
  526. }
  527. }
  528. err = services.SendMultiTemplateMsg(template, openIds, req.DataId, req.DataType)
  529. if err != nil {
  530. if req.DataType == Report {
  531. var report *models.Report
  532. report, err = models.GetReportById(req.DataId)
  533. if err != nil {
  534. return
  535. }
  536. _ = report.UpdateSendStatus(models.UNSEND)
  537. }
  538. if req.DataType == Audio {
  539. var media *models.Media
  540. media, err = models.GetMediaById(models.Audio, req.DataId)
  541. if err != nil {
  542. return
  543. }
  544. _ = media.UpdateSendStatus(models.UNSEND)
  545. }
  546. if req.DataType == Video {
  547. var media *models.Media
  548. media, err = models.GetMediaById(models.Video, req.DataId)
  549. if err != nil {
  550. return
  551. }
  552. _ = media.UpdateSendStatus(models.SEND)
  553. }
  554. br.Msg = "发送模板消息失败"
  555. br.ErrMsg = "发送模板消息失败,Err:" + err.Error()
  556. return
  557. }
  558. br.Ret = 200
  559. br.Success = true
  560. br.Data = nil
  561. br.Msg = "发送成功"
  562. }