message.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. if permissionIds != "" {
  55. permissionArr := strings.Split(permissionIds, ",")
  56. for _, permissionId := range permissionArr {
  57. perId, _ := strconv.Atoi(permissionId)
  58. permissionPars = append(permissionPars, perId)
  59. }
  60. permissionCondition += " AND chart_permission_id in (" + utils.GetOrmReplaceHolder(len(permissionPars)) + ")"
  61. classifyIds, err := models.GetClassifyIdsByPermissionIds(permissionCondition, permissionPars)
  62. if err != nil {
  63. br.Msg = "获取报告列表失败"
  64. br.ErrMsg = "eta获取品种信息失败,Err:" + err.Error()
  65. return
  66. }
  67. classifyCondition := " AND COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) in (" + utils.GetOrmReplaceHolder(len(classifyIds)) + ")"
  68. classifyPars = append(classifyPars, classifyIds)
  69. var ids []int
  70. ids, _ = models.GetETAReportIdsByCondition(classifyCondition, classifyPars)
  71. if err != nil {
  72. br.Msg = "获取报告列表失败"
  73. br.ErrMsg = "获取报告表ids失败,Err:" + err.Error()
  74. return
  75. }
  76. condition += " AND id in (" + utils.GetOrmReplaceHolder(len(ids)) + ") And source ='ETA'"
  77. pars = append(pars, ids)
  78. }
  79. if analystNames != "" {
  80. analystNameArr := strings.Split(analystNames, ",")
  81. condition += " AND author in (" + utils.GetOrmReplaceHolder(len(analystNameArr)) + ")"
  82. pars = append(pars, analystNameArr)
  83. }
  84. sortCondition := " ORDER BY published_time "
  85. if sortType == "" {
  86. sortType = "DESC"
  87. }
  88. sortCondition = sortCondition + sortType
  89. total, err := models.GetReportCountByCondition(condition, pars)
  90. if err != nil {
  91. br.Msg = "获取报告列表失败"
  92. br.ErrMsg = "获取报告列表统计失败,Err:" + err.Error()
  93. return
  94. }
  95. startSize := utils.StartIndex(currentIndex, pageSize)
  96. reportList, err := models.GetReportByCondition(condition, sortCondition, pars, startSize, pageSize)
  97. if err != nil {
  98. br.Msg = "获取报告列表失败"
  99. br.ErrMsg = "获取报告列表失败,Err:" + err.Error()
  100. return
  101. }
  102. var reportViewList []*models.ReportView
  103. for _, report := range reportList {
  104. reportView := report.ToView()
  105. reportViewList = append(reportViewList, reportView)
  106. }
  107. for _, report := range reportViewList {
  108. var classifyId int
  109. classifyId, err = models.GetETAReportById(report.OrgId)
  110. if err != nil {
  111. br.Msg = "获取报告列表失败"
  112. br.ErrMsg = "获取eta报告失败,Err:" + err.Error()
  113. return
  114. }
  115. var list []int
  116. list, err = models.GetByPermissionIdsByClassifyId(classifyId)
  117. if err != nil {
  118. br.Msg = "获取报告列表失败"
  119. br.ErrMsg = "获取eta品种信息失败,Err:" + err.Error()
  120. return
  121. }
  122. var names []string
  123. names, err = models.GetPermissionNames(list)
  124. if err != nil {
  125. br.Msg = "获取报告列表失败"
  126. br.ErrMsg = "获取eta品种信息失败,Err:" + err.Error()
  127. return
  128. }
  129. report.PermissionNames = strings.Join(names, ",")
  130. }
  131. page := paging.GetPaging(currentIndex, pageSize, total)
  132. resp := new(response.ReportListResp)
  133. resp.List = reportViewList
  134. resp.Paging = page
  135. br.Ret = 200
  136. br.Success = true
  137. br.Data = resp
  138. br.Msg = "获取成功"
  139. }
  140. // AudioList
  141. // @Title 研报列表
  142. // @Description pdf研报列表
  143. // @Param PageSize query int true "每页数据条数"
  144. // @Param CurrentIndex query int true "当前页页码,从1开始"
  145. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  146. // @Param KeyWord query string true "报告标题/创建人"
  147. // @Param SortType query string true "排序方式"
  148. // @Success 200 {object} models.ReportAuthorResp
  149. // @router /audioList [get]
  150. func (this *MessageController) AudioList() {
  151. br := new(models.BaseResponse).Init()
  152. defer func() {
  153. this.Data["json"] = br
  154. this.ServeJSON()
  155. }()
  156. pageSize, _ := this.GetInt("PageSize")
  157. currentIndex, _ := this.GetInt("CurrentIndex")
  158. permissionIds := this.GetString("PermissionIds")
  159. analystIds := this.GetString("AnalystIds")
  160. sortType := this.GetString("SortType")
  161. KeyWord := this.GetString("KeyWord")
  162. var permissionCondition string
  163. var condition string
  164. var pars []interface{}
  165. if pageSize <= 0 {
  166. pageSize = utils.PageSize20
  167. }
  168. if currentIndex <= 0 {
  169. currentIndex = 1
  170. }
  171. if KeyWord != "" {
  172. condition += " AND media_name like '%" + KeyWord + "%'"
  173. }
  174. var permissionPars []interface{}
  175. if permissionIds != "" {
  176. permissionArr := strings.Split(permissionIds, ",")
  177. for _, permissionId := range permissionArr {
  178. perId, _ := strconv.Atoi(permissionId)
  179. permissionPars = append(permissionPars, perId)
  180. }
  181. permissionCondition += " AND permission_id in (" + utils.GetOrmReplaceHolder(len(permissionPars)) + ")"
  182. ids, err := models.GetMappingsByCondition(permissionCondition, permissionPars)
  183. if err != nil {
  184. condition += " AND id in (" + utils.GetOrmReplaceHolder(len(ids)) + ")"
  185. pars = append(pars, ids)
  186. }
  187. }
  188. if analystIds != "" {
  189. analystIdArr := strings.Split(analystIds, ",")
  190. var authorIds []int
  191. for _, analystId := range analystIdArr {
  192. id, _ := strconv.Atoi(analystId)
  193. authorIds = append(authorIds, id)
  194. }
  195. condition += " AND author_id in (" + utils.GetOrmReplaceHolder(len(authorIds)) + ")"
  196. pars = append(pars, authorIds)
  197. }
  198. sortCondition := " ORDER BY published_time "
  199. if sortType == "" {
  200. sortType = "DESC"
  201. }
  202. sortCondition = sortCondition + sortType
  203. total, err := models.GetMediaCountByCondition(models.Audio, condition, pars)
  204. if err != nil {
  205. br.Msg = "获取研报列表失败"
  206. br.ErrMsg = "获取研报列表统计失败,Err:" + err.Error()
  207. return
  208. }
  209. startSize := utils.StartIndex(currentIndex, pageSize)
  210. reportList, err := models.GetMediaByCondition(models.Audio, condition, sortCondition, pars, startSize, pageSize)
  211. if err != nil {
  212. br.Msg = "获取研报列表失败"
  213. br.ErrMsg = "获取研报列表失败,Err:" + err.Error()
  214. return
  215. }
  216. var reportViewList []*models.MessageMedia
  217. for _, report := range reportList {
  218. reportView := report.ToMessageView()
  219. reportViewList = append(reportViewList, reportView)
  220. }
  221. page := paging.GetPaging(currentIndex, pageSize, total)
  222. resp := new(response.MessageMediaListResp)
  223. resp.List = reportViewList
  224. resp.Paging = page
  225. br.Ret = 200
  226. br.Success = true
  227. br.Data = resp
  228. br.Msg = "获取成功"
  229. }
  230. // VideoList
  231. // @Title 研报列表
  232. // @Description pdf研报列表
  233. // @Param PageSize query int true "每页数据条数"
  234. // @Param CurrentIndex query int true "当前页页码,从1开始"
  235. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  236. // @Param KeyWord query string true "报告标题/创建人"
  237. // @Param SortType query string true "排序方式"
  238. // @Success 200 {object} models.ReportAuthorResp
  239. // @router /videoList [get]
  240. func (this *MessageController) VideoList() {
  241. br := new(models.BaseResponse).Init()
  242. defer func() {
  243. this.Data["json"] = br
  244. this.ServeJSON()
  245. }()
  246. pageSize, _ := this.GetInt("PageSize")
  247. currentIndex, _ := this.GetInt("CurrentIndex")
  248. permissionIds := this.GetString("PermissionIds")
  249. analystIds := this.GetString("AnalystIds")
  250. sortType := this.GetString("SortType")
  251. KeyWord := this.GetString("KeyWord")
  252. var permissionCondition string
  253. var condition string
  254. var pars []interface{}
  255. if pageSize <= 0 {
  256. pageSize = utils.PageSize20
  257. }
  258. if currentIndex <= 0 {
  259. currentIndex = 1
  260. }
  261. if KeyWord != "" {
  262. condition += " AND media_name like '%" + KeyWord + "%'"
  263. }
  264. var permissionPars []interface{}
  265. if permissionIds != "" {
  266. permissionArr := strings.Split(permissionIds, ",")
  267. for _, permissionId := range permissionArr {
  268. perId, _ := strconv.Atoi(permissionId)
  269. permissionPars = append(permissionPars, perId)
  270. }
  271. permissionCondition += " AND permission_id in (" + utils.GetOrmReplaceHolder(len(permissionPars)) + ")"
  272. ids, err := models.GetMappingsByCondition(permissionCondition, permissionPars)
  273. if err != nil {
  274. condition += " AND id in (" + utils.GetOrmReplaceHolder(len(ids)) + ")"
  275. pars = append(pars, ids)
  276. }
  277. }
  278. if analystIds != "" {
  279. analystIdArr := strings.Split(analystIds, ",")
  280. var authorIds []int
  281. for _, analystId := range analystIdArr {
  282. id, _ := strconv.Atoi(analystId)
  283. authorIds = append(authorIds, id)
  284. }
  285. condition += " AND author_id in (" + utils.GetOrmReplaceHolder(len(authorIds)) + ")"
  286. pars = append(pars, authorIds)
  287. }
  288. sortCondition := " ORDER BY published_time "
  289. if sortType == "" {
  290. sortType = "DESC"
  291. }
  292. sortCondition = sortCondition + sortType
  293. total, err := models.GetMediaCountByCondition(models.Video, condition, pars)
  294. if err != nil {
  295. br.Msg = "获取视频列表失败"
  296. br.ErrMsg = "获取视频列表统计失败,Err:" + err.Error()
  297. return
  298. }
  299. startSize := utils.StartIndex(currentIndex, pageSize)
  300. List, err := models.GetMediaByCondition(models.Video, condition, sortCondition, pars, startSize, pageSize)
  301. if err != nil {
  302. br.Msg = "获取视频列表失败"
  303. br.ErrMsg = "获取视频列表失败,Err:" + err.Error()
  304. return
  305. }
  306. var reportViewList []*models.MessageMedia
  307. for _, report := range List {
  308. reportView := report.ToMessageView()
  309. reportViewList = append(reportViewList, reportView)
  310. }
  311. page := paging.GetPaging(currentIndex, pageSize, total)
  312. resp := new(response.MessageMediaListResp)
  313. resp.List = reportViewList
  314. resp.Paging = page
  315. br.Ret = 200
  316. br.Success = true
  317. br.Data = resp
  318. br.Msg = "获取成功"
  319. }
  320. const (
  321. Report string = "report"
  322. Audio string = "audio"
  323. Video string = "video"
  324. )
  325. // SendTemplateMsg
  326. // @Title 研报列表
  327. // @Description pdf研报列表
  328. // @Param PageSize query int true "每页数据条数"
  329. // @Param CurrentIndex query int true "当前页页码,从1开始"
  330. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  331. // @Param KeyWord query string true "报告标题/创建人"
  332. // @Param SortType query string true "排序方式"
  333. // @Success 200 {object} models.ReportAuthorResp
  334. // @router /sendTemplateMsg [post]
  335. func (this *MessageController) SendTemplateMsg() {
  336. br := new(models.BaseResponse).Init()
  337. defer func() {
  338. this.Data["json"] = br
  339. this.ServeJSON()
  340. }()
  341. var req request.WxMessageReq
  342. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  343. if req.DataId <= 0 {
  344. br.Msg = "DataId为空"
  345. return
  346. }
  347. if req.DataType == "" {
  348. br.Msg = "DataType为空"
  349. return
  350. }
  351. sendData := make(map[string]interface{})
  352. if req.DataType == Report {
  353. var report *models.Report
  354. report, err = models.GetReportById(req.DataId)
  355. if err != nil {
  356. br.Msg = "发送模版消息失败"
  357. br.ErrMsg = "获取报告失败"
  358. return
  359. }
  360. switch report.Source {
  361. case "ETA":
  362. var classifyId int
  363. var ids []int
  364. var names []string
  365. classifyId, err = models.GetETAReportIdByClassifyId(report.OrgId)
  366. ids, err = models.GetByPermissionIdsByClassifyId(classifyId)
  367. if err != nil {
  368. br.Msg = "发送模版消息失败"
  369. br.ErrMsg = fmt.Sprintf("获取eta品种信息失败,Err:%v", err)
  370. return
  371. }
  372. names, err = models.GetPermissionNames(ids)
  373. if err != nil {
  374. br.Msg = "发送模版消息失败"
  375. br.ErrMsg = fmt.Sprintf("获取eta品种信息失败,Err:%v", err)
  376. return
  377. }
  378. sendData["keyword1"] = map[string]interface{}{"value": strings.Join(names, ","), "color": "#173177"}
  379. case "HT":
  380. sendData["keyword1"] = map[string]interface{}{"value": "ht", "color": "#173177"}
  381. default:
  382. sendData["keyword1"] = map[string]interface{}{"value": "", "color": "#173177"}
  383. }
  384. sendData["keyword2"] = map[string]interface{}{"value": report.Title, "color": "#173177"}
  385. sendData["keyword3"] = map[string]interface{}{"value": report.PublishedTime, "color": "#173177"}
  386. sendData["keyword4"] = map[string]interface{}{"value": report.Abstract, "color": "#173177"}
  387. err = report.UpdateSendStatus(models.SEND)
  388. if err != nil {
  389. br.Msg = "发送模版消息失败"
  390. br.ErrMsg = fmt.Sprintf("更新发送状态失败,Err:%v", err)
  391. return
  392. }
  393. }
  394. if req.DataType == Audio {
  395. var media *models.Media
  396. media, err = models.GetMediaById(models.Audio, req.DataId)
  397. if err != nil {
  398. br.Msg = "发送模版消息失败"
  399. br.ErrMsg = "获取音频失败"
  400. return
  401. }
  402. sendData["keyword1"] = map[string]interface{}{"value": "线上音频", "color": "#173177"}
  403. sendData["keyword2"] = map[string]interface{}{"value": media.MediaName, "color": "#173177"}
  404. sendData["keyword3"] = map[string]interface{}{"value": media.CreatedTime.Format(time.DateTime), "color": "#173177"}
  405. err = media.UpdateSendStatus(models.SEND)
  406. if err != nil {
  407. br.Msg = "发送模版消息失败"
  408. br.ErrMsg = fmt.Sprintf("更新发送状态失败,Err:%v", err)
  409. return
  410. }
  411. }
  412. if req.DataType == Video {
  413. var media *models.Media
  414. media, err = models.GetMediaById(models.Video, req.DataId)
  415. if err != nil {
  416. br.Msg = "发送模版消息失败"
  417. br.ErrMsg = "获取音频失败"
  418. return
  419. }
  420. sendData["keyword1"] = map[string]interface{}{"value": "线上视频", "color": "#173177"}
  421. sendData["keyword2"] = map[string]interface{}{"value": media.MediaName, "color": "#173177"}
  422. sendData["keyword3"] = map[string]interface{}{"value": media.CreatedTime.Format(time.DateTime), "color": "#173177"}
  423. err = media.UpdateSendStatus(models.SEND)
  424. if err != nil {
  425. br.Msg = "发送模版消息失败"
  426. br.ErrMsg = fmt.Sprintf("更新发送状态失败,Err:%v", err)
  427. return
  428. }
  429. }
  430. users, err := models.GetTemplateUserList()
  431. if err != nil {
  432. br.Msg = "发送模版消息失败"
  433. br.ErrMsg = "获取用户OPENID失败,Err:" + err.Error()
  434. return
  435. }
  436. var openIds []*services.OpenIdList
  437. for _, user := range users {
  438. openId := &services.OpenIdList{
  439. OpenId: user.OpenId,
  440. UserId: user.Id,
  441. }
  442. openIds = append(openIds, openId)
  443. }
  444. err = services.SendMultiTemplateMsg(sendData, openIds, req.DataId, req.DataType)
  445. if err != nil {
  446. if req.DataType == Report {
  447. var report *models.Report
  448. report, err = models.GetReportById(req.DataId)
  449. if err != nil {
  450. return
  451. }
  452. _ = report.UpdateSendStatus(models.UNSEND)
  453. }
  454. if req.DataType == Audio {
  455. var media *models.Media
  456. media, err = models.GetMediaById(models.Audio, req.DataId)
  457. if err != nil {
  458. return
  459. }
  460. _ = media.UpdateSendStatus(models.UNSEND)
  461. }
  462. if req.DataType == Video {
  463. var media *models.Media
  464. media, err = models.GetMediaById(models.Video, req.DataId)
  465. if err != nil {
  466. return
  467. }
  468. _ = media.UpdateSendStatus(models.SEND)
  469. }
  470. br.Msg = "发送模板消息失败"
  471. br.ErrMsg = "发送模板消息失败,Err:" + err.Error()
  472. return
  473. }
  474. br.Ret = 200
  475. br.Success = true
  476. br.Data = nil
  477. br.Msg = "发送成功"
  478. }