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.
158 lines
5.1 KiB
158 lines
5.1 KiB
package pay
|
|
|
|
import (
|
|
"yunyan/comm"
|
|
"yunyan/lego/core"
|
|
"yunyan/lego/core/cbase"
|
|
"yunyan/lego/sys/mysql"
|
|
"yunyan/lego/sys/postgres"
|
|
"yunyan/pb"
|
|
)
|
|
|
|
// 代理模型
|
|
type modelComp struct {
|
|
cbase.ModuleCompBase
|
|
module *Pay
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *modelComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.ModuleCompBase.Init(service, module, comp, opt)
|
|
this.module = module.(*Pay)
|
|
if err = mysql.CreateTable(comm.TablePayOrder, &pb.DBPayOrder{}); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 根据商户订单号查询订单
|
|
func (this *modelComp) goodss() (models []*pb.DBGoods, err error) {
|
|
models = make([]*pb.DBGoods, 0)
|
|
err = mysql.Find(comm.TableGoods, &models, "")
|
|
return
|
|
}
|
|
|
|
func (this *modelComp) getUser(uid string) (user *pb.DBUser, err error) {
|
|
user = &pb.DBUser{}
|
|
err = mysql.FindOne(comm.TableUser, user, "uid=?", uid)
|
|
return
|
|
}
|
|
|
|
// getUserDeviceByMac 按 uid + MAC 取用户设备(下单时定位「当前使用设备」)。
|
|
func (this *modelComp) getUserDeviceByMac(uid, devicemac string) (model *pb.DBUserDivice, err error) {
|
|
model = &pb.DBUserDivice{}
|
|
err = mysql.FindOne(comm.TableUserdevice, model, "uid=? AND devicemac=?", uid, devicemac)
|
|
return
|
|
}
|
|
|
|
// getUserDeviceByLicense 按 uid + 授权码取用户设备(客户端只带 license 时用)。
|
|
func (this *modelComp) getUserDeviceByLicense(uid, license string) (model *pb.DBUserDivice, err error) {
|
|
model = &pb.DBUserDivice{}
|
|
err = mysql.FindOne(comm.TableUserdevice, model, "uid=? AND license=?", uid, license)
|
|
return
|
|
}
|
|
|
|
// getUserLastDevice 取用户最后绑定的那台设备(id 最大 = 最近一次绑定)。
|
|
// 客户端没带「当前使用设备」时用它兜底,与 DBUser.Lastbind* 同一语义、但能拿到 license/mac。
|
|
func (this *modelComp) getUserLastDevice(uid string) (model *pb.DBUserDivice, err error) {
|
|
models := make([]*pb.DBUserDivice, 0, 1)
|
|
err = mysql.Table(comm.TableUserdevice).Where("uid=?", uid).Order("id DESC").Limit(1).Find(&models).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(models) == 0 {
|
|
return nil, nil
|
|
}
|
|
return models[0], nil
|
|
}
|
|
|
|
// appGoods 取**管理后台配置**的商品(console 公共库 app_goods 表),按本部署的应用名隔离:
|
|
// 应用名来自 comm.AppName()(环境变量 ANALYZE_APP_NAME,与 console 应用注册表里的名字一致),
|
|
// 所以同一份代码部署给不同应用时,各自只读得到自己的商品,不会串。
|
|
// goodsId 就是下单时写进订单的 product_id(= 苹果/谷歌后台的内购商品 ID)。
|
|
// 应用名没配、或后台没配这个商品时返回 (nil, nil),交调用方决定怎么处理。
|
|
func (this *modelComp) appGoods(goodsId string) (model *comm.AppGoods, err error) {
|
|
appName := comm.AppName()
|
|
if appName == "" || goodsId == "" {
|
|
return nil, nil
|
|
}
|
|
model = &comm.AppGoods{}
|
|
if err = postgres.FindOne(comm.TableAppGoods, model, "app_name=? AND goods_id=?", appName, goodsId); err != nil {
|
|
if err == postgres.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
this.module.Errorln(err)
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|
|
|
|
// getProduct 取产品(订单快照需要产品所属品牌商)。
|
|
func (this *modelComp) getProduct(id uint32) (model *pb.DBProduct, err error) {
|
|
model = &pb.DBProduct{}
|
|
err = postgres.FindOne(comm.TableProduct, model, "id=?", id)
|
|
return
|
|
}
|
|
|
|
func (this *modelComp) updateUser(user *pb.DBUser) (err error) {
|
|
if err = mysql.Save(comm.TableUser, user); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 创建支付订单
|
|
func (this *modelComp) createPayOrder(order *pb.DBPayOrder) (err error) {
|
|
if err = mysql.Insert(comm.TablePayOrder, order); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 更新支付订单
|
|
func (this *modelComp) updatePayOrder(order *pb.DBPayOrder) (err error) {
|
|
if err = mysql.Save(comm.TablePayOrder, order); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 查询支付订单
|
|
func (this *modelComp) getPayOrder(orderId string) (model *pb.DBPayOrder, err error) {
|
|
model = &pb.DBPayOrder{}
|
|
if err = mysql.FindOne(comm.TablePayOrder, model, "order_id=?", orderId); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 查询支付订单
|
|
func (this *modelComp) getPayOrders(uid string) (model []*pb.DBPayOrder, err error) {
|
|
model = make([]*pb.DBPayOrder, 0)
|
|
if err = mysql.FindOne(comm.TablePayOrder, model, "uid=?", uid); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 查询用户支付订单列表(按创建时间倒序, 分页)
|
|
func (this *modelComp) getUserPayOrders(uid string, page, size int32) (models []*pb.DBPayOrder, total int64, err error) {
|
|
models = make([]*pb.DBPayOrder, 0)
|
|
tx := mysql.Table(comm.TablePayOrder).Where("uid = ?", uid)
|
|
if err = tx.Count(&total).Error; err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
offset := (page - 1) * size
|
|
if err = tx.Order("create_time DESC").Offset(int(offset)).Limit(int(size)).Find(&models).Error; err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 添加积分日志
|
|
func (this *modelComp) addIntegralLog(userlog *pb.DBUserUseLog) (err error) {
|
|
err = mysql.Insert(comm.TableUserUseLog, userlog)
|
|
return
|
|
}
|
|
|