package elm

import (
	aliyunsmsclient "github.com/KenmyZhang/aliyun-communicate"

	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/partner"
	"git.rosy.net.cn/jx-callback/globals"
)

const (
	ELM_SMS_REMINDERS_DAIPEISHONG_TEMPLATECODE   = "SMS_175573181" //饿了么待配送模板ID
	ELM_SMS_REMINDERS_PEISHOGNZHONG_TEMPLATECODE = "SMS_175583155" //饿了么配送中模板ID
)

func (c *PurchaseHandler) ClientUrgeOrder(orderID string) (err error) {
	utils.CallFuncAsync(func() {
		var err error
		order, err2 := partner.CurOrderManager.LoadOrder(orderID, model.VendorIDELM)
		if err = err2; err == nil {
			templateCode := ""
			var templateParams map[string]interface{}
			if order.Status == model.OrderStatusFinishedPickup {
				templateCode = ELM_SMS_REMINDERS_DAIPEISHONG_TEMPLATECODE
				templateParams = map[string]interface{}{
					"name": order.ConsigneeName,
				}
			} else if order.Status == model.OrderStatusDelivering {
				bill, err2 := partner.CurOrderManager.LoadWaybill(order.VendorWaybillID, order.WaybillVendorID)
				if err = err2; err == nil {
					templateCode = ELM_SMS_REMINDERS_PEISHOGNZHONG_TEMPLATECODE
					templateParams = map[string]interface{}{
						"name":   order.ConsigneeName,
						"number": bill.CourierMobile,
					}
				}
			}
			if templateCode != "" {
				smsClient := aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/")
				if globals.ReallyCallPlatformAPI {
					_, err = smsClient.Execute(globals.AliKey, globals.AliSecret, order.ConsigneeMobile, globals.SMSSignName, templateCode, string(utils.MustMarshal(templateParams)))
				}
			}
		}
		if err != nil {
			globals.SugarLogger.Warnf("ClientUrgeOrder orderID:%s failed with error:%v", orderID, err)
		}
	})
	return err
}
