123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- package service
- import (
- "bytes"
- "context"
- "errors"
- "fmt"
- "html/template"
- "time"
- //"go-common/app/job/main/up/conf"
- "go-common/app/admin/main/up/util"
- "go-common/app/job/main/up/dao/upcrm"
- "go-common/app/job/main/up/model/signmodel"
- "go-common/app/job/main/up/model/upcrmmodel"
- account "go-common/app/service/main/account/model"
- "go-common/library/log"
- "go-common/app/job/main/up/conf"
- )
- var (
- //ErrNoAdminName no admin name
- ErrNoAdminName = errors.New("no admin name")
- tmplSignDueTitle *template.Template
- tmplSignDueContent *template.Template
- tmplPayDueTitle *template.Template
- tmplPayDueContent *template.Template
- tmplTaskDueTitle *template.Template
- tmplTaskDueContent *template.Template
- )
- // use for template function call
- var funcHelper = template.FuncMap{
- "Now": time.Now,
- }
- func (s *Service) initEmailTemplate() (err error) {
- if conf.Conf.MailTemplateConf.SignTmplTitle == "" ||
- conf.Conf.MailTemplateConf.SignTmplContent == "" ||
- conf.Conf.MailTemplateConf.PayTmplTitle == "" ||
- conf.Conf.MailTemplateConf.PayTmplContent == "" ||
- conf.Conf.MailTemplateConf.TaskTmplTitle == "" ||
- conf.Conf.MailTemplateConf.TaskTmplContent == "" {
- err = fmt.Errorf(`mail template conf is invalid, check mail-template.toml file, make sure all the following has value:
- TaskTmplContent
- TaskTmplTitle
- PayTmplContent
- PayTmplTitle
- SignTmplContent
- SignTmplTitle`)
- return
- }
- tmplSignDueTitle, err = template.New("signTitle").Funcs(funcHelper).Parse(conf.Conf.MailTemplateConf.SignTmplTitle)
- if err != nil {
- log.Error("parse template fail, err=%v", err)
- return
- }
- tmplSignDueContent, err = template.New("signContent").Funcs(funcHelper).Parse(conf.Conf.MailTemplateConf.SignTmplContent)
- if err != nil {
- log.Error("parse template fail, err=%v", err)
- return
- }
- tmplPayDueTitle, err = template.New("payTitle").Funcs(funcHelper).Parse(conf.Conf.MailTemplateConf.PayTmplTitle)
- if err != nil {
- log.Error("parse template fail, err=%v", err)
- return
- }
- tmplPayDueContent, err = template.New("payContent").Funcs(funcHelper).Parse(conf.Conf.MailTemplateConf.PayTmplContent)
- if err != nil {
- log.Error("parse template fail, err=%v", err)
- return
- }
- tmplTaskDueTitle, err = template.New("taskTitle").Funcs(funcHelper).Parse(conf.Conf.MailTemplateConf.TaskTmplTitle)
- if err != nil {
- log.Error("parse template fail, err=%v", err)
- return
- }
- tmplTaskDueContent, err = template.New("taskContent").Funcs(funcHelper).Parse(conf.Conf.MailTemplateConf.TaskTmplContent)
- if err != nil {
- log.Error("parse template fail, err=%v", err)
- return
- }
- return
- }
- //CheckDateDueJob check task due
- /*
- 快到期的job提醒
- */
- func (s *Service) CheckDateDueJob(date time.Time) {
- log.Info("start run CheckDateDueJob, date=%s", date)
- s.checkSignUpDue(date)
- log.Info("finish run CheckDateDueJob, date=%s", date)
- }
- type dueData struct {
- Signs []*upcrm.SignWithName
- Pays []*upcrm.PayWithAdmin
- Tasks []*upcrm.TaskWithAdmin
- }
- func (d *dueData) addSign(sign *upcrm.SignWithName) {
- d.Signs = append(d.Signs, sign)
- }
- func (d *dueData) addPay(pay *upcrm.PayWithAdmin) {
- d.Pays = append(d.Pays, pay)
- }
- func (d *dueData) addTask(task *upcrm.TaskWithAdmin) {
- d.Tasks = append(d.Tasks, task)
- }
- func getOrCreate(dataMap map[string]*dueData, key string) *dueData {
- var data, ok = dataMap[key]
- if !ok {
- data = &dueData{}
- dataMap[key] = data
- }
- return data
- }
- func getName(infoMap map[int64]*account.Info, mid int64) string {
- if info, ok := infoMap[mid]; ok {
- return info.Name
- }
- return ""
- }
- func (s *Service) checkSignUpDue(date time.Time) {
- // 30天内到期 sign
- list, err := s.crmdb.GetDueSignUp(date, 30)
- s.crmdb.StartTask(upcrmmodel.TaskTypeSignCheckDue, date)
- defer func() {
- if err == nil {
- s.crmdb.FinishTask(upcrmmodel.TaskTypeSignCheckDue, date, upcrmmodel.TaskStateFinish)
- } else {
- s.crmdb.FinishTask(upcrmmodel.TaskTypeSignCheckDue, date, upcrmmodel.TaskStateError)
- }
- }()
- if err != nil {
- log.Error("fail to get due sign, date=%+v, err=%+v", date, err)
- return
- }
- var adminDueDataMap = make(map[string]*dueData)
- var ids []int64
- for _, v := range list {
- ids = append(ids, v.Mid)
- var data = getOrCreate(adminDueDataMap, v.AdminName)
- data.addSign(v)
- }
- // 7天内到期的pay
- listPayDue, err := s.crmdb.GetDuePay(date, 7)
- if err != nil {
- log.Error("fail to get due pay, date=%+v, err=%+v", date, err)
- return
- }
- for _, v := range listPayDue {
- ids = append(ids, v.Mid)
- var data = getOrCreate(adminDueDataMap, v.AdminName)
- data.addPay(v)
- }
- // 到期的任务,
- listTaskDue, err := s.crmdb.GetDueTask(date)
- if err != nil {
- log.Error("fail to get due task, date=%+v, err=%+v", date, err)
- return
- }
- for _, v := range listTaskDue {
- ids = append(ids, v.Mid)
- var data = getOrCreate(adminDueDataMap, v.AdminName)
- data.addTask(v)
- }
- ids = util.Unique(ids)
- infoMap, e := s.acc.GetCachedInfos(context.Background(), ids, "")
- if e == nil {
- for _, v := range list {
- v.Name = getName(infoMap, v.Mid)
- }
- for _, v := range listPayDue {
- v.Name = getName(infoMap, v.Mid)
- }
- for _, v := range listTaskDue {
- v.Name = getName(infoMap, v.Mid)
- }
- }
- for admin, v := range adminDueDataMap {
- var adminAll = append(conf.Conf.MailConf.DueMailReceivers, admin)
- // 发送sign到期邮件
- var due = v
- s.worker.Add(func() {
- var succIds []uint32
- if len(due.Signs) > 0 {
- var e = s.sendMailWithTemplate(due.Signs, tmplSignDueTitle, tmplSignDueContent, adminAll...)
- if e == nil {
- for _, data := range due.Signs {
- succIds = append(succIds, data.ID)
- }
- } else {
- log.Warn("fail to send email, err=%v", e)
- }
- // 更新邮件发送标记
- s.crmdb.UpdateEmailState(signmodel.TableNameSignUp, succIds, signmodel.EmailStateSendSucc)
- }
- if len(due.Pays) > 0 {
- // 发送pay到期邮件
- succIds = nil
- e = s.sendMailWithTemplate(due.Pays, tmplPayDueTitle, tmplPayDueContent, adminAll...)
- if e == nil {
- for _, data := range due.Pays {
- succIds = append(succIds, data.ID)
- }
- } else {
- log.Warn("fail to send email, err=%v", e)
- }
- // 更新邮件发送标记
- s.crmdb.UpdateEmailState(signmodel.TableNameSignPay, succIds, signmodel.EmailStateSendSucc)
- }
- if len(due.Tasks) > 0 {
- // 发送task到期邮件
- e = s.sendMailWithTemplate(due.Tasks, tmplTaskDueTitle, tmplTaskDueContent, adminAll...)
- if e != nil {
- log.Warn("fail to send email, err=%v", e)
- }
- // 这个没有邮件发送的标记
- //s.crmdb.UpdateEmailState(signmodel.TableNameSignPay, succIds, signmodel.EmailStateSendSucc)
- }
- })
- }
- }
- // data, data to generate email content
- // contentTmpl, template to generate email content
- // adminname, slice for all admin name
- //
- func (s *Service) sendMailWithTemplate(data interface{}, subjectTmpl, contentTmpl *template.Template, adminName ...string) (err error) {
- if contentTmpl == nil {
- err = fmt.Errorf("template for email is nil, data=%+v", data)
- log.Error("%s", err)
- return
- }
- var contentBuf = bytes.NewBuffer(nil)
- err = contentTmpl.Execute(contentBuf, data)
- if err != nil {
- log.Error("template fail to execute, err=%v", err)
- return
- }
- var subjectBuf = bytes.NewBuffer(nil)
- err = subjectTmpl.Execute(subjectBuf, data)
- if err != nil {
- log.Error("template fail to execute, err=%v", err)
- return
- }
- var addrs []string
- for _, v := range adminName {
- if v == "" {
- log.Warn("admin name is empty")
- continue
- }
- var addr = fmt.Sprintf("%s@bilibili.com", v)
- addrs = append(addrs, addr)
- }
- //log.Info("email sub=%s, content=%s", subjectBuf.String(), contentBuf.String())
- if len(addrs) == 0 {
- log.Error("admin name is empty, cannot send email, data=%+v", data)
- err = ErrNoAdminName
- return
- }
- log.Info("email send , sub=%s, admin=%s, data=%+v", subjectBuf.String(), adminName, data)
- err = s.maildao.SendMail(contentBuf.String(), subjectBuf.String(), addrs)
- return
- }
|