email.go 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  1. package english_report
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/company"
  7. "eta/eta_api/services"
  8. "eta/eta_api/services/alarm_msg"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/beego/beego/v2/server/web"
  12. "github.com/rdlucklib/rdluck_tools/paging"
  13. "github.com/tealeg/xlsx"
  14. "os"
  15. "path"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. // EnglishReportEmailController 英文研报邮箱/英文客户联系人
  21. type EnglishReportEmailController struct {
  22. controllers.BaseAuthController
  23. }
  24. type EnglishReportEmailCallBackController struct {
  25. web.Controller
  26. }
  27. // List
  28. // @Title 英文研报邮箱列表
  29. // @Description 英文研报邮箱列表
  30. // @Param CompanyId query int false "客户ID"
  31. // @Param Keywords query string false "关键词:联系人名称/邮箱地址"
  32. // @Param SortType query int false "点击量排序方式:1-倒序;2-正序"
  33. // @Param SortParam query int false "排序字段:1-点击量;2-点击时间 3-用户状态"
  34. // @Param ListParam query int false "筛选字段参数,用来筛选的字段, 枚举值:1:正式 、 2:临时 、 3:终止 、4:正式+临时 "
  35. // @Success 200 {object} models.EnglishReportEmailPageListResp
  36. // @router /email/list [get]
  37. func (this *EnglishReportEmailController) List() {
  38. br := new(models.BaseResponse).Init()
  39. br.IsSendEmail = false
  40. defer func() {
  41. this.Data["json"] = br
  42. this.ServeJSON()
  43. }()
  44. sysUser := this.SysUser
  45. if sysUser == nil {
  46. br.Msg = "请登录"
  47. br.ErrMsg = "请登录,SysUser Is Empty"
  48. br.Ret = 408
  49. return
  50. }
  51. companyId, _ := this.GetInt("CompanyId", 0)
  52. keywords := this.GetString("Keywords", "")
  53. sortType, _ := this.GetInt("SortType", 1)
  54. sortParam, _ := this.GetInt("SortParam", 1)
  55. listParam, _ := this.GetInt("ListParam", 1)
  56. var cond, order string
  57. var pars []interface{}
  58. if companyId > 0 {
  59. cond += ` AND a.company_id = ?`
  60. pars = append(pars, companyId)
  61. }
  62. if keywords != "" {
  63. k := "%" + keywords + "%"
  64. cond += ` AND (a.name LIKE ? OR a.email LIKE ? OR a.mobile LIKE ?)`
  65. pars = append(pars, k, k, k)
  66. }
  67. sortParamArr := []int{1, 2, 3}
  68. sortTypeArr := []int{1, 2}
  69. if utils.InArrayByInt(sortParamArr, sortParam) && utils.InArrayByInt(sortTypeArr, sortType) {
  70. pMap := map[int]string{1: "a.view_total", 2: "a.last_view_time", 3: "a.enabled"}
  71. tMap := map[int]string{1: "DESC", 2: "ASC"}
  72. order = fmt.Sprintf(` ORDER BY %s %s`, pMap[sortParam], tMap[sortType])
  73. }
  74. if listParam == 1 {
  75. cond += ` AND a.status = 1`
  76. }
  77. if listParam == 2 {
  78. cond += ` AND a.status = 2`
  79. }
  80. if listParam == 3 {
  81. cond += ` AND a.status = 3`
  82. }
  83. if listParam == 4 {
  84. cond += ` AND (a.status = 1 OR a.status = 2)`
  85. }
  86. var startSize int
  87. pageSize, _ := this.GetInt("PageSize")
  88. currentIndex, _ := this.GetInt("CurrentIndex")
  89. if pageSize <= 0 {
  90. pageSize = utils.PageSize20
  91. }
  92. if currentIndex <= 0 {
  93. currentIndex = 1
  94. }
  95. startSize = paging.StartIndex(currentIndex, pageSize)
  96. total, list, e := models.GetEnglishReportEmailPageList(cond, pars, order, startSize, pageSize)
  97. if e != nil {
  98. br.Msg = "获取失败"
  99. br.ErrMsg = "获取邮箱列表失败, Err: " + e.Error()
  100. return
  101. }
  102. respList := make([]*models.EnglishReportEmailResp, 0)
  103. for i := range list {
  104. item := &models.EnglishReportEmailResp{
  105. Id: list[i].Id,
  106. Name: list[i].Name,
  107. Email: list[i].Email,
  108. Mobile: list[i].Mobile,
  109. CountryCode: list[i].CountryCode,
  110. BusinessCardUrl: list[i].BusinessCardUrl,
  111. AdminName: list[i].AdminName,
  112. CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
  113. ViewTotal: list[i].ViewTotal,
  114. Enabled: list[i].Enabled,
  115. CompanyName: list[i].CompanyName,
  116. RegisterCompanyName: list[i].RegisterCompanyName,
  117. RegisterTime: list[i].RegisterTime.Format(utils.FormatDateTime),
  118. }
  119. if item.RegisterTime == "0001-01-01 00:00:00" {
  120. item.RegisterTime = ""
  121. }
  122. respList = append(respList, item)
  123. }
  124. page := paging.GetPaging(currentIndex, pageSize, total)
  125. resp := &models.EnglishReportEmailPageListResp{
  126. Paging: page,
  127. List: respList,
  128. }
  129. br.Ret = 200
  130. br.Success = true
  131. br.Msg = "获取成功"
  132. br.Data = resp
  133. }
  134. // Save
  135. // @Title 保存英文研报邮箱
  136. // @Description 保存英文研报邮箱
  137. // @Param request body models.EnglishReportEmailSaveReq true "type json string"
  138. // @Success 200 string "操作成功"
  139. // @router /email/save [post]
  140. func (this *EnglishReportEmailController) Save() {
  141. br := new(models.BaseResponse).Init()
  142. br.IsSendEmail = false
  143. defer func() {
  144. this.Data["json"] = br
  145. this.ServeJSON()
  146. }()
  147. sysUser := this.SysUser
  148. if sysUser == nil {
  149. br.Msg = "请登录"
  150. br.ErrMsg = "请登录,SysUser Is Empty"
  151. br.Ret = 408
  152. return
  153. }
  154. var req models.EnglishReportEmailSaveReq
  155. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  156. br.Msg = "参数解析异常!"
  157. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  158. return
  159. }
  160. if req.Name == "" {
  161. br.Msg = "请输入客户名称"
  162. return
  163. }
  164. req.Email = strings.Replace(req.Email, " ", "", -1)
  165. if req.Email == "" {
  166. br.Msg = "请输入邮箱"
  167. return
  168. }
  169. if !utils.ValidateEmailFormatat(req.Email) {
  170. br.Msg = "邮箱格式有误"
  171. return
  172. }
  173. // 英文客户以邮箱为主, 邮箱去重, 手机号非必填, 此处不做手机号去重
  174. exist, e := models.GetEnglishReportEmailByEmail(req.Email)
  175. if e != nil && e.Error() != utils.ErrNoRow() {
  176. br.Msg = "保存失败"
  177. br.ErrMsg = "获取同名邮箱失败, Err:" + e.Error()
  178. return
  179. }
  180. opType := 0
  181. now := time.Now().Local()
  182. item := new(models.EnglishReportEmail)
  183. if req.Id > 0 {
  184. // 更新
  185. if exist != nil && exist.Id != req.Id {
  186. br.Msg = "该邮箱已存在"
  187. return
  188. }
  189. if exist != nil {
  190. createTime, _ := time.Parse(utils.FormatDateTime, exist.CreateTime)
  191. modifyTime, _ := time.Parse(utils.FormatDateTime, exist.ModifyTime)
  192. item = &models.EnglishReportEmail{
  193. Id: exist.Id,
  194. CompanyId: exist.CompanyId,
  195. Name: exist.Name,
  196. Email: exist.Email,
  197. Mobile: exist.Mobile,
  198. CountryCode: exist.CountryCode,
  199. BusinessCardUrl: exist.BusinessCardUrl,
  200. ViewTotal: exist.ViewTotal,
  201. LastViewTime: exist.LastViewTime,
  202. IsDeleted: exist.IsDeleted,
  203. Enabled: exist.Enabled,
  204. AdminId: exist.AdminId,
  205. AdminName: exist.AdminName,
  206. Status: exist.Status,
  207. CompanyName: exist.CompanyName,
  208. CreateTime: createTime,
  209. ModifyTime: modifyTime,
  210. }
  211. } else {
  212. em, e := models.GetEnglishReportEmailById(req.Id)
  213. if e != nil {
  214. br.Msg = "保存失败"
  215. br.ErrMsg = "获取邮箱失败, Err:" + e.Error()
  216. return
  217. }
  218. item = em
  219. }
  220. item.Name = req.Name
  221. item.Email = req.Email
  222. item.Mobile = req.Mobile
  223. item.CountryCode = req.CountryCode
  224. item.BusinessCardUrl = req.BusinessCardUrl
  225. item.ModifyTime = now
  226. item.Enabled = req.Enabled
  227. updateCols := []string{"Name", "Email", "Mobile", "CountryCode", "BusinessCardUrl", "ModifyTime", "Enabled"}
  228. if e = item.Update(updateCols); e != nil {
  229. br.Msg = "保存失败"
  230. br.ErrMsg = "更新邮箱失败, Err:" + e.Error()
  231. return
  232. }
  233. opType = models.EnglishReportEmailOpLogTypeEdit
  234. } else {
  235. // 新增
  236. if exist != nil {
  237. respItem := &models.EnglishReportEmailResp{
  238. Id: exist.Id,
  239. Name: exist.Name,
  240. Email: exist.Email,
  241. Mobile: exist.Mobile,
  242. CountryCode: exist.CountryCode,
  243. BusinessCardUrl: exist.BusinessCardUrl,
  244. AdminName: exist.AdminName,
  245. CreateTime: exist.CreateTime,
  246. ViewTotal: exist.ViewTotal,
  247. Enabled: exist.Enabled,
  248. CompanyName: exist.CompanyName,
  249. Status: exist.Status,
  250. RegisterCompanyName: exist.RegisterCompanyName,
  251. RegisterTime: exist.RegisterTime,
  252. }
  253. if respItem.RegisterTime == "0001-01-01 00:00:00" {
  254. respItem.RegisterCompanyName = ""
  255. }
  256. br.Data = respItem
  257. br.Msg = "该邮箱已存在"
  258. br.Ret = 1001
  259. return
  260. }
  261. item = &models.EnglishReportEmail{
  262. CompanyId: req.CompanyId,
  263. Name: req.Name,
  264. Email: req.Email,
  265. Mobile: req.Mobile,
  266. CountryCode: req.CountryCode,
  267. BusinessCardUrl: req.BusinessCardUrl,
  268. AdminId: sysUser.AdminId,
  269. AdminName: sysUser.AdminName,
  270. CreateTime: now,
  271. ModifyTime: now,
  272. Enabled: req.Enabled,
  273. Status: 1,
  274. }
  275. if e = item.Create(); e != nil {
  276. br.Msg = "保存失败"
  277. br.ErrMsg = "新增邮箱失败, Err:" + e.Error()
  278. return
  279. }
  280. opType = models.EnglishReportEmailOpLogTypeAdd
  281. }
  282. // 更新客户状态
  283. go func() {
  284. _ = services.UpdateEnglishCompanyEnabledByCompanyId(item.CompanyId)
  285. }()
  286. // 记录日志
  287. go func() {
  288. opByte, e := json.Marshal(req)
  289. if e != nil {
  290. return
  291. }
  292. opData := string(opByte)
  293. opLog := &models.EnglishReportEmailOpLog{
  294. CompanyId: item.CompanyId,
  295. EmailId: item.Id,
  296. AdminId: sysUser.AdminId,
  297. OptionData: opData,
  298. OptionType: opType,
  299. CreateTime: now,
  300. }
  301. if e = opLog.Create(); e != nil {
  302. return
  303. }
  304. }()
  305. br.Ret = 200
  306. br.Success = true
  307. br.Msg = "操作成功"
  308. //br.Data = item
  309. }
  310. // Del
  311. // @Title 删除英文研报邮箱
  312. // @Description 删除英文研报邮箱
  313. // @Param request body models.EnglishReportEmailDelReq true "type json string"
  314. // @Success 200 string "操作成功"
  315. // @router /email/del [post]
  316. func (this *EnglishReportEmailController) Del() {
  317. br := new(models.BaseResponse).Init()
  318. br.IsSendEmail = false
  319. defer func() {
  320. this.Data["json"] = br
  321. this.ServeJSON()
  322. }()
  323. sysUser := this.SysUser
  324. if sysUser == nil {
  325. br.Msg = "请登录"
  326. br.ErrMsg = "请登录,SysUser Is Empty"
  327. br.Ret = 408
  328. return
  329. }
  330. var req models.EnglishReportEmailDelReq
  331. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  332. br.Msg = "参数解析异常!"
  333. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  334. return
  335. }
  336. if req.EmailId <= 0 {
  337. br.Msg = "参数有误"
  338. return
  339. }
  340. item, e := models.GetEnglishReportEmailById(req.EmailId)
  341. if e != nil {
  342. br.Msg = "联系人已被删除, 请勿重复操作"
  343. return
  344. }
  345. nowTime := time.Now().Local()
  346. item.IsDeleted = 1
  347. item.ModifyTime = nowTime
  348. updateCols := []string{"IsDeleted", "ModifyTime"}
  349. if e = item.Update(updateCols); e != nil {
  350. br.Msg = "删除失败"
  351. br.ErrMsg = "删除英文联系人失败, Err:" + e.Error()
  352. return
  353. }
  354. // 更新客户状态
  355. go func() {
  356. _ = services.UpdateEnglishCompanyEnabledByCompanyId(item.CompanyId)
  357. }()
  358. // 记录日志
  359. go func() {
  360. opByte, e := json.Marshal(req)
  361. if e != nil {
  362. return
  363. }
  364. opData := string(opByte)
  365. opLog := &models.EnglishReportEmailOpLog{
  366. CompanyId: item.CompanyId,
  367. EmailId: item.Id,
  368. AdminId: sysUser.AdminId,
  369. OptionData: opData,
  370. OptionType: models.EnglishReportEmailOpLogTypeDel,
  371. CreateTime: nowTime,
  372. }
  373. if e = opLog.Create(); e != nil {
  374. return
  375. }
  376. }()
  377. br.Ret = 200
  378. br.Success = true
  379. br.Msg = "操作成功"
  380. }
  381. // EditEnabled
  382. // @Title 启用/禁用英文研报邮箱
  383. // @Description 启用/禁用英文研报邮箱
  384. // @Param request body models.EnglishReportEditEnabledReq true "type json string"
  385. // @Success 200 string "操作成功"
  386. // @router /email/editEnabled [post]
  387. func (this *EnglishReportEmailController) EditEnabled() {
  388. br := new(models.BaseResponse).Init()
  389. br.IsSendEmail = false
  390. defer func() {
  391. this.Data["json"] = br
  392. this.ServeJSON()
  393. }()
  394. sysUser := this.SysUser
  395. if sysUser == nil {
  396. br.Msg = "请登录"
  397. br.ErrMsg = "请登录,SysUser Is Empty"
  398. br.Ret = 408
  399. return
  400. }
  401. var req models.EnglishReportEditEnabledReq
  402. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  403. br.Msg = "参数解析异常!"
  404. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  405. return
  406. }
  407. if req.EmailId <= 0 {
  408. br.Msg = "参数有误"
  409. return
  410. }
  411. if req.Enabled != 0 && req.Enabled != 1 {
  412. br.Msg = "参数有误"
  413. return
  414. }
  415. item, e := models.GetEnglishReportEmailById(req.EmailId)
  416. if e != nil {
  417. br.Msg = "联系人已被删除, 请勿操作"
  418. return
  419. }
  420. if item.Enabled == req.Enabled {
  421. br.Ret = 200
  422. br.Success = true
  423. br.Msg = "操作成功"
  424. return
  425. }
  426. // 更新联系人
  427. nowTime := time.Now().Local()
  428. item.Enabled = req.Enabled
  429. item.ModifyTime = nowTime
  430. updateCols := []string{"Enabled", "ModifyTime"}
  431. if e = item.Update(updateCols); e != nil {
  432. br.Msg = "操作失败"
  433. br.ErrMsg = "更新英文联系人失败, Err:" + e.Error()
  434. return
  435. }
  436. // 更新所属客户状态
  437. go func() {
  438. _ = services.UpdateEnglishCompanyEnabledByCompanyId(item.CompanyId)
  439. }()
  440. // 记录日志
  441. go func() {
  442. opByte, e := json.Marshal(req)
  443. if e != nil {
  444. return
  445. }
  446. opData := string(opByte)
  447. opLog := &models.EnglishReportEmailOpLog{
  448. CompanyId: item.CompanyId,
  449. EmailId: item.Id,
  450. AdminId: sysUser.AdminId,
  451. OptionData: opData,
  452. OptionType: models.EnglishReportEmailOpLogTypeEditEnabled,
  453. CreateTime: nowTime,
  454. }
  455. if e = opLog.Create(); e != nil {
  456. return
  457. }
  458. }()
  459. br.Ret = 200
  460. br.Success = true
  461. br.Msg = "操作成功"
  462. }
  463. // Send
  464. // @Title 群发推送
  465. // @Description 群发推送
  466. // @Param request body models.EnglishReportEmailSaveReq true "type json string"
  467. // @Success 200 string "操作成功"
  468. // @router /email/send [post]
  469. func (this *EnglishReportEmailController) Send() {
  470. br := new(models.BaseResponse).Init()
  471. br.IsSendEmail = false
  472. defer func() {
  473. this.Data["json"] = br
  474. this.ServeJSON()
  475. }()
  476. sysUser := this.SysUser
  477. if sysUser == nil {
  478. br.Msg = "请登录"
  479. br.ErrMsg = "请登录,SysUser Is Empty"
  480. br.Ret = 408
  481. return
  482. }
  483. var req models.EnglishReportEmailSendReq
  484. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  485. br.Msg = "参数解析异常!"
  486. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  487. return
  488. }
  489. if req.ReportId <= 0 {
  490. br.Msg = "请选择报告"
  491. return
  492. }
  493. if req.Theme == "" {
  494. br.Msg = "请输入邮件主题"
  495. return
  496. }
  497. //prefixTheme := "【HI China Macro and Commodity】"
  498. //if !strings.Contains(req.Theme, prefixTheme) {
  499. // req.Theme = prefixTheme + req.Theme
  500. //}
  501. runeThe := []rune(req.Theme)
  502. if len(runeThe) > 100 {
  503. br.Msg = "邮件主题不可超过100字符"
  504. return
  505. }
  506. // 获取报告信息
  507. reportInfo, e := models.GetEnglishReportItemById(req.ReportId)
  508. if e != nil {
  509. br.Msg = "报告信息有误"
  510. br.ErrMsg = "获取报告信息失败, Err: " + e.Error()
  511. return
  512. }
  513. if reportInfo.EmailState != 0 {
  514. br.Msg = "请勿重复发送"
  515. br.ErrMsg = fmt.Sprintf("报告群发邮件状态有误, 当前状态: %d", reportInfo.EmailState)
  516. return
  517. }
  518. if reportInfo.State != 2 {
  519. br.Msg = "报告未发布"
  520. br.ErrMsg = fmt.Sprintf("报告状态有误, 当前状态: %d", reportInfo.State)
  521. return
  522. }
  523. if reportInfo.Title == "" {
  524. br.Msg = "报告标题有误"
  525. br.ErrMsg = "报告标题为空, 不可推送"
  526. return
  527. }
  528. if reportInfo.Content == `` {
  529. br.Msg = "报告内容有误"
  530. br.ErrMsg = "报告内容为空, 不可推送"
  531. return
  532. }
  533. // 获取邮件配置
  534. conf := new(models.EnglishReportEmailConf)
  535. authKey := "english_report_email_conf"
  536. confAuth, e := company.GetConfigDetailByCode(authKey)
  537. if e != nil {
  538. br.Msg = "无权操作"
  539. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  540. return
  541. }
  542. if confAuth.ConfigValue == "" {
  543. br.Msg = "无权操作"
  544. br.ErrMsg = "群发邮件配置为空"
  545. return
  546. }
  547. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  548. br.Msg = "无权操作"
  549. br.ErrMsg = "群发邮件配置有误"
  550. return
  551. }
  552. authArr := strings.Split(conf.SendAuthGroup, ",")
  553. if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  554. br.Msg = "无权操作"
  555. return
  556. }
  557. // 指定品种的客户
  558. sendCompanyIds := make([]int, 0)
  559. if len(req.EnPermissions) > 0 {
  560. companyIds, e := models.GetEnglishCompanyIdsByEnPermissionIds(req.EnPermissions)
  561. if e != nil {
  562. br.Msg = "推送失败"
  563. br.ErrMsg = "获取指定品种的客户IDs失败, Err: " + e.Error()
  564. return
  565. }
  566. sendCompanyIds = companyIds
  567. }
  568. // 指定收件人列表
  569. sendEmailIds := make([]int, 0)
  570. if req.EmailIds != "" {
  571. emailIdArr := strings.Split(req.EmailIds, ",")
  572. if len(emailIdArr) == 0 {
  573. br.Msg = "指定收件人列表有误"
  574. br.ErrMsg = fmt.Sprintf("收件人列表为空, 收件人IDs: %s", req.EmailIds)
  575. return
  576. }
  577. for _, m := range emailIdArr {
  578. v, _ := strconv.Atoi(m)
  579. sendEmailIds = append(sendEmailIds, v)
  580. }
  581. }
  582. if len(sendCompanyIds) == 0 && len(sendEmailIds) == 0 {
  583. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  584. br.ErrMsg = "收件人列表为空, 不可推送"
  585. return
  586. }
  587. // 往正式客户id里面插入0的 目的是为了把临时客户找出来
  588. sendCompanyIds = append(sendCompanyIds, 0)
  589. emailCond := " AND enabled = 1 AND status <> 3"
  590. emailPars := make([]interface{}, 0)
  591. if len(sendCompanyIds) > 0 && len(sendEmailIds) > 0 {
  592. emailCond += ` AND (company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `) OR id IN (` + utils.GetOrmInReplace(len(sendEmailIds)) + `))`
  593. emailPars = append(emailPars, sendCompanyIds)
  594. emailPars = append(emailPars, sendEmailIds)
  595. }
  596. if len(sendCompanyIds) > 0 && len(sendEmailIds) == 0 {
  597. emailCond += ` AND company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `)`
  598. emailPars = append(emailPars, sendCompanyIds)
  599. }
  600. emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  601. if e != nil {
  602. br.Msg = "获取收件人列表失败"
  603. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  604. return
  605. }
  606. if len(emails) == 0 {
  607. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  608. br.ErrMsg = "收件人列表为空, 不可推送"
  609. return
  610. }
  611. // 获取模板
  612. confKey := "english_report_email_tmp"
  613. confTmp, e := company.GetConfigDetailByCode(confKey)
  614. if e != nil {
  615. br.Msg = "获取邮件模板失败"
  616. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  617. return
  618. }
  619. if confTmp.ConfigValue == `` {
  620. br.Msg = "邮件模板有误"
  621. br.ErrMsg = "邮件模板为空, 不可推送"
  622. return
  623. }
  624. // 同一篇报告避免重复推送给同一个邮件
  625. var cond string
  626. var pars []interface{}
  627. cond += ` AND report_id = ? AND send_status = 1 AND report_type = 0`
  628. pars = append(pars, req.ReportId)
  629. logs, e := models.GetEnglishReportEmailLogList(cond, pars)
  630. if e != nil {
  631. br.Msg = "推送失败"
  632. br.ErrMsg = "获取邮件发送日志列表失败, Err: " + e.Error()
  633. return
  634. }
  635. repeatMap := make(map[int]bool, 0)
  636. for i := range logs {
  637. if logs[i].SendStatus == 1 {
  638. repeatMap[logs[i].EmailId] = true
  639. }
  640. }
  641. // 推送信息
  642. nowTime := time.Now().Local()
  643. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  644. logData := make([]*models.EnglishReportEmailLog, 0)
  645. for i := range emails {
  646. if repeatMap[emails[i].Id] {
  647. continue
  648. }
  649. r := new(services.EnglishReportSendEmailRequest)
  650. r.ReportId = reportInfo.Id
  651. r.EmailId = emails[i].Id
  652. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  653. r.Subject = req.Theme
  654. r.FromAlias = conf.FromAlias
  655. // 获取报告Overview部分
  656. if reportInfo.Overview == "" {
  657. br.Msg = "报告Overview部分不可为空"
  658. return
  659. }
  660. overview := reportInfo.Overview
  661. // 填充模板
  662. t := reportInfo.PublishTime.Format("02/01/2006")
  663. l := fmt.Sprintf(utils.EnglishShareUrl+"/report/detail?code=%s", reportInfo.ReportCode)
  664. ct := confTmp.ConfigValue
  665. ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
  666. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
  667. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", overview, 1)
  668. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  669. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  670. r.HtmlBody = ct
  671. r.ReportTitle = reportInfo.Title
  672. r.ReportAbstract = reportInfo.Abstract
  673. r.ReportContent = overview
  674. r.ReportShareLink = l
  675. r.ReportTime = t
  676. sendData = append(sendData, r)
  677. // 日志
  678. logData = append(logData, &models.EnglishReportEmailLog{
  679. ReportId: reportInfo.Id,
  680. EmailId: emails[i].Id,
  681. Email: emails[i].Email,
  682. Source: models.EnglishReportEmailLogSourceAli,
  683. SendStatus: models.EnglishReportEmailLogStatusIng,
  684. CreateTime: nowTime,
  685. })
  686. }
  687. if len(sendData) == 0 {
  688. br.Msg = "无邮件可推送"
  689. return
  690. }
  691. // 写入日志
  692. emailLog := new(models.EnglishReportEmailLog)
  693. if e = emailLog.InsertMulti(logData); e != nil {
  694. br.Msg = "操作失败"
  695. br.ErrMsg = "批量写入群发邮件日志失败, Err: " + e.Error()
  696. return
  697. }
  698. // 修改推送状态
  699. updateCols := []string{"EmailState", "ModifyTime"}
  700. reportInfo.EmailState = 1
  701. reportInfo.ModifyTime = time.Now().Local()
  702. if e = reportInfo.Update(updateCols); e != nil {
  703. br.Msg = "操作失败"
  704. br.ErrMsg = "更新推送状态失败, Err: " + e.Error()
  705. return
  706. }
  707. // 群发邮件
  708. go func() {
  709. services.BatchSendAliEnglishReportEmail(sendData)
  710. }()
  711. br.Ret = 200
  712. br.Success = true
  713. br.Msg = "操作成功"
  714. }
  715. // PvList
  716. // @Title 英文研报邮箱PV列表
  717. // @Description 英文研报邮箱PV列表
  718. // @Param ReportId query int false "英文研报ID"
  719. // @Param ReportType query int false "类型:0英文研报,1英文线上路演"
  720. // @Success 200 {object} models.EnglishReportEmailPvResp
  721. // @router /email/pv_list [get]
  722. func (this *EnglishReportEmailController) PvList() {
  723. br := new(models.BaseResponse).Init()
  724. br.IsSendEmail = false
  725. defer func() {
  726. this.Data["json"] = br
  727. this.ServeJSON()
  728. }()
  729. sysUser := this.SysUser
  730. if sysUser == nil {
  731. br.Msg = "请登录"
  732. br.ErrMsg = "请登录,SysUser Is Empty"
  733. br.Ret = 408
  734. return
  735. }
  736. reportId, e := this.GetInt("ReportId", 0)
  737. if reportId == 0 {
  738. br.Msg = "参数有误"
  739. return
  740. }
  741. reportType, e := this.GetInt("ReportType", 0)
  742. var cond string
  743. var pars []interface{}
  744. cond += ` AND a.report_id = ? AND a.report_type= ?`
  745. pars = append(pars, reportId, reportType)
  746. var startSize int
  747. pageSize, _ := this.GetInt("PageSize")
  748. currentIndex, _ := this.GetInt("CurrentIndex")
  749. if pageSize <= 0 {
  750. pageSize = utils.PageSize20
  751. }
  752. if currentIndex <= 0 {
  753. currentIndex = 1
  754. }
  755. startSize = paging.StartIndex(currentIndex, pageSize)
  756. total, list, e := models.GetEnglishReportEmailPvPageList(cond, pars, startSize, pageSize)
  757. if e != nil {
  758. br.Msg = "获取失败"
  759. br.ErrMsg = "获取英文研报邮箱pv列表失败, Err: " + e.Error()
  760. return
  761. }
  762. page := paging.GetPaging(currentIndex, pageSize, total)
  763. resp := &models.EnglishReportEmailPvPageListResp{
  764. Paging: page,
  765. List: list,
  766. }
  767. br.Ret = 200
  768. br.Success = true
  769. br.Msg = "获取成功"
  770. br.Data = resp
  771. }
  772. // ImportListMatch
  773. // @Title 英文邮箱/联系人批量导入
  774. // @Description 英文邮箱/联系人批量导入
  775. // @Param File query file true "文件"
  776. // @Param CompanyId query int true "客户ID"
  777. // @Success 200 string "导入成功"
  778. // @router /email/import [post]
  779. func (this *EnglishReportEmailController) ImportListMatch() {
  780. br := new(models.BaseResponse).Init()
  781. defer func() {
  782. this.Data["json"] = br
  783. this.ServeJSON()
  784. }()
  785. sysUser := this.SysUser
  786. if sysUser == nil {
  787. br.Msg = "请重新登录"
  788. return
  789. }
  790. companyId, _ := this.GetInt("CompanyId")
  791. if companyId <= 0 {
  792. br.Msg = "请选择客户"
  793. return
  794. }
  795. companyItem, e := models.GetEnglishCompanyById(companyId)
  796. if e != nil {
  797. if e.Error() == utils.ErrNoRow() {
  798. br.Msg = "客户已被删除, 请刷新页面"
  799. return
  800. }
  801. br.Msg = "操作失败"
  802. br.ErrMsg = "获取客户信息失败, Err: " + e.Error()
  803. return
  804. }
  805. file, h, err := this.GetFile("File")
  806. if err != nil {
  807. br.Msg = "获取文件失败"
  808. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  809. return
  810. }
  811. defer func() {
  812. _ = file.Close()
  813. }()
  814. ext := path.Ext(h.Filename)
  815. if ext != ".xlsx" && ext != ".xls" {
  816. br.Msg = "请上传Excel文件"
  817. return
  818. }
  819. uploadDir := "static/xls"
  820. err = os.MkdirAll(uploadDir, 766)
  821. if err != nil {
  822. br.Msg = "存储目录创建失败"
  823. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  824. return
  825. }
  826. uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  827. err = this.SaveToFile("File", uploadPath)
  828. if err != nil {
  829. br.Msg = "文件保存失败"
  830. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  831. return
  832. }
  833. defer func() {
  834. _ = os.Remove(uploadPath)
  835. }()
  836. xlFile, err := xlsx.OpenFile(uploadPath)
  837. if err != nil {
  838. br.Msg = "文件导入失败"
  839. br.ErrMsg = "文件导入失败,Err:" + err.Error()
  840. return
  841. }
  842. // 重复邮箱校验
  843. var cond string
  844. var pars []interface{}
  845. emails, e := models.GetEnglishReportEmailList(cond, pars, "")
  846. if e != nil {
  847. br.Msg = "导入失败"
  848. br.ErrMsg = "获取邮箱列表失败, Err:" + e.Error()
  849. return
  850. }
  851. emailArr := make([]string, 0)
  852. for i := range emails {
  853. emailArr = append(emailArr, emails[i].Email)
  854. }
  855. importList := make([]*models.EnglishReportEmailSaveReq, 0)
  856. // 遍历sheet页读取
  857. titleRow := []string{"联系人姓名", "国际区号", "手机号", "邮箱地址"}
  858. for _, sheet := range xlFile.Sheets {
  859. // 遍历行读取
  860. maxRow := sheet.MaxRow
  861. for i := 0; i < maxRow; i++ {
  862. // 首行
  863. if i == 0 {
  864. row := sheet.Row(i)
  865. cells := row.Cells
  866. for k, cell := range cells {
  867. text := cell.String()
  868. for k2 := range titleRow {
  869. if k > k2 {
  870. break
  871. }
  872. if text != titleRow[k] && !strings.Contains(text, titleRow[k]) {
  873. br.Msg = fmt.Sprintf("匹配失败, 请按模板导入(%s)", titleRow[k])
  874. br.ErrMsg = "模板格式错误,title:" + text
  875. return
  876. }
  877. }
  878. }
  879. }
  880. // 数据行
  881. if i >= 1 {
  882. row := sheet.Row(i)
  883. cells := row.Cells
  884. var userName, userCountryCode, userMobile, userEmail string
  885. for k, cell := range cells {
  886. v := utils.TrimStr(cell.String())
  887. if k == 0 {
  888. userName = v
  889. continue
  890. }
  891. if k == 1 {
  892. userCountryCode = v
  893. continue
  894. }
  895. if k == 2 {
  896. userMobile = v
  897. continue
  898. }
  899. if k == 3 {
  900. userEmail = v
  901. }
  902. }
  903. // 数据为空则读取下一行
  904. if userName == "" || userEmail == "" {
  905. continue
  906. }
  907. if !utils.ValidateEmailFormatat(userEmail) {
  908. br.Msg = "存在错误的邮箱格式, 请检查后重新导入"
  909. return
  910. }
  911. // 重复校验
  912. if utils.InArrayByStr(emailArr, userEmail) {
  913. continue
  914. }
  915. emailArr = append(emailArr, userEmail)
  916. importList = append(importList, &models.EnglishReportEmailSaveReq{
  917. CompanyId: companyId,
  918. Name: userName,
  919. Email: userEmail,
  920. Mobile: userMobile,
  921. CountryCode: userCountryCode,
  922. Enabled: 1,
  923. })
  924. }
  925. }
  926. }
  927. if len(importList) == 0 {
  928. br.Msg = "无有效数据可导入"
  929. return
  930. }
  931. nowTime := time.Now().Local()
  932. newEmails := make([]*models.EnglishReportEmail, 0)
  933. newLogs := make([]*models.EnglishReportEmailOpLog, 0)
  934. for i := range importList {
  935. v := &models.EnglishReportEmail{
  936. CompanyId: importList[i].CompanyId,
  937. CompanyName: companyItem.CompanyName,
  938. Name: importList[i].Name,
  939. Email: importList[i].Email,
  940. Mobile: importList[i].Mobile,
  941. CountryCode: importList[i].CountryCode,
  942. Enabled: importList[i].Enabled,
  943. AdminId: sysUser.AdminId,
  944. AdminName: sysUser.AdminName,
  945. Status: 1,
  946. CreateTime: nowTime,
  947. ModifyTime: nowTime,
  948. }
  949. newEmails = append(newEmails, v)
  950. newByte, e := json.Marshal(importList[i])
  951. if e != nil {
  952. br.Msg = "导入失败"
  953. br.ErrMsg = "新邮箱/联系转JSON失败, Err: " + e.Error()
  954. return
  955. }
  956. newLogs = append(newLogs, &models.EnglishReportEmailOpLog{
  957. CompanyId: importList[i].CompanyId,
  958. AdminId: sysUser.AdminId,
  959. OptionData: string(newByte),
  960. OptionType: models.EnglishReportEmailOpLogTypeImport,
  961. CreateTime: nowTime,
  962. })
  963. }
  964. if e = models.MultiCreateEnglishEmail(newEmails, newLogs); e != nil {
  965. br.Msg = "导入失败"
  966. br.ErrMsg = "批量新增英文邮箱/联系人失败, Err: " + e.Error()
  967. return
  968. }
  969. br.Msg = "导入成功"
  970. br.Ret = 200
  971. br.Success = true
  972. }
  973. // ViewList
  974. // @Title 英文研报邮箱/联系人阅读列表
  975. // @Description 英文研报邮箱/联系人阅读列表
  976. // @Param EmailId query int false "英文邮箱/联系人ID"
  977. // @Param SortParam query int false "排序字段:1-点击量;2-点击时间"
  978. // @Param SortType query int false "排序方式:1-倒序;2-正序"
  979. // @Param EnPermissionIds query string false "品种权限IDs(字符串)"
  980. // @Success 200 {object} models.EnglishEmailViewPageListResp
  981. // @router /email/view_list [get]
  982. func (this *EnglishReportEmailController) ViewList() {
  983. br := new(models.BaseResponse).Init()
  984. br.IsSendEmail = false
  985. defer func() {
  986. this.Data["json"] = br
  987. this.ServeJSON()
  988. }()
  989. sysUser := this.SysUser
  990. if sysUser == nil {
  991. br.Msg = "请登录"
  992. br.ErrMsg = "请登录,SysUser Is Empty"
  993. br.Ret = 408
  994. return
  995. }
  996. emailId, e := this.GetInt("EmailId", 0)
  997. if emailId <= 0 {
  998. br.Msg = "参数有误"
  999. return
  1000. }
  1001. // 默认点击时间降序
  1002. sortParam, _ := this.GetInt("SortParam", 2)
  1003. sortType, _ := this.GetInt("SortType", 1)
  1004. clickType := this.GetString("ClickType", "")
  1005. strPermissionIds := this.GetString("EnPermissionIds", "")
  1006. var cond, order string
  1007. var pars []interface{}
  1008. cond += ` AND email_id = ? `
  1009. pars = append(pars, emailId)
  1010. if clickType != "" {
  1011. cond += ` AND report_type = ? `
  1012. pars = append(pars, clickType)
  1013. }
  1014. // 品种筛选
  1015. if strPermissionIds != "" {
  1016. permissionIdArr := strings.Split(strPermissionIds, ",")
  1017. permissionIds := make([]int, 0)
  1018. for _, s := range permissionIdArr {
  1019. p, e := strconv.Atoi(s)
  1020. if e != nil {
  1021. br.Msg = "品种权限有误"
  1022. br.ErrMsg = "品种权限筛选有误"
  1023. return
  1024. }
  1025. permissionIds = append(permissionIds, p)
  1026. }
  1027. if len(permissionIds) == 0 {
  1028. br.Msg = "品种权限有误"
  1029. br.ErrMsg = "品种权限筛选ID为空"
  1030. return
  1031. }
  1032. reportIds, e := models.GetEnglishReportIdsByEnPermissionIds(permissionIds)
  1033. if e != nil {
  1034. br.Msg = "获取失败"
  1035. br.ErrMsg = "获取报告IDs失败, Err: " + e.Error()
  1036. return
  1037. }
  1038. if len(reportIds) > 0 {
  1039. cond += fmt.Sprintf(` AND report_id IN (%s) `, utils.GetOrmInReplace(len(reportIds)))
  1040. pars = append(pars, reportIds)
  1041. } else {
  1042. cond += ` AND 1=2`
  1043. }
  1044. }
  1045. var startSize int
  1046. pageSize, _ := this.GetInt("PageSize")
  1047. currentIndex, _ := this.GetInt("CurrentIndex")
  1048. if pageSize <= 0 {
  1049. pageSize = utils.PageSize20
  1050. }
  1051. if currentIndex <= 0 {
  1052. currentIndex = 1
  1053. }
  1054. startSize = paging.StartIndex(currentIndex, pageSize)
  1055. sortArr := []int{1, 2}
  1056. if utils.InArrayByInt(sortArr, sortParam) && utils.InArrayByInt(sortArr, sortType) {
  1057. pMap := map[int]string{1: "view_total", 2: "last_view_time"}
  1058. tMap := map[int]string{1: "DESC", 2: "ASC"}
  1059. order = fmt.Sprintf(` ORDER BY %s %s`, pMap[sortParam], tMap[sortType])
  1060. }
  1061. total, list, e := models.GetEnglishEmailViewPageList(cond, pars, order, startSize, pageSize)
  1062. if e != nil {
  1063. br.Msg = "获取失败"
  1064. br.ErrMsg = "英文研报邮箱/联系人阅读列表失败, Err: " + e.Error()
  1065. return
  1066. }
  1067. // 报告标题,报告类型
  1068. reportMap := make(map[int]*models.EnglishReport, 0)
  1069. videoMap := make(map[int]*models.EnglishVideo, 0)
  1070. if len(list) > 0 {
  1071. reportIds := make([]int, 0)
  1072. videoIds := make([]int, 0)
  1073. for i := range list {
  1074. if list[i].ClickType == 0 {
  1075. reportIds = append(reportIds, list[i].ReportId)
  1076. } else if list[i].ClickType == 1 {
  1077. videoIds = append(videoIds, list[i].ReportId)
  1078. }
  1079. }
  1080. fields := []string{"id", "title", "classify_name_first", "classify_name_second"}
  1081. if len(reportIds) > 0 {
  1082. reports, e := models.GetEnglishReportByIds(reportIds, fields)
  1083. if e != nil {
  1084. br.Msg = "获取失败"
  1085. br.ErrMsg = "获取英文研报列表失败, Err: " + e.Error()
  1086. return
  1087. }
  1088. for i := range reports {
  1089. reportMap[reports[i].Id] = reports[i]
  1090. }
  1091. }
  1092. if len(videoIds) > 0 {
  1093. videos, e := models.GetEnglishVideoByIds(videoIds, fields)
  1094. if e != nil {
  1095. br.Msg = "获取失败"
  1096. br.ErrMsg = "获取英文研报列表失败, Err: " + e.Error()
  1097. return
  1098. }
  1099. for i := range videos {
  1100. videoMap[videos[i].Id] = videos[i]
  1101. }
  1102. }
  1103. }
  1104. for i := range list {
  1105. if r, ok := reportMap[list[i].ReportId]; ok {
  1106. list[i].ReportTitle = r.Title
  1107. list[i].ReportType = r.ClassifyNameFirst
  1108. if r.ClassifyNameSecond != "" {
  1109. list[i].ReportType += ` / ` + r.ClassifyNameSecond
  1110. }
  1111. } else if v, ok1 := videoMap[list[i].ReportId]; ok1 {
  1112. list[i].ReportTitle = v.Title
  1113. list[i].ReportType = v.ClassifyNameFirst
  1114. if v.ClassifyNameSecond != "" {
  1115. list[i].ReportType += ` / ` + v.ClassifyNameSecond
  1116. }
  1117. }
  1118. }
  1119. page := paging.GetPaging(currentIndex, pageSize, total)
  1120. resp := &models.EnglishEmailViewPageListResp{
  1121. Paging: page,
  1122. List: list,
  1123. }
  1124. br.Ret = 200
  1125. br.Success = true
  1126. br.Msg = "获取成功"
  1127. br.Data = resp
  1128. }
  1129. // LogList
  1130. // @Title 邮件群发日志列表
  1131. // @Description 邮件群发日志列表
  1132. // @Param ReportId query int true "报告ID"
  1133. // @Param ReportType query int true "类型:0英文研报,1英文线上路演"
  1134. // @Param SendStatus query int false "发送状态:-1-已发送;0-发送失败;1-发送成功"
  1135. // @Param Keywords query string false "关键词:邮箱"
  1136. // @Success 200 {object} models.EnglishReportEmailLogPageListResp
  1137. // @router /email/log_list [get]
  1138. func (this *EnglishReportEmailController) LogList() {
  1139. br := new(models.BaseResponse).Init()
  1140. br.IsSendEmail = false
  1141. defer func() {
  1142. this.Data["json"] = br
  1143. this.ServeJSON()
  1144. }()
  1145. sysUser := this.SysUser
  1146. if sysUser == nil {
  1147. br.Msg = "请登录"
  1148. br.ErrMsg = "请登录,SysUser Is Empty"
  1149. br.Ret = 408
  1150. return
  1151. }
  1152. reportId, _ := this.GetInt("ReportId", 0)
  1153. if reportId <= 0 {
  1154. br.Msg = "参数有误"
  1155. return
  1156. }
  1157. reportType, _ := this.GetInt("ReportType", 0)
  1158. keywords := this.GetString("Keywords", "")
  1159. var startSize int
  1160. pageSize, _ := this.GetInt("PageSize")
  1161. currentIndex, _ := this.GetInt("CurrentIndex")
  1162. if pageSize <= 0 {
  1163. pageSize = utils.PageSize20
  1164. }
  1165. if currentIndex <= 0 {
  1166. currentIndex = 1
  1167. }
  1168. startSize = paging.StartIndex(currentIndex, pageSize)
  1169. var cond string
  1170. var pars []interface{}
  1171. cond += ` AND report_id = ? AND report_type = ?`
  1172. pars = append(pars, reportId, reportType)
  1173. // 推送状态
  1174. status := this.GetString("SendStatus", "")
  1175. if status != "" {
  1176. statusInt, e := strconv.Atoi(status)
  1177. if e != nil {
  1178. br.Msg = "状态类型有误"
  1179. return
  1180. }
  1181. cond += ` AND send_status = ?`
  1182. pars = append(pars, statusInt)
  1183. }
  1184. // 关键词
  1185. if keywords != "" {
  1186. k := fmt.Sprint("%", keywords, "%")
  1187. cond += ` AND email LIKE ? `
  1188. pars = append(pars, k)
  1189. }
  1190. total, list, e := models.GetEnglishReportEmailLogPageList(cond, pars, "", startSize, pageSize)
  1191. if e != nil {
  1192. br.Msg = "获取失败"
  1193. br.ErrMsg = "获取邮箱列表失败, Err: " + e.Error()
  1194. return
  1195. }
  1196. respList := make([]*models.EnglishReportEmailLogPageList, 0)
  1197. for i := range list {
  1198. v := new(models.EnglishReportEmailLogPageList)
  1199. // 推送结果
  1200. v.ResultMsg = "发送成功"
  1201. if list[i].ErrMsg != "" {
  1202. v.ResultMsg = list[i].ErrMsg
  1203. }
  1204. v.SendId = list[i].Id
  1205. v.ReportId = list[i].ReportId
  1206. v.EmailId = list[i].EmailId
  1207. v.Email = list[i].Email
  1208. v.SendStatus = list[i].SendStatus
  1209. v.Source = list[i].Source
  1210. v.CreateTime = list[i].CreateTime.Format(utils.FormatDateTime)
  1211. respList = append(respList, v)
  1212. }
  1213. page := paging.GetPaging(currentIndex, pageSize, total)
  1214. resp := &models.EnglishReportEmailLogPageListResp{
  1215. Paging: page,
  1216. List: respList,
  1217. }
  1218. br.Ret = 200
  1219. br.Success = true
  1220. br.Msg = "获取成功"
  1221. br.Data = resp
  1222. }
  1223. // Resend
  1224. // @Title 重新推送
  1225. // @Description 群发推送
  1226. // @Param request body models.EnglishReportEmailResendReq true "type json string"
  1227. // @Success 200 string "操作成功"
  1228. // @router /email/resend [post]
  1229. func (this *EnglishReportEmailController) Resend() {
  1230. br := new(models.BaseResponse).Init()
  1231. br.IsSendEmail = false
  1232. defer func() {
  1233. this.Data["json"] = br
  1234. this.ServeJSON()
  1235. }()
  1236. sysUser := this.SysUser
  1237. if sysUser == nil {
  1238. br.Msg = "请登录"
  1239. br.ErrMsg = "请登录,SysUser Is Empty"
  1240. br.Ret = 408
  1241. return
  1242. }
  1243. var req models.EnglishReportEmailResendReq
  1244. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  1245. br.Msg = "参数解析异常!"
  1246. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1247. return
  1248. }
  1249. if req.ReportId <= 0 {
  1250. br.Msg = "请选择报告"
  1251. return
  1252. }
  1253. // 获取报告信息
  1254. reportInfo, e := models.GetEnglishReportItemById(req.ReportId)
  1255. if e != nil {
  1256. br.Msg = "报告信息有误"
  1257. br.ErrMsg = "获取报告信息失败, Err: " + e.Error()
  1258. return
  1259. }
  1260. if reportInfo.State != 2 {
  1261. br.Msg = "报告未发布"
  1262. br.ErrMsg = fmt.Sprintf("报告状态有误, 当前状态: %d", reportInfo.State)
  1263. return
  1264. }
  1265. if reportInfo.Title == "" {
  1266. br.Msg = "报告标题有误"
  1267. br.ErrMsg = "报告标题为空, 不可推送"
  1268. return
  1269. }
  1270. if reportInfo.Content == `` {
  1271. br.Msg = "报告内容有误"
  1272. br.ErrMsg = "报告内容为空, 不可推送"
  1273. return
  1274. }
  1275. if reportInfo.Abstract == `` {
  1276. br.Msg = "报告摘要为空"
  1277. br.ErrMsg = "报告摘要为空, 不可推送"
  1278. return
  1279. }
  1280. if reportInfo.Overview == "" {
  1281. br.Msg = "报告Overview部分不可为空"
  1282. br.ErrMsg = "报告Overview为空, 不可推送"
  1283. return
  1284. }
  1285. // 重新发送超过100字符直接截取
  1286. theme := reportInfo.Abstract
  1287. runeAbs := []rune(reportInfo.Abstract)
  1288. runeThe := make([]rune, 0)
  1289. if len(runeAbs) > 100 {
  1290. runeThe = runeAbs[:100]
  1291. theme = string(runeThe)
  1292. }
  1293. // 获取邮件配置
  1294. conf := new(models.EnglishReportEmailConf)
  1295. authKey := "english_report_email_conf"
  1296. confAuth, e := company.GetConfigDetailByCode(authKey)
  1297. if e != nil {
  1298. br.Msg = "无权操作"
  1299. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  1300. return
  1301. }
  1302. if confAuth.ConfigValue == "" {
  1303. br.Msg = "无权操作"
  1304. br.ErrMsg = "群发邮件配置为空"
  1305. return
  1306. }
  1307. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  1308. br.Msg = "无权操作"
  1309. br.ErrMsg = "群发邮件配置有误"
  1310. return
  1311. }
  1312. authArr := strings.Split(conf.SendAuthGroup, ",")
  1313. if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  1314. br.Msg = "无权操作"
  1315. return
  1316. }
  1317. // 获取模板
  1318. confKey := "english_report_email_tmp"
  1319. confTmp, e := company.GetConfigDetailByCode(confKey)
  1320. if e != nil {
  1321. br.Msg = "获取邮件模板失败"
  1322. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  1323. return
  1324. }
  1325. if confTmp.ConfigValue == `` {
  1326. br.Msg = "邮件模板有误"
  1327. br.ErrMsg = "邮件模板为空, 不可推送"
  1328. return
  1329. }
  1330. // 获取发送失败的日志
  1331. emailIds := make([]int, 0)
  1332. emails := make([]*models.EnglishReportEmail, 0)
  1333. logIds := make([]int, 0)
  1334. if req.SendId > 0 {
  1335. logItem, e := models.GetEnglishReportEmailLogById(req.SendId)
  1336. if e != nil {
  1337. br.Msg = "发送失败"
  1338. br.ErrMsg = "获取原邮件推送日志失败, Err: " + e.Error()
  1339. return
  1340. }
  1341. emailIds = append(emailIds, logItem.EmailId)
  1342. logIds = append(logIds, logItem.Id)
  1343. } else {
  1344. var logCond string
  1345. var logPars []interface{}
  1346. logCond += ` AND report_id = ? AND send_status = 0 AND report_type=0`
  1347. logPars = append(logPars, req.ReportId)
  1348. logList, e := models.GetEnglishReportEmailLogList(logCond, logPars)
  1349. if e != nil {
  1350. br.Msg = "发送失败"
  1351. br.ErrMsg = "获取原邮件推送日志列表失败, Err: " + e.Error()
  1352. return
  1353. }
  1354. for i := range logList {
  1355. emailIds = append(emailIds, logList[i].EmailId)
  1356. logIds = append(logIds, logList[i].Id)
  1357. }
  1358. }
  1359. // 获取收件人列表
  1360. // 注: 此处不从失败日志中取email, 因为有可能是地址有误导致的推送失败
  1361. if len(emailIds) > 0 {
  1362. var emailCond string
  1363. var emailPars []interface{}
  1364. emailCond += ` AND id IN (` + utils.GetOrmInReplace(len(emailIds)) + `) and enabled = 1 `
  1365. emailPars = append(emailPars, emailIds)
  1366. em, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  1367. if e != nil {
  1368. br.Msg = "获取收件人列表失败"
  1369. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  1370. return
  1371. }
  1372. emails = em
  1373. }
  1374. if len(emails) == 0 {
  1375. br.Msg = "无邮件可推送"
  1376. return
  1377. }
  1378. // 推送信息
  1379. nowTime := time.Now().Local()
  1380. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  1381. logData := make([]*models.EnglishReportEmailLog, 0)
  1382. for i := range emails {
  1383. r := new(services.EnglishReportSendEmailRequest)
  1384. r.ReportId = reportInfo.Id
  1385. r.EmailId = emails[i].Id
  1386. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  1387. r.Subject = theme
  1388. r.FromAlias = conf.FromAlias
  1389. // 填充模板
  1390. t := reportInfo.PublishTime.Format("02/01/2006")
  1391. l := fmt.Sprintf(utils.EnglishShareUrl+"/report/detail?code=%s", reportInfo.ReportCode)
  1392. ct := confTmp.ConfigValue
  1393. ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
  1394. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
  1395. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", reportInfo.Overview, 1)
  1396. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  1397. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  1398. r.HtmlBody = ct
  1399. r.ReportTitle = reportInfo.Title
  1400. r.ReportAbstract = reportInfo.Abstract
  1401. r.ReportContent = reportInfo.Overview
  1402. r.ReportShareLink = l
  1403. r.ReportTime = t
  1404. sendData = append(sendData, r)
  1405. // 日志
  1406. logData = append(logData, &models.EnglishReportEmailLog{
  1407. ReportId: reportInfo.Id,
  1408. EmailId: emails[i].Id,
  1409. Email: emails[i].Email,
  1410. Source: models.EnglishReportEmailLogSourceAli,
  1411. SendStatus: models.EnglishReportEmailLogStatusIng,
  1412. CreateTime: nowTime,
  1413. })
  1414. }
  1415. // 标记原有日志为已删除
  1416. if len(logIds) > 0 {
  1417. if e = models.DeleteEnglishReportEmailLogByIds(logIds); e != nil {
  1418. br.Msg = "发送失败"
  1419. br.ErrMsg = "删除原邮件日志失败, Err: " + e.Error()
  1420. return
  1421. }
  1422. }
  1423. // 写入新的日志
  1424. emailLog := new(models.EnglishReportEmailLog)
  1425. if e = emailLog.InsertMulti(logData); e != nil {
  1426. br.Msg = "操作失败"
  1427. br.ErrMsg = "批量写入群发邮件日志失败, Err: " + e.Error()
  1428. return
  1429. }
  1430. // 群发邮件
  1431. go func() {
  1432. services.BatchSendAliEnglishReportEmail(sendData)
  1433. }()
  1434. br.Ret = 200
  1435. br.Success = true
  1436. br.Msg = "操作成功"
  1437. }
  1438. // SendCallBack
  1439. // @Title 群发邮件回调
  1440. // @Description 群发邮件回调
  1441. // @Param request body services.TencentEmailCallBack true "type json string"
  1442. // @Success 200 string "操作成功"
  1443. // @router /email/send/call_back [post]
  1444. func (this *EnglishReportEmailCallBackController) SendCallBack() {
  1445. // 腾讯云后台同一个发信地址只能配置一个回调, 测试环境就不用了, 给正式环境用
  1446. if utils.RunMode != "release" {
  1447. return
  1448. }
  1449. var req services.TencentEmailCallBack
  1450. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  1451. return
  1452. }
  1453. // 仅预警失败的回调事件
  1454. alarmTypes := []string{"dropped", "bounce"}
  1455. if !utils.InArrayByStr(alarmTypes, req.Event) {
  1456. return
  1457. }
  1458. reqByte, e := json.Marshal(req)
  1459. if e != nil {
  1460. return
  1461. }
  1462. reqStr := string(reqByte)
  1463. alarm_msg.SendAlarmMsg("腾讯云邮件推送回调信息:"+reqStr, 1)
  1464. // 存在失败情况则更新日志
  1465. var cond string
  1466. var pars []interface{}
  1467. cond = ` AND is_deleted = 0 AND email = ? AND source = ? AND result LIKE ? `
  1468. pars = append(pars, req.Email, models.EnglishReportEmailLogSourceTencent, fmt.Sprint("%", req.BulkId, "%"))
  1469. logItem, e := models.GetEnglishReportEmailLog(cond, pars)
  1470. if e != nil {
  1471. return
  1472. }
  1473. updateCols := []string{"SendStatus", "CallbackData", "ErrMsg"}
  1474. logItem.SendStatus = models.EnglishReportEmailLogStatusFail
  1475. logItem.CallbackData = reqStr
  1476. logItem.ErrMsg = req.Reason
  1477. if e = logItem.Update(updateCols); e != nil {
  1478. return
  1479. }
  1480. }
  1481. // VideoSend
  1482. // @Title 线上路演群发推送
  1483. // @Description 线上路演群发推送
  1484. // @Param request body models.EnglishReportEmailSaveReq true "type json string"
  1485. // @Success 200 string "操作成功"
  1486. // @router /email/video/send [post]
  1487. func (this *EnglishReportEmailController) VideoSend() {
  1488. br := new(models.BaseResponse).Init()
  1489. br.IsSendEmail = false
  1490. defer func() {
  1491. this.Data["json"] = br
  1492. this.ServeJSON()
  1493. }()
  1494. sysUser := this.SysUser
  1495. if sysUser == nil {
  1496. br.Msg = "请登录"
  1497. br.ErrMsg = "请登录,SysUser Is Empty"
  1498. br.Ret = 408
  1499. return
  1500. }
  1501. var req models.EnglishReportEmailSendReq
  1502. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  1503. br.Msg = "参数解析异常!"
  1504. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1505. return
  1506. }
  1507. if req.ReportId <= 0 {
  1508. br.Msg = "请选择路演视频"
  1509. return
  1510. }
  1511. if req.Theme == "" {
  1512. br.Msg = "请输入邮件主题"
  1513. return
  1514. }
  1515. //prefixTheme := "【HI China Macro and Commodity】"
  1516. //if !strings.Contains(req.Theme, prefixTheme) {
  1517. // req.Theme = prefixTheme + req.Theme
  1518. //}
  1519. runeThe := []rune(req.Theme)
  1520. if len(runeThe) > 100 {
  1521. br.Msg = "邮件主题不可超过100字符"
  1522. return
  1523. }
  1524. // 获取路演视频信息
  1525. videoInfo, e := models.GetEnglishVideoItemById(req.ReportId)
  1526. if e != nil {
  1527. br.Msg = "路演视频信息有误"
  1528. br.ErrMsg = "获取路演视频信息失败, Err: " + e.Error()
  1529. return
  1530. }
  1531. if videoInfo.EmailState != 0 {
  1532. br.Msg = "请勿重复发送"
  1533. br.ErrMsg = fmt.Sprintf("路演视频群发邮件状态有误, 当前状态: %d", videoInfo.EmailState)
  1534. return
  1535. }
  1536. if videoInfo.State != 2 {
  1537. br.Msg = "路演视频未发布"
  1538. br.ErrMsg = fmt.Sprintf("路演视频状态有误, 当前状态: %d", videoInfo.State)
  1539. return
  1540. }
  1541. if videoInfo.Title == "" {
  1542. br.Msg = "路演视频标题有误"
  1543. br.ErrMsg = "路演视频标题为空, 不可推送"
  1544. return
  1545. }
  1546. if videoInfo.VideoUrl == `` {
  1547. br.Msg = "路演视频地址为空, 不可推送"
  1548. br.ErrMsg = "路演视频内容为空, 不可推送"
  1549. return
  1550. }
  1551. // 获取路演视频Overview部分
  1552. if videoInfo.Overview == "" {
  1553. br.Msg = "路演视频Overview部分不可为空"
  1554. return
  1555. }
  1556. // 获取邮件配置
  1557. conf := new(models.EnglishReportEmailConf)
  1558. authKey := "english_report_email_conf"
  1559. confAuth, e := company.GetConfigDetailByCode(authKey)
  1560. if e != nil {
  1561. br.Msg = "无权操作"
  1562. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  1563. return
  1564. }
  1565. if confAuth.ConfigValue == "" {
  1566. br.Msg = "无权操作"
  1567. br.ErrMsg = "群发邮件配置为空"
  1568. return
  1569. }
  1570. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  1571. br.Msg = "无权操作"
  1572. br.ErrMsg = "群发邮件配置有误"
  1573. return
  1574. }
  1575. authArr := strings.Split(conf.SendAuthGroup, ",")
  1576. if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  1577. br.Msg = "无权操作"
  1578. return
  1579. }
  1580. // 指定品种的客户
  1581. sendCompanyIds := make([]int, 0)
  1582. if len(req.EnPermissions) > 0 {
  1583. companyIds, e := models.GetEnglishCompanyIdsByEnPermissionIds(req.EnPermissions)
  1584. if e != nil {
  1585. br.Msg = "推送失败"
  1586. br.ErrMsg = "获取指定品种的客户IDs失败, Err: " + e.Error()
  1587. return
  1588. }
  1589. sendCompanyIds = companyIds
  1590. }
  1591. // 指定收件人列表
  1592. sendEmailIds := make([]int, 0)
  1593. if req.EmailIds != "" {
  1594. emailIdArr := strings.Split(req.EmailIds, ",")
  1595. if len(emailIdArr) == 0 {
  1596. br.Msg = "指定收件人列表有误"
  1597. br.ErrMsg = fmt.Sprintf("收件人列表为空, 收件人IDs: %s", req.EmailIds)
  1598. return
  1599. }
  1600. for _, m := range emailIdArr {
  1601. v, _ := strconv.Atoi(m)
  1602. sendEmailIds = append(sendEmailIds, v)
  1603. }
  1604. }
  1605. if len(sendCompanyIds) == 0 && len(sendEmailIds) == 0 {
  1606. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  1607. br.ErrMsg = "收件人列表为空, 不可推送"
  1608. return
  1609. }
  1610. // 往正式客户id里面插入0的 目的是为了把临时客户找出来
  1611. sendCompanyIds = append(sendCompanyIds, 0)
  1612. emailCond := " AND enabled = 1 AND status <> 3"
  1613. emailPars := make([]interface{}, 0)
  1614. if len(sendCompanyIds) > 0 && len(sendEmailIds) > 0 {
  1615. emailCond += ` AND (company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `) OR id IN (` + utils.GetOrmInReplace(len(sendEmailIds)) + `))`
  1616. emailPars = append(emailPars, sendCompanyIds)
  1617. emailPars = append(emailPars, sendEmailIds)
  1618. }
  1619. if len(sendCompanyIds) > 0 && len(sendEmailIds) == 0 {
  1620. emailCond += ` AND company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `)`
  1621. emailPars = append(emailPars, sendCompanyIds)
  1622. }
  1623. emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  1624. if e != nil {
  1625. br.Msg = "获取收件人列表失败"
  1626. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  1627. return
  1628. }
  1629. if len(emails) == 0 {
  1630. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  1631. br.ErrMsg = "收件人列表为空, 不可推送"
  1632. return
  1633. }
  1634. // 获取模板
  1635. confKey := "english_video_email_tmp"
  1636. confTmp, e := company.GetConfigDetailByCode(confKey)
  1637. if e != nil {
  1638. br.Msg = "获取邮件模板失败"
  1639. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  1640. return
  1641. }
  1642. if confTmp.ConfigValue == `` {
  1643. br.Msg = "邮件模板有误"
  1644. br.ErrMsg = "邮件模板为空, 不可推送"
  1645. return
  1646. }
  1647. // 同一篇路演视频避免重复推送给同一个邮件
  1648. var cond string
  1649. var pars []interface{}
  1650. cond += ` AND report_id = ? AND send_status = 1 AND report_type = 1`
  1651. pars = append(pars, req.ReportId)
  1652. logs, e := models.GetEnglishReportEmailLogList(cond, pars)
  1653. if e != nil {
  1654. br.Msg = "推送失败"
  1655. br.ErrMsg = "获取邮件发送日志列表失败, Err: " + e.Error()
  1656. return
  1657. }
  1658. repeatMap := make(map[int]bool, 0)
  1659. for i := range logs {
  1660. if logs[i].SendStatus == 1 {
  1661. repeatMap[logs[i].EmailId] = true
  1662. }
  1663. }
  1664. // 推送信息
  1665. nowTime := time.Now().Local()
  1666. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  1667. logData := make([]*models.EnglishReportEmailLog, 0)
  1668. for i := range emails {
  1669. if repeatMap[emails[i].Id] {
  1670. continue
  1671. }
  1672. r := new(services.EnglishReportSendEmailRequest)
  1673. r.ReportId = videoInfo.Id
  1674. r.EmailId = emails[i].Id
  1675. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  1676. r.Subject = req.Theme
  1677. r.FromAlias = conf.FromAlias
  1678. overview := videoInfo.Overview
  1679. // 填充模板
  1680. t := videoInfo.PublishTime.Format("02/01/2006")
  1681. l := fmt.Sprintf(utils.EnglishShareUrl+"/roadshow/detail?code=%s&ShareEmail=%d", videoInfo.VideoCode, emails[i].Id)
  1682. ct := confTmp.ConfigValue
  1683. ct = strings.Replace(ct, "{{REPORT_TITLE}}", videoInfo.Title, 1)
  1684. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", videoInfo.Abstract, 1)
  1685. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", overview, 1)
  1686. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  1687. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  1688. r.HtmlBody = ct
  1689. r.ReportTitle = videoInfo.Title
  1690. r.ReportAbstract = videoInfo.Abstract
  1691. r.ReportContent = overview
  1692. r.ReportShareLink = l
  1693. r.ReportTime = t
  1694. sendData = append(sendData, r)
  1695. // 日志
  1696. logData = append(logData, &models.EnglishReportEmailLog{
  1697. ReportId: videoInfo.Id,
  1698. ReportType: 1,
  1699. EmailId: emails[i].Id,
  1700. Email: emails[i].Email,
  1701. Source: models.EnglishReportEmailLogSourceAli,
  1702. SendStatus: models.EnglishReportEmailLogStatusIng,
  1703. CreateTime: nowTime,
  1704. })
  1705. }
  1706. if len(sendData) == 0 {
  1707. br.Msg = "无邮件可推送"
  1708. return
  1709. }
  1710. // 写入日志
  1711. emailLog := new(models.EnglishReportEmailLog)
  1712. if e = emailLog.InsertMulti(logData); e != nil {
  1713. br.Msg = "操作失败"
  1714. br.ErrMsg = "批量写入群发邮件日志失败, Err: " + e.Error()
  1715. return
  1716. }
  1717. // 修改推送状态
  1718. updateCols := []string{"EmailState", "ModifyTime"}
  1719. videoInfo.EmailState = 1
  1720. videoInfo.ModifyTime = time.Now().Local()
  1721. if e = videoInfo.Update(updateCols); e != nil {
  1722. br.Msg = "操作失败"
  1723. br.ErrMsg = "更新推送状态失败, Err: " + e.Error()
  1724. return
  1725. }
  1726. // 群发邮件
  1727. go func() {
  1728. services.BatchSendAliEnglishReportEmail(sendData)
  1729. }()
  1730. br.Ret = 200
  1731. br.Success = true
  1732. br.Msg = "操作成功"
  1733. }
  1734. // VideoResend
  1735. // @Title 线上路演重新推送
  1736. // @Description 线上路演重新推送
  1737. // @Param request body models.EnglishReportEmailResendReq true "type json string"
  1738. // @Success 200 string "操作成功"
  1739. // @router /email/video/resend [post]
  1740. func (this *EnglishReportEmailController) VideoResend() {
  1741. br := new(models.BaseResponse).Init()
  1742. br.IsSendEmail = false
  1743. defer func() {
  1744. this.Data["json"] = br
  1745. this.ServeJSON()
  1746. }()
  1747. sysUser := this.SysUser
  1748. if sysUser == nil {
  1749. br.Msg = "请登录"
  1750. br.ErrMsg = "请登录,SysUser Is Empty"
  1751. br.Ret = 408
  1752. return
  1753. }
  1754. var req models.EnglishReportEmailResendReq
  1755. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  1756. br.Msg = "参数解析异常!"
  1757. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1758. return
  1759. }
  1760. if req.ReportId <= 0 {
  1761. br.Msg = "请选择路演视频"
  1762. return
  1763. }
  1764. // 获取路演视频信息
  1765. reportInfo, e := models.GetEnglishVideoItemById(req.ReportId)
  1766. if e != nil {
  1767. br.Msg = "路演视频信息有误"
  1768. br.ErrMsg = "获取路演视频信息失败, Err: " + e.Error()
  1769. return
  1770. }
  1771. if reportInfo.State != 2 {
  1772. br.Msg = "路演视频未发布"
  1773. br.ErrMsg = fmt.Sprintf("路演视频状态有误, 当前状态: %d", reportInfo.State)
  1774. return
  1775. }
  1776. if reportInfo.Title == "" {
  1777. br.Msg = "路演视频标题有误"
  1778. br.ErrMsg = "路演视频标题为空, 不可推送"
  1779. return
  1780. }
  1781. if reportInfo.Abstract == `` {
  1782. br.Msg = "路演视频摘要为空"
  1783. br.ErrMsg = "路演视频摘要为空, 不可推送"
  1784. return
  1785. }
  1786. if reportInfo.Overview == "" {
  1787. br.Msg = "路演视频Overview部分不可为空"
  1788. br.ErrMsg = "路演视频Overview为空, 不可推送"
  1789. return
  1790. }
  1791. // 重新发送超过100字符直接截取
  1792. theme := reportInfo.Abstract
  1793. runeAbs := []rune(reportInfo.Abstract)
  1794. runeThe := make([]rune, 0)
  1795. if len(runeAbs) > 100 {
  1796. runeThe = runeAbs[:100]
  1797. theme = string(runeThe)
  1798. }
  1799. // 获取邮件配置
  1800. conf := new(models.EnglishReportEmailConf)
  1801. authKey := "english_report_email_conf"
  1802. confAuth, e := company.GetConfigDetailByCode(authKey)
  1803. if e != nil {
  1804. br.Msg = "无权操作"
  1805. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  1806. return
  1807. }
  1808. if confAuth.ConfigValue == "" {
  1809. br.Msg = "无权操作"
  1810. br.ErrMsg = "群发邮件配置为空"
  1811. return
  1812. }
  1813. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  1814. br.Msg = "无权操作"
  1815. br.ErrMsg = "群发邮件配置有误"
  1816. return
  1817. }
  1818. authArr := strings.Split(conf.SendAuthGroup, ",")
  1819. if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  1820. br.Msg = "无权操作"
  1821. return
  1822. }
  1823. // 获取模板
  1824. confKey := "english_video_email_tmp"
  1825. confTmp, e := company.GetConfigDetailByCode(confKey)
  1826. if e != nil {
  1827. br.Msg = "获取邮件模板失败"
  1828. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  1829. return
  1830. }
  1831. if confTmp.ConfigValue == `` {
  1832. br.Msg = "邮件模板有误"
  1833. br.ErrMsg = "邮件模板为空, 不可推送"
  1834. return
  1835. }
  1836. // 获取发送失败的日志
  1837. emailIds := make([]int, 0)
  1838. emails := make([]*models.EnglishReportEmail, 0)
  1839. logIds := make([]int, 0)
  1840. if req.SendId > 0 {
  1841. logItem, e := models.GetEnglishReportEmailLogById(req.SendId)
  1842. if e != nil {
  1843. br.Msg = "发送失败"
  1844. br.ErrMsg = "获取原邮件推送日志失败, Err: " + e.Error()
  1845. return
  1846. }
  1847. emailIds = append(emailIds, logItem.EmailId)
  1848. logIds = append(logIds, logItem.Id)
  1849. } else {
  1850. var logCond string
  1851. var logPars []interface{}
  1852. logCond += ` AND report_id = ? AND send_status = 0 AND report_type=1`
  1853. logPars = append(logPars, req.ReportId)
  1854. logList, e := models.GetEnglishReportEmailLogList(logCond, logPars)
  1855. if e != nil {
  1856. br.Msg = "发送失败"
  1857. br.ErrMsg = "获取原邮件推送日志列表失败, Err: " + e.Error()
  1858. return
  1859. }
  1860. for i := range logList {
  1861. emailIds = append(emailIds, logList[i].EmailId)
  1862. logIds = append(logIds, logList[i].Id)
  1863. }
  1864. }
  1865. // 获取收件人列表
  1866. // 注: 此处不从失败日志中取email, 因为有可能是地址有误导致的推送失败
  1867. if len(emailIds) > 0 {
  1868. var emailCond string
  1869. var emailPars []interface{}
  1870. emailCond += ` AND id IN (` + utils.GetOrmInReplace(len(emailIds)) + `) and enabled = 1 `
  1871. emailPars = append(emailPars, emailIds)
  1872. em, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  1873. if e != nil {
  1874. br.Msg = "获取收件人列表失败"
  1875. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  1876. return
  1877. }
  1878. emails = em
  1879. }
  1880. if len(emails) == 0 {
  1881. br.Msg = "无邮件可推送"
  1882. return
  1883. }
  1884. // 推送信息
  1885. nowTime := time.Now().Local()
  1886. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  1887. logData := make([]*models.EnglishReportEmailLog, 0)
  1888. for i := range emails {
  1889. r := new(services.EnglishReportSendEmailRequest)
  1890. r.ReportId = reportInfo.Id
  1891. r.EmailId = emails[i].Id
  1892. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  1893. r.Subject = theme
  1894. r.FromAlias = conf.FromAlias
  1895. // 填充模板
  1896. t := reportInfo.PublishTime.Format("02/01/2006")
  1897. l := fmt.Sprintf(utils.EnglishShareUrl+"/roadshow/detail?code=%s&ShareEmail=%d", reportInfo.VideoCode, emails[i].Id)
  1898. ct := confTmp.ConfigValue
  1899. ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
  1900. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
  1901. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", reportInfo.Overview, 1)
  1902. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  1903. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  1904. r.HtmlBody = ct
  1905. r.ReportTitle = reportInfo.Title
  1906. r.ReportAbstract = reportInfo.Abstract
  1907. r.ReportContent = reportInfo.Overview
  1908. r.ReportShareLink = l
  1909. r.ReportTime = t
  1910. sendData = append(sendData, r)
  1911. // 日志
  1912. logData = append(logData, &models.EnglishReportEmailLog{
  1913. ReportId: reportInfo.Id,
  1914. ReportType: 1,
  1915. EmailId: emails[i].Id,
  1916. Email: emails[i].Email,
  1917. Source: models.EnglishReportEmailLogSourceAli,
  1918. SendStatus: models.EnglishReportEmailLogStatusIng,
  1919. CreateTime: nowTime,
  1920. })
  1921. }
  1922. // 标记原有日志为已删除
  1923. if len(logIds) > 0 {
  1924. if e = models.DeleteEnglishReportEmailLogByIds(logIds); e != nil {
  1925. br.Msg = "发送失败"
  1926. br.ErrMsg = "删除原邮件日志失败, Err: " + e.Error()
  1927. return
  1928. }
  1929. }
  1930. // 写入新的日志
  1931. emailLog := new(models.EnglishReportEmailLog)
  1932. if e = emailLog.InsertMulti(logData); e != nil {
  1933. br.Msg = "操作失败"
  1934. br.ErrMsg = "批量写入群发邮件日志失败, Err: " + e.Error()
  1935. return
  1936. }
  1937. // 群发邮件
  1938. go func() {
  1939. services.BatchSendAliEnglishReportEmail(sendData)
  1940. }()
  1941. br.Ret = 200
  1942. br.Success = true
  1943. br.Msg = "操作成功"
  1944. }
  1945. // EditEnabled
  1946. // @Title 移动至当前客户
  1947. // @Description 移动至当前客户
  1948. // @Param request body models.EnglishReportEditEnabledReq true "type json string"
  1949. // @Success 200 string "操作成功"
  1950. // @router /email/moveToCurrent [post]
  1951. func (this *EnglishReportEmailController) MoveToCurrent() {
  1952. br := new(models.BaseResponse).Init()
  1953. br.IsSendEmail = false
  1954. defer func() {
  1955. this.Data["json"] = br
  1956. this.ServeJSON()
  1957. }()
  1958. sysUser := this.SysUser
  1959. if sysUser == nil {
  1960. br.Msg = "请登录"
  1961. br.ErrMsg = "请登录,SysUser Is Empty"
  1962. br.Ret = 408
  1963. return
  1964. }
  1965. var req models.EnglishReportMoveReq
  1966. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  1967. br.Msg = "参数解析异常!"
  1968. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1969. return
  1970. }
  1971. if req.EmailId <= 0 {
  1972. br.Msg = "参数有误"
  1973. return
  1974. }
  1975. if req.CompanyId <= 0 {
  1976. br.Msg = "参数有误"
  1977. return
  1978. }
  1979. item, e := models.GetEnglishReportEmailById(req.EmailId)
  1980. if e != nil {
  1981. br.Msg = "联系人已被删除, 请勿操作"
  1982. return
  1983. }
  1984. // 更新联系人
  1985. nowTime := time.Now().Local()
  1986. item.CompanyId = req.CompanyId
  1987. item.ModifyTime = nowTime
  1988. item.Status = 1
  1989. updateCols := []string{"CompanyId", "ModifyTime", "Status"}
  1990. if e = item.Update(updateCols); e != nil {
  1991. br.Msg = "操作失败"
  1992. br.ErrMsg = "更新英文联系人失败, Err:" + e.Error()
  1993. return
  1994. }
  1995. // 更新所属客户状态
  1996. //go func() {
  1997. // _ = services.UpdateEnglishCompanyEnabledByCompanyId(item.CompanyId)
  1998. //}()
  1999. // 记录日志
  2000. go func() {
  2001. opByte, e := json.Marshal(req)
  2002. if e != nil {
  2003. return
  2004. }
  2005. opData := string(opByte)
  2006. opLog := &models.EnglishReportEmailOpLog{
  2007. CompanyId: item.CompanyId,
  2008. EmailId: item.Id,
  2009. AdminId: sysUser.AdminId,
  2010. OptionData: opData,
  2011. OptionType: models.EnglishReportEmailOpLogTypeMoveToCurrent,
  2012. CreateTime: nowTime,
  2013. }
  2014. if e = opLog.Create(); e != nil {
  2015. return
  2016. }
  2017. }()
  2018. br.Ret = 200
  2019. br.Success = true
  2020. br.Msg = "操作成功"
  2021. }