email.go 61 KB

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