123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package accounting
- import (
- "context"
- "gd_management/apis"
- "gd_management/common.in/config"
- "gd_management/common.in/storage"
- "gd_management/common.in/utils"
- "gd_management/errors"
- "gd_management/thirdparty"
- "fmt"
- "gopkg.in/gomail.v2"
- "github.com/astaxie/beego/orm"
- "go.uber.org/zap"
- "strconv"
- "strings"
- "time"
- )
- type BillNotifyInfo struct{
- ContactNumber string `json:"contact_number"`
- Email string `json:"email"`
- CompanyName string `json:"company_name"`
- BillCreateTime string `json:"bill_create_time"`
- BillTime string `json:"bill_time"`
- }
- func SendToMail(to, body,billTime string) error {
- host := config.Conf.Warning.MailHost
- password := config.Conf.Warning.MailPassword
- user := config.Conf.Warning.MailUser
- hostIp := ""
- hostPort := ""
- array := strings.Split(host, ":")
- if len(array) > 1 {
- hostIp = array[0]
- hostPort = array[1]
- } else {
- hostIp = host
- hostPort = "465"
- }
- mailConn := map[string]string{
- "user": user,
- "pass": password,
- "host": hostIp,
- "port": hostPort,
- }
- port, _ := strconv.Atoi(mailConn["port"])
- mailTo := strings.Split(to, ";")
- m := gomail.NewMessage()
- m.SetHeader("From", m.FormatAddress(mailConn["user"],"深云智能"))
- m.SetHeader("To", mailTo...)
- m.SetHeader("Subject", fmt.Sprintf("账单通知(%s)",billTime))
- m.SetBody("text/html", body)
- d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
- err := d.DialAndSend(m)
- return err
- }
- func SendMail(to ,merchantName , month,billTime string){
- if to == "" || merchantName == ""{
- fmt.Println("send email is null")
- return
- }
- body := fmt.Sprintf(`<html><body><p>尊敬的%s:</p>
- <p> 您上月账单已生成,请前往车链平台(https://chelian.sygd.com)确认。若%s前无操作,到期账单将自动确认。如发现账单有任何疑问请随时联系商务人员,感谢您的合作。</p>
- <p><br></p>
- <p> 操作入口: 我的->账单列表</p>
- <p><br></p>
- <p align="right">成都深云智能科技有限公司</p>
- <p><br></p></body></html>`,merchantName,month)
- err := SendToMail(to,body,billTime)
- if err != nil{
- fmt.Println("send email error :",err)
- }
- }
- func BillNotify(ctx context.Context, req *apis.BillNotifyReq, reply *apis.BillNotifyReply) error {
- // update t_gd_merchants set merchant_type=1 where id=8
- if req.Month == "" {
- req.Month = time.Now().AddDate(0, -1, 0).Format("2006-01")
- }
- task := func(o orm.Ormer) error {
- var phoneList []BillNotifyInfo
- 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)
- _, err := o.Raw(sql).QueryRows(&phoneList)
- if err != nil {
- l.Error("func",
- zap.String("call", "BillConfirm"),
- zap.String("args", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- confirmTime,_ := config.Conf.OrderConfirmTime.Int64()
- tempId ,_:=config.Conf.ThirdPart.RongLian.BillNotifyTemplateId.Int64()
- for _,v := range phoneList{
- billTime ,_ := time.Parse("2006-01-02",v.BillCreateTime,)
- billConfirmTime := billTime.AddDate(0,0,int(confirmTime)).Format("2006-01-02")
- // 发送邮件
- SendMail(v.Email,v.CompanyName,billConfirmTime,v.BillTime)
- if tempId != 0{
- data := []interface{}{billConfirmTime}
- if v.ContactNumber != ""{
- if err = thirdparty.SendTemplateSMS(v.ContactNumber, data, int(tempId), config.Conf.ThirdPart.RongLian); err != nil {
- l.Error("func",
- zap.String("call", "ThirdParty.SendTemplateSMS"),
- zap.String("params", v.ContactNumber),
- zap.String("error", err.Error()))
- }
- }
- }
- }
- return nil
- }
- tasks := []storage.DbaTasker{}
- tasks = append(tasks, storage.GenerateDbaTask(task))
- storage.ExecTrans(tasks...)
- //l.Debug(utils.MarshalJsonString(req, reply))
- return nil
- }
|