123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247 |
- package english_report
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/models/company"
- "eta/eta_api/services"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/utils"
- "fmt"
- "os"
- "path"
- "strconv"
- "strings"
- "time"
- "github.com/beego/beego/v2/server/web"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/tealeg/xlsx"
- )
- // EnglishReportEmailController 英文研报邮箱/英文客户联系人
- type EnglishReportEmailController struct {
- controllers.BaseAuthController
- }
- type EnglishReportEmailCallBackController struct {
- web.Controller
- }
- // List
- // @Title 英文研报邮箱列表
- // @Description 英文研报邮箱列表
- // @Param CompanyId query int false "客户ID"
- // @Param Keywords query string false "关键词:联系人名称/邮箱地址"
- // @Param SortType query int false "点击量排序方式:1-倒序;2-正序"
- // @Param SortParam query int false "排序字段:1-点击量;2-点击时间 3-用户状态"
- // @Param ListParam query int false "筛选字段参数,用来筛选的字段, 枚举值:1:正式 、 2:临时 、 3:终止 、4:正式+临时 "
- // @Success 200 {object} models.EnglishReportEmailPageListResp
- // @router /email/list [get]
- func (this *EnglishReportEmailController) List() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- companyId, _ := this.GetInt("CompanyId", 0)
- keywords := this.GetString("Keywords", "")
- sortType, _ := this.GetInt("SortType", 1)
- sortParam, _ := this.GetInt("SortParam", 1)
- listParam, _ := this.GetInt("ListParam", 1)
- var cond, order string
- var pars []interface{}
- if companyId > 0 {
- cond += ` AND a.company_id = ?`
- pars = append(pars, companyId)
- }
- if keywords != "" {
- k := "%" + keywords + "%"
- cond += ` AND (a.name LIKE ? OR a.email LIKE ? OR a.mobile LIKE ?)`
- pars = append(pars, k, k, k)
- }
- sortParamArr := []int{1, 2, 3}
- sortTypeArr := []int{1, 2}
- if utils.InArrayByInt(sortParamArr, sortParam) && utils.InArrayByInt(sortTypeArr, sortType) {
- pMap := map[int]string{1: "a.view_total", 2: "a.last_view_time", 3: "a.enabled"}
- tMap := map[int]string{1: "DESC", 2: "ASC"}
- order = fmt.Sprintf(` ORDER BY %s %s`, pMap[sortParam], tMap[sortType])
- }
- if listParam == 1 {
- cond += ` AND a.status = 1`
- }
- if listParam == 2 {
- cond += ` AND a.status = 2`
- }
- if listParam == 3 {
- cond += ` AND a.status = 3`
- }
- if listParam == 4 {
- cond += ` AND (a.status = 1 OR a.status = 2)`
- }
- var startSize int
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- total, list, e := models.GetEnglishReportEmailPageList(cond, pars, order, startSize, pageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取邮箱列表失败, Err: " + e.Error()
- return
- }
- respList := make([]*models.EnglishReportEmailResp, 0)
- for i := range list {
- item := &models.EnglishReportEmailResp{
- Id: list[i].Id,
- Name: list[i].Name,
- Email: list[i].Email,
- Mobile: list[i].Mobile,
- CountryCode: list[i].CountryCode,
- BusinessCardUrl: list[i].BusinessCardUrl,
- AdminName: list[i].AdminName,
- CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
- ViewTotal: list[i].ViewTotal,
- Enabled: list[i].Enabled,
- CompanyName: list[i].CompanyName,
- RegisterCompanyName: list[i].RegisterCompanyName,
- RegisterTime: list[i].RegisterTime.Format(utils.FormatDateTime),
- }
- if item.RegisterTime == "0001-01-01 00:00:00" {
- item.RegisterTime = ""
- }
- respList = append(respList, item)
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := &models.EnglishReportEmailPageListResp{
- Paging: page,
- List: respList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Save
- // @Title 保存英文研报邮箱
- // @Description 保存英文研报邮箱
- // @Param request body models.EnglishReportEmailSaveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /email/save [post]
- func (this *EnglishReportEmailController) Save() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishReportEmailSaveReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.Name == "" {
- br.Msg = "请输入客户名称"
- return
- }
- req.Email = strings.Replace(req.Email, " ", "", -1)
- if req.Email == "" {
- br.Msg = "请输入邮箱"
- return
- }
- if !utils.ValidateEmailFormatat(req.Email) {
- br.Msg = "邮箱格式有误"
- return
- }
- // 英文客户以邮箱为主, 邮箱去重, 手机号非必填, 此处不做手机号去重
- exist, e := models.GetEnglishReportEmailByEmail(req.Email)
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "保存失败"
- br.ErrMsg = "获取同名邮箱失败, Err:" + e.Error()
- return
- }
- opType := 0
- now := time.Now().Local()
- item := new(models.EnglishReportEmail)
- if req.Id > 0 {
- // 更新
- if exist != nil && exist.Id != req.Id {
- br.Msg = "该邮箱已存在"
- return
- }
- if exist != nil {
- createTime, _ := time.Parse(utils.FormatDateTime, exist.CreateTime)
- modifyTime, _ := time.Parse(utils.FormatDateTime, exist.ModifyTime)
- item = &models.EnglishReportEmail{
- Id: exist.Id,
- CompanyId: exist.CompanyId,
- Name: exist.Name,
- Email: exist.Email,
- Mobile: exist.Mobile,
- CountryCode: exist.CountryCode,
- BusinessCardUrl: exist.BusinessCardUrl,
- ViewTotal: exist.ViewTotal,
- LastViewTime: exist.LastViewTime,
- IsDeleted: exist.IsDeleted,
- Enabled: exist.Enabled,
- AdminId: exist.AdminId,
- AdminName: exist.AdminName,
- Status: exist.Status,
- CompanyName: exist.CompanyName,
- CreateTime: createTime,
- ModifyTime: modifyTime,
- }
- } else {
- em, e := models.GetEnglishReportEmailById(req.Id)
- if e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "获取邮箱失败, Err:" + e.Error()
- return
- }
- item = em
- }
- item.Name = req.Name
- item.Email = req.Email
- item.Mobile = req.Mobile
- item.CountryCode = req.CountryCode
- item.BusinessCardUrl = req.BusinessCardUrl
- item.ModifyTime = now
- item.Enabled = req.Enabled
- updateCols := []string{"Name", "Email", "Mobile", "CountryCode", "BusinessCardUrl", "ModifyTime", "Enabled"}
- if e = item.Update(updateCols); e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "更新邮箱失败, Err:" + e.Error()
- return
- }
- opType = models.EnglishReportEmailOpLogTypeEdit
- } else {
- // 新增
- if exist != nil {
- respItem := &models.EnglishReportEmailResp{
- Id: exist.Id,
- Name: exist.Name,
- Email: exist.Email,
- Mobile: exist.Mobile,
- CountryCode: exist.CountryCode,
- BusinessCardUrl: exist.BusinessCardUrl,
- AdminName: exist.AdminName,
- CreateTime: exist.CreateTime,
- ViewTotal: exist.ViewTotal,
- Enabled: exist.Enabled,
- CompanyName: exist.CompanyName,
- Status: exist.Status,
- RegisterCompanyName: exist.RegisterCompanyName,
- RegisterTime: exist.RegisterTime,
- }
- if respItem.RegisterTime == "0001-01-01 00:00:00" {
- respItem.RegisterCompanyName = ""
- }
- br.Data = respItem
- br.Msg = "该邮箱已存在"
- br.Ret = 1001
- return
- }
- item = &models.EnglishReportEmail{
- CompanyId: req.CompanyId,
- Name: req.Name,
- Email: req.Email,
- Mobile: req.Mobile,
- CountryCode: req.CountryCode,
- BusinessCardUrl: req.BusinessCardUrl,
- AdminId: sysUser.AdminId,
- AdminName: sysUser.AdminName,
- CreateTime: now,
- ModifyTime: now,
- Enabled: req.Enabled,
- Status: 1,
- }
- if e = item.Create(); e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "新增邮箱失败, Err:" + e.Error()
- return
- }
- opType = models.EnglishReportEmailOpLogTypeAdd
- }
- // 更新客户状态
- go func() {
- _ = services.UpdateEnglishCompanyEnabledByCompanyId(item.CompanyId)
- }()
- // 记录日志
- go func() {
- opByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData := string(opByte)
- opLog := &models.EnglishReportEmailOpLog{
- CompanyId: item.CompanyId,
- EmailId: item.Id,
- AdminId: sysUser.AdminId,
- OptionData: opData,
- OptionType: opType,
- CreateTime: now,
- }
- if e = opLog.Create(); e != nil {
- return
- }
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- //br.Data = item
- }
- // Del
- // @Title 删除英文研报邮箱
- // @Description 删除英文研报邮箱
- // @Param request body models.EnglishReportEmailDelReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /email/del [post]
- func (this *EnglishReportEmailController) Del() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishReportEmailDelReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EmailId <= 0 {
- br.Msg = "参数有误"
- return
- }
- item, e := models.GetEnglishReportEmailById(req.EmailId)
- if e != nil {
- br.Msg = "联系人已被删除, 请勿重复操作"
- return
- }
- nowTime := time.Now().Local()
- item.IsDeleted = 1
- item.ModifyTime = nowTime
- updateCols := []string{"IsDeleted", "ModifyTime"}
- if e = item.Update(updateCols); e != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除英文联系人失败, Err:" + e.Error()
- return
- }
- // 更新客户状态
- go func() {
- _ = services.UpdateEnglishCompanyEnabledByCompanyId(item.CompanyId)
- }()
- // 记录日志
- go func() {
- opByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData := string(opByte)
- opLog := &models.EnglishReportEmailOpLog{
- CompanyId: item.CompanyId,
- EmailId: item.Id,
- AdminId: sysUser.AdminId,
- OptionData: opData,
- OptionType: models.EnglishReportEmailOpLogTypeDel,
- CreateTime: nowTime,
- }
- if e = opLog.Create(); e != nil {
- return
- }
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // EditEnabled
- // @Title 启用/禁用英文研报邮箱
- // @Description 启用/禁用英文研报邮箱
- // @Param request body models.EnglishReportEditEnabledReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /email/editEnabled [post]
- func (this *EnglishReportEmailController) EditEnabled() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishReportEditEnabledReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EmailId <= 0 {
- br.Msg = "参数有误"
- return
- }
- if req.Enabled != 0 && req.Enabled != 1 {
- br.Msg = "参数有误"
- return
- }
- item, e := models.GetEnglishReportEmailById(req.EmailId)
- if e != nil {
- br.Msg = "联系人已被删除, 请勿操作"
- return
- }
- if item.Enabled == req.Enabled {
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- return
- }
- // 更新联系人
- nowTime := time.Now().Local()
- item.Enabled = req.Enabled
- item.ModifyTime = nowTime
- updateCols := []string{"Enabled", "ModifyTime"}
- if e = item.Update(updateCols); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新英文联系人失败, Err:" + e.Error()
- return
- }
- // 更新所属客户状态
- go func() {
- _ = services.UpdateEnglishCompanyEnabledByCompanyId(item.CompanyId)
- }()
- // 记录日志
- go func() {
- opByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData := string(opByte)
- opLog := &models.EnglishReportEmailOpLog{
- CompanyId: item.CompanyId,
- EmailId: item.Id,
- AdminId: sysUser.AdminId,
- OptionData: opData,
- OptionType: models.EnglishReportEmailOpLogTypeEditEnabled,
- CreateTime: nowTime,
- }
- if e = opLog.Create(); e != nil {
- return
- }
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // Send
- // @Title 群发推送
- // @Description 群发推送
- // @Param request body models.EnglishReportEmailSaveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /email/send [post]
- func (this *EnglishReportEmailController) Send() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishReportEmailSendReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportId <= 0 {
- br.Msg = "请选择报告"
- return
- }
- if req.Theme == "" {
- br.Msg = "请输入邮件主题"
- return
- }
- //prefixTheme := "【HI China Macro and Commodity】"
- //if !strings.Contains(req.Theme, prefixTheme) {
- // req.Theme = prefixTheme + req.Theme
- //}
- runeThe := []rune(req.Theme)
- if len(runeThe) > 100 {
- br.Msg = "邮件主题不可超过100字符"
- return
- }
- // 获取报告信息
- reportInfo, e := models.GetEnglishReportItemById(req.ReportId)
- if e != nil {
- br.Msg = "报告信息有误"
- br.ErrMsg = "获取报告信息失败, Err: " + e.Error()
- return
- }
- if reportInfo.EmailState != 0 {
- br.Msg = "请勿重复发送"
- br.ErrMsg = fmt.Sprintf("报告群发邮件状态有误, 当前状态: %d", reportInfo.EmailState)
- return
- }
- if reportInfo.State != 2 {
- br.Msg = "报告未发布"
- br.ErrMsg = fmt.Sprintf("报告状态有误, 当前状态: %d", reportInfo.State)
- return
- }
- if reportInfo.Title == "" {
- br.Msg = "报告标题有误"
- br.ErrMsg = "报告标题为空, 不可推送"
- return
- }
- if reportInfo.Content == `` {
- br.Msg = "报告内容有误"
- br.ErrMsg = "报告内容为空, 不可推送"
- return
- }
- // 获取邮件配置
- conf := new(models.EnglishReportEmailConf)
- authKey := "english_report_email_conf"
- confAuth, e := company.GetConfigDetailByCode(authKey)
- if e != nil {
- br.Msg = "无权操作"
- br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
- return
- }
- if confAuth.ConfigValue == "" {
- br.Msg = "无权操作"
- br.ErrMsg = "群发邮件配置为空"
- return
- }
- if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
- br.Msg = "无权操作"
- br.ErrMsg = "群发邮件配置有误"
- return
- }
- authArr := strings.Split(conf.SendAuthGroup, ",")
- if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
- br.Msg = "无权操作"
- return
- }
- noCompanyIdsMap := make(map[int]struct{})
- if len(req.NoCompanyIds) > 0 {
- for _, v := range req.NoCompanyIds {
- noCompanyIdsMap[v] = struct{}{}
- }
- }
- // 指定品种的客户
- sendCompanyIds := make([]int, 0)
- if len(req.EnPermissions) > 0 {
- companyIds, e := models.GetEnglishCompanyIdsByEnPermissionIds(req.EnPermissions)
- if e != nil {
- br.Msg = "推送失败"
- br.ErrMsg = "获取指定品种的客户IDs失败, Err: " + e.Error()
- return
- }
- for _, v := range companyIds {
- if _, ok := noCompanyIdsMap[v]; !ok {
- sendCompanyIds = append(sendCompanyIds, v)
- }
- }
- }
- // 指定收件人列表
- sendEmailIds := make([]int, 0)
- if req.EmailIds != "" {
- emailIdArr := strings.Split(req.EmailIds, ",")
- if len(emailIdArr) == 0 {
- br.Msg = "指定收件人列表有误"
- br.ErrMsg = fmt.Sprintf("收件人列表为空, 收件人IDs: %s", req.EmailIds)
- return
- }
- for _, m := range emailIdArr {
- v, _ := strconv.Atoi(m)
- sendEmailIds = append(sendEmailIds, v)
- }
- }
- if len(sendCompanyIds) == 0 && len(sendEmailIds) == 0 {
- br.Msg = "收件人列表为空(或收件人账号被禁用)"
- br.ErrMsg = "收件人列表为空, 不可推送"
- return
- }
- // 往正式客户id里面插入0的 目的是为了把临时客户找出来
- sendCompanyIds = append(sendCompanyIds, 0)
- emailCond := " AND enabled = 1 AND status <> 3"
- emailPars := make([]interface{}, 0)
- if len(sendCompanyIds) > 0 && len(sendEmailIds) > 0 {
- emailCond += ` AND (company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `) OR id IN (` + utils.GetOrmInReplace(len(sendEmailIds)) + `))`
- emailPars = append(emailPars, sendCompanyIds)
- emailPars = append(emailPars, sendEmailIds)
- }
- if len(sendCompanyIds) > 0 && len(sendEmailIds) == 0 {
- emailCond += ` AND company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `)`
- emailPars = append(emailPars, sendCompanyIds)
- }
- emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
- if e != nil {
- br.Msg = "获取收件人列表失败"
- br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
- return
- }
- if len(emails) == 0 {
- br.Msg = "收件人列表为空(或收件人账号被禁用)"
- br.ErrMsg = "收件人列表为空, 不可推送"
- return
- }
- // 获取模板
- confKey := "english_report_email_tmp"
- confTmp, e := company.GetConfigDetailByCode(confKey)
- if e != nil {
- br.Msg = "获取邮件模板失败"
- br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
- return
- }
- if confTmp.ConfigValue == `` {
- br.Msg = "邮件模板有误"
- br.ErrMsg = "邮件模板为空, 不可推送"
- return
- }
- // 同一篇报告避免重复推送给同一个邮件
- var cond string
- var pars []interface{}
- cond += ` AND report_id = ? AND send_status = 1 AND report_type = 0`
- pars = append(pars, req.ReportId)
- logs, e := models.GetEnglishReportEmailLogList(cond, pars)
- if e != nil {
- br.Msg = "推送失败"
- br.ErrMsg = "获取邮件发送日志列表失败, Err: " + e.Error()
- return
- }
- repeatMap := make(map[int]bool, 0)
- for i := range logs {
- if logs[i].SendStatus == 1 {
- repeatMap[logs[i].EmailId] = true
- }
- }
- // 推送信息
- nowTime := time.Now().Local()
- sendData := make([]*services.EnglishReportSendEmailRequest, 0)
- logData := make([]*models.EnglishReportEmailLog, 0)
- for i := range emails {
- if repeatMap[emails[i].Id] {
- continue
- }
- r := new(services.EnglishReportSendEmailRequest)
- r.ReportId = reportInfo.Id
- r.EmailId = emails[i].Id
- r.Email = strings.Replace(emails[i].Email, " ", "", -1)
- r.Subject = req.Theme
- r.FromAlias = conf.FromAlias
- // 获取报告Overview部分
- if reportInfo.Overview == "" {
- br.Msg = "报告Overview部分不可为空"
- return
- }
- overview := reportInfo.Overview
- // 填充模板
- t := reportInfo.PublishTime.Format("02/01/2006")
- l := fmt.Sprintf(utils.EnglishShareUrl+"/report/detail?code=%s", reportInfo.ReportCode)
- ct := confTmp.ConfigValue
- ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
- ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
- ct = strings.Replace(ct, "{{REPORT_CONTENT}}", overview, 1)
- ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
- ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
- r.HtmlBody = ct
- r.ReportTitle = reportInfo.Title
- r.ReportAbstract = reportInfo.Abstract
- r.ReportContent = overview
- r.ReportShareLink = l
- r.ReportTime = t
- logItem := &models.EnglishReportEmailLog{
- ReportId: reportInfo.Id,
- EmailId: emails[i].Id,
- Email: emails[i].Email,
- Source: models.EnglishReportEmailLogSourceAli,
- SendStatus: models.EnglishReportEmailLogStatusIng,
- CreateTime: nowTime,
- }
- r.EmailLog = logItem
- sendData = append(sendData, r)
- // 日志
- logData = append(logData, logItem)
- }
- if len(sendData) == 0 {
- br.Msg = "无邮件可推送"
- return
- }
- // 写入日志
- for _, v := range logData {
- if e = v.Create(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "写入群发邮件日志失败, Err: " + e.Error()
- return
- }
- }
- //emailLog := new(models.EnglishReportEmailLog)
- //if e = emailLog.InsertMulti(logData); e != nil {
- // br.Msg = "操作失败"
- // br.ErrMsg = "批量写入群发邮件日志失败, Err: " + e.Error()
- // return
- //}
- // 修改推送状态
- updateCols := []string{"EmailState", "ModifyTime"}
- reportInfo.EmailState = 1
- reportInfo.ModifyTime = time.Now().Local()
- if e = reportInfo.Update(updateCols); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新推送状态失败, Err: " + e.Error()
- return
- }
- // 群发邮件
- go func() {
- services.BatchSendAliEnglishReportEmail(sendData)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // PvList
- // @Title 英文研报邮箱PV列表
- // @Description 英文研报邮箱PV列表
- // @Param ReportId query int false "英文研报ID"
- // @Param ReportType query int false "类型:0英文研报,1英文线上路演"
- // @Success 200 {object} models.EnglishReportEmailPvResp
- // @router /email/pv_list [get]
- func (this *EnglishReportEmailController) PvList() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- reportId, e := this.GetInt("ReportId", 0)
- if reportId == 0 {
- br.Msg = "参数有误"
- return
- }
- reportType, e := this.GetInt("ReportType", 0)
- var cond string
- var pars []interface{}
- cond += ` AND a.report_id = ? AND a.report_type= ?`
- pars = append(pars, reportId, reportType)
- var startSize int
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- total, list, e := models.GetEnglishReportEmailPvPageList(cond, pars, startSize, pageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取英文研报邮箱pv列表失败, Err: " + e.Error()
- return
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := &models.EnglishReportEmailPvPageListResp{
- Paging: page,
- List: list,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // UvList
- // @Title 英文研报邮箱UV列表
- // @Description 英文研报邮箱UV列表
- // @Param ReportId query int false "英文研报ID"
- // @Param ReportType query int false "类型:0英文研报,1英文线上路演"
- // @Success 200 {object} models.EnglishReportEmailPvResp
- // @router /email/uv_list [get]
- func (this *EnglishReportEmailController) UvList() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- reportId, e := this.GetInt("ReportId", 0)
- if reportId == 0 {
- br.Msg = "参数有误"
- return
- }
- reportType, e := this.GetInt("ReportType", 0)
- var cond string
- var pars []interface{}
- cond += ` AND a.report_id = ? AND a.report_type= ?`
- pars = append(pars, reportId, reportType)
- var startSize int
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- total, list, e := models.GetEnglishReportEmailUvPageList(cond, pars, startSize, pageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取英文研报邮箱uv列表失败, Err: " + e.Error()
- return
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := &models.EnglishReportEmailUvPageListResp{
- Paging: page,
- List: list,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // ImportListMatch
- // @Title 英文邮箱/联系人批量导入
- // @Description 英文邮箱/联系人批量导入
- // @Param File query file true "文件"
- // @Param CompanyId query int true "客户ID"
- // @Success 200 string "导入成功"
- // @router /email/import [post]
- func (this *EnglishReportEmailController) ImportListMatch() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- companyId, _ := this.GetInt("CompanyId")
- if companyId <= 0 {
- br.Msg = "请选择客户"
- return
- }
- companyItem, e := models.GetEnglishCompanyById(companyId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "客户已被删除, 请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取客户信息失败, Err: " + e.Error()
- return
- }
- file, h, err := this.GetFile("File")
- if err != nil {
- br.Msg = "获取文件失败"
- br.ErrMsg = "获取文件失败,Err:" + err.Error()
- return
- }
- defer func() {
- _ = file.Close()
- }()
- ext := path.Ext(h.Filename)
- if ext != ".xlsx" && ext != ".xls" {
- br.Msg = "请上传Excel文件"
- return
- }
- uploadDir := "static/xls"
- err = os.MkdirAll(uploadDir, utils.DIR_MOD)
- if err != nil {
- br.Msg = "存储目录创建失败"
- br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
- return
- }
- uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
- err = this.SaveToFile("File", uploadPath)
- if err != nil {
- br.Msg = "文件保存失败"
- br.ErrMsg = "文件保存失败,Err:" + err.Error()
- return
- }
- defer func() {
- _ = os.Remove(uploadPath)
- }()
- xlFile, err := xlsx.OpenFile(uploadPath)
- if err != nil {
- br.Msg = "文件导入失败"
- br.ErrMsg = "文件导入失败,Err:" + err.Error()
- return
- }
- // 重复邮箱校验
- var cond string
- var pars []interface{}
- emails, e := models.GetEnglishReportEmailList(cond, pars, "")
- if e != nil {
- br.Msg = "导入失败"
- br.ErrMsg = "获取邮箱列表失败, Err:" + e.Error()
- return
- }
- emailArr := make([]string, 0)
- for i := range emails {
- emailArr = append(emailArr, emails[i].Email)
- }
- importList := make([]*models.EnglishReportEmailSaveReq, 0)
- // 遍历sheet页读取
- titleRow := []string{"联系人姓名", "国际区号", "手机号", "邮箱地址"}
- for _, sheet := range xlFile.Sheets {
- // 遍历行读取
- maxRow := sheet.MaxRow
- for i := 0; i < maxRow; i++ {
- // 首行
- if i == 0 {
- row := sheet.Row(i)
- cells := row.Cells
- for k, cell := range cells {
- text := cell.String()
- for k2 := range titleRow {
- if k > k2 {
- break
- }
- if text != titleRow[k] && !strings.Contains(text, titleRow[k]) {
- br.Msg = fmt.Sprintf("匹配失败, 请按模板导入(%s)", titleRow[k])
- br.ErrMsg = "模板格式错误,title:" + text
- return
- }
- }
- }
- }
- // 数据行
- if i >= 1 {
- row := sheet.Row(i)
- cells := row.Cells
- var userName, userCountryCode, userMobile, userEmail string
- for k, cell := range cells {
- v := utils.TrimStr(cell.String())
- if k == 0 {
- userName = v
- continue
- }
- if k == 1 {
- userCountryCode = v
- continue
- }
- if k == 2 {
- userMobile = v
- continue
- }
- if k == 3 {
- userEmail = v
- }
- }
- // 数据为空则读取下一行
- if userName == "" || userEmail == "" {
- continue
- }
- if !utils.ValidateEmailFormatat(userEmail) {
- br.Msg = "存在错误的邮箱格式, 请检查后重新导入"
- return
- }
- // 重复校验
- if utils.InArrayByStr(emailArr, userEmail) {
- continue
- }
- emailArr = append(emailArr, userEmail)
- importList = append(importList, &models.EnglishReportEmailSaveReq{
- CompanyId: companyId,
- Name: userName,
- Email: userEmail,
- Mobile: userMobile,
- CountryCode: userCountryCode,
- Enabled: 1,
- })
- }
- }
- }
- if len(importList) == 0 {
- br.Msg = "无有效数据可导入"
- return
- }
- nowTime := time.Now().Local()
- newEmails := make([]*models.EnglishReportEmail, 0)
- newLogs := make([]*models.EnglishReportEmailOpLog, 0)
- for i := range importList {
- v := &models.EnglishReportEmail{
- CompanyId: importList[i].CompanyId,
- CompanyName: companyItem.CompanyName,
- Name: importList[i].Name,
- Email: importList[i].Email,
- Mobile: importList[i].Mobile,
- CountryCode: importList[i].CountryCode,
- Enabled: importList[i].Enabled,
- AdminId: sysUser.AdminId,
- AdminName: sysUser.AdminName,
- Status: 1,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- }
- newEmails = append(newEmails, v)
- newByte, e := json.Marshal(importList[i])
- if e != nil {
- br.Msg = "导入失败"
- br.ErrMsg = "新邮箱/联系转JSON失败, Err: " + e.Error()
- return
- }
- newLogs = append(newLogs, &models.EnglishReportEmailOpLog{
- CompanyId: importList[i].CompanyId,
- AdminId: sysUser.AdminId,
- OptionData: string(newByte),
- OptionType: models.EnglishReportEmailOpLogTypeImport,
- CreateTime: nowTime,
- })
- }
- if e = models.MultiCreateEnglishEmail(newEmails, newLogs); e != nil {
- br.Msg = "导入失败"
- br.ErrMsg = "批量新增英文邮箱/联系人失败, Err: " + e.Error()
- return
- }
- br.Msg = "导入成功"
- br.Ret = 200
- br.Success = true
- }
- // ViewList
- // @Title 英文研报邮箱/联系人阅读列表
- // @Description 英文研报邮箱/联系人阅读列表
- // @Param EmailId query int false "英文邮箱/联系人ID"
- // @Param SortParam query int false "排序字段:1-点击量;2-点击时间"
- // @Param SortType query int false "排序方式:1-倒序;2-正序"
- // @Param EnPermissionIds query string false "品种权限IDs(字符串)"
- // @Success 200 {object} models.EnglishEmailViewPageListResp
- // @router /email/view_list [get]
- func (this *EnglishReportEmailController) ViewList() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- emailId, e := this.GetInt("EmailId", 0)
- if emailId <= 0 {
- br.Msg = "参数有误"
- return
- }
- // 默认点击时间降序
- sortParam, _ := this.GetInt("SortParam", 2)
- sortType, _ := this.GetInt("SortType", 1)
- clickType := this.GetString("ClickType", "")
- strPermissionIds := this.GetString("EnPermissionIds", "")
- var cond, order string
- var pars []interface{}
- cond += ` AND email_id = ? `
- pars = append(pars, emailId)
- if clickType != "" {
- cond += ` AND report_type = ? `
- pars = append(pars, clickType)
- }
- // 品种筛选
- if strPermissionIds != "" {
- permissionIdArr := strings.Split(strPermissionIds, ",")
- permissionIds := make([]int, 0)
- for _, s := range permissionIdArr {
- p, e := strconv.Atoi(s)
- if e != nil {
- br.Msg = "品种权限有误"
- br.ErrMsg = "品种权限筛选有误"
- return
- }
- permissionIds = append(permissionIds, p)
- }
- if len(permissionIds) == 0 {
- br.Msg = "品种权限有误"
- br.ErrMsg = "品种权限筛选ID为空"
- return
- }
- reportIds, e := models.GetEnglishReportIdsByEnPermissionIds(permissionIds)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取报告IDs失败, Err: " + e.Error()
- return
- }
- if len(reportIds) > 0 {
- cond += fmt.Sprintf(` AND report_id IN (%s) `, utils.GetOrmInReplace(len(reportIds)))
- pars = append(pars, reportIds)
- } else {
- cond += ` AND 1=2`
- }
- }
- var startSize int
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- sortArr := []int{1, 2}
- if utils.InArrayByInt(sortArr, sortParam) && utils.InArrayByInt(sortArr, sortType) {
- pMap := map[int]string{1: "view_total", 2: "last_view_time"}
- tMap := map[int]string{1: "DESC", 2: "ASC"}
- order = fmt.Sprintf(` ORDER BY %s %s`, pMap[sortParam], tMap[sortType])
- }
- total, list, e := models.GetEnglishEmailViewPageList(cond, pars, order, startSize, pageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "英文研报邮箱/联系人阅读列表失败, Err: " + e.Error()
- return
- }
- // 报告标题,报告类型
- reportMap := make(map[int]*models.EnglishReport, 0)
- videoMap := make(map[int]*models.EnglishVideo, 0)
- if len(list) > 0 {
- reportIds := make([]int, 0)
- videoIds := make([]int, 0)
- for i := range list {
- if list[i].ClickType == 0 {
- reportIds = append(reportIds, list[i].ReportId)
- } else if list[i].ClickType == 1 {
- videoIds = append(videoIds, list[i].ReportId)
- }
- }
- fields := []string{"id", "title", "classify_name_first", "classify_name_second"}
- if len(reportIds) > 0 {
- reports, e := models.GetEnglishReportByIds(reportIds, fields)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取英文研报列表失败, Err: " + e.Error()
- return
- }
- for i := range reports {
- reportMap[reports[i].Id] = reports[i]
- }
- }
- if len(videoIds) > 0 {
- videos, e := models.GetEnglishVideoByIds(videoIds, fields)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取英文研报列表失败, Err: " + e.Error()
- return
- }
- for i := range videos {
- videoMap[videos[i].Id] = videos[i]
- }
- }
- }
- for i := range list {
- if r, ok := reportMap[list[i].ReportId]; ok {
- list[i].ReportTitle = r.Title
- list[i].ReportType = r.ClassifyNameFirst
- if r.ClassifyNameSecond != "" {
- list[i].ReportType += ` / ` + r.ClassifyNameSecond
- }
- } else if v, ok1 := videoMap[list[i].ReportId]; ok1 {
- list[i].ReportTitle = v.Title
- list[i].ReportType = v.ClassifyNameFirst
- if v.ClassifyNameSecond != "" {
- list[i].ReportType += ` / ` + v.ClassifyNameSecond
- }
- }
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := &models.EnglishEmailViewPageListResp{
- Paging: page,
- List: list,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // LogList
- // @Title 邮件群发日志列表
- // @Description 邮件群发日志列表
- // @Param ReportId query int true "报告ID"
- // @Param ReportType query int true "类型:0英文研报,1英文线上路演"
- // @Param SendStatus query int false "发送状态:-1-已发送;0-发送失败;1-发送成功"
- // @Param Keywords query string false "关键词:邮箱"
- // @Success 200 {object} models.EnglishReportEmailLogPageListResp
- // @router /email/log_list [get]
- func (this *EnglishReportEmailController) LogList() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- reportId, _ := this.GetInt("ReportId", 0)
- if reportId <= 0 {
- br.Msg = "参数有误"
- return
- }
- reportType, _ := this.GetInt("ReportType", 0)
- keywords := this.GetString("Keywords", "")
- var startSize int
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- var cond string
- var pars []interface{}
- cond += ` AND report_id = ? AND report_type = ?`
- pars = append(pars, reportId, reportType)
- // 推送状态
- status := this.GetString("SendStatus", "")
- if status != "" {
- statusInt, e := strconv.Atoi(status)
- if e != nil {
- br.Msg = "状态类型有误"
- return
- }
- cond += ` AND send_status = ?`
- pars = append(pars, statusInt)
- }
- // 关键词
- if keywords != "" {
- k := fmt.Sprint("%", keywords, "%")
- cond += ` AND email LIKE ? `
- pars = append(pars, k)
- }
- total, list, e := models.GetEnglishReportEmailLogPageList(cond, pars, "", startSize, pageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取邮箱列表失败, Err: " + e.Error()
- return
- }
- respList := make([]*models.EnglishReportEmailLogPageList, 0)
- for i := range list {
- v := new(models.EnglishReportEmailLogPageList)
- // 推送结果
- v.ResultMsg = "发送成功"
- if list[i].ErrMsg != "" {
- v.ResultMsg = list[i].ErrMsg
- }
- v.SendId = list[i].Id
- v.ReportId = list[i].ReportId
- v.EmailId = list[i].EmailId
- v.Email = list[i].Email
- v.SendStatus = list[i].SendStatus
- v.Source = list[i].Source
- v.CreateTime = list[i].CreateTime.Format(utils.FormatDateTime)
- respList = append(respList, v)
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := &models.EnglishReportEmailLogPageListResp{
- Paging: page,
- List: respList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Resend
- // @Title 重新推送
- // @Description 群发推送
- // @Param request body models.EnglishReportEmailResendReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /email/resend [post]
- func (this *EnglishReportEmailController) Resend() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishReportEmailResendReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportId <= 0 {
- br.Msg = "请选择报告"
- return
- }
- // 获取报告信息
- reportInfo, e := models.GetEnglishReportItemById(req.ReportId)
- if e != nil {
- br.Msg = "报告信息有误"
- br.ErrMsg = "获取报告信息失败, Err: " + e.Error()
- return
- }
- if reportInfo.State != 2 {
- br.Msg = "报告未发布"
- br.ErrMsg = fmt.Sprintf("报告状态有误, 当前状态: %d", reportInfo.State)
- return
- }
- if reportInfo.Title == "" {
- br.Msg = "报告标题有误"
- br.ErrMsg = "报告标题为空, 不可推送"
- return
- }
- if reportInfo.Content == `` {
- br.Msg = "报告内容有误"
- br.ErrMsg = "报告内容为空, 不可推送"
- return
- }
- if reportInfo.Abstract == `` {
- br.Msg = "报告摘要为空"
- br.ErrMsg = "报告摘要为空, 不可推送"
- return
- }
- if reportInfo.Overview == "" {
- br.Msg = "报告Overview部分不可为空"
- br.ErrMsg = "报告Overview为空, 不可推送"
- return
- }
- // 重新发送超过100字符直接截取
- theme := reportInfo.Abstract
- runeAbs := []rune(reportInfo.Abstract)
- runeThe := make([]rune, 0)
- if len(runeAbs) > 100 {
- runeThe = runeAbs[:100]
- theme = string(runeThe)
- }
- // 获取邮件配置
- conf := new(models.EnglishReportEmailConf)
- authKey := "english_report_email_conf"
- confAuth, e := company.GetConfigDetailByCode(authKey)
- if e != nil {
- br.Msg = "无权操作"
- br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
- return
- }
- if confAuth.ConfigValue == "" {
- br.Msg = "无权操作"
- br.ErrMsg = "群发邮件配置为空"
- return
- }
- if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
- br.Msg = "无权操作"
- br.ErrMsg = "群发邮件配置有误"
- return
- }
- authArr := strings.Split(conf.SendAuthGroup, ",")
- if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
- br.Msg = "无权操作"
- return
- }
- // 获取模板
- confKey := "english_report_email_tmp"
- confTmp, e := company.GetConfigDetailByCode(confKey)
- if e != nil {
- br.Msg = "获取邮件模板失败"
- br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
- return
- }
- if confTmp.ConfigValue == `` {
- br.Msg = "邮件模板有误"
- br.ErrMsg = "邮件模板为空, 不可推送"
- return
- }
- // 获取发送失败的日志
- emailIds := make([]int, 0)
- emails := make([]*models.EnglishReportEmail, 0)
- logIds := make([]int, 0)
- if req.SendId > 0 {
- logItem, e := models.GetEnglishReportEmailLogById(req.SendId)
- if e != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "获取原邮件推送日志失败, Err: " + e.Error()
- return
- }
- emailIds = append(emailIds, logItem.EmailId)
- logIds = append(logIds, logItem.Id)
- } else {
- var logCond string
- var logPars []interface{}
- logCond += ` AND report_id = ? AND send_status = 0 AND report_type=0`
- logPars = append(logPars, req.ReportId)
- logList, e := models.GetEnglishReportEmailLogList(logCond, logPars)
- if e != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "获取原邮件推送日志列表失败, Err: " + e.Error()
- return
- }
- for i := range logList {
- emailIds = append(emailIds, logList[i].EmailId)
- logIds = append(logIds, logList[i].Id)
- }
- }
- // 获取收件人列表
- // 注: 此处不从失败日志中取email, 因为有可能是地址有误导致的推送失败
- if len(emailIds) > 0 {
- var emailCond string
- var emailPars []interface{}
- emailCond += ` AND id IN (` + utils.GetOrmInReplace(len(emailIds)) + `) and enabled = 1 `
- emailPars = append(emailPars, emailIds)
- em, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
- if e != nil {
- br.Msg = "获取收件人列表失败"
- br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
- return
- }
- emails = em
- }
- if len(emails) == 0 {
- br.Msg = "无邮件可推送"
- return
- }
- // 推送信息
- nowTime := time.Now().Local()
- sendData := make([]*services.EnglishReportSendEmailRequest, 0)
- logData := make([]*models.EnglishReportEmailLog, 0)
- for i := range emails {
- r := new(services.EnglishReportSendEmailRequest)
- r.ReportId = reportInfo.Id
- r.EmailId = emails[i].Id
- r.Email = strings.Replace(emails[i].Email, " ", "", -1)
- r.Subject = theme
- r.FromAlias = conf.FromAlias
- // 填充模板
- t := reportInfo.PublishTime.Format("02/01/2006")
- l := fmt.Sprintf(utils.EnglishShareUrl+"/report/detail?code=%s", reportInfo.ReportCode)
- ct := confTmp.ConfigValue
- ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
- ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
- ct = strings.Replace(ct, "{{REPORT_CONTENT}}", reportInfo.Overview, 1)
- ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
- ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
- r.HtmlBody = ct
- r.ReportTitle = reportInfo.Title
- r.ReportAbstract = reportInfo.Abstract
- r.ReportContent = reportInfo.Overview
- r.ReportShareLink = l
- r.ReportTime = t
- logItem := &models.EnglishReportEmailLog{
- ReportId: reportInfo.Id,
- EmailId: emails[i].Id,
- Email: emails[i].Email,
- Source: models.EnglishReportEmailLogSourceAli,
- SendStatus: models.EnglishReportEmailLogStatusIng,
- CreateTime: nowTime,
- }
- r.EmailLog = logItem
- sendData = append(sendData, r)
- // 日志
- logData = append(logData, logItem)
- }
- // 标记原有日志为已删除
- if len(logIds) > 0 {
- if e = models.DeleteEnglishReportEmailLogByIds(logIds); e != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "删除原邮件日志失败, Err: " + e.Error()
- return
- }
- }
- // 写入新的日志
- for _, v := range logData {
- if e = v.Create(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "写入群发邮件日志失败, Err: " + e.Error()
- return
- }
- }
- // 群发邮件
- go func() {
- services.BatchSendAliEnglishReportEmail(sendData)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // SendCallBack
- // @Title 群发邮件回调
- // @Description 群发邮件回调
- // @Param request body services.TencentEmailCallBack true "type json string"
- // @Success 200 string "操作成功"
- // @router /email/send/call_back [post]
- func (this *EnglishReportEmailCallBackController) SendCallBack() {
- // 腾讯云后台同一个发信地址只能配置一个回调, 测试环境就不用了, 给正式环境用
- if utils.RunMode != "release" {
- return
- }
- var req services.TencentEmailCallBack
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- return
- }
- // 仅预警失败的回调事件
- alarmTypes := []string{"dropped", "bounce"}
- if !utils.InArrayByStr(alarmTypes, req.Event) {
- return
- }
- reqByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- reqStr := string(reqByte)
- alarm_msg.SendAlarmMsg("腾讯云邮件推送回调信息:"+reqStr, 1)
- // 存在失败情况则更新日志
- var cond string
- var pars []interface{}
- cond = ` AND is_deleted = 0 AND email = ? AND source = ? AND result LIKE ? `
- pars = append(pars, req.Email, models.EnglishReportEmailLogSourceTencent, fmt.Sprint("%", req.BulkId, "%"))
- logItem, e := models.GetEnglishReportEmailLog(cond, pars)
- if e != nil {
- return
- }
- updateCols := []string{"SendStatus", "CallbackData", "ErrMsg"}
- logItem.SendStatus = models.EnglishReportEmailLogStatusFail
- logItem.CallbackData = reqStr
- logItem.ErrMsg = req.Reason
- if e = logItem.Update(updateCols); e != nil {
- return
- }
- }
- // VideoSend
- // @Title 线上路演群发推送
- // @Description 线上路演群发推送
- // @Param request body models.EnglishReportEmailSaveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /email/video/send [post]
- func (this *EnglishReportEmailController) VideoSend() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishReportEmailSendReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportId <= 0 {
- br.Msg = "请选择路演视频"
- return
- }
- if req.Theme == "" {
- br.Msg = "请输入邮件主题"
- return
- }
- //prefixTheme := "【HI China Macro and Commodity】"
- //if !strings.Contains(req.Theme, prefixTheme) {
- // req.Theme = prefixTheme + req.Theme
- //}
- runeThe := []rune(req.Theme)
- if len(runeThe) > 100 {
- br.Msg = "邮件主题不可超过100字符"
- return
- }
- // 获取路演视频信息
- videoInfo, e := models.GetEnglishVideoItemById(req.ReportId)
- if e != nil {
- br.Msg = "路演视频信息有误"
- br.ErrMsg = "获取路演视频信息失败, Err: " + e.Error()
- return
- }
- if videoInfo.EmailState != 0 {
- br.Msg = "请勿重复发送"
- br.ErrMsg = fmt.Sprintf("路演视频群发邮件状态有误, 当前状态: %d", videoInfo.EmailState)
- return
- }
- if videoInfo.State != 2 {
- br.Msg = "路演视频未发布"
- br.ErrMsg = fmt.Sprintf("路演视频状态有误, 当前状态: %d", videoInfo.State)
- return
- }
- if videoInfo.Title == "" {
- br.Msg = "路演视频标题有误"
- br.ErrMsg = "路演视频标题为空, 不可推送"
- return
- }
- if videoInfo.VideoUrl == `` {
- br.Msg = "路演视频地址为空, 不可推送"
- br.ErrMsg = "路演视频内容为空, 不可推送"
- return
- }
- // 获取路演视频Overview部分
- if videoInfo.Overview == "" {
- br.Msg = "路演视频Overview部分不可为空"
- return
- }
- // 获取邮件配置
- conf := new(models.EnglishReportEmailConf)
- authKey := "english_report_email_conf"
- confAuth, e := company.GetConfigDetailByCode(authKey)
- if e != nil {
- br.Msg = "无权操作"
- br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
- return
- }
- if confAuth.ConfigValue == "" {
- br.Msg = "无权操作"
- br.ErrMsg = "群发邮件配置为空"
- return
- }
- if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
- br.Msg = "无权操作"
- br.ErrMsg = "群发邮件配置有误"
- return
- }
- authArr := strings.Split(conf.SendAuthGroup, ",")
- if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
- br.Msg = "无权操作"
- return
- }
- // 指定品种的客户
- sendCompanyIds := make([]int, 0)
- if len(req.EnPermissions) > 0 {
- companyIds, e := models.GetEnglishCompanyIdsByEnPermissionIds(req.EnPermissions)
- if e != nil {
- br.Msg = "推送失败"
- br.ErrMsg = "获取指定品种的客户IDs失败, Err: " + e.Error()
- return
- }
- sendCompanyIds = companyIds
- }
- // 指定收件人列表
- sendEmailIds := make([]int, 0)
- if req.EmailIds != "" {
- emailIdArr := strings.Split(req.EmailIds, ",")
- if len(emailIdArr) == 0 {
- br.Msg = "指定收件人列表有误"
- br.ErrMsg = fmt.Sprintf("收件人列表为空, 收件人IDs: %s", req.EmailIds)
- return
- }
- for _, m := range emailIdArr {
- v, _ := strconv.Atoi(m)
- sendEmailIds = append(sendEmailIds, v)
- }
- }
- if len(sendCompanyIds) == 0 && len(sendEmailIds) == 0 {
- br.Msg = "收件人列表为空(或收件人账号被禁用)"
- br.ErrMsg = "收件人列表为空, 不可推送"
- return
- }
- // 往正式客户id里面插入0的 目的是为了把临时客户找出来
- sendCompanyIds = append(sendCompanyIds, 0)
- emailCond := " AND enabled = 1 AND status <> 3"
- emailPars := make([]interface{}, 0)
- if len(sendCompanyIds) > 0 && len(sendEmailIds) > 0 {
- emailCond += ` AND (company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `) OR id IN (` + utils.GetOrmInReplace(len(sendEmailIds)) + `))`
- emailPars = append(emailPars, sendCompanyIds)
- emailPars = append(emailPars, sendEmailIds)
- }
- if len(sendCompanyIds) > 0 && len(sendEmailIds) == 0 {
- emailCond += ` AND company_id IN (` + utils.GetOrmInReplace(len(sendCompanyIds)) + `)`
- emailPars = append(emailPars, sendCompanyIds)
- }
- emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
- if e != nil {
- br.Msg = "获取收件人列表失败"
- br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
- return
- }
- if len(emails) == 0 {
- br.Msg = "收件人列表为空(或收件人账号被禁用)"
- br.ErrMsg = "收件人列表为空, 不可推送"
- return
- }
- // 获取模板
- confKey := "english_video_email_tmp"
- confTmp, e := company.GetConfigDetailByCode(confKey)
- if e != nil {
- br.Msg = "获取邮件模板失败"
- br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
- return
- }
- if confTmp.ConfigValue == `` {
- br.Msg = "邮件模板有误"
- br.ErrMsg = "邮件模板为空, 不可推送"
- return
- }
- // 同一篇路演视频避免重复推送给同一个邮件
- var cond string
- var pars []interface{}
- cond += ` AND report_id = ? AND send_status = 1 AND report_type = 1`
- pars = append(pars, req.ReportId)
- logs, e := models.GetEnglishReportEmailLogList(cond, pars)
- if e != nil {
- br.Msg = "推送失败"
- br.ErrMsg = "获取邮件发送日志列表失败, Err: " + e.Error()
- return
- }
- repeatMap := make(map[int]bool, 0)
- for i := range logs {
- if logs[i].SendStatus == 1 {
- repeatMap[logs[i].EmailId] = true
- }
- }
- // 推送信息
- nowTime := time.Now().Local()
- sendData := make([]*services.EnglishReportSendEmailRequest, 0)
- logData := make([]*models.EnglishReportEmailLog, 0)
- for i := range emails {
- if repeatMap[emails[i].Id] {
- continue
- }
- r := new(services.EnglishReportSendEmailRequest)
- r.ReportId = videoInfo.Id
- r.EmailId = emails[i].Id
- r.Email = strings.Replace(emails[i].Email, " ", "", -1)
- r.Subject = req.Theme
- r.FromAlias = conf.FromAlias
- overview := videoInfo.Overview
- // 填充模板
- t := videoInfo.PublishTime.Format("02/01/2006")
- l := fmt.Sprintf(utils.EnglishShareUrl+"/roadshow/detail?code=%s&ShareEmail=%d", videoInfo.VideoCode, emails[i].Id)
- ct := confTmp.ConfigValue
- ct = strings.Replace(ct, "{{REPORT_TITLE}}", videoInfo.Title, 1)
- ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", videoInfo.Abstract, 1)
- ct = strings.Replace(ct, "{{REPORT_CONTENT}}", overview, 1)
- ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
- ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
- r.HtmlBody = ct
- r.ReportTitle = videoInfo.Title
- r.ReportAbstract = videoInfo.Abstract
- r.ReportContent = overview
- r.ReportShareLink = l
- r.ReportTime = t
- logItem := &models.EnglishReportEmailLog{
- ReportId: videoInfo.Id,
- ReportType: 1,
- EmailId: emails[i].Id,
- Email: emails[i].Email,
- Source: models.EnglishReportEmailLogSourceAli,
- SendStatus: models.EnglishReportEmailLogStatusIng,
- CreateTime: nowTime,
- }
- r.EmailLog = logItem
- sendData = append(sendData, r)
- // 日志
- logData = append(logData, logItem)
- }
- if len(sendData) == 0 {
- br.Msg = "无邮件可推送"
- return
- }
- // 写入日志
- for _, v := range logData {
- if e = v.Create(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "写入群发邮件日志失败, Err: " + e.Error()
- return
- }
- }
- // 修改推送状态
- updateCols := []string{"EmailState", "ModifyTime"}
- videoInfo.EmailState = 1
- videoInfo.ModifyTime = time.Now().Local()
- if e = videoInfo.Update(updateCols); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新推送状态失败, Err: " + e.Error()
- return
- }
- // 群发邮件
- go func() {
- services.BatchSendAliEnglishReportEmail(sendData)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // VideoResend
- // @Title 线上路演重新推送
- // @Description 线上路演重新推送
- // @Param request body models.EnglishReportEmailResendReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /email/video/resend [post]
- func (this *EnglishReportEmailController) VideoResend() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishReportEmailResendReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportId <= 0 {
- br.Msg = "请选择路演视频"
- return
- }
- // 获取路演视频信息
- reportInfo, e := models.GetEnglishVideoItemById(req.ReportId)
- if e != nil {
- br.Msg = "路演视频信息有误"
- br.ErrMsg = "获取路演视频信息失败, Err: " + e.Error()
- return
- }
- if reportInfo.State != 2 {
- br.Msg = "路演视频未发布"
- br.ErrMsg = fmt.Sprintf("路演视频状态有误, 当前状态: %d", reportInfo.State)
- return
- }
- if reportInfo.Title == "" {
- br.Msg = "路演视频标题有误"
- br.ErrMsg = "路演视频标题为空, 不可推送"
- return
- }
- if reportInfo.Abstract == `` {
- br.Msg = "路演视频摘要为空"
- br.ErrMsg = "路演视频摘要为空, 不可推送"
- return
- }
- if reportInfo.Overview == "" {
- br.Msg = "路演视频Overview部分不可为空"
- br.ErrMsg = "路演视频Overview为空, 不可推送"
- return
- }
- // 重新发送超过100字符直接截取
- theme := reportInfo.Abstract
- runeAbs := []rune(reportInfo.Abstract)
- runeThe := make([]rune, 0)
- if len(runeAbs) > 100 {
- runeThe = runeAbs[:100]
- theme = string(runeThe)
- }
- // 获取邮件配置
- conf := new(models.EnglishReportEmailConf)
- authKey := "english_report_email_conf"
- confAuth, e := company.GetConfigDetailByCode(authKey)
- if e != nil {
- br.Msg = "无权操作"
- br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
- return
- }
- if confAuth.ConfigValue == "" {
- br.Msg = "无权操作"
- br.ErrMsg = "群发邮件配置为空"
- return
- }
- if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
- br.Msg = "无权操作"
- br.ErrMsg = "群发邮件配置有误"
- return
- }
- authArr := strings.Split(conf.SendAuthGroup, ",")
- if !utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
- br.Msg = "无权操作"
- return
- }
- // 获取模板
- confKey := "english_video_email_tmp"
- confTmp, e := company.GetConfigDetailByCode(confKey)
- if e != nil {
- br.Msg = "获取邮件模板失败"
- br.ErrMsg = "获取邮件模板失败, Err: " + e.Error()
- return
- }
- if confTmp.ConfigValue == `` {
- br.Msg = "邮件模板有误"
- br.ErrMsg = "邮件模板为空, 不可推送"
- return
- }
- // 获取发送失败的日志
- emailIds := make([]int, 0)
- emails := make([]*models.EnglishReportEmail, 0)
- logIds := make([]int, 0)
- if req.SendId > 0 {
- logItem, e := models.GetEnglishReportEmailLogById(req.SendId)
- if e != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "获取原邮件推送日志失败, Err: " + e.Error()
- return
- }
- emailIds = append(emailIds, logItem.EmailId)
- logIds = append(logIds, logItem.Id)
- } else {
- var logCond string
- var logPars []interface{}
- logCond += ` AND report_id = ? AND send_status = 0 AND report_type=1`
- logPars = append(logPars, req.ReportId)
- logList, e := models.GetEnglishReportEmailLogList(logCond, logPars)
- if e != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "获取原邮件推送日志列表失败, Err: " + e.Error()
- return
- }
- for i := range logList {
- emailIds = append(emailIds, logList[i].EmailId)
- logIds = append(logIds, logList[i].Id)
- }
- }
- // 获取收件人列表
- // 注: 此处不从失败日志中取email, 因为有可能是地址有误导致的推送失败
- if len(emailIds) > 0 {
- var emailCond string
- var emailPars []interface{}
- emailCond += ` AND id IN (` + utils.GetOrmInReplace(len(emailIds)) + `) and enabled = 1 `
- emailPars = append(emailPars, emailIds)
- em, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
- if e != nil {
- br.Msg = "获取收件人列表失败"
- br.ErrMsg = "获取收件人列表失败, Err: " + e.Error()
- return
- }
- emails = em
- }
- if len(emails) == 0 {
- br.Msg = "无邮件可推送"
- return
- }
- // 推送信息
- nowTime := time.Now().Local()
- sendData := make([]*services.EnglishReportSendEmailRequest, 0)
- logData := make([]*models.EnglishReportEmailLog, 0)
- for i := range emails {
- r := new(services.EnglishReportSendEmailRequest)
- r.ReportId = reportInfo.Id
- r.EmailId = emails[i].Id
- r.Email = strings.Replace(emails[i].Email, " ", "", -1)
- r.Subject = theme
- r.FromAlias = conf.FromAlias
- // 填充模板
- t := reportInfo.PublishTime.Format("02/01/2006")
- l := fmt.Sprintf(utils.EnglishShareUrl+"/roadshow/detail?code=%s&ShareEmail=%d", reportInfo.VideoCode, emails[i].Id)
- ct := confTmp.ConfigValue
- ct = strings.Replace(ct, "{{REPORT_TITLE}}", reportInfo.Title, 1)
- ct = strings.Replace(ct, "{{REPORT_ABSTRACT}}", reportInfo.Abstract, 1)
- ct = strings.Replace(ct, "{{REPORT_CONTENT}}", reportInfo.Overview, 1)
- ct = strings.Replace(ct, "{{REPORT_SHARE_LINK}}", l, 1)
- ct = strings.Replace(ct, "{{REPORT_TIME}}", t, 1)
- r.HtmlBody = ct
- r.ReportTitle = reportInfo.Title
- r.ReportAbstract = reportInfo.Abstract
- r.ReportContent = reportInfo.Overview
- r.ReportShareLink = l
- r.ReportTime = t
- logItem := &models.EnglishReportEmailLog{
- ReportId: reportInfo.Id,
- ReportType: 1,
- EmailId: emails[i].Id,
- Email: emails[i].Email,
- Source: models.EnglishReportEmailLogSourceAli,
- SendStatus: models.EnglishReportEmailLogStatusIng,
- CreateTime: nowTime,
- }
- r.EmailLog = logItem
- sendData = append(sendData, r)
- // 日志
- logData = append(logData, logItem)
- }
- // 标记原有日志为已删除
- if len(logIds) > 0 {
- if e = models.DeleteEnglishReportEmailLogByIds(logIds); e != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "删除原邮件日志失败, Err: " + e.Error()
- return
- }
- }
- // 写入新的日志
- for _, v := range logData {
- if e = v.Create(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "写入群发邮件日志失败, Err: " + e.Error()
- return
- }
- }
- // 群发邮件
- go func() {
- services.BatchSendAliEnglishReportEmail(sendData)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // EditEnabled
- // @Title 移动至当前客户
- // @Description 移动至当前客户
- // @Param request body models.EnglishReportEditEnabledReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /email/moveToCurrent [post]
- func (this *EnglishReportEmailController) MoveToCurrent() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishReportMoveReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EmailId <= 0 {
- br.Msg = "参数有误"
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数有误"
- return
- }
- item, e := models.GetEnglishReportEmailById(req.EmailId)
- if e != nil {
- br.Msg = "联系人已被删除, 请勿操作"
- return
- }
- // 更新联系人
- nowTime := time.Now().Local()
- item.CompanyId = req.CompanyId
- item.ModifyTime = nowTime
- item.Status = 1
- updateCols := []string{"CompanyId", "ModifyTime", "Status"}
- if e = item.Update(updateCols); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新英文联系人失败, Err:" + e.Error()
- return
- }
- // 更新所属客户状态
- //go func() {
- // _ = services.UpdateEnglishCompanyEnabledByCompanyId(item.CompanyId)
- //}()
- // 记录日志
- go func() {
- opByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData := string(opByte)
- opLog := &models.EnglishReportEmailOpLog{
- CompanyId: item.CompanyId,
- EmailId: item.Id,
- AdminId: sysUser.AdminId,
- OptionData: opData,
- OptionType: models.EnglishReportEmailOpLogTypeMoveToCurrent,
- CreateTime: nowTime,
- }
- if e = opLog.Create(); e != nil {
- return
- }
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
|