You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.3 KiB
95 lines
3.3 KiB
4 years ago
|
package hook
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"git.oa00.com/go/mysql"
|
||
|
"github.com/shopspring/decimal"
|
||
|
"gopkg.in/gomail.v2"
|
||
|
"gorm.io/gorm"
|
||
|
"log"
|
||
|
"recook/configs"
|
||
|
"recook/internal/v2/logic/finance"
|
||
|
"recook/internal/v2/logic/source/wallet"
|
||
|
finance2 "recook/internal/v2/model/finance"
|
||
|
"recook/internal/v2/model/gys"
|
||
|
"recook/internal/v2/model/gys/enterprise"
|
||
|
manage "recook/internal/v2/model/recook/order"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
var OrderHook = &orderHook{}
|
||
|
|
||
|
type orderHook struct {
|
||
|
}
|
||
|
|
||
|
// Express @Title 发货
|
||
|
func (o *orderHook) Express(orderGoodsDetail *manage.RecookOrderGoodsDetailModel) error {
|
||
|
enterpriseStateModel := enterprise.GysEnterpriseStateModel{UserId: orderGoodsDetail.VendorId}
|
||
|
mysql.Db.Where(&enterpriseStateModel).First(&enterpriseStateModel)
|
||
|
|
||
|
sourceModel := gys.GysSourceModel{Id: enterpriseStateModel.Source}
|
||
|
mysql.Db.First(&sourceModel)
|
||
|
purchasePrice := orderGoodsDetail.PurchasePrice.Mul(sourceModel.Coefficient).Round(2)
|
||
|
freezeAmount := purchasePrice.Mul(decimal.NewFromInt(int64(orderGoodsDetail.Quantity))).Add(orderGoodsDetail.ExpressFee)
|
||
|
//邮件提醒
|
||
|
EmailEarlyWarning(sourceModel.Id)
|
||
|
|
||
|
return mysql.Db.Transaction(func(tx *gorm.DB) error {
|
||
|
// 冻结金额
|
||
|
if sourceModel.BillType == gys.GysSourceModelBillTypeDeposit {
|
||
|
if err := wallet.DepositLogic.Freeze(tx, enterpriseStateModel.Source, freezeAmount); err != nil {
|
||
|
return errors.New("冻结金额")
|
||
|
}
|
||
|
}
|
||
|
// 添加结算信息
|
||
|
if err := finance.BillLogic.Add(tx, enterpriseStateModel.Source, orderGoodsDetail.VendorId, orderGoodsDetail.OrderId, orderGoodsDetail.Id, orderGoodsDetail.GoodsId, orderGoodsDetail.SkuId, purchasePrice); err != nil {
|
||
|
log.Println("[express hook] err:", err)
|
||
|
return errors.New("发货失败")
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// Finish @Title 订单完结
|
||
|
func (o *orderHook) Finish(order *manage.RecookOrderInfoModel) error {
|
||
|
return mysql.Db.Transaction(func(tx *gorm.DB) error {
|
||
|
// 添加结算信息
|
||
|
if err := finance.BillLogic.Finish(tx, order.Id); err != nil {
|
||
|
log.Println("[express hook] err:", err)
|
||
|
return errors.New("发货失败")
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
|
}
|
||
|
|
||
|
//邮件提醒预存款不足
|
||
|
func EmailEarlyWarning(sourceId uint) {
|
||
|
var sourceDetail gys.GysSourceWallet
|
||
|
mysql.Db.Model(&sourceDetail).Where("source_id=?", sourceId).Where("deposit_early_warning_status=?", 1).First(&sourceDetail)
|
||
|
sourceName := gys.GysSourceModel{}
|
||
|
mysql.Db.Model(&sourceName).First(&sourceName, "id=?", sourceId)
|
||
|
if sourceName.BillType == 2 && sourceDetail.DepositEarlyWarningAmount.GreaterThanOrEqual(sourceDetail.DepositAmount) {
|
||
|
//get emails
|
||
|
var emails []finance2.RecookEmailEarlyWarning
|
||
|
mysql.Db.Model(&emails).Find(&emails)
|
||
|
var list []string
|
||
|
for _, v := range emails {
|
||
|
list = append(list, v.Email)
|
||
|
}
|
||
|
str := strings.Join(list, ",")
|
||
|
|
||
|
m := gomail.NewMessage()
|
||
|
m.SetHeader("From", configs.Config_Gys_Email_Setting_Mail)
|
||
|
m.SetHeader("To", str, "monsoon.zhu@akuhome.com")
|
||
|
m.SetHeader("Subject", "运营商预存款不足提醒")
|
||
|
body := fmt.Sprintf("%s.预存款临近预警线,请充值,现还有预存款:%v", sourceName.Name, sourceDetail.DepositAmount)
|
||
|
m.SetBody("text/html", body)
|
||
|
d := gomail.NewDialer(configs.Config_Gys_Email_Setting_Smtp, configs.Config_Gys_Email_Setting_Port, configs.Config_Gys_Email_Setting_User, configs.Config_Gys_Email_Setting_Pass)
|
||
|
if err := d.DialAndSend(m); err != nil {
|
||
|
fmt.Println(err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|