bill_notify.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package accounting
  4. import (
  5. "context"
  6. "gd_management/apis"
  7. "gd_management/common.in/config"
  8. "gd_management/common.in/storage"
  9. "gd_management/common.in/utils"
  10. "gd_management/errors"
  11. "gd_management/thirdparty"
  12. "fmt"
  13. "gopkg.in/gomail.v2"
  14. "github.com/astaxie/beego/orm"
  15. "go.uber.org/zap"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. type BillNotifyInfo struct{
  21. ContactNumber string `json:"contact_number"`
  22. Email string `json:"email"`
  23. CompanyName string `json:"company_name"`
  24. BillCreateTime string `json:"bill_create_time"`
  25. BillTime string `json:"bill_time"`
  26. }
  27. func SendToMail(to, body,billTime string) error {
  28. host := config.Conf.Warning.MailHost
  29. password := config.Conf.Warning.MailPassword
  30. user := config.Conf.Warning.MailUser
  31. hostIp := ""
  32. hostPort := ""
  33. array := strings.Split(host, ":")
  34. if len(array) > 1 {
  35. hostIp = array[0]
  36. hostPort = array[1]
  37. } else {
  38. hostIp = host
  39. hostPort = "465"
  40. }
  41. mailConn := map[string]string{
  42. "user": user,
  43. "pass": password,
  44. "host": hostIp,
  45. "port": hostPort,
  46. }
  47. port, _ := strconv.Atoi(mailConn["port"])
  48. mailTo := strings.Split(to, ";")
  49. m := gomail.NewMessage()
  50. m.SetHeader("From", m.FormatAddress(mailConn["user"],"深云智能"))
  51. m.SetHeader("To", mailTo...)
  52. m.SetHeader("Subject", fmt.Sprintf("账单通知(%s)",billTime))
  53. m.SetBody("text/html", body)
  54. d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
  55. err := d.DialAndSend(m)
  56. return err
  57. }
  58. func SendMail(to ,merchantName , month,billTime string){
  59. if to == "" || merchantName == ""{
  60. fmt.Println("send email is null")
  61. return
  62. }
  63. body := fmt.Sprintf(`<html><body><p>尊敬的%s:</p>
  64. <p>&nbsp; &nbsp; &nbsp; &nbsp;您上月账单已生成,请前往车链平台(https://chelian.sygd.com)确认。若%s前无操作,到期账单将自动确认。如发现账单有任何疑问请随时联系商务人员,感谢您的合作。</p>
  65. <p><br></p>
  66. <p>&nbsp; &nbsp; &nbsp; &nbsp;操作入口: 我的-&gt;账单列表</p>
  67. <p><br></p>
  68. <p align="right">成都深云智能科技有限公司</p>
  69. <p><br></p></body></html>`,merchantName,month)
  70. err := SendToMail(to,body,billTime)
  71. if err != nil{
  72. fmt.Println("send email error :",err)
  73. }
  74. }
  75. func BillNotify(ctx context.Context, req *apis.BillNotifyReq, reply *apis.BillNotifyReply) error {
  76. // update t_gd_merchants set merchant_type=1 where id=8
  77. if req.Month == "" {
  78. req.Month = time.Now().AddDate(0, -1, 0).Format("2006-01")
  79. }
  80. task := func(o orm.Ormer) error {
  81. var phoneList []BillNotifyInfo
  82. sql := fmt.Sprintf(`select t2.contact_number,t2.email,t2.company_name,t1.bill_create_time,t1.bill_time from t_gd_bill as t1 left join t_gd_merchants as t2 on t1.merchant_id=t2.id where t1.bill_time="%s" and t1.bill_type=2 and t2.is_send_notify=1 group by t1.merchant_id`, req.Month)
  83. _, err := o.Raw(sql).QueryRows(&phoneList)
  84. if err != nil {
  85. l.Error("func",
  86. zap.String("call", "BillConfirm"),
  87. zap.String("args", utils.MarshalJsonString(req)),
  88. zap.String("error", err.Error()))
  89. return errors.DataBaseError
  90. }
  91. confirmTime,_ := config.Conf.OrderConfirmTime.Int64()
  92. tempId ,_:=config.Conf.ThirdPart.RongLian.BillNotifyTemplateId.Int64()
  93. for _,v := range phoneList{
  94. billTime ,_ := time.Parse("2006-01-02",v.BillCreateTime,)
  95. billConfirmTime := billTime.AddDate(0,0,int(confirmTime)).Format("2006-01-02")
  96. // 发送邮件
  97. SendMail(v.Email,v.CompanyName,billConfirmTime,v.BillTime)
  98. if tempId != 0{
  99. data := []interface{}{billConfirmTime}
  100. if v.ContactNumber != ""{
  101. if err = thirdparty.SendTemplateSMS(v.ContactNumber, data, int(tempId), config.Conf.ThirdPart.RongLian); err != nil {
  102. l.Error("func",
  103. zap.String("call", "ThirdParty.SendTemplateSMS"),
  104. zap.String("params", v.ContactNumber),
  105. zap.String("error", err.Error()))
  106. }
  107. }
  108. }
  109. }
  110. return nil
  111. }
  112. tasks := []storage.DbaTasker{}
  113. tasks = append(tasks, storage.GenerateDbaTask(task))
  114. storage.ExecTrans(tasks...)
  115. //l.Debug(utils.MarshalJsonString(req, reply))
  116. return nil
  117. }