email.go 21 KB

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