This commit is contained in:
2026-01-16 18:23:43 +08:00
parent 1417804b66
commit 9f509924b3
8 changed files with 393 additions and 142 deletions

View File

@@ -151,7 +151,7 @@ func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *type
if newPayStatus != yunyinOrder.PayStatus { if newPayStatus != yunyinOrder.PayStatus {
yunyinOrder.PayStatus = newPayStatus yunyinOrder.PayStatus = newPayStatus
if updateYunyinErr := l.svcCtx.YunyinSignPayOrderModel.Update(l.ctx, yunyinOrder); updateYunyinErr != nil { if _, updateYunyinErr := l.svcCtx.YunyinSignPayOrderModel.Update(l.ctx, nil, yunyinOrder); updateYunyinErr != nil {
logx.Errorf("更新云印签订单支付状态失败: %v", updateYunyinErr) logx.Errorf("更新云印签订单支付状态失败: %v", updateYunyinErr)
} else { } else {
logx.Infof("成功更新云印签订单支付状态订单ID: %s, 新支付状态: %d", order.Id, newPayStatus) logx.Infof("成功更新云印签订单支付状态订单ID: %s, 新支付状态: %d", order.Id, newPayStatus)

View File

@@ -17,6 +17,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/Masterminds/squirrel"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
@@ -140,8 +141,21 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
// 查询用户是否有未完成的签署(待签署且待支付) // 查询用户是否有未完成的签署(待签署且待支付)
var yunYinSignPayResult *service.CreateYunYinSignPayOrderResult var yunYinSignPayResult *service.CreateYunYinSignPayOrderResult
unfinishedOrder, findUnfinishedErr := l.svcCtx.YunyinSignPayOrderModel.FindUnfinishedByUserId(l.ctx, userID) // 使用 SelectBuilder 查询未完成的签署订单del_state 由系统自动处理)
if findUnfinishedErr == nil && unfinishedOrder != nil { unfinishedBuilder := l.svcCtx.YunyinSignPayOrderModel.SelectBuilder().
Where(squirrel.Eq{
"user_id": userID,
"sign_status": 0, // 待签署
"pay_status": 0, // 待支付
}).
OrderBy("create_time DESC").
Limit(1)
unfinishedOrders, findUnfinishedErr := l.svcCtx.YunyinSignPayOrderModel.FindAll(l.ctx, unfinishedBuilder, "")
var unfinishedOrder *model.YunyinSignPayOrder
if findUnfinishedErr == nil && len(unfinishedOrders) > 0 {
unfinishedOrder = unfinishedOrders[0]
}
if unfinishedOrder != nil {
// 复用未完成的签署,只获取新的支付链接 // 复用未完成的签署,只获取新的支付链接
logx.Infof("复用未完成的云印签签署任务ID: %s, 参与者ID: %s", unfinishedOrder.TaskId, unfinishedOrder.ParticipantId) logx.Infof("复用未完成的云印签签署任务ID: %s, 参与者ID: %s", unfinishedOrder.TaskId, unfinishedOrder.ParticipantId)
@@ -382,7 +396,7 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
Version: 0, Version: 0,
} }
_, insertYunYinErr := l.svcCtx.YunyinSignPayOrderModel.InsertWithSession(l.ctx, session, &yunyinSignPayOrder) _, insertYunYinErr := l.svcCtx.YunyinSignPayOrderModel.Insert(l.ctx, session, &yunyinSignPayOrder)
if insertYunYinErr != nil { if insertYunYinErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存云印签订单失败: %+v", insertYunYinErr) return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存云印签订单失败: %+v", insertYunYinErr)
} }

View File

@@ -166,7 +166,7 @@ func (l *YunYinSignPayCallbackLogic) YunYinSignPayCallback(w http.ResponseWriter
// 更新支付状态为已支付1 // 更新支付状态为已支付1
if yunyinOrder.PayStatus != 1 { if yunyinOrder.PayStatus != 1 {
yunyinOrder.PayStatus = 1 yunyinOrder.PayStatus = 1
if updateYunyinErr := l.svcCtx.YunyinSignPayOrderModel.Update(l.ctx, yunyinOrder); updateYunyinErr != nil { if _, updateYunyinErr := l.svcCtx.YunyinSignPayOrderModel.Update(l.ctx, nil, yunyinOrder); updateYunyinErr != nil {
logx.Errorf("云印签回调更新云印签订单支付状态失败订单ID: %s, 错误: %v", order.Id, updateYunyinErr) logx.Errorf("云印签回调更新云印签订单支付状态失败订单ID: %s, 错误: %v", order.Id, updateYunyinErr)
} else { } else {
logx.Infof("云印签回调成功更新云印签订单支付状态订单ID: %s", order.Id) logx.Infof("云印签回调成功更新云印签订单支付状态订单ID: %s", order.Id)

View File

@@ -100,7 +100,7 @@ func (l *YunYinSignPayRefundLogic) YunYinSignPayRefund(req *types.YunYinSignPayR
yunyinOrder.PayStatus = 2 yunyinOrder.PayStatus = 2
} }
// 部分退款保持已支付状态1但可以通过其他字段记录退款金额 // 部分退款保持已支付状态1但可以通过其他字段记录退款金额
if updateYunyinErr := l.svcCtx.YunyinSignPayOrderModel.Update(l.ctx, yunyinOrder); updateYunyinErr != nil { if _, updateYunyinErr := l.svcCtx.YunyinSignPayOrderModel.Update(l.ctx, nil, yunyinOrder); updateYunyinErr != nil {
logx.Errorf("更新云印签订单状态失败: %v", updateYunyinErr) logx.Errorf("更新云印签订单状态失败: %v", updateYunyinErr)
} }
} }

View File

@@ -1,10 +1,6 @@
package model package model
import ( import (
"context"
"database/sql"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stores/sqlx"
) )
@@ -16,8 +12,6 @@ type (
// and implement the added methods in customYunyinSignPayOrderModel. // and implement the added methods in customYunyinSignPayOrderModel.
YunyinSignPayOrderModel interface { YunyinSignPayOrderModel interface {
yunyinSignPayOrderModel yunyinSignPayOrderModel
FindUnfinishedByUserId(ctx context.Context, userId string) (*YunyinSignPayOrder, error)
InsertWithSession(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) (sql.Result, error)
} }
customYunyinSignPayOrderModel struct { customYunyinSignPayOrderModel struct {
@@ -26,35 +20,8 @@ type (
) )
// NewYunyinSignPayOrderModel returns a model for the database table. // NewYunyinSignPayOrderModel returns a model for the database table.
func NewYunyinSignPayOrderModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) YunyinSignPayOrderModel { func NewYunyinSignPayOrderModel(conn sqlx.SqlConn, c cache.CacheConf) YunyinSignPayOrderModel {
return &customYunyinSignPayOrderModel{ return &customYunyinSignPayOrderModel{
defaultYunyinSignPayOrderModel: newYunyinSignPayOrderModel(conn, c, opts...), defaultYunyinSignPayOrderModel: newYunyinSignPayOrderModel(conn, c),
} }
} }
// FindUnfinishedByUserId 查找用户未完成的签署(待签署且待支付)
func (m *customYunyinSignPayOrderModel) FindUnfinishedByUserId(ctx context.Context, userId string) (*YunyinSignPayOrder, error) {
query := fmt.Sprintf("select %s from %s where `user_id` = ? and `sign_status` = ? and `pay_status` = ? and `del_state` = ? order by `create_time` desc limit 1", yunyinSignPayOrderRows, m.table)
var resp YunyinSignPayOrder
err := m.QueryRowNoCacheCtx(ctx, &resp, query, userId, 0, 0, 0)
switch err {
case nil:
return &resp, nil
case sqlx.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
// InsertWithSession 在事务中插入数据
func (m *customYunyinSignPayOrderModel) InsertWithSession(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) (sql.Result, error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, yunyinSignPayOrderRowsExpectAutoSet)
if session != nil {
return session.ExecCtx(ctx, query, data.Id, data.OrderId, data.UserId, data.TaskId, data.ParticipantId, data.Amount, data.PayType, data.SignStatus, data.PayStatus, data.SourceOrderCode, data.UserMobile, data.UserName, data.DeleteTime, data.DelState, data.Version)
}
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
return conn.ExecCtx(ctx, query, data.Id, data.OrderId, data.UserId, data.TaskId, data.ParticipantId, data.Amount, data.PayType, data.SignStatus, data.PayStatus, data.SourceOrderCode, data.UserMobile, data.UserName, data.DeleteTime, data.DelState, data.Version)
})
}

View File

@@ -1,6 +1,4 @@
// Code generated by goctl. DO NOT EDIT. // Code generated by goctl. DO NOT EDIT!
// versions:
// goctl version: 1.8.3
package model package model
@@ -9,20 +7,26 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"strings" "strings"
"reflect"
"time" "time"
"github.com/Masterminds/squirrel"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx" "github.com/zeromicro/go-zero/core/stringx"
"jnc-server/common/globalkey"
) )
var ( var (
yunyinSignPayOrderFieldNames = builder.RawFieldNames(&YunyinSignPayOrder{}) yunyinSignPayOrderFieldNames = builder.RawFieldNames(&YunyinSignPayOrder{})
yunyinSignPayOrderRows = strings.Join(yunyinSignPayOrderFieldNames, ",") yunyinSignPayOrderRows = strings.Join(yunyinSignPayOrderFieldNames, ",")
yunyinSignPayOrderRowsExpectAutoSet = strings.Join(stringx.Remove(yunyinSignPayOrderFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") yunyinSignPayOrderRowsExpectAutoSet = strings.Join(stringx.Remove(yunyinSignPayOrderFieldNames, "`create_time`", "`update_time`"), ",")
yunyinSignPayOrderRowsWithPlaceHolder = strings.Join(stringx.Remove(yunyinSignPayOrderFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" yunyinSignPayOrderRowsWithPlaceHolder = strings.Join(stringx.Remove(yunyinSignPayOrderFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
cacheJncYunyinSignPayOrderIdPrefix = "cache:jnc:yunyinSignPayOrder:id:" cacheJncYunyinSignPayOrderIdPrefix = "cache:jnc:yunyinSignPayOrder:id:"
cacheJncYunyinSignPayOrderOrderIdPrefix = "cache:jnc:yunyinSignPayOrder:orderId:" cacheJncYunyinSignPayOrderOrderIdPrefix = "cache:jnc:yunyinSignPayOrder:orderId:"
@@ -31,12 +35,23 @@ var (
type ( type (
yunyinSignPayOrderModel interface { yunyinSignPayOrderModel interface {
Insert(ctx context.Context, data *YunyinSignPayOrder) (sql.Result, error) Insert(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) (sql.Result, error)
FindOne(ctx context.Context, id string) (*YunyinSignPayOrder, error) FindOne(ctx context.Context, id string) (*YunyinSignPayOrder, error)
FindOneByOrderId(ctx context.Context, orderId string) (*YunyinSignPayOrder, error) FindOneByOrderId(ctx context.Context, orderId string) (*YunyinSignPayOrder, error)
FindOneByTaskId(ctx context.Context, taskId string) (*YunyinSignPayOrder, error) FindOneByTaskId(ctx context.Context, taskId string) (*YunyinSignPayOrder, error)
Update(ctx context.Context, data *YunyinSignPayOrder) error Update(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) (sql.Result, error)
Delete(ctx context.Context, id string) error UpdateWithVersion(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) error
Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
SelectBuilder() squirrel.SelectBuilder
DeleteSoft(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) error
FindSum(ctx context.Context, sumBuilder squirrel.SelectBuilder, field string) (float64, error)
FindCount(ctx context.Context, countBuilder squirrel.SelectBuilder, field string) (int64, error)
FindAll(ctx context.Context, rowBuilder squirrel.SelectBuilder, orderBy string) ([]*YunyinSignPayOrder, error)
FindPageListByPage(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*YunyinSignPayOrder, error)
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*YunyinSignPayOrder, int64, error)
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*YunyinSignPayOrder, error)
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*YunyinSignPayOrder, error)
Delete(ctx context.Context, session sqlx.Session, id string) error
} }
defaultYunyinSignPayOrderModel struct { defaultYunyinSignPayOrderModel struct {
@@ -65,35 +80,50 @@ type (
} }
) )
func newYunyinSignPayOrderModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultYunyinSignPayOrderModel { func newYunyinSignPayOrderModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultYunyinSignPayOrderModel {
return &defaultYunyinSignPayOrderModel{ return &defaultYunyinSignPayOrderModel{
CachedConn: sqlc.NewConn(conn, c, opts...), CachedConn: sqlc.NewConn(conn, c),
table: "`yunyin_sign_pay_order`", table: "`yunyin_sign_pay_order`",
} }
} }
func (m *defaultYunyinSignPayOrderModel) Delete(ctx context.Context, id string) error { func (m *defaultYunyinSignPayOrderModel) Insert(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) (sql.Result, error) {
data, err := m.FindOne(ctx, id) data.DelState = globalkey.DelStateNo
if err != nil { m.insertUUID(data)
return err jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, data.Id)
}
jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, id)
jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, data.OrderId) jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, data.OrderId)
jncYunyinSignPayOrderTaskIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderTaskIdPrefix, data.TaskId) jncYunyinSignPayOrderTaskIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderTaskIdPrefix, data.TaskId)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table) query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, yunyinSignPayOrderRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, id) if session != nil {
return session.ExecCtx(ctx, query, data.Id, data.OrderId, data.UserId, data.TaskId, data.ParticipantId, data.Amount, data.PayType, data.SignStatus, data.PayStatus, data.SourceOrderCode, data.UserMobile, data.UserName, data.DeleteTime, data.DelState, data.Version)
}
return conn.ExecCtx(ctx, query, data.Id, data.OrderId, data.UserId, data.TaskId, data.ParticipantId, data.Amount, data.PayType, data.SignStatus, data.PayStatus, data.SourceOrderCode, data.UserMobile, data.UserName, data.DeleteTime, data.DelState, data.Version)
}, jncYunyinSignPayOrderIdKey, jncYunyinSignPayOrderOrderIdKey, jncYunyinSignPayOrderTaskIdKey) }, jncYunyinSignPayOrderIdKey, jncYunyinSignPayOrderOrderIdKey, jncYunyinSignPayOrderTaskIdKey)
return err }
func (m *defaultYunyinSignPayOrderModel) insertUUID(data *YunyinSignPayOrder) {
t := reflect.TypeOf(data).Elem()
v := reflect.ValueOf(data).Elem()
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
if sf.Tag.Get("db") == "id" {
f := v.Field(i)
if f.IsValid() && f.CanSet() && f.Kind() == reflect.String {
if f.String() == "" {
f.SetString(uuid.NewString())
}
}
break
}
}
} }
func (m *defaultYunyinSignPayOrderModel) FindOne(ctx context.Context, id string) (*YunyinSignPayOrder, error) { func (m *defaultYunyinSignPayOrderModel) FindOne(ctx context.Context, id string) (*YunyinSignPayOrder, error) {
jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, id) jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, id)
var resp YunyinSignPayOrder var resp YunyinSignPayOrder
err := m.QueryRowCtx(ctx, &resp, jncYunyinSignPayOrderIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { err := m.QueryRowCtx(ctx, &resp, jncYunyinSignPayOrderIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", yunyinSignPayOrderRows, m.table) query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", yunyinSignPayOrderRows, m.table)
return conn.QueryRowCtx(ctx, v, query, id) return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
}) })
switch err { switch err {
case nil: case nil:
@@ -108,9 +138,9 @@ func (m *defaultYunyinSignPayOrderModel) FindOne(ctx context.Context, id string)
func (m *defaultYunyinSignPayOrderModel) FindOneByOrderId(ctx context.Context, orderId string) (*YunyinSignPayOrder, error) { func (m *defaultYunyinSignPayOrderModel) FindOneByOrderId(ctx context.Context, orderId string) (*YunyinSignPayOrder, error) {
jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, orderId) jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, orderId)
var resp YunyinSignPayOrder var resp YunyinSignPayOrder
err := m.QueryRowIndexCtx(ctx, &resp, jncYunyinSignPayOrderOrderIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { err := m.QueryRowIndexCtx(ctx, &resp, jncYunyinSignPayOrderOrderIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
query := fmt.Sprintf("select %s from %s where `order_id` = ? limit 1", yunyinSignPayOrderRows, m.table) query := fmt.Sprintf("select %s from %s where `order_id` = ? and del_state = ? limit 1", yunyinSignPayOrderRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, orderId); err != nil { if err := conn.QueryRowCtx(ctx, &resp, query, orderId, globalkey.DelStateNo); err != nil {
return nil, err return nil, err
} }
return resp.Id, nil return resp.Id, nil
@@ -128,9 +158,9 @@ func (m *defaultYunyinSignPayOrderModel) FindOneByOrderId(ctx context.Context, o
func (m *defaultYunyinSignPayOrderModel) FindOneByTaskId(ctx context.Context, taskId string) (*YunyinSignPayOrder, error) { func (m *defaultYunyinSignPayOrderModel) FindOneByTaskId(ctx context.Context, taskId string) (*YunyinSignPayOrder, error) {
jncYunyinSignPayOrderTaskIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderTaskIdPrefix, taskId) jncYunyinSignPayOrderTaskIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderTaskIdPrefix, taskId)
var resp YunyinSignPayOrder var resp YunyinSignPayOrder
err := m.QueryRowIndexCtx(ctx, &resp, jncYunyinSignPayOrderTaskIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { err := m.QueryRowIndexCtx(ctx, &resp, jncYunyinSignPayOrderTaskIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
query := fmt.Sprintf("select %s from %s where `task_id` = ? limit 1", yunyinSignPayOrderRows, m.table) query := fmt.Sprintf("select %s from %s where `task_id` = ? and del_state = ? limit 1", yunyinSignPayOrderRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, taskId); err != nil { if err := conn.QueryRowCtx(ctx, &resp, query, taskId, globalkey.DelStateNo); err != nil {
return nil, err return nil, err
} }
return resp.Id, nil return resp.Id, nil
@@ -145,40 +175,285 @@ func (m *defaultYunyinSignPayOrderModel) FindOneByTaskId(ctx context.Context, ta
} }
} }
func (m *defaultYunyinSignPayOrderModel) Insert(ctx context.Context, data *YunyinSignPayOrder) (sql.Result, error) { func (m *defaultYunyinSignPayOrderModel) Update(ctx context.Context, session sqlx.Session, newData *YunyinSignPayOrder) (sql.Result, error) {
data, err := m.FindOne(ctx, newData.Id)
if err != nil {
return nil, err
}
jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, data.Id) jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, data.Id)
jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, data.OrderId) jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, data.OrderId)
jncYunyinSignPayOrderTaskIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderTaskIdPrefix, data.TaskId) jncYunyinSignPayOrderTaskIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderTaskIdPrefix, data.TaskId)
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, yunyinSignPayOrderRowsExpectAutoSet) query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, yunyinSignPayOrderRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, data.Id, data.OrderId, data.UserId, data.TaskId, data.ParticipantId, data.Amount, data.PayType, data.SignStatus, data.PayStatus, data.SourceOrderCode, data.UserMobile, data.UserName, data.DeleteTime, data.DelState, data.Version) if session != nil {
return session.ExecCtx(ctx, query, newData.OrderId, newData.UserId, newData.TaskId, newData.ParticipantId, newData.Amount, newData.PayType, newData.SignStatus, newData.PayStatus, newData.SourceOrderCode, newData.UserMobile, newData.UserName, newData.DeleteTime, newData.DelState, newData.Version, newData.Id)
}
return conn.ExecCtx(ctx, query, newData.OrderId, newData.UserId, newData.TaskId, newData.ParticipantId, newData.Amount, newData.PayType, newData.SignStatus, newData.PayStatus, newData.SourceOrderCode, newData.UserMobile, newData.UserName, newData.DeleteTime, newData.DelState, newData.Version, newData.Id)
}, jncYunyinSignPayOrderIdKey, jncYunyinSignPayOrderOrderIdKey, jncYunyinSignPayOrderTaskIdKey) }, jncYunyinSignPayOrderIdKey, jncYunyinSignPayOrderOrderIdKey, jncYunyinSignPayOrderTaskIdKey)
return ret, err
} }
func (m *defaultYunyinSignPayOrderModel) Update(ctx context.Context, newData *YunyinSignPayOrder) error { func (m *defaultYunyinSignPayOrderModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *YunyinSignPayOrder) error {
oldVersion := newData.Version
newData.Version += 1
var sqlResult sql.Result
var err error
data, err := m.FindOne(ctx, newData.Id) data, err := m.FindOne(ctx, newData.Id)
if err != nil { if err != nil {
return err return err
} }
jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, data.Id) jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, data.Id)
jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, data.OrderId) jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, data.OrderId)
jncYunyinSignPayOrderTaskIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderTaskIdPrefix, data.TaskId) jncYunyinSignPayOrderTaskIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderTaskIdPrefix, data.TaskId)
sqlResult, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, yunyinSignPayOrderRowsWithPlaceHolder)
if session != nil {
return session.ExecCtx(ctx, query, newData.OrderId, newData.UserId, newData.TaskId, newData.ParticipantId, newData.Amount, newData.PayType, newData.SignStatus, newData.PayStatus, newData.SourceOrderCode, newData.UserMobile, newData.UserName, newData.DeleteTime, newData.DelState, newData.Version, newData.Id, oldVersion)
}
return conn.ExecCtx(ctx, query, newData.OrderId, newData.UserId, newData.TaskId, newData.ParticipantId, newData.Amount, newData.PayType, newData.SignStatus, newData.PayStatus, newData.SourceOrderCode, newData.UserMobile, newData.UserName, newData.DeleteTime, newData.DelState, newData.Version, newData.Id, oldVersion)
}, jncYunyinSignPayOrderIdKey, jncYunyinSignPayOrderOrderIdKey, jncYunyinSignPayOrderTaskIdKey)
if err != nil {
return err
}
updateCount, err := sqlResult.RowsAffected()
if err != nil {
return err
}
if updateCount == 0 {
return ErrNoRowsUpdate
}
return nil
}
func (m *defaultYunyinSignPayOrderModel) DeleteSoft(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) error {
data.DelState = globalkey.DelStateYes
data.DeleteTime = sql.NullTime{Time: time.Now(), Valid: true}
if err := m.UpdateWithVersion(ctx, session, data); err != nil {
return errors.Wrapf(errors.New("delete soft failed "), "YunyinSignPayOrderModel delete err : %+v", err)
}
return nil
}
func (m *defaultYunyinSignPayOrderModel) FindSum(ctx context.Context, builder squirrel.SelectBuilder, field string) (float64, error) {
if len(field) == 0 {
return 0, errors.Wrapf(errors.New("FindSum Least One Field"), "FindSum Least One Field")
}
builder = builder.Columns("IFNULL(SUM(" + field + "),0)")
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).ToSql()
if err != nil {
return 0, err
}
var resp float64
err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
switch err {
case nil:
return resp, nil
default:
return 0, err
}
}
func (m *defaultYunyinSignPayOrderModel) FindCount(ctx context.Context, builder squirrel.SelectBuilder, field string) (int64, error) {
if len(field) == 0 {
return 0, errors.Wrapf(errors.New("FindCount Least One Field"), "FindCount Least One Field")
}
builder = builder.Columns("COUNT(" + field + ")")
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).ToSql()
if err != nil {
return 0, err
}
var resp int64
err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
switch err {
case nil:
return resp, nil
default:
return 0, err
}
}
func (m *defaultYunyinSignPayOrderModel) FindAll(ctx context.Context, builder squirrel.SelectBuilder, orderBy string) ([]*YunyinSignPayOrder, error) {
builder = builder.Columns(yunyinSignPayOrderRows)
if orderBy == "" {
builder = builder.OrderBy("id DESC")
} else {
builder = builder.OrderBy(orderBy)
}
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).ToSql()
if err != nil {
return nil, err
}
var resp []*YunyinSignPayOrder
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
switch err {
case nil:
return resp, nil
default:
return nil, err
}
}
func (m *defaultYunyinSignPayOrderModel) FindPageListByPage(ctx context.Context, builder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*YunyinSignPayOrder, error) {
builder = builder.Columns(yunyinSignPayOrderRows)
if orderBy == "" {
builder = builder.OrderBy("id DESC")
} else {
builder = builder.OrderBy(orderBy)
}
if page < 1 {
page = 1
}
offset := (page - 1) * pageSize
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).Offset(uint64(offset)).Limit(uint64(pageSize)).ToSql()
if err != nil {
return nil, err
}
var resp []*YunyinSignPayOrder
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
switch err {
case nil:
return resp, nil
default:
return nil, err
}
}
func (m *defaultYunyinSignPayOrderModel) FindPageListByPageWithTotal(ctx context.Context, builder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*YunyinSignPayOrder, int64, error) {
total, err := m.FindCount(ctx, builder, "id")
if err != nil {
return nil, 0, err
}
builder = builder.Columns(yunyinSignPayOrderRows)
if orderBy == "" {
builder = builder.OrderBy("id DESC")
} else {
builder = builder.OrderBy(orderBy)
}
if page < 1 {
page = 1
}
offset := (page - 1) * pageSize
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).Offset(uint64(offset)).Limit(uint64(pageSize)).ToSql()
if err != nil {
return nil, total, err
}
var resp []*YunyinSignPayOrder
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
switch err {
case nil:
return resp, total, nil
default:
return nil, total, err
}
}
func (m *defaultYunyinSignPayOrderModel) FindPageListByIdDESC(ctx context.Context, builder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*YunyinSignPayOrder, error) {
builder = builder.Columns(yunyinSignPayOrderRows)
if preMinId > 0 {
builder = builder.Where(" id < ? ", preMinId)
}
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).OrderBy("id DESC").Limit(uint64(pageSize)).ToSql()
if err != nil {
return nil, err
}
var resp []*YunyinSignPayOrder
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
switch err {
case nil:
return resp, nil
default:
return nil, err
}
}
func (m *defaultYunyinSignPayOrderModel) FindPageListByIdASC(ctx context.Context, builder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*YunyinSignPayOrder, error) {
builder = builder.Columns(yunyinSignPayOrderRows)
if preMaxId > 0 {
builder = builder.Where(" id > ? ", preMaxId)
}
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).OrderBy("id ASC").Limit(uint64(pageSize)).ToSql()
if err != nil {
return nil, err
}
var resp []*YunyinSignPayOrder
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
switch err {
case nil:
return resp, nil
default:
return nil, err
}
}
func (m *defaultYunyinSignPayOrderModel) Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
return m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
return fn(ctx, session)
})
}
func (m *defaultYunyinSignPayOrderModel) SelectBuilder() squirrel.SelectBuilder {
return squirrel.Select().From(m.table)
}
func (m *defaultYunyinSignPayOrderModel) Delete(ctx context.Context, session sqlx.Session, id string) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, id)
jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, data.OrderId)
jncYunyinSignPayOrderTaskIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderTaskIdPrefix, data.TaskId)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, yunyinSignPayOrderRowsWithPlaceHolder) query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
return conn.ExecCtx(ctx, query, newData.OrderId, newData.UserId, newData.TaskId, newData.ParticipantId, newData.Amount, newData.PayType, newData.SignStatus, newData.PayStatus, newData.SourceOrderCode, newData.UserMobile, newData.UserName, newData.DeleteTime, newData.DelState, newData.Version, newData.Id) if session != nil {
return session.ExecCtx(ctx, query, id)
}
return conn.ExecCtx(ctx, query, id)
}, jncYunyinSignPayOrderIdKey, jncYunyinSignPayOrderOrderIdKey, jncYunyinSignPayOrderTaskIdKey) }, jncYunyinSignPayOrderIdKey, jncYunyinSignPayOrderOrderIdKey, jncYunyinSignPayOrderTaskIdKey)
return err return err
} }
func (m *defaultYunyinSignPayOrderModel) formatPrimary(primary interface{}) string {
func (m *defaultYunyinSignPayOrderModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, primary) return fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, primary)
} }
func (m *defaultYunyinSignPayOrderModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
func (m *defaultYunyinSignPayOrderModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", yunyinSignPayOrderRows, m.table)
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", yunyinSignPayOrderRows, m.table) return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo)
return conn.QueryRowCtx(ctx, v, query, primary)
} }
func (m *defaultYunyinSignPayOrderModel) tableName() string { func (m *defaultYunyinSignPayOrderModel) tableName() string {

View File

@@ -64,63 +64,63 @@ foreach ($table in $tables) {
goctl model mysql datasource -url="jnc:5vg67b3UNHu8@tcp(127.0.0.1:21301)/jnc" -table="$table" -dir="./model" --home="$HOME_DIR" -cache=true --style=goZero goctl model mysql datasource -url="jnc:5vg67b3UNHu8@tcp(127.0.0.1:21301)/jnc" -table="$table" -dir="./model" --home="$HOME_DIR" -cache=true --style=goZero
# 移动生成的文件到目标目录 # 移动生成的文件到目标目录
# if (Test-Path $OUTPUT_DIR) { if (Test-Path $OUTPUT_DIR) {
# $sourceFiles = Get-ChildItem -Path $OUTPUT_DIR -File $sourceFiles = Get-ChildItem -Path $OUTPUT_DIR -File
# foreach ($file in $sourceFiles) { foreach ($file in $sourceFiles) {
# $fileName = $file.Name $fileName = $file.Name
# $targetPath = Join-Path $TARGET_DIR $fileName $targetPath = Join-Path $TARGET_DIR $fileName
# $sourcePath = $file.FullName $sourcePath = $file.FullName
# # 检查文件类型并决定是否移动 # 检查文件类型并决定是否移动
# $shouldMove = $false $shouldMove = $false
# $shouldOverwrite = $false $shouldOverwrite = $false
# if ($fileName -eq "vars.go") { if ($fileName -eq "vars.go") {
# # vars.go: 如果目标目录不存在才移动 # vars.go: 如果目标目录不存在才移动
# if (-not (Test-Path $targetPath)) { if (-not (Test-Path $targetPath)) {
# $shouldMove = $true $shouldMove = $true
# Write-Host " 移动 $fileName (vars.go 不存在于目标目录)" -ForegroundColor Yellow Write-Host " 移动 $fileName (vars.go 不存在于目标目录)" -ForegroundColor Yellow
# } }
# else { else {
# Write-Host " 跳过 $fileName (vars.go 已存在于目标目录,防止覆盖)" -ForegroundColor Cyan Write-Host " 跳过 $fileName (vars.go 已存在于目标目录,防止覆盖)" -ForegroundColor Cyan
# } }
# } }
# elseif ($fileName -match "_gen\.go$") { elseif ($fileName -match "_gen\.go$") {
# # 带 _gen 后缀的文件: 直接覆盖 # 带 _gen 后缀的文件: 直接覆盖
# $shouldMove = $true $shouldMove = $true
# $shouldOverwrite = $true $shouldOverwrite = $true
# Write-Host " 移动 $fileName (覆盖 _gen 文件)" -ForegroundColor Yellow Write-Host " 移动 $fileName (覆盖 _gen 文件)" -ForegroundColor Yellow
# } }
# else { else {
# # 不带 _gen 后缀的文件: 如果目标目录不存在才移动 # 不带 _gen 后缀的文件: 如果目标目录不存在才移动
# if (-not (Test-Path $targetPath)) { if (-not (Test-Path $targetPath)) {
# $shouldMove = $true $shouldMove = $true
# Write-Host " 移动 $fileName (非 _gen 文件不存在于目标目录)" -ForegroundColor Yellow Write-Host " 移动 $fileName (非 _gen 文件不存在于目标目录)" -ForegroundColor Yellow
# } }
# else { else {
# Write-Host " 跳过 $fileName (非 _gen 文件已存在于目标目录,防止覆盖)" -ForegroundColor Cyan Write-Host " 跳过 $fileName (非 _gen 文件已存在于目标目录,防止覆盖)" -ForegroundColor Cyan
# } }
# } }
# # 执行移动操作 # 执行移动操作
# if ($shouldMove) { if ($shouldMove) {
# # 确保目标目录存在 # 确保目标目录存在
# if (-not (Test-Path $TARGET_DIR)) { if (-not (Test-Path $TARGET_DIR)) {
# New-Item -ItemType Directory -Path $TARGET_DIR -Force | Out-Null New-Item -ItemType Directory -Path $TARGET_DIR -Force | Out-Null
# } }
# # 如果目标文件存在且需要覆盖,先删除 # 如果目标文件存在且需要覆盖,先删除
# if ($shouldOverwrite -and (Test-Path $targetPath)) { if ($shouldOverwrite -and (Test-Path $targetPath)) {
# Remove-Item -Path $targetPath -Force Remove-Item -Path $targetPath -Force
# } }
# # 移动文件 # 移动文件
# Move-Item -Path $sourcePath -Destination $targetPath -Force Move-Item -Path $sourcePath -Destination $targetPath -Force
# Write-Host " ✓ 已移动到: $targetPath" -ForegroundColor Green Write-Host " ✓ 已移动到: $targetPath" -ForegroundColor Green
# } }
# } }
# } }
} }
Write-Host "" Write-Host ""

View File

@@ -1,5 +0,0 @@
package model
import "github.com/zeromicro/go-zero/core/stores/sqlx"
var ErrNotFound = sqlx.ErrNotFound