email.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. package english_report
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hongze_ETA_mobile_api/controllers"
  7. "hongze/hongze_ETA_mobile_api/models"
  8. "hongze/hongze_ETA_mobile_api/models/company"
  9. "hongze/hongze_ETA_mobile_api/services"
  10. "hongze/hongze_ETA_mobile_api/utils"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // EnglishReportEmailController 英文研报邮箱/英文客户联系人
  16. type EnglishReportEmailController struct {
  17. controllers.BaseAuthController
  18. }
  19. // List
  20. // @Title 英文研报邮箱列表
  21. // @Description 英文研报邮箱列表
  22. // @Param CompanyId query int false "客户ID"
  23. // @Param Keywords query string false "关键词:联系人名称/邮箱地址"
  24. // @Param SortType query int false "点击量排序方式:1-倒序;2-正序"
  25. // @Param SortParam query int false "排序字段:1-点击量;2-点击时间"
  26. // @Param ListParam query int false "筛选字段参数,用来筛选的字段, 枚举值:1:正式 、 2:临时 、 3:终止 、4:正式+临时 "
  27. // @Success 200 {object} models.EnglishReportEmailPageListResp
  28. // @router /email/list [get]
  29. func (this *EnglishReportEmailController) List() {
  30. br := new(models.BaseResponse).Init()
  31. br.IsSendEmail = false
  32. defer func() {
  33. this.Data["json"] = br
  34. this.ServeJSON()
  35. }()
  36. sysUser := this.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录,SysUser Is Empty"
  40. br.Ret = 408
  41. return
  42. }
  43. companyId, _ := this.GetInt("CompanyId", 0)
  44. keywords := this.GetString("Keywords", "")
  45. sortType, _ := this.GetInt("SortType", 1)
  46. sortParam, _ := this.GetInt("SortParam", 1)
  47. listParam, _ := this.GetInt("ListParam", 1)
  48. var cond, order string
  49. var pars []interface{}
  50. if companyId > 0 {
  51. cond += ` AND a.company_id = ?`
  52. pars = append(pars, companyId)
  53. }
  54. if keywords != "" {
  55. k := "%" + keywords + "%"
  56. cond += ` AND (a.name LIKE ? OR a.email LIKE ?)`
  57. pars = append(pars, k, k)
  58. }
  59. if sortParam == 1 {
  60. if sortType == 1 {
  61. order = ` ORDER BY a.view_total DESC`
  62. } else {
  63. order = ` ORDER BY a.view_total ASC`
  64. }
  65. } else {
  66. if sortType == 1 {
  67. order = ` ORDER BY a.create_time DESC`
  68. } else {
  69. order = ` ORDER BY a.create_time ASC`
  70. }
  71. }
  72. if listParam == 1 {
  73. cond += ` AND a.status = 1`
  74. }
  75. if listParam == 2 {
  76. cond += ` AND a.status = 2`
  77. }
  78. if listParam == 3 {
  79. cond += ` AND a.status = 3`
  80. }
  81. if listParam == 4 {
  82. cond += ` AND (a.status = 1 OR a.status = 2)`
  83. }
  84. var startSize int
  85. pageSize, _ := this.GetInt("PageSize")
  86. currentIndex, _ := this.GetInt("CurrentIndex")
  87. if pageSize <= 0 {
  88. pageSize = utils.PageSize20
  89. }
  90. if currentIndex <= 0 {
  91. currentIndex = 1
  92. }
  93. startSize = paging.StartIndex(currentIndex, pageSize)
  94. total, list, e := models.GetEnglishReportEmailPageList(cond, pars, order, startSize, pageSize)
  95. if e != nil {
  96. br.Msg = "获取失败"
  97. br.ErrMsg = "获取邮箱列表失败, Err: " + e.Error()
  98. return
  99. }
  100. respList := make([]*models.EnglishReportEmailResp, 0)
  101. for i := range list {
  102. item := &models.EnglishReportEmailResp{
  103. Id: list[i].Id,
  104. Name: list[i].Name,
  105. Email: list[i].Email,
  106. Mobile: list[i].Mobile,
  107. CountryCode: list[i].CountryCode,
  108. BusinessCardUrl: list[i].BusinessCardUrl,
  109. AdminName: list[i].AdminName,
  110. CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
  111. ViewTotal: list[i].ViewTotal,
  112. Enabled: list[i].Enabled,
  113. CompanyName: list[i].CompanyName,
  114. RegisterCompanyName: list[i].RegisterCompanyName,
  115. RegisterTime: list[i].RegisterTime.Format(utils.FormatDateTime),
  116. }
  117. if item.RegisterTime == "0001-01-01 00:00:00" {
  118. item.RegisterTime = ""
  119. }
  120. respList = append(respList, item)
  121. }
  122. page := paging.GetPaging(currentIndex, pageSize, total)
  123. resp := &models.EnglishReportEmailPageListResp{
  124. Paging: page,
  125. List: respList,
  126. }
  127. br.Ret = 200
  128. br.Success = true
  129. br.Msg = "获取成功"
  130. br.Data = resp
  131. }
  132. // Send
  133. // @Title 群发推送
  134. // @Description 群发推送
  135. // @Param request body models.EnglishReportEmailSaveReq true "type json string"
  136. // @Success 200 string "操作成功"
  137. // @router /email/send [post]
  138. func (this *EnglishReportEmailController) Send() {
  139. br := new(models.BaseResponse).Init()
  140. br.IsSendEmail = false
  141. defer func() {
  142. this.Data["json"] = br
  143. this.ServeJSON()
  144. }()
  145. sysUser := this.SysUser
  146. if sysUser == nil {
  147. br.Msg = "请登录"
  148. br.ErrMsg = "请登录,SysUser Is Empty"
  149. br.Ret = 408
  150. return
  151. }
  152. var req models.EnglishReportEmailSendReq
  153. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  154. br.Msg = "参数解析异常!"
  155. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  156. return
  157. }
  158. if req.ReportId <= 0 {
  159. br.Msg = "请选择报告"
  160. return
  161. }
  162. if req.Theme == "" {
  163. br.Msg = "请输入邮件主题"
  164. return
  165. }
  166. //prefixTheme := "【HI China Macro and Commodity】"
  167. //if !strings.Contains(req.Theme, prefixTheme) {
  168. // req.Theme = prefixTheme + req.Theme
  169. //}
  170. runeThe := []rune(req.Theme)
  171. if len(runeThe) > 100 {
  172. br.Msg = "邮件主题不可超过100字符"
  173. return
  174. }
  175. // 获取报告信息
  176. reportInfo, e := models.GetEnglishReportItemById(req.ReportId)
  177. if e != nil {
  178. br.Msg = "报告信息有误"
  179. br.ErrMsg = "获取报告信息失败, Err: " + e.Error()
  180. return
  181. }
  182. if reportInfo.EmailState != 0 {
  183. br.Msg = "请勿重复发送"
  184. br.ErrMsg = fmt.Sprintf("报告群发邮件状态有误, 当前状态: %d", reportInfo.EmailState)
  185. return
  186. }
  187. if reportInfo.State != 2 {
  188. br.Msg = "报告未发布"
  189. br.ErrMsg = fmt.Sprintf("报告状态有误, 当前状态: %d", reportInfo.State)
  190. return
  191. }
  192. if reportInfo.Title == "" {
  193. br.Msg = "报告标题有误"
  194. br.ErrMsg = "报告标题为空, 不可推送"
  195. return
  196. }
  197. if reportInfo.Content == `` {
  198. br.Msg = "报告内容有误"
  199. br.ErrMsg = "报告内容为空, 不可推送"
  200. return
  201. }
  202. // 获取邮件配置
  203. conf := new(models.EnglishReportEmailConf)
  204. authKey := "english_report_email_conf"
  205. confAuth, e := company.GetConfigDetailByCode(authKey)
  206. if e != nil {
  207. br.Msg = "无权操作"
  208. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  209. return
  210. }
  211. if confAuth.ConfigValue == "" {
  212. br.Msg = "无权操作"
  213. br.ErrMsg = "群发邮件配置为空"
  214. return
  215. }
  216. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  217. br.Msg = "无权操作"
  218. br.ErrMsg = "群发邮件配置有误"
  219. return
  220. }
  221. authArr := strings.Split(conf.SendAuthGroup, ",")
  222. if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  223. br.Msg = "无权操作"
  224. return
  225. }
  226. // 获取收件人列表
  227. emailCond := " and enabled = 1 "
  228. var emailPars []interface{}
  229. if req.EmailIds != "" {
  230. emailIdArr := strings.Split(req.EmailIds, ",")
  231. if len(emailIdArr) == 0 {
  232. br.Msg = "收件人列表有误"
  233. br.ErrMsg = fmt.Sprintf("收件人列表为空, 收件人IDs: %s", req.EmailIds)
  234. return
  235. }
  236. emailCond += ` AND id IN (` + req.EmailIds + `)`
  237. }
  238. emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  239. if e != nil {
  240. br.Msg = "获取收件人列表失败"
  241. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  242. return
  243. }
  244. if len(emails) == 0 {
  245. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  246. br.ErrMsg = "收件人列表为空, 不可推送"
  247. return
  248. }
  249. // 获取模板
  250. confKey := "english_report_email_tmp"
  251. confTmp, e := company.GetConfigDetailByCode(confKey)
  252. if e != nil {
  253. br.Msg = "获取邮件模板失败"
  254. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  255. return
  256. }
  257. if confTmp.ConfigValue == `` {
  258. br.Msg = "邮件模板有误"
  259. br.ErrMsg = "邮件模板为空, 不可推送"
  260. return
  261. }
  262. // 同一篇报告避免重复推送给同一个邮件
  263. var cond string
  264. var pars []interface{}
  265. cond += ` AND report_id = ? AND send_status = 1 AND report_type = 0`
  266. pars = append(pars, req.ReportId)
  267. logs, e := models.GetEnglishReportEmailLogList(cond, pars)
  268. if e != nil {
  269. br.Msg = "推送失败"
  270. br.ErrMsg = "获取邮件发送日志列表失败, Err: " + e.Error()
  271. return
  272. }
  273. repeatMap := make(map[int]bool, 0)
  274. for i := range logs {
  275. if logs[i].SendStatus == 1 {
  276. repeatMap[logs[i].EmailId] = true
  277. }
  278. }
  279. // 推送信息
  280. nowTime := time.Now().Local()
  281. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  282. logData := make([]*models.EnglishReportEmailLog, 0)
  283. for i := range emails {
  284. if repeatMap[emails[i].Id] {
  285. continue
  286. }
  287. r := new(services.EnglishReportSendEmailRequest)
  288. r.ReportId = reportInfo.Id
  289. r.EmailId = emails[i].Id
  290. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  291. r.Subject = req.Theme
  292. r.FromAlias = conf.FromAlias
  293. // 获取报告Overview部分
  294. if reportInfo.Overview == "" {
  295. br.Msg = "报告Overview部分不可为空"
  296. return
  297. }
  298. overview := reportInfo.Overview
  299. // 填充模板
  300. t := reportInfo.PublishTime.Format("02/01/2006")
  301. l := fmt.Sprintf(utils.EnglishShareUrl+"/report/detail?code=%s", reportInfo.ReportCode)
  302. ct := confTmp.ConfigValue
  303. ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
  304. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
  305. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", overview, 1)
  306. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  307. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  308. r.HtmlBody = ct
  309. r.ReportTitle = reportInfo.Title
  310. r.ReportAbstract = reportInfo.Abstract
  311. r.ReportContent = overview
  312. r.ReportShareLink = l
  313. r.ReportTime = t
  314. sendData = append(sendData, r)
  315. // 日志
  316. logData = append(logData, &models.EnglishReportEmailLog{
  317. ReportId: reportInfo.Id,
  318. EmailId: emails[i].Id,
  319. Email: emails[i].Email,
  320. Source: models.EnglishReportEmailLogSourceAli,
  321. SendStatus: models.EnglishReportEmailLogStatusIng,
  322. CreateTime: nowTime,
  323. })
  324. }
  325. if len(sendData) == 0 {
  326. br.Msg = "无邮件可推送"
  327. return
  328. }
  329. // 写入日志
  330. emailLog := new(models.EnglishReportEmailLog)
  331. if e = emailLog.InsertMulti(logData); e != nil {
  332. br.Msg = "操作失败"
  333. br.ErrMsg = "批量写入群发邮件日志失败, Err: " + e.Error()
  334. return
  335. }
  336. // 修改推送状态
  337. updateCols := []string{"EmailState", "ModifyTime"}
  338. reportInfo.EmailState = 1
  339. reportInfo.ModifyTime = time.Now().Local()
  340. if e = reportInfo.Update(updateCols); e != nil {
  341. br.Msg = "操作失败"
  342. br.ErrMsg = "更新推送状态失败, Err: " + e.Error()
  343. return
  344. }
  345. // 群发邮件
  346. go func() {
  347. services.BatchSendAliEnglishReportEmail(sendData)
  348. }()
  349. br.Ret = 200
  350. br.Success = true
  351. br.Msg = "操作成功"
  352. }
  353. // LogList
  354. // @Title 邮件群发日志列表
  355. // @Description 邮件群发日志列表
  356. // @Param ReportId query int true "报告ID"
  357. // @Param ReportType query int true "类型:0英文研报,1英文线上路演"
  358. // @Param SendStatus query int false "发送状态:-1-已发送;0-发送失败;1-发送成功"
  359. // @Param Keywords query string false "关键词:邮箱"
  360. // @Success 200 {object} models.EnglishReportEmailLogPageListResp
  361. // @router /email/log_list [get]
  362. func (this *EnglishReportEmailController) LogList() {
  363. br := new(models.BaseResponse).Init()
  364. br.IsSendEmail = false
  365. defer func() {
  366. this.Data["json"] = br
  367. this.ServeJSON()
  368. }()
  369. sysUser := this.SysUser
  370. if sysUser == nil {
  371. br.Msg = "请登录"
  372. br.ErrMsg = "请登录,SysUser Is Empty"
  373. br.Ret = 408
  374. return
  375. }
  376. reportId, _ := this.GetInt("ReportId", 0)
  377. if reportId <= 0 {
  378. br.Msg = "参数有误"
  379. return
  380. }
  381. reportType, _ := this.GetInt("ReportType", 0)
  382. keywords := this.GetString("Keywords", "")
  383. var startSize int
  384. pageSize, _ := this.GetInt("PageSize")
  385. currentIndex, _ := this.GetInt("CurrentIndex")
  386. if pageSize <= 0 {
  387. pageSize = utils.PageSize20
  388. }
  389. if currentIndex <= 0 {
  390. currentIndex = 1
  391. }
  392. startSize = paging.StartIndex(currentIndex, pageSize)
  393. var cond string
  394. var pars []interface{}
  395. cond += ` AND report_id = ? AND report_type = ?`
  396. pars = append(pars, reportId, reportType)
  397. // 推送状态
  398. status := this.GetString("SendStatus", "")
  399. if status != "" {
  400. statusInt, e := strconv.Atoi(status)
  401. if e != nil {
  402. br.Msg = "状态类型有误"
  403. return
  404. }
  405. cond += ` AND send_status = ?`
  406. pars = append(pars, statusInt)
  407. }
  408. // 关键词
  409. if keywords != "" {
  410. k := fmt.Sprint("%", keywords, "%")
  411. cond += ` AND email LIKE ? `
  412. pars = append(pars, k)
  413. }
  414. total, list, e := models.GetEnglishReportEmailLogPageList(cond, pars, "", startSize, pageSize)
  415. if e != nil {
  416. br.Msg = "获取失败"
  417. br.ErrMsg = "获取邮箱列表失败, Err: " + e.Error()
  418. return
  419. }
  420. respList := make([]*models.EnglishReportEmailLogPageList, 0)
  421. for i := range list {
  422. v := new(models.EnglishReportEmailLogPageList)
  423. // 推送结果
  424. v.ResultMsg = "发送成功"
  425. if list[i].ErrMsg != "" {
  426. v.ResultMsg = list[i].ErrMsg
  427. }
  428. v.SendId = list[i].Id
  429. v.ReportId = list[i].ReportId
  430. v.EmailId = list[i].EmailId
  431. v.Email = list[i].Email
  432. v.SendStatus = list[i].SendStatus
  433. v.Source = list[i].Source
  434. v.CreateTime = list[i].CreateTime.Format(utils.FormatDateTime)
  435. respList = append(respList, v)
  436. }
  437. page := paging.GetPaging(currentIndex, pageSize, total)
  438. resp := &models.EnglishReportEmailLogPageListResp{
  439. Paging: page,
  440. List: respList,
  441. }
  442. br.Ret = 200
  443. br.Success = true
  444. br.Msg = "获取成功"
  445. br.Data = resp
  446. }
  447. // Resend
  448. // @Title 重新推送
  449. // @Description 群发推送
  450. // @Param request body models.EnglishReportEmailResendReq true "type json string"
  451. // @Success 200 string "操作成功"
  452. // @router /email/resend [post]
  453. func (this *EnglishReportEmailController) Resend() {
  454. br := new(models.BaseResponse).Init()
  455. br.IsSendEmail = false
  456. defer func() {
  457. this.Data["json"] = br
  458. this.ServeJSON()
  459. }()
  460. sysUser := this.SysUser
  461. if sysUser == nil {
  462. br.Msg = "请登录"
  463. br.ErrMsg = "请登录,SysUser Is Empty"
  464. br.Ret = 408
  465. return
  466. }
  467. var req models.EnglishReportEmailResendReq
  468. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  469. br.Msg = "参数解析异常!"
  470. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  471. return
  472. }
  473. if req.ReportId <= 0 {
  474. br.Msg = "请选择报告"
  475. return
  476. }
  477. // 获取报告信息
  478. reportInfo, e := models.GetEnglishReportItemById(req.ReportId)
  479. if e != nil {
  480. br.Msg = "报告信息有误"
  481. br.ErrMsg = "获取报告信息失败, Err: " + e.Error()
  482. return
  483. }
  484. if reportInfo.State != 2 {
  485. br.Msg = "报告未发布"
  486. br.ErrMsg = fmt.Sprintf("报告状态有误, 当前状态: %d", reportInfo.State)
  487. return
  488. }
  489. if reportInfo.Title == "" {
  490. br.Msg = "报告标题有误"
  491. br.ErrMsg = "报告标题为空, 不可推送"
  492. return
  493. }
  494. if reportInfo.Content == `` {
  495. br.Msg = "报告内容有误"
  496. br.ErrMsg = "报告内容为空, 不可推送"
  497. return
  498. }
  499. if reportInfo.Abstract == `` {
  500. br.Msg = "报告摘要为空"
  501. br.ErrMsg = "报告摘要为空, 不可推送"
  502. return
  503. }
  504. if reportInfo.Overview == "" {
  505. br.Msg = "报告Overview部分不可为空"
  506. br.ErrMsg = "报告Overview为空, 不可推送"
  507. return
  508. }
  509. // 重新发送超过100字符直接截取
  510. theme := reportInfo.Abstract
  511. runeAbs := []rune(reportInfo.Abstract)
  512. runeThe := make([]rune, 0)
  513. if len(runeAbs) > 100 {
  514. runeThe = runeAbs[:100]
  515. theme = string(runeThe)
  516. }
  517. // 获取邮件配置
  518. conf := new(models.EnglishReportEmailConf)
  519. authKey := "english_report_email_conf"
  520. confAuth, e := company.GetConfigDetailByCode(authKey)
  521. if e != nil {
  522. br.Msg = "无权操作"
  523. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  524. return
  525. }
  526. if confAuth.ConfigValue == "" {
  527. br.Msg = "无权操作"
  528. br.ErrMsg = "群发邮件配置为空"
  529. return
  530. }
  531. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  532. br.Msg = "无权操作"
  533. br.ErrMsg = "群发邮件配置有误"
  534. return
  535. }
  536. authArr := strings.Split(conf.SendAuthGroup, ",")
  537. if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  538. br.Msg = "无权操作"
  539. return
  540. }
  541. // 获取模板
  542. confKey := "english_report_email_tmp"
  543. confTmp, e := company.GetConfigDetailByCode(confKey)
  544. if e != nil {
  545. br.Msg = "获取邮件模板失败"
  546. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  547. return
  548. }
  549. if confTmp.ConfigValue == `` {
  550. br.Msg = "邮件模板有误"
  551. br.ErrMsg = "邮件模板为空, 不可推送"
  552. return
  553. }
  554. // 获取发送失败的日志
  555. emailIds := make([]int, 0)
  556. emails := make([]*models.EnglishReportEmail, 0)
  557. logIds := make([]int, 0)
  558. if req.SendId > 0 {
  559. logItem, e := models.GetEnglishReportEmailLogById(req.SendId)
  560. if e != nil {
  561. br.Msg = "发送失败"
  562. br.ErrMsg = "获取原邮件推送日志失败, Err: " + e.Error()
  563. return
  564. }
  565. emailIds = append(emailIds, logItem.EmailId)
  566. logIds = append(logIds, logItem.Id)
  567. } else {
  568. var logCond string
  569. var logPars []interface{}
  570. logCond += ` AND report_id = ? AND send_status = 0 AND report_type=0`
  571. logPars = append(logPars, req.ReportId)
  572. logList, e := models.GetEnglishReportEmailLogList(logCond, logPars)
  573. if e != nil {
  574. br.Msg = "发送失败"
  575. br.ErrMsg = "获取原邮件推送日志列表失败, Err: " + e.Error()
  576. return
  577. }
  578. for i := range logList {
  579. emailIds = append(emailIds, logList[i].EmailId)
  580. logIds = append(logIds, logList[i].Id)
  581. }
  582. }
  583. // 获取收件人列表
  584. // 注: 此处不从失败日志中取email, 因为有可能是地址有误导致的推送失败
  585. if len(emailIds) > 0 {
  586. var emailCond string
  587. var emailPars []interface{}
  588. emailCond += ` AND id IN (` + utils.GetOrmInReplace(len(emailIds)) + `) and enabled = 1 `
  589. emailPars = append(emailPars, emailIds)
  590. em, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  591. if e != nil {
  592. br.Msg = "获取收件人列表失败"
  593. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  594. return
  595. }
  596. emails = em
  597. }
  598. if len(emails) == 0 {
  599. br.Msg = "无邮件可推送"
  600. return
  601. }
  602. // 推送信息
  603. nowTime := time.Now().Local()
  604. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  605. logData := make([]*models.EnglishReportEmailLog, 0)
  606. for i := range emails {
  607. r := new(services.EnglishReportSendEmailRequest)
  608. r.ReportId = reportInfo.Id
  609. r.EmailId = emails[i].Id
  610. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  611. r.Subject = theme
  612. r.FromAlias = conf.FromAlias
  613. // 填充模板
  614. t := reportInfo.PublishTime.Format("02/01/2006")
  615. l := fmt.Sprintf(utils.EnglishShareUrl+"/report/detail?code=%s", reportInfo.ReportCode)
  616. ct := confTmp.ConfigValue
  617. ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
  618. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
  619. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", reportInfo.Overview, 1)
  620. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  621. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  622. r.HtmlBody = ct
  623. r.ReportTitle = reportInfo.Title
  624. r.ReportAbstract = reportInfo.Abstract
  625. r.ReportContent = reportInfo.Overview
  626. r.ReportShareLink = l
  627. r.ReportTime = t
  628. sendData = append(sendData, r)
  629. // 日志
  630. logData = append(logData, &models.EnglishReportEmailLog{
  631. ReportId: reportInfo.Id,
  632. EmailId: emails[i].Id,
  633. Email: emails[i].Email,
  634. Source: models.EnglishReportEmailLogSourceAli,
  635. SendStatus: models.EnglishReportEmailLogStatusIng,
  636. CreateTime: nowTime,
  637. })
  638. }
  639. // 标记原有日志为已删除
  640. if len(logIds) > 0 {
  641. if e = models.DeleteEnglishReportEmailLogByIds(logIds); e != nil {
  642. br.Msg = "发送失败"
  643. br.ErrMsg = "删除原邮件日志失败, Err: " + e.Error()
  644. return
  645. }
  646. }
  647. // 写入新的日志
  648. emailLog := new(models.EnglishReportEmailLog)
  649. if e = emailLog.InsertMulti(logData); e != nil {
  650. br.Msg = "操作失败"
  651. br.ErrMsg = "批量写入群发邮件日志失败, Err: " + e.Error()
  652. return
  653. }
  654. // 群发邮件
  655. go func() {
  656. services.BatchSendAliEnglishReportEmail(sendData)
  657. }()
  658. br.Ret = 200
  659. br.Success = true
  660. br.Msg = "操作成功"
  661. }