229 lines
6.7 KiB
Go
229 lines
6.7 KiB
Go
package smsmsg
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
aliyunsmsclient "github.com/KenmyZhang/aliyun-communicate"
|
|
|
|
"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 { //globals.EnableStoreWrite {
|
|
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)
|
|
}
|
|
}
|
|
// else {
|
|
// if order != nil {
|
|
// err = updateStoreSMSNotifyMark(order)
|
|
// }
|
|
// }
|
|
}
|
|
}
|
|
}
|
|
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 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
|
|
}
|