email.go 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183
  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. noCompanyIdsMap := make(map[int]struct{})
  558. if len(req.NoCompanyIds) > 0 {
  559. for _, v := range req.NoCompanyIds {
  560. noCompanyIdsMap[v] = struct{}{}
  561. }
  562. }
  563. // 指定品种的客户
  564. sendCompanyIds := make([]int, 0)
  565. if len(req.EnPermissions) > 0 {
  566. companyIds, e := models.GetEnglishCompanyIdsByEnPermissionIds(req.EnPermissions)
  567. if e != nil {
  568. br.Msg = "推送失败"
  569. br.ErrMsg = "获取指定品种的客户IDs失败, Err: " + e.Error()
  570. return
  571. }
  572. for _, v := range companyIds {
  573. if _, ok := noCompanyIdsMap[v]; !ok {
  574. sendCompanyIds = append(sendCompanyIds, v)
  575. }
  576. }
  577. }
  578. // 指定收件人列表
  579. sendEmailIds := make([]int, 0)
  580. if req.EmailIds != "" {
  581. emailIdArr := strings.Split(req.EmailIds, ",")
  582. if len(emailIdArr) == 0 {
  583. br.Msg = "指定收件人列表有误"
  584. br.ErrMsg = fmt.Sprintf("收件人列表为空, 收件人IDs: %s", req.EmailIds)
  585. return
  586. }
  587. for _, m := range emailIdArr {
  588. v, _ := strconv.Atoi(m)
  589. sendEmailIds = append(sendEmailIds, v)
  590. }
  591. }
  592. if len(sendCompanyIds) == 0 && len(sendEmailIds) == 0 {
  593. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  594. br.ErrMsg = "收件人列表为空, 不可推送"
  595. return
  596. }
  597. // 往正式客户id里面插入0的 目的是为了把临时客户找出来
  598. sendCompanyIds = append(sendCompanyIds, 0)
  599. emailCond := " AND enabled = 1 AND status <> 3"
  600. emailPars := make([]interface{}, 0)
  601. if len(sendCompanyIds) > 0 && len(sendEmailIds) > 0 {
  602. emailCond += ` AND (company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `) OR id IN (` + utils.GetOrmInReplace(len(sendEmailIds)) + `))`
  603. emailPars = append(emailPars, sendCompanyIds)
  604. emailPars = append(emailPars, sendEmailIds)
  605. }
  606. if len(sendCompanyIds) > 0 && len(sendEmailIds) == 0 {
  607. emailCond += ` AND company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `)`
  608. emailPars = append(emailPars, sendCompanyIds)
  609. }
  610. emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  611. if e != nil {
  612. br.Msg = "获取收件人列表失败"
  613. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  614. return
  615. }
  616. if len(emails) == 0 {
  617. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  618. br.ErrMsg = "收件人列表为空, 不可推送"
  619. return
  620. }
  621. // 获取模板
  622. confKey := "english_report_email_tmp"
  623. confTmp, e := company.GetConfigDetailByCode(confKey)
  624. if e != nil {
  625. br.Msg = "获取邮件模板失败"
  626. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  627. return
  628. }
  629. if confTmp.ConfigValue == `` {
  630. br.Msg = "邮件模板有误"
  631. br.ErrMsg = "邮件模板为空, 不可推送"
  632. return
  633. }
  634. // 同一篇报告避免重复推送给同一个邮件
  635. var cond string
  636. var pars []interface{}
  637. cond += ` AND report_id = ? AND send_status = 1 AND report_type = 0`
  638. pars = append(pars, req.ReportId)
  639. logs, e := models.GetEnglishReportEmailLogList(cond, pars)
  640. if e != nil {
  641. br.Msg = "推送失败"
  642. br.ErrMsg = "获取邮件发送日志列表失败, Err: " + e.Error()
  643. return
  644. }
  645. repeatMap := make(map[int]bool, 0)
  646. for i := range logs {
  647. if logs[i].SendStatus == 1 {
  648. repeatMap[logs[i].EmailId] = true
  649. }
  650. }
  651. // 推送信息
  652. nowTime := time.Now().Local()
  653. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  654. logData := make([]*models.EnglishReportEmailLog, 0)
  655. for i := range emails {
  656. if repeatMap[emails[i].Id] {
  657. continue
  658. }
  659. r := new(services.EnglishReportSendEmailRequest)
  660. r.ReportId = reportInfo.Id
  661. r.EmailId = emails[i].Id
  662. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  663. r.Subject = req.Theme
  664. r.FromAlias = conf.FromAlias
  665. // 获取报告Overview部分
  666. if reportInfo.Overview == "" {
  667. br.Msg = "报告Overview部分不可为空"
  668. return
  669. }
  670. overview := reportInfo.Overview
  671. // 填充模板
  672. t := reportInfo.PublishTime.Format("02/01/2006")
  673. l := fmt.Sprintf(utils.EnglishShareUrl+"/report/detail?code=%s", reportInfo.ReportCode)
  674. ct := confTmp.ConfigValue
  675. ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
  676. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
  677. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", overview, 1)
  678. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  679. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  680. r.HtmlBody = ct
  681. r.ReportTitle = reportInfo.Title
  682. r.ReportAbstract = reportInfo.Abstract
  683. r.ReportContent = overview
  684. r.ReportShareLink = l
  685. r.ReportTime = t
  686. logItem := &models.EnglishReportEmailLog{
  687. ReportId: reportInfo.Id,
  688. EmailId: emails[i].Id,
  689. Email: emails[i].Email,
  690. Source: models.EnglishReportEmailLogSourceAli,
  691. SendStatus: models.EnglishReportEmailLogStatusIng,
  692. CreateTime: nowTime,
  693. }
  694. r.EmailLog = logItem
  695. sendData = append(sendData, r)
  696. // 日志
  697. logData = append(logData, logItem)
  698. }
  699. if len(sendData) == 0 {
  700. br.Msg = "无邮件可推送"
  701. return
  702. }
  703. // 写入日志
  704. for _, v := range logData {
  705. if e = v.Create(); e != nil {
  706. br.Msg = "操作失败"
  707. br.ErrMsg = "写入群发邮件日志失败, Err: " + e.Error()
  708. return
  709. }
  710. }
  711. //emailLog := new(models.EnglishReportEmailLog)
  712. //if e = emailLog.InsertMulti(logData); e != nil {
  713. // br.Msg = "操作失败"
  714. // br.ErrMsg = "批量写入群发邮件日志失败, Err: " + e.Error()
  715. // return
  716. //}
  717. // 修改推送状态
  718. updateCols := []string{"EmailState", "ModifyTime"}
  719. reportInfo.EmailState = 1
  720. reportInfo.ModifyTime = time.Now().Local()
  721. if e = reportInfo.Update(updateCols); e != nil {
  722. br.Msg = "操作失败"
  723. br.ErrMsg = "更新推送状态失败, Err: " + e.Error()
  724. return
  725. }
  726. // 群发邮件
  727. go func() {
  728. services.BatchSendAliEnglishReportEmail(sendData)
  729. }()
  730. br.Ret = 200
  731. br.Success = true
  732. br.Msg = "操作成功"
  733. }
  734. // PvList
  735. // @Title 英文研报邮箱PV列表
  736. // @Description 英文研报邮箱PV列表
  737. // @Param ReportId query int false "英文研报ID"
  738. // @Param ReportType query int false "类型:0英文研报,1英文线上路演"
  739. // @Success 200 {object} models.EnglishReportEmailPvResp
  740. // @router /email/pv_list [get]
  741. func (this *EnglishReportEmailController) PvList() {
  742. br := new(models.BaseResponse).Init()
  743. br.IsSendEmail = false
  744. defer func() {
  745. this.Data["json"] = br
  746. this.ServeJSON()
  747. }()
  748. sysUser := this.SysUser
  749. if sysUser == nil {
  750. br.Msg = "请登录"
  751. br.ErrMsg = "请登录,SysUser Is Empty"
  752. br.Ret = 408
  753. return
  754. }
  755. reportId, e := this.GetInt("ReportId", 0)
  756. if reportId == 0 {
  757. br.Msg = "参数有误"
  758. return
  759. }
  760. reportType, e := this.GetInt("ReportType", 0)
  761. var cond string
  762. var pars []interface{}
  763. cond += ` AND a.report_id = ? AND a.report_type= ?`
  764. pars = append(pars, reportId, reportType)
  765. var startSize int
  766. pageSize, _ := this.GetInt("PageSize")
  767. currentIndex, _ := this.GetInt("CurrentIndex")
  768. if pageSize <= 0 {
  769. pageSize = utils.PageSize20
  770. }
  771. if currentIndex <= 0 {
  772. currentIndex = 1
  773. }
  774. startSize = paging.StartIndex(currentIndex, pageSize)
  775. total, list, e := models.GetEnglishReportEmailPvPageList(cond, pars, startSize, pageSize)
  776. if e != nil {
  777. br.Msg = "获取失败"
  778. br.ErrMsg = "获取英文研报邮箱pv列表失败, Err: " + e.Error()
  779. return
  780. }
  781. page := paging.GetPaging(currentIndex, pageSize, total)
  782. resp := &models.EnglishReportEmailPvPageListResp{
  783. Paging: page,
  784. List: list,
  785. }
  786. br.Ret = 200
  787. br.Success = true
  788. br.Msg = "获取成功"
  789. br.Data = resp
  790. }
  791. // ImportListMatch
  792. // @Title 英文邮箱/联系人批量导入
  793. // @Description 英文邮箱/联系人批量导入
  794. // @Param File query file true "文件"
  795. // @Param CompanyId query int true "客户ID"
  796. // @Success 200 string "导入成功"
  797. // @router /email/import [post]
  798. func (this *EnglishReportEmailController) ImportListMatch() {
  799. br := new(models.BaseResponse).Init()
  800. defer func() {
  801. this.Data["json"] = br
  802. this.ServeJSON()
  803. }()
  804. sysUser := this.SysUser
  805. if sysUser == nil {
  806. br.Msg = "请重新登录"
  807. return
  808. }
  809. companyId, _ := this.GetInt("CompanyId")
  810. if companyId <= 0 {
  811. br.Msg = "请选择客户"
  812. return
  813. }
  814. companyItem, e := models.GetEnglishCompanyById(companyId)
  815. if e != nil {
  816. if e.Error() == utils.ErrNoRow() {
  817. br.Msg = "客户已被删除, 请刷新页面"
  818. return
  819. }
  820. br.Msg = "操作失败"
  821. br.ErrMsg = "获取客户信息失败, Err: " + e.Error()
  822. return
  823. }
  824. file, h, err := this.GetFile("File")
  825. if err != nil {
  826. br.Msg = "获取文件失败"
  827. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  828. return
  829. }
  830. defer func() {
  831. _ = file.Close()
  832. }()
  833. ext := path.Ext(h.Filename)
  834. if ext != ".xlsx" && ext != ".xls" {
  835. br.Msg = "请上传Excel文件"
  836. return
  837. }
  838. uploadDir := "static/xls"
  839. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  840. if err != nil {
  841. br.Msg = "存储目录创建失败"
  842. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  843. return
  844. }
  845. uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  846. err = this.SaveToFile("File", uploadPath)
  847. if err != nil {
  848. br.Msg = "文件保存失败"
  849. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  850. return
  851. }
  852. defer func() {
  853. _ = os.Remove(uploadPath)
  854. }()
  855. xlFile, err := xlsx.OpenFile(uploadPath)
  856. if err != nil {
  857. br.Msg = "文件导入失败"
  858. br.ErrMsg = "文件导入失败,Err:" + err.Error()
  859. return
  860. }
  861. // 重复邮箱校验
  862. var cond string
  863. var pars []interface{}
  864. emails, e := models.GetEnglishReportEmailList(cond, pars, "")
  865. if e != nil {
  866. br.Msg = "导入失败"
  867. br.ErrMsg = "获取邮箱列表失败, Err:" + e.Error()
  868. return
  869. }
  870. emailArr := make([]string, 0)
  871. for i := range emails {
  872. emailArr = append(emailArr, emails[i].Email)
  873. }
  874. importList := make([]*models.EnglishReportEmailSaveReq, 0)
  875. // 遍历sheet页读取
  876. titleRow := []string{"联系人姓名", "国际区号", "手机号", "邮箱地址"}
  877. for _, sheet := range xlFile.Sheets {
  878. // 遍历行读取
  879. maxRow := sheet.MaxRow
  880. for i := 0; i < maxRow; i++ {
  881. // 首行
  882. if i == 0 {
  883. row := sheet.Row(i)
  884. cells := row.Cells
  885. for k, cell := range cells {
  886. text := cell.String()
  887. for k2 := range titleRow {
  888. if k > k2 {
  889. break
  890. }
  891. if text != titleRow[k] && !strings.Contains(text, titleRow[k]) {
  892. br.Msg = fmt.Sprintf("匹配失败, 请按模板导入(%s)", titleRow[k])
  893. br.ErrMsg = "模板格式错误,title:" + text
  894. return
  895. }
  896. }
  897. }
  898. }
  899. // 数据行
  900. if i >= 1 {
  901. row := sheet.Row(i)
  902. cells := row.Cells
  903. var userName, userCountryCode, userMobile, userEmail string
  904. for k, cell := range cells {
  905. v := utils.TrimStr(cell.String())
  906. if k == 0 {
  907. userName = v
  908. continue
  909. }
  910. if k == 1 {
  911. userCountryCode = v
  912. continue
  913. }
  914. if k == 2 {
  915. userMobile = v
  916. continue
  917. }
  918. if k == 3 {
  919. userEmail = v
  920. }
  921. }
  922. // 数据为空则读取下一行
  923. if userName == "" || userEmail == "" {
  924. continue
  925. }
  926. if !utils.ValidateEmailFormatat(userEmail) {
  927. br.Msg = "存在错误的邮箱格式, 请检查后重新导入"
  928. return
  929. }
  930. // 重复校验
  931. if utils.InArrayByStr(emailArr, userEmail) {
  932. continue
  933. }
  934. emailArr = append(emailArr, userEmail)
  935. importList = append(importList, &models.EnglishReportEmailSaveReq{
  936. CompanyId: companyId,
  937. Name: userName,
  938. Email: userEmail,
  939. Mobile: userMobile,
  940. CountryCode: userCountryCode,
  941. Enabled: 1,
  942. })
  943. }
  944. }
  945. }
  946. if len(importList) == 0 {
  947. br.Msg = "无有效数据可导入"
  948. return
  949. }
  950. nowTime := time.Now().Local()
  951. newEmails := make([]*models.EnglishReportEmail, 0)
  952. newLogs := make([]*models.EnglishReportEmailOpLog, 0)
  953. for i := range importList {
  954. v := &models.EnglishReportEmail{
  955. CompanyId: importList[i].CompanyId,
  956. CompanyName: companyItem.CompanyName,
  957. Name: importList[i].Name,
  958. Email: importList[i].Email,
  959. Mobile: importList[i].Mobile,
  960. CountryCode: importList[i].CountryCode,
  961. Enabled: importList[i].Enabled,
  962. AdminId: sysUser.AdminId,
  963. AdminName: sysUser.AdminName,
  964. Status: 1,
  965. CreateTime: nowTime,
  966. ModifyTime: nowTime,
  967. }
  968. newEmails = append(newEmails, v)
  969. newByte, e := json.Marshal(importList[i])
  970. if e != nil {
  971. br.Msg = "导入失败"
  972. br.ErrMsg = "新邮箱/联系转JSON失败, Err: " + e.Error()
  973. return
  974. }
  975. newLogs = append(newLogs, &models.EnglishReportEmailOpLog{
  976. CompanyId: importList[i].CompanyId,
  977. AdminId: sysUser.AdminId,
  978. OptionData: string(newByte),
  979. OptionType: models.EnglishReportEmailOpLogTypeImport,
  980. CreateTime: nowTime,
  981. })
  982. }
  983. if e = models.MultiCreateEnglishEmail(newEmails, newLogs); e != nil {
  984. br.Msg = "导入失败"
  985. br.ErrMsg = "批量新增英文邮箱/联系人失败, Err: " + e.Error()
  986. return
  987. }
  988. br.Msg = "导入成功"
  989. br.Ret = 200
  990. br.Success = true
  991. }
  992. // ViewList
  993. // @Title 英文研报邮箱/联系人阅读列表
  994. // @Description 英文研报邮箱/联系人阅读列表
  995. // @Param EmailId query int false "英文邮箱/联系人ID"
  996. // @Param SortParam query int false "排序字段:1-点击量;2-点击时间"
  997. // @Param SortType query int false "排序方式:1-倒序;2-正序"
  998. // @Param EnPermissionIds query string false "品种权限IDs(字符串)"
  999. // @Success 200 {object} models.EnglishEmailViewPageListResp
  1000. // @router /email/view_list [get]
  1001. func (this *EnglishReportEmailController) ViewList() {
  1002. br := new(models.BaseResponse).Init()
  1003. br.IsSendEmail = false
  1004. defer func() {
  1005. this.Data["json"] = br
  1006. this.ServeJSON()
  1007. }()
  1008. sysUser := this.SysUser
  1009. if sysUser == nil {
  1010. br.Msg = "请登录"
  1011. br.ErrMsg = "请登录,SysUser Is Empty"
  1012. br.Ret = 408
  1013. return
  1014. }
  1015. emailId, e := this.GetInt("EmailId", 0)
  1016. if emailId <= 0 {
  1017. br.Msg = "参数有误"
  1018. return
  1019. }
  1020. // 默认点击时间降序
  1021. sortParam, _ := this.GetInt("SortParam", 2)
  1022. sortType, _ := this.GetInt("SortType", 1)
  1023. clickType := this.GetString("ClickType", "")
  1024. strPermissionIds := this.GetString("EnPermissionIds", "")
  1025. var cond, order string
  1026. var pars []interface{}
  1027. cond += ` AND email_id = ? `
  1028. pars = append(pars, emailId)
  1029. if clickType != "" {
  1030. cond += ` AND report_type = ? `
  1031. pars = append(pars, clickType)
  1032. }
  1033. // 品种筛选
  1034. if strPermissionIds != "" {
  1035. permissionIdArr := strings.Split(strPermissionIds, ",")
  1036. permissionIds := make([]int, 0)
  1037. for _, s := range permissionIdArr {
  1038. p, e := strconv.Atoi(s)
  1039. if e != nil {
  1040. br.Msg = "品种权限有误"
  1041. br.ErrMsg = "品种权限筛选有误"
  1042. return
  1043. }
  1044. permissionIds = append(permissionIds, p)
  1045. }
  1046. if len(permissionIds) == 0 {
  1047. br.Msg = "品种权限有误"
  1048. br.ErrMsg = "品种权限筛选ID为空"
  1049. return
  1050. }
  1051. reportIds, e := models.GetEnglishReportIdsByEnPermissionIds(permissionIds)
  1052. if e != nil {
  1053. br.Msg = "获取失败"
  1054. br.ErrMsg = "获取报告IDs失败, Err: " + e.Error()
  1055. return
  1056. }
  1057. if len(reportIds) > 0 {
  1058. cond += fmt.Sprintf(` AND report_id IN (%s) `, utils.GetOrmInReplace(len(reportIds)))
  1059. pars = append(pars, reportIds)
  1060. } else {
  1061. cond += ` AND 1=2`
  1062. }
  1063. }
  1064. var startSize int
  1065. pageSize, _ := this.GetInt("PageSize")
  1066. currentIndex, _ := this.GetInt("CurrentIndex")
  1067. if pageSize <= 0 {
  1068. pageSize = utils.PageSize20
  1069. }
  1070. if currentIndex <= 0 {
  1071. currentIndex = 1
  1072. }
  1073. startSize = paging.StartIndex(currentIndex, pageSize)
  1074. sortArr := []int{1, 2}
  1075. if utils.InArrayByInt(sortArr, sortParam) && utils.InArrayByInt(sortArr, sortType) {
  1076. pMap := map[int]string{1: "view_total", 2: "last_view_time"}
  1077. tMap := map[int]string{1: "DESC", 2: "ASC"}
  1078. order = fmt.Sprintf(` ORDER BY %s %s`, pMap[sortParam], tMap[sortType])
  1079. }
  1080. total, list, e := models.GetEnglishEmailViewPageList(cond, pars, order, startSize, pageSize)
  1081. if e != nil {
  1082. br.Msg = "获取失败"
  1083. br.ErrMsg = "英文研报邮箱/联系人阅读列表失败, Err: " + e.Error()
  1084. return
  1085. }
  1086. // 报告标题,报告类型
  1087. reportMap := make(map[int]*models.EnglishReport, 0)
  1088. videoMap := make(map[int]*models.EnglishVideo, 0)
  1089. if len(list) > 0 {
  1090. reportIds := make([]int, 0)
  1091. videoIds := make([]int, 0)
  1092. for i := range list {
  1093. if list[i].ClickType == 0 {
  1094. reportIds = append(reportIds, list[i].ReportId)
  1095. } else if list[i].ClickType == 1 {
  1096. videoIds = append(videoIds, list[i].ReportId)
  1097. }
  1098. }
  1099. fields := []string{"id", "title", "classify_name_first", "classify_name_second"}
  1100. if len(reportIds) > 0 {
  1101. reports, e := models.GetEnglishReportByIds(reportIds, fields)
  1102. if e != nil {
  1103. br.Msg = "获取失败"
  1104. br.ErrMsg = "获取英文研报列表失败, Err: " + e.Error()
  1105. return
  1106. }
  1107. for i := range reports {
  1108. reportMap[reports[i].Id] = reports[i]
  1109. }
  1110. }
  1111. if len(videoIds) > 0 {
  1112. videos, e := models.GetEnglishVideoByIds(videoIds, fields)
  1113. if e != nil {
  1114. br.Msg = "获取失败"
  1115. br.ErrMsg = "获取英文研报列表失败, Err: " + e.Error()
  1116. return
  1117. }
  1118. for i := range videos {
  1119. videoMap[videos[i].Id] = videos[i]
  1120. }
  1121. }
  1122. }
  1123. for i := range list {
  1124. if r, ok := reportMap[list[i].ReportId]; ok {
  1125. list[i].ReportTitle = r.Title
  1126. list[i].ReportType = r.ClassifyNameFirst
  1127. if r.ClassifyNameSecond != "" {
  1128. list[i].ReportType += ` / ` + r.ClassifyNameSecond
  1129. }
  1130. } else if v, ok1 := videoMap[list[i].ReportId]; ok1 {
  1131. list[i].ReportTitle = v.Title
  1132. list[i].ReportType = v.ClassifyNameFirst
  1133. if v.ClassifyNameSecond != "" {
  1134. list[i].ReportType += ` / ` + v.ClassifyNameSecond
  1135. }
  1136. }
  1137. }
  1138. page := paging.GetPaging(currentIndex, pageSize, total)
  1139. resp := &models.EnglishEmailViewPageListResp{
  1140. Paging: page,
  1141. List: list,
  1142. }
  1143. br.Ret = 200
  1144. br.Success = true
  1145. br.Msg = "获取成功"
  1146. br.Data = resp
  1147. }
  1148. // LogList
  1149. // @Title 邮件群发日志列表
  1150. // @Description 邮件群发日志列表
  1151. // @Param ReportId query int true "报告ID"
  1152. // @Param ReportType query int true "类型:0英文研报,1英文线上路演"
  1153. // @Param SendStatus query int false "发送状态:-1-已发送;0-发送失败;1-发送成功"
  1154. // @Param Keywords query string false "关键词:邮箱"
  1155. // @Success 200 {object} models.EnglishReportEmailLogPageListResp
  1156. // @router /email/log_list [get]
  1157. func (this *EnglishReportEmailController) LogList() {
  1158. br := new(models.BaseResponse).Init()
  1159. br.IsSendEmail = false
  1160. defer func() {
  1161. this.Data["json"] = br
  1162. this.ServeJSON()
  1163. }()
  1164. sysUser := this.SysUser
  1165. if sysUser == nil {
  1166. br.Msg = "请登录"
  1167. br.ErrMsg = "请登录,SysUser Is Empty"
  1168. br.Ret = 408
  1169. return
  1170. }
  1171. reportId, _ := this.GetInt("ReportId", 0)
  1172. if reportId <= 0 {
  1173. br.Msg = "参数有误"
  1174. return
  1175. }
  1176. reportType, _ := this.GetInt("ReportType", 0)
  1177. keywords := this.GetString("Keywords", "")
  1178. var startSize int
  1179. pageSize, _ := this.GetInt("PageSize")
  1180. currentIndex, _ := this.GetInt("CurrentIndex")
  1181. if pageSize <= 0 {
  1182. pageSize = utils.PageSize20
  1183. }
  1184. if currentIndex <= 0 {
  1185. currentIndex = 1
  1186. }
  1187. startSize = paging.StartIndex(currentIndex, pageSize)
  1188. var cond string
  1189. var pars []interface{}
  1190. cond += ` AND report_id = ? AND report_type = ?`
  1191. pars = append(pars, reportId, reportType)
  1192. // 推送状态
  1193. status := this.GetString("SendStatus", "")
  1194. if status != "" {
  1195. statusInt, e := strconv.Atoi(status)
  1196. if e != nil {
  1197. br.Msg = "状态类型有误"
  1198. return
  1199. }
  1200. cond += ` AND send_status = ?`
  1201. pars = append(pars, statusInt)
  1202. }
  1203. // 关键词
  1204. if keywords != "" {
  1205. k := fmt.Sprint("%", keywords, "%")
  1206. cond += ` AND email LIKE ? `
  1207. pars = append(pars, k)
  1208. }
  1209. total, list, e := models.GetEnglishReportEmailLogPageList(cond, pars, "", startSize, pageSize)
  1210. if e != nil {
  1211. br.Msg = "获取失败"
  1212. br.ErrMsg = "获取邮箱列表失败, Err: " + e.Error()
  1213. return
  1214. }
  1215. respList := make([]*models.EnglishReportEmailLogPageList, 0)
  1216. for i := range list {
  1217. v := new(models.EnglishReportEmailLogPageList)
  1218. // 推送结果
  1219. v.ResultMsg = "发送成功"
  1220. if list[i].ErrMsg != "" {
  1221. v.ResultMsg = list[i].ErrMsg
  1222. }
  1223. v.SendId = list[i].Id
  1224. v.ReportId = list[i].ReportId
  1225. v.EmailId = list[i].EmailId
  1226. v.Email = list[i].Email
  1227. v.SendStatus = list[i].SendStatus
  1228. v.Source = list[i].Source
  1229. v.CreateTime = list[i].CreateTime.Format(utils.FormatDateTime)
  1230. respList = append(respList, v)
  1231. }
  1232. page := paging.GetPaging(currentIndex, pageSize, total)
  1233. resp := &models.EnglishReportEmailLogPageListResp{
  1234. Paging: page,
  1235. List: respList,
  1236. }
  1237. br.Ret = 200
  1238. br.Success = true
  1239. br.Msg = "获取成功"
  1240. br.Data = resp
  1241. }
  1242. // Resend
  1243. // @Title 重新推送
  1244. // @Description 群发推送
  1245. // @Param request body models.EnglishReportEmailResendReq true "type json string"
  1246. // @Success 200 string "操作成功"
  1247. // @router /email/resend [post]
  1248. func (this *EnglishReportEmailController) Resend() {
  1249. br := new(models.BaseResponse).Init()
  1250. br.IsSendEmail = false
  1251. defer func() {
  1252. this.Data["json"] = br
  1253. this.ServeJSON()
  1254. }()
  1255. sysUser := this.SysUser
  1256. if sysUser == nil {
  1257. br.Msg = "请登录"
  1258. br.ErrMsg = "请登录,SysUser Is Empty"
  1259. br.Ret = 408
  1260. return
  1261. }
  1262. var req models.EnglishReportEmailResendReq
  1263. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  1264. br.Msg = "参数解析异常!"
  1265. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1266. return
  1267. }
  1268. if req.ReportId <= 0 {
  1269. br.Msg = "请选择报告"
  1270. return
  1271. }
  1272. // 获取报告信息
  1273. reportInfo, e := models.GetEnglishReportItemById(req.ReportId)
  1274. if e != nil {
  1275. br.Msg = "报告信息有误"
  1276. br.ErrMsg = "获取报告信息失败, Err: " + e.Error()
  1277. return
  1278. }
  1279. if reportInfo.State != 2 {
  1280. br.Msg = "报告未发布"
  1281. br.ErrMsg = fmt.Sprintf("报告状态有误, 当前状态: %d", reportInfo.State)
  1282. return
  1283. }
  1284. if reportInfo.Title == "" {
  1285. br.Msg = "报告标题有误"
  1286. br.ErrMsg = "报告标题为空, 不可推送"
  1287. return
  1288. }
  1289. if reportInfo.Content == `` {
  1290. br.Msg = "报告内容有误"
  1291. br.ErrMsg = "报告内容为空, 不可推送"
  1292. return
  1293. }
  1294. if reportInfo.Abstract == `` {
  1295. br.Msg = "报告摘要为空"
  1296. br.ErrMsg = "报告摘要为空, 不可推送"
  1297. return
  1298. }
  1299. if reportInfo.Overview == "" {
  1300. br.Msg = "报告Overview部分不可为空"
  1301. br.ErrMsg = "报告Overview为空, 不可推送"
  1302. return
  1303. }
  1304. // 重新发送超过100字符直接截取
  1305. theme := reportInfo.Abstract
  1306. runeAbs := []rune(reportInfo.Abstract)
  1307. runeThe := make([]rune, 0)
  1308. if len(runeAbs) > 100 {
  1309. runeThe = runeAbs[:100]
  1310. theme = string(runeThe)
  1311. }
  1312. // 获取邮件配置
  1313. conf := new(models.EnglishReportEmailConf)
  1314. authKey := "english_report_email_conf"
  1315. confAuth, e := company.GetConfigDetailByCode(authKey)
  1316. if e != nil {
  1317. br.Msg = "无权操作"
  1318. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  1319. return
  1320. }
  1321. if confAuth.ConfigValue == "" {
  1322. br.Msg = "无权操作"
  1323. br.ErrMsg = "群发邮件配置为空"
  1324. return
  1325. }
  1326. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  1327. br.Msg = "无权操作"
  1328. br.ErrMsg = "群发邮件配置有误"
  1329. return
  1330. }
  1331. authArr := strings.Split(conf.SendAuthGroup, ",")
  1332. if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  1333. br.Msg = "无权操作"
  1334. return
  1335. }
  1336. // 获取模板
  1337. confKey := "english_report_email_tmp"
  1338. confTmp, e := company.GetConfigDetailByCode(confKey)
  1339. if e != nil {
  1340. br.Msg = "获取邮件模板失败"
  1341. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  1342. return
  1343. }
  1344. if confTmp.ConfigValue == `` {
  1345. br.Msg = "邮件模板有误"
  1346. br.ErrMsg = "邮件模板为空, 不可推送"
  1347. return
  1348. }
  1349. // 获取发送失败的日志
  1350. emailIds := make([]int, 0)
  1351. emails := make([]*models.EnglishReportEmail, 0)
  1352. logIds := make([]int, 0)
  1353. if req.SendId > 0 {
  1354. logItem, e := models.GetEnglishReportEmailLogById(req.SendId)
  1355. if e != nil {
  1356. br.Msg = "发送失败"
  1357. br.ErrMsg = "获取原邮件推送日志失败, Err: " + e.Error()
  1358. return
  1359. }
  1360. emailIds = append(emailIds, logItem.EmailId)
  1361. logIds = append(logIds, logItem.Id)
  1362. } else {
  1363. var logCond string
  1364. var logPars []interface{}
  1365. logCond += ` AND report_id = ? AND send_status = 0 AND report_type=0`
  1366. logPars = append(logPars, req.ReportId)
  1367. logList, e := models.GetEnglishReportEmailLogList(logCond, logPars)
  1368. if e != nil {
  1369. br.Msg = "发送失败"
  1370. br.ErrMsg = "获取原邮件推送日志列表失败, Err: " + e.Error()
  1371. return
  1372. }
  1373. for i := range logList {
  1374. emailIds = append(emailIds, logList[i].EmailId)
  1375. logIds = append(logIds, logList[i].Id)
  1376. }
  1377. }
  1378. // 获取收件人列表
  1379. // 注: 此处不从失败日志中取email, 因为有可能是地址有误导致的推送失败
  1380. if len(emailIds) > 0 {
  1381. var emailCond string
  1382. var emailPars []interface{}
  1383. emailCond += ` AND id IN (` + utils.GetOrmInReplace(len(emailIds)) + `) and enabled = 1 `
  1384. emailPars = append(emailPars, emailIds)
  1385. em, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  1386. if e != nil {
  1387. br.Msg = "获取收件人列表失败"
  1388. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  1389. return
  1390. }
  1391. emails = em
  1392. }
  1393. if len(emails) == 0 {
  1394. br.Msg = "无邮件可推送"
  1395. return
  1396. }
  1397. // 推送信息
  1398. nowTime := time.Now().Local()
  1399. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  1400. logData := make([]*models.EnglishReportEmailLog, 0)
  1401. for i := range emails {
  1402. r := new(services.EnglishReportSendEmailRequest)
  1403. r.ReportId = reportInfo.Id
  1404. r.EmailId = emails[i].Id
  1405. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  1406. r.Subject = theme
  1407. r.FromAlias = conf.FromAlias
  1408. // 填充模板
  1409. t := reportInfo.PublishTime.Format("02/01/2006")
  1410. l := fmt.Sprintf(utils.EnglishShareUrl+"/report/detail?code=%s", reportInfo.ReportCode)
  1411. ct := confTmp.ConfigValue
  1412. ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
  1413. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
  1414. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", reportInfo.Overview, 1)
  1415. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  1416. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  1417. r.HtmlBody = ct
  1418. r.ReportTitle = reportInfo.Title
  1419. r.ReportAbstract = reportInfo.Abstract
  1420. r.ReportContent = reportInfo.Overview
  1421. r.ReportShareLink = l
  1422. r.ReportTime = t
  1423. logItem := &models.EnglishReportEmailLog{
  1424. ReportId: reportInfo.Id,
  1425. EmailId: emails[i].Id,
  1426. Email: emails[i].Email,
  1427. Source: models.EnglishReportEmailLogSourceAli,
  1428. SendStatus: models.EnglishReportEmailLogStatusIng,
  1429. CreateTime: nowTime,
  1430. }
  1431. r.EmailLog = logItem
  1432. sendData = append(sendData, r)
  1433. // 日志
  1434. logData = append(logData, logItem)
  1435. }
  1436. // 标记原有日志为已删除
  1437. if len(logIds) > 0 {
  1438. if e = models.DeleteEnglishReportEmailLogByIds(logIds); e != nil {
  1439. br.Msg = "发送失败"
  1440. br.ErrMsg = "删除原邮件日志失败, Err: " + e.Error()
  1441. return
  1442. }
  1443. }
  1444. // 写入新的日志
  1445. for _, v := range logData {
  1446. if e = v.Create(); e != nil {
  1447. br.Msg = "操作失败"
  1448. br.ErrMsg = "写入群发邮件日志失败, Err: " + e.Error()
  1449. return
  1450. }
  1451. }
  1452. // 群发邮件
  1453. go func() {
  1454. services.BatchSendAliEnglishReportEmail(sendData)
  1455. }()
  1456. br.Ret = 200
  1457. br.Success = true
  1458. br.Msg = "操作成功"
  1459. }
  1460. // SendCallBack
  1461. // @Title 群发邮件回调
  1462. // @Description 群发邮件回调
  1463. // @Param request body services.TencentEmailCallBack true "type json string"
  1464. // @Success 200 string "操作成功"
  1465. // @router /email/send/call_back [post]
  1466. func (this *EnglishReportEmailCallBackController) SendCallBack() {
  1467. // 腾讯云后台同一个发信地址只能配置一个回调, 测试环境就不用了, 给正式环境用
  1468. if utils.RunMode != "release" {
  1469. return
  1470. }
  1471. var req services.TencentEmailCallBack
  1472. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  1473. return
  1474. }
  1475. // 仅预警失败的回调事件
  1476. alarmTypes := []string{"dropped", "bounce"}
  1477. if !utils.InArrayByStr(alarmTypes, req.Event) {
  1478. return
  1479. }
  1480. reqByte, e := json.Marshal(req)
  1481. if e != nil {
  1482. return
  1483. }
  1484. reqStr := string(reqByte)
  1485. alarm_msg.SendAlarmMsg("腾讯云邮件推送回调信息:"+reqStr, 1)
  1486. // 存在失败情况则更新日志
  1487. var cond string
  1488. var pars []interface{}
  1489. cond = ` AND is_deleted = 0 AND email = ? AND source = ? AND result LIKE ? `
  1490. pars = append(pars, req.Email, models.EnglishReportEmailLogSourceTencent, fmt.Sprint("%", req.BulkId, "%"))
  1491. logItem, e := models.GetEnglishReportEmailLog(cond, pars)
  1492. if e != nil {
  1493. return
  1494. }
  1495. updateCols := []string{"SendStatus", "CallbackData", "ErrMsg"}
  1496. logItem.SendStatus = models.EnglishReportEmailLogStatusFail
  1497. logItem.CallbackData = reqStr
  1498. logItem.ErrMsg = req.Reason
  1499. if e = logItem.Update(updateCols); e != nil {
  1500. return
  1501. }
  1502. }
  1503. // VideoSend
  1504. // @Title 线上路演群发推送
  1505. // @Description 线上路演群发推送
  1506. // @Param request body models.EnglishReportEmailSaveReq true "type json string"
  1507. // @Success 200 string "操作成功"
  1508. // @router /email/video/send [post]
  1509. func (this *EnglishReportEmailController) VideoSend() {
  1510. br := new(models.BaseResponse).Init()
  1511. br.IsSendEmail = false
  1512. defer func() {
  1513. this.Data["json"] = br
  1514. this.ServeJSON()
  1515. }()
  1516. sysUser := this.SysUser
  1517. if sysUser == nil {
  1518. br.Msg = "请登录"
  1519. br.ErrMsg = "请登录,SysUser Is Empty"
  1520. br.Ret = 408
  1521. return
  1522. }
  1523. var req models.EnglishReportEmailSendReq
  1524. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  1525. br.Msg = "参数解析异常!"
  1526. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1527. return
  1528. }
  1529. if req.ReportId <= 0 {
  1530. br.Msg = "请选择路演视频"
  1531. return
  1532. }
  1533. if req.Theme == "" {
  1534. br.Msg = "请输入邮件主题"
  1535. return
  1536. }
  1537. //prefixTheme := "【HI China Macro and Commodity】"
  1538. //if !strings.Contains(req.Theme, prefixTheme) {
  1539. // req.Theme = prefixTheme + req.Theme
  1540. //}
  1541. runeThe := []rune(req.Theme)
  1542. if len(runeThe) > 100 {
  1543. br.Msg = "邮件主题不可超过100字符"
  1544. return
  1545. }
  1546. // 获取路演视频信息
  1547. videoInfo, e := models.GetEnglishVideoItemById(req.ReportId)
  1548. if e != nil {
  1549. br.Msg = "路演视频信息有误"
  1550. br.ErrMsg = "获取路演视频信息失败, Err: " + e.Error()
  1551. return
  1552. }
  1553. if videoInfo.EmailState != 0 {
  1554. br.Msg = "请勿重复发送"
  1555. br.ErrMsg = fmt.Sprintf("路演视频群发邮件状态有误, 当前状态: %d", videoInfo.EmailState)
  1556. return
  1557. }
  1558. if videoInfo.State != 2 {
  1559. br.Msg = "路演视频未发布"
  1560. br.ErrMsg = fmt.Sprintf("路演视频状态有误, 当前状态: %d", videoInfo.State)
  1561. return
  1562. }
  1563. if videoInfo.Title == "" {
  1564. br.Msg = "路演视频标题有误"
  1565. br.ErrMsg = "路演视频标题为空, 不可推送"
  1566. return
  1567. }
  1568. if videoInfo.VideoUrl == `` {
  1569. br.Msg = "路演视频地址为空, 不可推送"
  1570. br.ErrMsg = "路演视频内容为空, 不可推送"
  1571. return
  1572. }
  1573. // 获取路演视频Overview部分
  1574. if videoInfo.Overview == "" {
  1575. br.Msg = "路演视频Overview部分不可为空"
  1576. return
  1577. }
  1578. // 获取邮件配置
  1579. conf := new(models.EnglishReportEmailConf)
  1580. authKey := "english_report_email_conf"
  1581. confAuth, e := company.GetConfigDetailByCode(authKey)
  1582. if e != nil {
  1583. br.Msg = "无权操作"
  1584. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  1585. return
  1586. }
  1587. if confAuth.ConfigValue == "" {
  1588. br.Msg = "无权操作"
  1589. br.ErrMsg = "群发邮件配置为空"
  1590. return
  1591. }
  1592. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  1593. br.Msg = "无权操作"
  1594. br.ErrMsg = "群发邮件配置有误"
  1595. return
  1596. }
  1597. authArr := strings.Split(conf.SendAuthGroup, ",")
  1598. if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  1599. br.Msg = "无权操作"
  1600. return
  1601. }
  1602. // 指定品种的客户
  1603. sendCompanyIds := make([]int, 0)
  1604. if len(req.EnPermissions) > 0 {
  1605. companyIds, e := models.GetEnglishCompanyIdsByEnPermissionIds(req.EnPermissions)
  1606. if e != nil {
  1607. br.Msg = "推送失败"
  1608. br.ErrMsg = "获取指定品种的客户IDs失败, Err: " + e.Error()
  1609. return
  1610. }
  1611. sendCompanyIds = companyIds
  1612. }
  1613. // 指定收件人列表
  1614. sendEmailIds := make([]int, 0)
  1615. if req.EmailIds != "" {
  1616. emailIdArr := strings.Split(req.EmailIds, ",")
  1617. if len(emailIdArr) == 0 {
  1618. br.Msg = "指定收件人列表有误"
  1619. br.ErrMsg = fmt.Sprintf("收件人列表为空, 收件人IDs: %s", req.EmailIds)
  1620. return
  1621. }
  1622. for _, m := range emailIdArr {
  1623. v, _ := strconv.Atoi(m)
  1624. sendEmailIds = append(sendEmailIds, v)
  1625. }
  1626. }
  1627. if len(sendCompanyIds) == 0 && len(sendEmailIds) == 0 {
  1628. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  1629. br.ErrMsg = "收件人列表为空, 不可推送"
  1630. return
  1631. }
  1632. // 往正式客户id里面插入0的 目的是为了把临时客户找出来
  1633. sendCompanyIds = append(sendCompanyIds, 0)
  1634. emailCond := " AND enabled = 1 AND status <> 3"
  1635. emailPars := make([]interface{}, 0)
  1636. if len(sendCompanyIds) > 0 && len(sendEmailIds) > 0 {
  1637. emailCond += ` AND (company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `) OR id IN (` + utils.GetOrmInReplace(len(sendEmailIds)) + `))`
  1638. emailPars = append(emailPars, sendCompanyIds)
  1639. emailPars = append(emailPars, sendEmailIds)
  1640. }
  1641. if len(sendCompanyIds) > 0 && len(sendEmailIds) == 0 {
  1642. emailCond += ` AND company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `)`
  1643. emailPars = append(emailPars, sendCompanyIds)
  1644. }
  1645. emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  1646. if e != nil {
  1647. br.Msg = "获取收件人列表失败"
  1648. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  1649. return
  1650. }
  1651. if len(emails) == 0 {
  1652. br.Msg = "收件人列表为空(或收件人账号被禁用)"
  1653. br.ErrMsg = "收件人列表为空, 不可推送"
  1654. return
  1655. }
  1656. // 获取模板
  1657. confKey := "english_video_email_tmp"
  1658. confTmp, e := company.GetConfigDetailByCode(confKey)
  1659. if e != nil {
  1660. br.Msg = "获取邮件模板失败"
  1661. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  1662. return
  1663. }
  1664. if confTmp.ConfigValue == `` {
  1665. br.Msg = "邮件模板有误"
  1666. br.ErrMsg = "邮件模板为空, 不可推送"
  1667. return
  1668. }
  1669. // 同一篇路演视频避免重复推送给同一个邮件
  1670. var cond string
  1671. var pars []interface{}
  1672. cond += ` AND report_id = ? AND send_status = 1 AND report_type = 1`
  1673. pars = append(pars, req.ReportId)
  1674. logs, e := models.GetEnglishReportEmailLogList(cond, pars)
  1675. if e != nil {
  1676. br.Msg = "推送失败"
  1677. br.ErrMsg = "获取邮件发送日志列表失败, Err: " + e.Error()
  1678. return
  1679. }
  1680. repeatMap := make(map[int]bool, 0)
  1681. for i := range logs {
  1682. if logs[i].SendStatus == 1 {
  1683. repeatMap[logs[i].EmailId] = true
  1684. }
  1685. }
  1686. // 推送信息
  1687. nowTime := time.Now().Local()
  1688. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  1689. logData := make([]*models.EnglishReportEmailLog, 0)
  1690. for i := range emails {
  1691. if repeatMap[emails[i].Id] {
  1692. continue
  1693. }
  1694. r := new(services.EnglishReportSendEmailRequest)
  1695. r.ReportId = videoInfo.Id
  1696. r.EmailId = emails[i].Id
  1697. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  1698. r.Subject = req.Theme
  1699. r.FromAlias = conf.FromAlias
  1700. overview := videoInfo.Overview
  1701. // 填充模板
  1702. t := videoInfo.PublishTime.Format("02/01/2006")
  1703. l := fmt.Sprintf(utils.EnglishShareUrl+"/roadshow/detail?code=%s&ShareEmail=%d", videoInfo.VideoCode, emails[i].Id)
  1704. ct := confTmp.ConfigValue
  1705. ct = strings.Replace(ct, "{{REPORT_TITLE}}", videoInfo.Title, 1)
  1706. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", videoInfo.Abstract, 1)
  1707. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", overview, 1)
  1708. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  1709. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  1710. r.HtmlBody = ct
  1711. r.ReportTitle = videoInfo.Title
  1712. r.ReportAbstract = videoInfo.Abstract
  1713. r.ReportContent = overview
  1714. r.ReportShareLink = l
  1715. r.ReportTime = t
  1716. logItem := &models.EnglishReportEmailLog{
  1717. ReportId: videoInfo.Id,
  1718. ReportType: 1,
  1719. EmailId: emails[i].Id,
  1720. Email: emails[i].Email,
  1721. Source: models.EnglishReportEmailLogSourceAli,
  1722. SendStatus: models.EnglishReportEmailLogStatusIng,
  1723. CreateTime: nowTime,
  1724. }
  1725. r.EmailLog = logItem
  1726. sendData = append(sendData, r)
  1727. // 日志
  1728. logData = append(logData, logItem)
  1729. }
  1730. if len(sendData) == 0 {
  1731. br.Msg = "无邮件可推送"
  1732. return
  1733. }
  1734. // 写入日志
  1735. for _, v := range logData {
  1736. if e = v.Create(); e != nil {
  1737. br.Msg = "操作失败"
  1738. br.ErrMsg = "写入群发邮件日志失败, Err: " + e.Error()
  1739. return
  1740. }
  1741. }
  1742. // 修改推送状态
  1743. updateCols := []string{"EmailState", "ModifyTime"}
  1744. videoInfo.EmailState = 1
  1745. videoInfo.ModifyTime = time.Now().Local()
  1746. if e = videoInfo.Update(updateCols); e != nil {
  1747. br.Msg = "操作失败"
  1748. br.ErrMsg = "更新推送状态失败, Err: " + e.Error()
  1749. return
  1750. }
  1751. // 群发邮件
  1752. go func() {
  1753. services.BatchSendAliEnglishReportEmail(sendData)
  1754. }()
  1755. br.Ret = 200
  1756. br.Success = true
  1757. br.Msg = "操作成功"
  1758. }
  1759. // VideoResend
  1760. // @Title 线上路演重新推送
  1761. // @Description 线上路演重新推送
  1762. // @Param request body models.EnglishReportEmailResendReq true "type json string"
  1763. // @Success 200 string "操作成功"
  1764. // @router /email/video/resend [post]
  1765. func (this *EnglishReportEmailController) VideoResend() {
  1766. br := new(models.BaseResponse).Init()
  1767. br.IsSendEmail = false
  1768. defer func() {
  1769. this.Data["json"] = br
  1770. this.ServeJSON()
  1771. }()
  1772. sysUser := this.SysUser
  1773. if sysUser == nil {
  1774. br.Msg = "请登录"
  1775. br.ErrMsg = "请登录,SysUser Is Empty"
  1776. br.Ret = 408
  1777. return
  1778. }
  1779. var req models.EnglishReportEmailResendReq
  1780. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  1781. br.Msg = "参数解析异常!"
  1782. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1783. return
  1784. }
  1785. if req.ReportId <= 0 {
  1786. br.Msg = "请选择路演视频"
  1787. return
  1788. }
  1789. // 获取路演视频信息
  1790. reportInfo, e := models.GetEnglishVideoItemById(req.ReportId)
  1791. if e != nil {
  1792. br.Msg = "路演视频信息有误"
  1793. br.ErrMsg = "获取路演视频信息失败, Err: " + e.Error()
  1794. return
  1795. }
  1796. if reportInfo.State != 2 {
  1797. br.Msg = "路演视频未发布"
  1798. br.ErrMsg = fmt.Sprintf("路演视频状态有误, 当前状态: %d", reportInfo.State)
  1799. return
  1800. }
  1801. if reportInfo.Title == "" {
  1802. br.Msg = "路演视频标题有误"
  1803. br.ErrMsg = "路演视频标题为空, 不可推送"
  1804. return
  1805. }
  1806. if reportInfo.Abstract == `` {
  1807. br.Msg = "路演视频摘要为空"
  1808. br.ErrMsg = "路演视频摘要为空, 不可推送"
  1809. return
  1810. }
  1811. if reportInfo.Overview == "" {
  1812. br.Msg = "路演视频Overview部分不可为空"
  1813. br.ErrMsg = "路演视频Overview为空, 不可推送"
  1814. return
  1815. }
  1816. // 重新发送超过100字符直接截取
  1817. theme := reportInfo.Abstract
  1818. runeAbs := []rune(reportInfo.Abstract)
  1819. runeThe := make([]rune, 0)
  1820. if len(runeAbs) > 100 {
  1821. runeThe = runeAbs[:100]
  1822. theme = string(runeThe)
  1823. }
  1824. // 获取邮件配置
  1825. conf := new(models.EnglishReportEmailConf)
  1826. authKey := "english_report_email_conf"
  1827. confAuth, e := company.GetConfigDetailByCode(authKey)
  1828. if e != nil {
  1829. br.Msg = "无权操作"
  1830. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  1831. return
  1832. }
  1833. if confAuth.ConfigValue == "" {
  1834. br.Msg = "无权操作"
  1835. br.ErrMsg = "群发邮件配置为空"
  1836. return
  1837. }
  1838. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  1839. br.Msg = "无权操作"
  1840. br.ErrMsg = "群发邮件配置有误"
  1841. return
  1842. }
  1843. authArr := strings.Split(conf.SendAuthGroup, ",")
  1844. if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  1845. br.Msg = "无权操作"
  1846. return
  1847. }
  1848. // 获取模板
  1849. confKey := "english_video_email_tmp"
  1850. confTmp, e := company.GetConfigDetailByCode(confKey)
  1851. if e != nil {
  1852. br.Msg = "获取邮件模板失败"
  1853. br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
  1854. return
  1855. }
  1856. if confTmp.ConfigValue == `` {
  1857. br.Msg = "邮件模板有误"
  1858. br.ErrMsg = "邮件模板为空, 不可推送"
  1859. return
  1860. }
  1861. // 获取发送失败的日志
  1862. emailIds := make([]int, 0)
  1863. emails := make([]*models.EnglishReportEmail, 0)
  1864. logIds := make([]int, 0)
  1865. if req.SendId > 0 {
  1866. logItem, e := models.GetEnglishReportEmailLogById(req.SendId)
  1867. if e != nil {
  1868. br.Msg = "发送失败"
  1869. br.ErrMsg = "获取原邮件推送日志失败, Err: " + e.Error()
  1870. return
  1871. }
  1872. emailIds = append(emailIds, logItem.EmailId)
  1873. logIds = append(logIds, logItem.Id)
  1874. } else {
  1875. var logCond string
  1876. var logPars []interface{}
  1877. logCond += ` AND report_id = ? AND send_status = 0 AND report_type=1`
  1878. logPars = append(logPars, req.ReportId)
  1879. logList, e := models.GetEnglishReportEmailLogList(logCond, logPars)
  1880. if e != nil {
  1881. br.Msg = "发送失败"
  1882. br.ErrMsg = "获取原邮件推送日志列表失败, Err: " + e.Error()
  1883. return
  1884. }
  1885. for i := range logList {
  1886. emailIds = append(emailIds, logList[i].EmailId)
  1887. logIds = append(logIds, logList[i].Id)
  1888. }
  1889. }
  1890. // 获取收件人列表
  1891. // 注: 此处不从失败日志中取email, 因为有可能是地址有误导致的推送失败
  1892. if len(emailIds) > 0 {
  1893. var emailCond string
  1894. var emailPars []interface{}
  1895. emailCond += ` AND id IN (` + utils.GetOrmInReplace(len(emailIds)) + `) and enabled = 1 `
  1896. emailPars = append(emailPars, emailIds)
  1897. em, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  1898. if e != nil {
  1899. br.Msg = "获取收件人列表失败"
  1900. br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
  1901. return
  1902. }
  1903. emails = em
  1904. }
  1905. if len(emails) == 0 {
  1906. br.Msg = "无邮件可推送"
  1907. return
  1908. }
  1909. // 推送信息
  1910. nowTime := time.Now().Local()
  1911. sendData := make([]*services.EnglishReportSendEmailRequest, 0)
  1912. logData := make([]*models.EnglishReportEmailLog, 0)
  1913. for i := range emails {
  1914. r := new(services.EnglishReportSendEmailRequest)
  1915. r.ReportId = reportInfo.Id
  1916. r.EmailId = emails[i].Id
  1917. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  1918. r.Subject = theme
  1919. r.FromAlias = conf.FromAlias
  1920. // 填充模板
  1921. t := reportInfo.PublishTime.Format("02/01/2006")
  1922. l := fmt.Sprintf(utils.EnglishShareUrl+"/roadshow/detail?code=%s&ShareEmail=%d", reportInfo.VideoCode, emails[i].Id)
  1923. ct := confTmp.ConfigValue
  1924. ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
  1925. ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
  1926. ct = strings.Replace(ct, "{{REPORT_CONTENT}}", reportInfo.Overview, 1)
  1927. ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
  1928. ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
  1929. r.HtmlBody = ct
  1930. r.ReportTitle = reportInfo.Title
  1931. r.ReportAbstract = reportInfo.Abstract
  1932. r.ReportContent = reportInfo.Overview
  1933. r.ReportShareLink = l
  1934. r.ReportTime = t
  1935. logItem := &models.EnglishReportEmailLog{
  1936. ReportId: reportInfo.Id,
  1937. ReportType: 1,
  1938. EmailId: emails[i].Id,
  1939. Email: emails[i].Email,
  1940. Source: models.EnglishReportEmailLogSourceAli,
  1941. SendStatus: models.EnglishReportEmailLogStatusIng,
  1942. CreateTime: nowTime,
  1943. }
  1944. r.EmailLog = logItem
  1945. sendData = append(sendData, r)
  1946. // 日志
  1947. logData = append(logData, logItem)
  1948. }
  1949. // 标记原有日志为已删除
  1950. if len(logIds) > 0 {
  1951. if e = models.DeleteEnglishReportEmailLogByIds(logIds); e != nil {
  1952. br.Msg = "发送失败"
  1953. br.ErrMsg = "删除原邮件日志失败, Err: " + e.Error()
  1954. return
  1955. }
  1956. }
  1957. // 写入新的日志
  1958. for _, v := range logData {
  1959. if e = v.Create(); e != nil {
  1960. br.Msg = "操作失败"
  1961. br.ErrMsg = "写入群发邮件日志失败, Err: " + e.Error()
  1962. return
  1963. }
  1964. }
  1965. // 群发邮件
  1966. go func() {
  1967. services.BatchSendAliEnglishReportEmail(sendData)
  1968. }()
  1969. br.Ret = 200
  1970. br.Success = true
  1971. br.Msg = "操作成功"
  1972. }
  1973. // EditEnabled
  1974. // @Title 移动至当前客户
  1975. // @Description 移动至当前客户
  1976. // @Param request body models.EnglishReportEditEnabledReq true "type json string"
  1977. // @Success 200 string "操作成功"
  1978. // @router /email/moveToCurrent [post]
  1979. func (this *EnglishReportEmailController) MoveToCurrent() {
  1980. br := new(models.BaseResponse).Init()
  1981. br.IsSendEmail = false
  1982. defer func() {
  1983. this.Data["json"] = br
  1984. this.ServeJSON()
  1985. }()
  1986. sysUser := this.SysUser
  1987. if sysUser == nil {
  1988. br.Msg = "请登录"
  1989. br.ErrMsg = "请登录,SysUser Is Empty"
  1990. br.Ret = 408
  1991. return
  1992. }
  1993. var req models.EnglishReportMoveReq
  1994. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  1995. br.Msg = "参数解析异常!"
  1996. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1997. return
  1998. }
  1999. if req.EmailId <= 0 {
  2000. br.Msg = "参数有误"
  2001. return
  2002. }
  2003. if req.CompanyId <= 0 {
  2004. br.Msg = "参数有误"
  2005. return
  2006. }
  2007. item, e := models.GetEnglishReportEmailById(req.EmailId)
  2008. if e != nil {
  2009. br.Msg = "联系人已被删除, 请勿操作"
  2010. return
  2011. }
  2012. // 更新联系人
  2013. nowTime := time.Now().Local()
  2014. item.CompanyId = req.CompanyId
  2015. item.ModifyTime = nowTime
  2016. item.Status = 1
  2017. updateCols := []string{"CompanyId", "ModifyTime", "Status"}
  2018. if e = item.Update(updateCols); e != nil {
  2019. br.Msg = "操作失败"
  2020. br.ErrMsg = "更新英文联系人失败, Err:" + e.Error()
  2021. return
  2022. }
  2023. // 更新所属客户状态
  2024. //go func() {
  2025. // _ = services.UpdateEnglishCompanyEnabledByCompanyId(item.CompanyId)
  2026. //}()
  2027. // 记录日志
  2028. go func() {
  2029. opByte, e := json.Marshal(req)
  2030. if e != nil {
  2031. return
  2032. }
  2033. opData := string(opByte)
  2034. opLog := &models.EnglishReportEmailOpLog{
  2035. CompanyId: item.CompanyId,
  2036. EmailId: item.Id,
  2037. AdminId: sysUser.AdminId,
  2038. OptionData: opData,
  2039. OptionType: models.EnglishReportEmailOpLogTypeMoveToCurrent,
  2040. CreateTime: nowTime,
  2041. }
  2042. if e = opLog.Create(); e != nil {
  2043. return
  2044. }
  2045. }()
  2046. br.Ret = 200
  2047. br.Success = true
  2048. br.Msg = "操作成功"
  2049. }