email.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. sendCompanyIds := make([]int, 0)
  228. if len(req.EnPermissions) > 0 {
  229. companyIds, e := models.GetEnglishCompanyIdsByEnPermissionIds(req.EnPermissions)
  230. if e != nil {
  231. br.Msg = "推送失败"
  232. br.ErrMsg = "获取指定品种的客户IDs失败, Err: " + e.Error()
  233. return
  234. }
  235. sendCompanyIds = companyIds
  236. }
  237. // 指定收件人列表
  238. sendEmailIds := make([]int, 0)
  239. if req.EmailIds != "" {
  240. emailIdArr := strings.Split(req.EmailIds, ",")
  241. if len(emailIdArr) == 0 {
  242. br.Msg = "指定收件人列表有误"
  243. br.ErrMsg = fmt.Sprintf("收件人列表为空, 收件人IDs: %s", req.EmailIds)
  244. return
  245. }
  246. for _, m := range emailIdArr {
  247. v, _ := strconv.Atoi(m)
  248. sendEmailIds = append(sendEmailIds, v)
  249. }
  250. }
  251. if len(sendCompanyIds) == 0 && len(sendEmailIds) == 0 {
  252. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  253. br.ErrMsg = "收件人列表为空, 不可推送"
  254. return
  255. }
  256. emailCond := " AND enabled = 1 AND status <> 3"
  257. emailPars := make([]interface{}, 0)
  258. if len(sendCompanyIds) > 0 && len(sendEmailIds) > 0 {
  259. emailCond += ` AND (company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `) OR id IN (` + utils.GetOrmInReplace(len(sendEmailIds)) + `))`
  260. emailPars = append(emailPars, sendCompanyIds)
  261. emailPars = append(emailPars, sendEmailIds)
  262. }
  263. if len(sendCompanyIds) > 0 && len(sendEmailIds) == 0 {
  264. emailCond += ` AND company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `)`
  265. emailPars = append(emailPars, sendCompanyIds)
  266. }
  267. if len(sendCompanyIds) == 0 && len(sendEmailIds) > 0 {
  268. emailCond += ` AND id IN (` + utils.GetOrmInReplace(len(sendEmailIds)) + `)`
  269. emailPars = append(emailPars, sendEmailIds)
  270. }
  271. emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  272. if e != nil {
  273. br.Msg = "获取收件人列表失败"
  274. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  275. return
  276. }
  277. if len(emails) == 0 {
  278. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  279. br.ErrMsg = "收件人列表为空, 不可推送"
  280. return
  281. }
  282. // 获取模板
  283. confKey := "english_report_email_tmp"
  284. confTmp, e := company.GetConfigDetailByCode(confKey)
  285. if e != nil {
  286. br.Msg = "获取邮件模板失败"
  287. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  288. return
  289. }
  290. if confTmp.ConfigValue == `` {
  291. br.Msg = "邮件模板有误"
  292. br.ErrMsg = "邮件模板为空, 不可推送"
  293. return
  294. }
  295. // 同一篇报告避免重复推送给同一个邮件
  296. var cond string
  297. var pars []interface{}
  298. cond += ` AND report_id = ? AND send_status = 1 AND report_type = 0`
  299. pars = append(pars, req.ReportId)
  300. logs, e := models.GetEnglishReportEmailLogList(cond, pars)
  301. if e != nil {
  302. br.Msg = "推送失败"
  303. br.ErrMsg = "获取邮件发送日志列表失败, Err: " + e.Error()
  304. return
  305. }
  306. repeatMap := make(map[int]bool, 0)
  307. for i := range logs {
  308. if logs[i].SendStatus == 1 {
  309. repeatMap[logs[i].EmailId] = true
  310. }
  311. }
  312. // 推送信息
  313. nowTime := time.Now().Local()
  314. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  315. logData := make([]*models.EnglishReportEmailLog, 0)
  316. for i := range emails {
  317. if repeatMap[emails[i].Id] {
  318. continue
  319. }
  320. r := new(services.EnglishReportSendEmailRequest)
  321. r.ReportId = reportInfo.Id
  322. r.EmailId = emails[i].Id
  323. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  324. r.Subject = req.Theme
  325. r.FromAlias = conf.FromAlias
  326. // 获取报告Overview部分
  327. if reportInfo.Overview == "" {
  328. br.Msg = "报告Overview部分不可为空"
  329. return
  330. }
  331. overview := reportInfo.Overview
  332. // 填充模板
  333. t := reportInfo.PublishTime.Format("02/01/2006")
  334. l := fmt.Sprintf(utils.EnglishShareUrl+"/report/detail?code=%s", reportInfo.ReportCode)
  335. ct := confTmp.ConfigValue
  336. ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
  337. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
  338. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", overview, 1)
  339. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  340. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  341. r.HtmlBody = ct
  342. r.ReportTitle = reportInfo.Title
  343. r.ReportAbstract = reportInfo.Abstract
  344. r.ReportContent = overview
  345. r.ReportShareLink = l
  346. r.ReportTime = t
  347. sendData = append(sendData, r)
  348. // 日志
  349. logData = append(logData, &models.EnglishReportEmailLog{
  350. ReportId: reportInfo.Id,
  351. EmailId: emails[i].Id,
  352. Email: emails[i].Email,
  353. Source: models.EnglishReportEmailLogSourceAli,
  354. SendStatus: models.EnglishReportEmailLogStatusIng,
  355. CreateTime: nowTime,
  356. })
  357. }
  358. if len(sendData) == 0 {
  359. br.Msg = "无邮件可推送"
  360. return
  361. }
  362. // 写入日志
  363. emailLog := new(models.EnglishReportEmailLog)
  364. if e = emailLog.InsertMulti(logData); e != nil {
  365. br.Msg = "操作失败"
  366. br.ErrMsg = "批量写入群发邮件日志失败, Err: " + e.Error()
  367. return
  368. }
  369. // 修改推送状态
  370. updateCols := []string{"EmailState", "ModifyTime"}
  371. reportInfo.EmailState = 1
  372. reportInfo.ModifyTime = time.Now().Local()
  373. if e = reportInfo.Update(updateCols); e != nil {
  374. br.Msg = "操作失败"
  375. br.ErrMsg = "更新推送状态失败, Err: " + e.Error()
  376. return
  377. }
  378. // 群发邮件
  379. go func() {
  380. services.BatchSendAliEnglishReportEmail(sendData)
  381. }()
  382. br.Ret = 200
  383. br.Success = true
  384. br.Msg = "操作成功"
  385. }
  386. // LogList
  387. // @Title 邮件群发日志列表
  388. // @Description 邮件群发日志列表
  389. // @Param ReportId query int true "报告ID"
  390. // @Param ReportType query int true "类型:0英文研报,1英文线上路演"
  391. // @Param SendStatus query int false "发送状态:-1-已发送;0-发送失败;1-发送成功"
  392. // @Param Keywords query string false "关键词:邮箱"
  393. // @Success 200 {object} models.EnglishReportEmailLogPageListResp
  394. // @router /email/log_list [get]
  395. func (this *EnglishReportEmailController) LogList() {
  396. br := new(models.BaseResponse).Init()
  397. br.IsSendEmail = false
  398. defer func() {
  399. this.Data["json"] = br
  400. this.ServeJSON()
  401. }()
  402. sysUser := this.SysUser
  403. if sysUser == nil {
  404. br.Msg = "请登录"
  405. br.ErrMsg = "请登录,SysUser Is Empty"
  406. br.Ret = 408
  407. return
  408. }
  409. reportId, _ := this.GetInt("ReportId", 0)
  410. if reportId <= 0 {
  411. br.Msg = "参数有误"
  412. return
  413. }
  414. reportType, _ := this.GetInt("ReportType", 0)
  415. keywords := this.GetString("Keywords", "")
  416. var startSize int
  417. pageSize, _ := this.GetInt("PageSize")
  418. currentIndex, _ := this.GetInt("CurrentIndex")
  419. if pageSize <= 0 {
  420. pageSize = utils.PageSize20
  421. }
  422. if currentIndex <= 0 {
  423. currentIndex = 1
  424. }
  425. startSize = paging.StartIndex(currentIndex, pageSize)
  426. var cond string
  427. var pars []interface{}
  428. cond += ` AND report_id = ? AND report_type = ?`
  429. pars = append(pars, reportId, reportType)
  430. // 推送状态
  431. status := this.GetString("SendStatus", "")
  432. if status != "" {
  433. statusInt, e := strconv.Atoi(status)
  434. if e != nil {
  435. br.Msg = "状态类型有误"
  436. return
  437. }
  438. cond += ` AND send_status = ?`
  439. pars = append(pars, statusInt)
  440. }
  441. // 关键词
  442. if keywords != "" {
  443. k := fmt.Sprint("%", keywords, "%")
  444. cond += ` AND email LIKE ? `
  445. pars = append(pars, k)
  446. }
  447. total, list, e := models.GetEnglishReportEmailLogPageList(cond, pars, "", startSize, pageSize)
  448. if e != nil {
  449. br.Msg = "获取失败"
  450. br.ErrMsg = "获取邮箱列表失败, Err: " + e.Error()
  451. return
  452. }
  453. respList := make([]*models.EnglishReportEmailLogPageList, 0)
  454. for i := range list {
  455. v := new(models.EnglishReportEmailLogPageList)
  456. // 推送结果
  457. v.ResultMsg = "发送成功"
  458. if list[i].ErrMsg != "" {
  459. v.ResultMsg = list[i].ErrMsg
  460. }
  461. v.SendId = list[i].Id
  462. v.ReportId = list[i].ReportId
  463. v.EmailId = list[i].EmailId
  464. v.Email = list[i].Email
  465. v.SendStatus = list[i].SendStatus
  466. v.Source = list[i].Source
  467. v.CreateTime = list[i].CreateTime.Format(utils.FormatDateTime)
  468. respList = append(respList, v)
  469. }
  470. page := paging.GetPaging(currentIndex, pageSize, total)
  471. resp := &models.EnglishReportEmailLogPageListResp{
  472. Paging: page,
  473. List: respList,
  474. }
  475. br.Ret = 200
  476. br.Success = true
  477. br.Msg = "获取成功"
  478. br.Data = resp
  479. }
  480. // Resend
  481. // @Title 重新推送
  482. // @Description 群发推送
  483. // @Param request body models.EnglishReportEmailResendReq true "type json string"
  484. // @Success 200 string "操作成功"
  485. // @router /email/resend [post]
  486. func (this *EnglishReportEmailController) Resend() {
  487. br := new(models.BaseResponse).Init()
  488. br.IsSendEmail = false
  489. defer func() {
  490. this.Data["json"] = br
  491. this.ServeJSON()
  492. }()
  493. sysUser := this.SysUser
  494. if sysUser == nil {
  495. br.Msg = "请登录"
  496. br.ErrMsg = "请登录,SysUser Is Empty"
  497. br.Ret = 408
  498. return
  499. }
  500. var req models.EnglishReportEmailResendReq
  501. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  502. br.Msg = "参数解析异常!"
  503. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  504. return
  505. }
  506. if req.ReportId <= 0 {
  507. br.Msg = "请选择报告"
  508. return
  509. }
  510. // 获取报告信息
  511. reportInfo, e := models.GetEnglishReportItemById(req.ReportId)
  512. if e != nil {
  513. br.Msg = "报告信息有误"
  514. br.ErrMsg = "获取报告信息失败, Err: " + e.Error()
  515. return
  516. }
  517. if reportInfo.State != 2 {
  518. br.Msg = "报告未发布"
  519. br.ErrMsg = fmt.Sprintf("报告状态有误, 当前状态: %d", reportInfo.State)
  520. return
  521. }
  522. if reportInfo.Title == "" {
  523. br.Msg = "报告标题有误"
  524. br.ErrMsg = "报告标题为空, 不可推送"
  525. return
  526. }
  527. if reportInfo.Content == `` {
  528. br.Msg = "报告内容有误"
  529. br.ErrMsg = "报告内容为空, 不可推送"
  530. return
  531. }
  532. if reportInfo.Abstract == `` {
  533. br.Msg = "报告摘要为空"
  534. br.ErrMsg = "报告摘要为空, 不可推送"
  535. return
  536. }
  537. if reportInfo.Overview == "" {
  538. br.Msg = "报告Overview部分不可为空"
  539. br.ErrMsg = "报告Overview为空, 不可推送"
  540. return
  541. }
  542. // 重新发送超过100字符直接截取
  543. theme := reportInfo.Abstract
  544. runeAbs := []rune(reportInfo.Abstract)
  545. runeThe := make([]rune, 0)
  546. if len(runeAbs) > 100 {
  547. runeThe = runeAbs[:100]
  548. theme = string(runeThe)
  549. }
  550. // 获取邮件配置
  551. conf := new(models.EnglishReportEmailConf)
  552. authKey := "english_report_email_conf"
  553. confAuth, e := company.GetConfigDetailByCode(authKey)
  554. if e != nil {
  555. br.Msg = "无权操作"
  556. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  557. return
  558. }
  559. if confAuth.ConfigValue == "" {
  560. br.Msg = "无权操作"
  561. br.ErrMsg = "群发邮件配置为空"
  562. return
  563. }
  564. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  565. br.Msg = "无权操作"
  566. br.ErrMsg = "群发邮件配置有误"
  567. return
  568. }
  569. authArr := strings.Split(conf.SendAuthGroup, ",")
  570. if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  571. br.Msg = "无权操作"
  572. return
  573. }
  574. // 获取模板
  575. confKey := "english_report_email_tmp"
  576. confTmp, e := company.GetConfigDetailByCode(confKey)
  577. if e != nil {
  578. br.Msg = "获取邮件模板失败"
  579. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  580. return
  581. }
  582. if confTmp.ConfigValue == `` {
  583. br.Msg = "邮件模板有误"
  584. br.ErrMsg = "邮件模板为空, 不可推送"
  585. return
  586. }
  587. // 获取发送失败的日志
  588. emailIds := make([]int, 0)
  589. emails := make([]*models.EnglishReportEmail, 0)
  590. logIds := make([]int, 0)
  591. if req.SendId > 0 {
  592. logItem, e := models.GetEnglishReportEmailLogById(req.SendId)
  593. if e != nil {
  594. br.Msg = "发送失败"
  595. br.ErrMsg = "获取原邮件推送日志失败, Err: " + e.Error()
  596. return
  597. }
  598. emailIds = append(emailIds, logItem.EmailId)
  599. logIds = append(logIds, logItem.Id)
  600. } else {
  601. var logCond string
  602. var logPars []interface{}
  603. logCond += ` AND report_id = ? AND send_status = 0 AND report_type=0`
  604. logPars = append(logPars, req.ReportId)
  605. logList, e := models.GetEnglishReportEmailLogList(logCond, logPars)
  606. if e != nil {
  607. br.Msg = "发送失败"
  608. br.ErrMsg = "获取原邮件推送日志列表失败, Err: " + e.Error()
  609. return
  610. }
  611. for i := range logList {
  612. emailIds = append(emailIds, logList[i].EmailId)
  613. logIds = append(logIds, logList[i].Id)
  614. }
  615. }
  616. // 获取收件人列表
  617. // 注: 此处不从失败日志中取email, 因为有可能是地址有误导致的推送失败
  618. if len(emailIds) > 0 {
  619. var emailCond string
  620. var emailPars []interface{}
  621. emailCond += ` AND id IN (` + utils.GetOrmInReplace(len(emailIds)) + `) and enabled = 1 `
  622. emailPars = append(emailPars, emailIds)
  623. em, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  624. if e != nil {
  625. br.Msg = "获取收件人列表失败"
  626. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  627. return
  628. }
  629. emails = em
  630. }
  631. if len(emails) == 0 {
  632. br.Msg = "无邮件可推送"
  633. return
  634. }
  635. // 推送信息
  636. nowTime := time.Now().Local()
  637. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  638. logData := make([]*models.EnglishReportEmailLog, 0)
  639. for i := range emails {
  640. r := new(services.EnglishReportSendEmailRequest)
  641. r.ReportId = reportInfo.Id
  642. r.EmailId = emails[i].Id
  643. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  644. r.Subject = theme
  645. r.FromAlias = conf.FromAlias
  646. // 填充模板
  647. t := reportInfo.PublishTime.Format("02/01/2006")
  648. l := fmt.Sprintf(utils.EnglishShareUrl+"/report/detail?code=%s", reportInfo.ReportCode)
  649. ct := confTmp.ConfigValue
  650. ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
  651. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
  652. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", reportInfo.Overview, 1)
  653. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  654. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  655. r.HtmlBody = ct
  656. r.ReportTitle = reportInfo.Title
  657. r.ReportAbstract = reportInfo.Abstract
  658. r.ReportContent = reportInfo.Overview
  659. r.ReportShareLink = l
  660. r.ReportTime = t
  661. sendData = append(sendData, r)
  662. // 日志
  663. logData = append(logData, &models.EnglishReportEmailLog{
  664. ReportId: reportInfo.Id,
  665. EmailId: emails[i].Id,
  666. Email: emails[i].Email,
  667. Source: models.EnglishReportEmailLogSourceAli,
  668. SendStatus: models.EnglishReportEmailLogStatusIng,
  669. CreateTime: nowTime,
  670. })
  671. }
  672. // 标记原有日志为已删除
  673. if len(logIds) > 0 {
  674. if e = models.DeleteEnglishReportEmailLogByIds(logIds); e != nil {
  675. br.Msg = "发送失败"
  676. br.ErrMsg = "删除原邮件日志失败, Err: " + e.Error()
  677. return
  678. }
  679. }
  680. // 写入新的日志
  681. emailLog := new(models.EnglishReportEmailLog)
  682. if e = emailLog.InsertMulti(logData); e != nil {
  683. br.Msg = "操作失败"
  684. br.ErrMsg = "批量写入群发邮件日志失败, Err: " + e.Error()
  685. return
  686. }
  687. // 群发邮件
  688. go func() {
  689. services.BatchSendAliEnglishReportEmail(sendData)
  690. }()
  691. br.Ret = 200
  692. br.Success = true
  693. br.Msg = "操作成功"
  694. }