email.go 60 KB

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