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.
 
 
 
 
 
 

147 lines
4.7 KiB

package pay
import (
"fmt"
"time"
"yunyan/comm"
"yunyan/lego/base"
"yunyan/lego/core"
"yunyan/lego/sys/log"
"yunyan/modules"
"yunyan/pb"
)
type apiComp struct {
modules.MCompHttpGate
service base.IRPCXService
module *Pay
options *Options
}
func (this *apiComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.MCompHttpGate.Init(service, module, comp, options)
this.service = service.(base.IRPCXService)
this.module = module.(*Pay)
this.options = options.(*Options)
return
}
func (this *apiComp) Start() (err error) {
if err = this.MCompHttpGate.Start(); err != nil {
return
}
if m, e := this.service.GetModule(comm.ModuleAnalyze); e == nil {
this.module.analyze, _ = m.(comm.IAnalyze)
}
return
}
// 更具订单信息 给用户下发资源
func (this *apiComp) deliverResourceByOrder(user *pb.DBUser, order *pb.DBPayOrder) (err error) {
var (
goodss []*pb.DBGoods
userlog *pb.DBUserUseLog
)
// 1. 检查订单状态
if order.Status != pb.PayOrderStatus_PAY_ORDER_PAID {
err = fmt.Errorf("订单状态错误,无法下发资源: %v", order.Status)
return
}
goodss, err = this.module.model.goodss()
if err != nil {
err = fmt.Errorf("查询商品信息失败: %v", err)
return
}
now := time.Now()
userlog = &pb.DBUserUseLog{
Uid: order.Uid,
Logtype: pb.UserLogType_Pay,
Ts: now.Unix(),
}
for _, v := range goodss {
if v.Id == order.ProductId {
if v.Viptime > 0 {
user.Viplv = 1
prev := user.Vipexptime
days := int64(v.Viptime) * 24 * 60 * 60
mode := "reset"
if user.Vipexptime == 0 || user.Vipexptime < now.Unix() {
user.Vipexptime = now.Unix() + days
} else {
user.Vipexptime = user.Vipexptime + days
mode = "accumulate"
}
userlog.Addvipday = int64(v.Viptime)
this.module.Debug("支付发货",
log.Field{Key: "now", Value: now.Format("2006-01-02 15:04:05")},
log.Field{Key: "prev", Value: prev},
log.Field{Key: "days", Value: days},
log.Field{Key: "mode", Value: mode},
log.Field{Key: "Vipexptime", Value: user.Vipexptime})
}
if v.Ainum > 0 {
user.Aichatintegral += v.Ainum
user.Aichattotalintegral = user.Aichatintegral
userlog.Addagentintegral = v.Ainum
this.module.Debug("支付发货", log.Field{Key: "Aichatintegral", Value: user.Aichatintegral})
}
if v.Meetnum > 0 {
user.Meetintegral += v.Meetnum
user.Meettotalintegral = user.Meetintegral
userlog.Addmeetsecond = int64(v.Meetnum)
this.module.Debug("支付发货", log.Field{Key: "Meetintegral", Value: user.Meetintegral})
}
if v.Tradenum > 0 {
user.Tradeintegral += v.Tradenum
user.Tradetotalintegral = user.Tradeintegral
userlog.Addtradesecond = int64(v.Tradenum)
this.module.Debug("支付发货", log.Field{Key: "Tradeintegral", Value: user.Tradeintegral})
}
userlog.Extra = fmt.Sprintf(`{"order_id":"%s","product_id":"%s","product_name":"%s","amount":%d,"payment_method":%d,"add_vip_days":%d,"add_ai_integral":%d,"add_trade_second":%d,"add_meet_second":%d}`,
order.OrderId, order.ProductId, order.ProductName, order.Amount, order.PaymentMethod,
userlog.Addvipday, userlog.Addagentintegral, userlog.Addtradesecond, userlog.Addmeetsecond)
}
}
// 2. 更新用户信息
if err = this.module.model.updateUser(user); err != nil {
err = fmt.Errorf("更新用户信息失败: %v", err)
return
}
// 3. 写消费日志
if err = this.module.model.addIntegralLog(userlog); err != nil {
err = fmt.Errorf("添加积分日志失败: %v", err)
return
}
// 4. 埋点:订单支付完成 + 资源发放
if this.module.analyze != nil {
// 归属用订单自己的来源设备快照(下单时冻结),与月结算口径一致;
// 老订单没有快照时回退用户最后绑定信息。
paidPid, paidChannel := order.SrcProductid, order.SrcChannelid
if paidPid == 0 {
paidPid = user.Lastbindproductid
}
if paidChannel == "" {
paidChannel = user.Lastbindchannelid
}
this.module.analyze.Report(&comm.StatEvent{
Type: comm.StatEventOrderPaid,
ProductId: paidPid,
ChannelId: paidChannel,
Uid: order.Uid,
Amount: order.Amount,
})
if userlog.Addvipday > 0 || userlog.Addagentintegral > 0 || userlog.Addtradesecond > 0 || userlog.Addmeetsecond > 0 {
this.module.analyze.Report(&comm.StatEvent{
Type: comm.StatEventResourceGrant,
ProductId: paidPid,
ChannelId: paidChannel,
Uid: order.Uid,
GrantVipDay: userlog.Addvipday,
GrantAiIntegral: userlog.Addagentintegral,
GrantTradeSecond: userlog.Addtradesecond,
GrantMeetSecond: userlog.Addmeetsecond,
})
}
}
return
}