mail.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/ep/merlin/model"
  7. "gopkg.in/gomail.v2"
  8. )
  9. const (
  10. _merlinUIAddr = "http://merlin.bilibili.co"
  11. )
  12. // SendMail send mail.
  13. func (s *Service) SendMail(mailType int, machine *model.Machine) (err error) {
  14. var (
  15. mailSendHead = ""
  16. mailSendContext = ""
  17. m = gomail.NewMessage()
  18. user *model.User
  19. )
  20. switch mailType {
  21. //将要过期提醒
  22. case model.MailTypeMachineWillExpired:
  23. if user, err = s.dao.FindUserByUserName(machine.Username); err != nil {
  24. return
  25. }
  26. m.SetHeader("To", user.EMail)
  27. mailSendHead = fmt.Sprintf("machine named [%s] will be expired on %s", machine.Name, machine.EndTime.Format(model.TimeFormat))
  28. m.SetHeader("Subject", mailSendHead)
  29. delayLink := fmt.Sprintf("http://merlin.bilibili.co/#/machine-list?machine_name=%s&username=&page_num=1&page_size=10", machine.Name)
  30. mailSendContext = fmt.Sprintf("<br>可前往Merlin平台申请延期</br><br>链接: <a href=%s>点击</a></br>", delayLink)
  31. //机器删除提醒
  32. case model.MailTypeMachineDeleted:
  33. if user, err = s.dao.FindUserByUserName(machine.Username); err != nil {
  34. return
  35. }
  36. m.SetHeader("To", user.EMail)
  37. mailSendHead = fmt.Sprintf("machine named [%s] has been deleted on %s", machine.Name, time.Now().Format(model.TimeFormat))
  38. m.SetHeader("Subject", mailSendHead)
  39. //机器删除失败提醒
  40. case model.MailTypeTaskDeleteMachineFailed:
  41. m.SetHeader("To", s.c.Mail.NoticeOwner[0])
  42. mailSendHead = fmt.Sprintf("machine named [%s] has been failed to delete by task on %s", machine.Name, time.Now().Format(model.TimeFormat))
  43. m.SetHeader("Subject", mailSendHead)
  44. }
  45. if mailSendHead != "" {
  46. ml := &model.MailLog{
  47. ReceiverName: m.GetHeader("To")[0],
  48. MailType: mailType,
  49. SendHead: mailSendHead,
  50. SendContext: mailSendContext,
  51. }
  52. s.dao.SendMail(m)
  53. s.dao.InsertMailLog(ml)
  54. }
  55. return
  56. }
  57. // SendMailDeleteMachine Send Mail Delete Machine.
  58. func (s *Service) SendMailDeleteMachine(username string, machine *model.Machine) (err error) {
  59. var (
  60. mailSendHead = ""
  61. m = gomail.NewMessage()
  62. user *model.User
  63. )
  64. if user, err = s.dao.FindUserByUserName(machine.Username); err != nil {
  65. return
  66. }
  67. //是不是删自己机器
  68. if username == machine.Username {
  69. m.SetHeader("To", user.EMail)
  70. mailSendHead = fmt.Sprintf("Machine named [%s] has been deleted on %s", machine.Name, time.Now().Format(model.TimeFormat))
  71. m.SetHeader("Subject", mailSendHead)
  72. } else {
  73. var delUser *model.User
  74. if delUser, err = s.dao.FindUserByUserName(username); err != nil {
  75. m.SetHeader("To", user.EMail)
  76. } else {
  77. m.SetHeader("To", user.EMail, delUser.EMail)
  78. }
  79. mailSendHead = fmt.Sprintf("Machine named [%s] has been deleted on %s by %s", machine.Name, time.Now().Format(model.TimeFormat), username)
  80. m.SetHeader("Subject", mailSendHead)
  81. }
  82. for _, header := range m.GetHeader("To") {
  83. ml := &model.MailLog{
  84. ReceiverName: header,
  85. MailType: model.MailTypeMachineDeleted,
  86. SendHead: mailSendHead,
  87. SendContext: "",
  88. }
  89. s.dao.SendMail(m)
  90. s.dao.InsertMailLog(ml)
  91. }
  92. return
  93. }
  94. // SendMailApplyDelayMachineEndTime send mail apply delay machine end time.
  95. func (s *Service) SendMailApplyDelayMachineEndTime(ctx context.Context, auditor, applicant string, machineID int64, currentEndTime, applyEndTime time.Time) (err error) {
  96. var (
  97. userInfo *model.User
  98. machine *model.Machine
  99. )
  100. if userInfo, err = s.QueryUserInfo(ctx, auditor); err != nil {
  101. return
  102. }
  103. if machine, err = s.dao.QueryMachine(machineID); err != nil {
  104. return
  105. }
  106. timeNow := time.Now().Format(model.TimeFormat)
  107. currentET := currentEndTime.Format(model.TimeFormat)
  108. applyET := applyEndTime.Format(model.TimeFormat)
  109. mailSendHead, mailSendContext := applyDelayMachineEndTimeHeadAndContext(machine.Name, applicant, timeNow, currentET, applyET, _merlinUIAddr)
  110. err = s.sendMail(userInfo.EMail, mailSendHead, mailSendContext, model.MailTypeApplyDelayMachineEndTime)
  111. return
  112. }
  113. // SendMailAuditResult send mail audit result.
  114. func (s *Service) SendMailAuditResult(ctx context.Context, auditor, applicant string, machineID int64, auditResult bool) (err error) {
  115. var (
  116. userInfo *model.User
  117. machine *model.Machine
  118. )
  119. if userInfo, err = s.QueryUserInfo(ctx, applicant); err != nil {
  120. return
  121. }
  122. if machine, err = s.dao.QueryMachine(machineID); err != nil {
  123. return
  124. }
  125. timeNow := time.Now().Format(model.TimeFormat)
  126. mailSendHead, mailSendContext := auditResultHeadAndContext(machine.Name, timeNow, auditor, _merlinUIAddr, auditResult)
  127. err = s.sendMail(userInfo.EMail, mailSendHead, mailSendContext, model.MailTypeAuditDelayMachineEndTime)
  128. return
  129. }
  130. func (s *Service) sendMail(receiver, header, body string, mailType int) (err error) {
  131. m := gomail.NewMessage()
  132. m.SetHeader("To", receiver)
  133. m.SetHeader("Subject", header)
  134. m.SetBody("text/html", body)
  135. s.dao.SendMail(m)
  136. ml := &model.MailLog{
  137. ReceiverName: receiver,
  138. MailType: mailType,
  139. SendHead: header,
  140. SendContext: body,
  141. }
  142. err = s.dao.InsertMailLog(ml)
  143. return
  144. }
  145. func applyDelayMachineEndTimeHeadAndContext(machineName, applicant, timeNow, currentEndTime, applyEndTime, operateLink string) (head, context string) {
  146. head = fmt.Sprintf("申请延期机器 机器名:[%s] %s", machineName, timeNow)
  147. context = fmt.Sprintf("<br>申请延期机器: %s</br>"+
  148. "<br>申请者: %s</br>"+
  149. "<br>当前过期时间: %s</br>"+
  150. "<br>申请延期时间: %s</br>"+
  151. "<br>操作链接: <a href=%s>点击</a></br>", machineName, applicant, currentEndTime, applyEndTime, operateLink)
  152. return
  153. }
  154. func auditResultHeadAndContext(machineName, timeNow, auditor, operateLink string, auditResult bool) (head, context string) {
  155. auditResultStr := "驳回"
  156. if auditResult {
  157. auditResultStr = "通过"
  158. }
  159. head = fmt.Sprintf("申请延期机器审批结果 机器名:[%s] %s", machineName, timeNow)
  160. context = fmt.Sprintf("<br>申请延期机器: %s</br>"+
  161. "<br>审批者: %s</br>"+
  162. "<br>申请结果: %s</br>"+
  163. "<br>查看链接: <a href=%s>点击</a></br>", machineName, auditor, auditResultStr, operateLink)
  164. return
  165. }
  166. // SendMailForMultiUsers Send Mail For Multi Users.
  167. func (s *Service) SendMailForMultiUsers(ctx context.Context, receivers []string, mailSendHead string) (err error) {
  168. for _, receiver := range receivers {
  169. var userInfo *model.User
  170. if userInfo, err = s.QueryUserInfo(ctx, receiver); err != nil {
  171. continue
  172. }
  173. err = s.sendMail(userInfo.EMail, mailSendHead, "", model.MailTypeMachineTransfer)
  174. }
  175. return
  176. }