Files
jx-callback/business/jxutils/smsmsg/smsmsg.go
suyl 2e2a636228 aa
2021-08-31 10:26:27 +08:00

296 lines
9.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package smsmsg
import (
"fmt"
"git.rosy.net.cn/jx-callback/business/partner"
aliyunsmsclient "github.com/KenmyZhang/aliyun-communicate"
dyvmsapiclient "github.com/alibabacloud-go/dyvmsapi-20170525-2.0.2/client"
"github.com/alibabacloud-go/tea/tea"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/baseapi/utils/errlist"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
)
var (
warningMap = map[string]int{
"isv.AMOUNT_NOT_ENOUGH": 1,
"isv.ACCOUNT_ABNORMAL": 1,
"isv.OUT_OF_SERVICE": 1,
"isv.DAY_LIMIT_CONTROL": 1,
}
)
func SendSMSMsg(mobileList []string, signName, templateCode string, templateParam map[string]interface{}, order *model.GoodsOrder) (err error) {
if len(mobileList) > 0 {
errList := errlist.New()
mobileList = jxutils.StringMap2List(jxutils.StringList2Map(mobileList))
for _, mobileNum := range mobileList {
if mobileNum != "" {
globals.SugarLogger.Debugf("SendSMSMsg mobileNum:%s, templateCode:%s", mobileNum, templateCode)
if true {
if response, err := api.SMSClient.Execute(globals.AliKey, globals.AliSecret, mobileNum, signName, templateCode, string(utils.MustMarshal(templateParam))); err != nil {
globals.SugarLogger.Warnf("SendSMSMsg mobileNum:%s failed with error:%v", mobileNum, err)
errList.AddErr(err)
} else if response.Code != aliyunsmsclient.ResponseCodeOk {
errMsg := fmt.Sprintf("SendSMSMsg mobileNum:%s failed with response:%s", mobileNum, utils.Format4Output(response, true))
errList.AddErr(fmt.Errorf(errMsg))
if warningMap[response.Code] == 1 {
globals.SugarLogger.Warnf(errMsg)
} else {
globals.SugarLogger.Infof(errMsg)
}
}
}
}
}
err = errList.GetErrListAsOne()
}
return err
}
func SendVoiceMsg(mobileList []string, templateParam map[string]interface{}) (err error) {
if len(mobileList) > 0 {
errList := errlist.New()
mobileList = jxutils.StringMap2List(jxutils.StringList2Map(mobileList))
for _, mobileNum := range mobileList {
if mobileNum != "" {
request := &dyvmsapiclient.SingleCallByTtsRequest{
CalledNumber: tea.String(mobileNum),
TtsCode: tea.String(globals.SMSVoicePickOrderTemplate),
TtsParam: tea.String(string(utils.MustMarshal(templateParam))),
}
res, _err := api.VoiceClient.SingleCallByTts(request)
if _err != nil {
errList.AddErr(err)
}
if *res.Body.Code != "OK" {
globals.SugarLogger.Debugf("SendVoiceMsg mobileNum:%s failed with response:%s", mobileNum, utils.Format4Output(res, true))
errList.AddErr(fmt.Errorf(*res.Body.Message))
} else {
globals.SugarLogger.Debugf("SendVoiceMsg mobileNum:%s success with response:%s", mobileNum, utils.Format4Output(res, true))
}
}
}
err = errList.GetErrListAsOne()
}
return err
}
func getOrderNotifyPhone(order *model.GoodsOrder) (phoneList []string) {
return dao.GetOrderNotifyPhones(dao.GetDB(), jxutils.GetSaleStoreIDFromOrder(order))
}
func NotifyNewOrder(order *model.GoodsOrder) (err error) {
if isPushSMS(order) {
temp := ""
var price int64
store, _ := dao.GetStoreDetail(dao.GetDB(), jxutils.GetSaleStoreIDFromOrder(order), order.VendorID, order.VendorOrgCode)
if store.VendorPayPercentage < 50 && store.VendorPayPercentage != 0 {
temp = globals.SMSNewOrderTemplate
price = order.ActualPayPrice
} else if store.VendorPayPercentage > 50 {
temp = globals.SMSNewOrderTemplateQ
price = order.ShopPrice
} else if store.VendorPayPercentage == 0 {
if store.PayPercentage > 50 {
temp = globals.SMSNewOrderTemplateQ
price = order.ShopPrice
} else {
temp = globals.SMSNewOrderTemplate
price = order.ActualPayPrice
}
}
err = SendSMSMsg(getOrderNotifyPhone(order), globals.SMSSignName, temp, map[string]interface{}{
"daySeq": order.OrderSeq,
"consigneeName": order.ConsigneeName,
"payMoney": jxutils.IntPrice2StandardString(price),
}, order)
}
return err
}
func NotifyPickOrder(order *model.GoodsOrder) (err error) {
//if isPushSMS(order) {
store, _ := dao.GetStoreDetail(dao.GetDB(), jxutils.GetSaleStoreIDFromOrder(order), order.VendorID, order.VendorOrgCode)
mobileList := []string{}
if store.Tel1 != "" {
mobileList = append(mobileList, store.Tel1)
}
if store.Tel2 != "" {
mobileList = append(mobileList, store.Tel2)
}
if order.NotifyType == 0 && store.SMSNotify != 0 && store.IsOrder == model.NO && store.ID != model.MatterStoreID && store.ID != model.JdShopMainStoreID && len(mobileList) > 0 {
globals.SugarLogger.Debugf("NotifyPickOrder orderID: %s , smsNotify :%d", order.VendorOrderID, store.SMSNotify)
switch store.SMSNotify {
case model.NotifyTypeSMS:
if store.BrandIsOpen&model.BrandOpenSMS != 0 {
if err = SendSMSMsg(mobileList, globals.SMSSignName, globals.SMSPickOrderTemplate, nil, order); err == nil {
order.NotifyType = int(store.SMSNotify)
partner.CurOrderManager.UpdateOrderFields(order, []string{"NotifyType"})
}
} else {
globals.SugarLogger.Debugf("NotifyPickOrder sms brand is close , orderID: %s ,isOpen %d", order.VendorOrderID, store.BrandIsOpen)
}
case model.NotifyTypeVoice:
if store.MarketManPhone == "" {
store.MarketManPhone = "18048531223"
}
if store.BrandIsOpen&model.BrandOpenVoice != 0 {
if err = SendVoiceMsg(mobileList, map[string]interface{}{
"tel": store.MarketManPhone,
}); err == nil {
order.NotifyType = int(store.SMSNotify)
partner.CurOrderManager.UpdateOrderFields(order, []string{"NotifyType"})
}
} else {
globals.SugarLogger.Debugf("NotifyPickOrder voice brand is close , orderID: %s ,isOpen %d", order.VendorOrderID, store.BrandIsOpen)
}
}
}
//}
return err
}
func NotifyOrderCanceled(order *model.GoodsOrder) (err error) {
err = SendSMSMsg(getOrderNotifyPhone(order), globals.SMSSignName, globals.SMSOrderCanceledTemplate, map[string]interface{}{
"vendorName": model.VendorChineseNames[order.VendorID],
"seq": order.OrderSeq,
"orderID": order.VendorOrderID,
}, order)
return err
}
func isPushSMS(order *model.GoodsOrder) bool {
storeID := 0
if order.StoreID == 0 {
storeID = order.JxStoreID
} else {
storeID = order.StoreID
}
stores, _ := dao.GetStoresMapList(dao.GetDB(), []int{order.VendorID}, []int{storeID}, nil, model.StoreStatusAll, model.StoreIsSyncAll, "", "", "")
if len(stores) > 0 {
if stores[0].IsOrder == model.NO {
if storeID == model.MatterStoreID || storeID == model.JdShopMainStoreID {
return false
} else {
return true
}
} else {
return false
}
} else {
return false
}
}
func updateStoreSMSNotifyMark(order *model.GoodsOrder) (err error) {
var db = dao.GetDB()
stores, _ := dao.GetStoreList(db, []int{order.StoreID}, nil, nil, nil, nil, "")
if len(stores) > 0 {
stores[0].SMSNotifyMark = model.YES
_, err = dao.UpdateEntity(db, stores[0], "SMSNotifyMark")
}
return err
}
//每月向用户发送
func NotifyNewUserOrder(order *model.GoodsOrder) (err error) {
var (
db = dao.GetDB()
storeTel string
storeID int
mobile string
)
if order.StoreID == 0 {
storeID = order.JxStoreID
} else {
storeID = order.StoreID
}
if order.ConsigneeMobile2 != "" {
mobile = order.ConsigneeMobile2
uoSMS, err := dao.GetUserOrderSMS(db, mobile, "")
stores, _ := dao.GetStoreList(db, []int{storeID}, nil, nil, nil, nil, "")
if len(stores) > 0 {
if stores[0].Tel1 == "" {
storeTel = stores[0].Tel2
} else {
storeTel = stores[0].Tel1
}
}
if uoSMS == nil {
uoSMSc := &model.UserOrderSms{
Mobile: mobile,
Name: order.ConsigneeName,
VendorUserID: order.VendorUserID,
TotalCount: 0,
SMSMark: model.NO,
}
err = dao.CreateEntity(db, uoSMSc)
err = SendSMSMsg([]string{uoSMSc.Mobile}, globals.SMSSignName, globals.SMSNewUserOrderTemplate, map[string]interface{}{
"tel": storeTel,
}, nil)
if err == nil {
uoSMS2, _ := dao.GetUserOrderSMS(db, mobile, "")
uoSMS2.SMSMark = model.YES
uoSMS2.TotalCount++
_, err = dao.UpdateEntity(db, uoSMS2, "SMSMark", "TotalCount")
}
} else {
if uoSMS.SMSMark != model.YES {
err = SendSMSMsg([]string{uoSMS.Mobile}, globals.SMSSignName, globals.SMSNewUserOrderTemplate, map[string]interface{}{
"tel": storeTel,
}, nil)
if err == nil {
uoSMS.SMSMark = model.YES
uoSMS.TotalCount++
_, err = dao.UpdateEntity(db, uoSMS, "SMSMark", "TotalCount")
}
}
}
}
return err
}
//给配送员发短信
func NotifyNewCourierOrder(bill *model.Waybill) (err error) {
var (
db = dao.GetDB()
storeTel string
storeID int
)
order := &model.GoodsOrder{}
order.VendorOrderID = bill.VendorOrderID
err = dao.GetEntity(db, order, "VendorOrderID")
if order.StoreID == 0 {
storeID = order.JxStoreID
} else {
storeID = order.StoreID
}
stores, _ := dao.GetStoreList(db, []int{storeID}, nil, nil, nil, nil, "")
if len(stores) > 0 {
if stores[0].Tel1 == "" {
storeTel = stores[0].Tel2
} else {
storeTel = stores[0].Tel1
}
}
err = SendSMSMsg([]string{bill.CourierMobile}, globals.SMSSignName, globals.SMSNewOrderTemplate, map[string]interface{}{
"tel": storeTel,
}, nil)
return err
}
//京西订单配送员取货后,给用户发短信提醒
func NotifyJxOrder(order *model.GoodsOrder, bill *model.Waybill) (err error) {
err = SendSMSMsg([]string{order.ConsigneeMobile}, globals.SMSSignName, globals.SMSJxOrderDelivering, map[string]interface{}{
"phone": bill.CourierMobile,
}, order)
return err
}