From 9f509924b33d06e744edbb6ee4599729f326d37b Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Fri, 16 Jan 2026 18:23:43 +0800 Subject: [PATCH] f --- .../internal/logic/pay/paymentchecklogic.go | 2 +- .../api/internal/logic/pay/paymentlogic.go | 20 +- .../logic/pay/yunyinsignpaycallbacklogic.go | 2 +- .../logic/pay/yunyinsignpayrefundlogic.go | 2 +- app/main/model/yunyinSignPayOrderModel.go | 37 +- app/main/model/yunyinSignPayOrderModel_gen.go | 365 +++++++++++++++--- deploy/script/gen_models.ps1 | 102 ++--- deploy/script/model/vars.go | 5 - 8 files changed, 393 insertions(+), 142 deletions(-) delete mode 100644 deploy/script/model/vars.go diff --git a/app/main/api/internal/logic/pay/paymentchecklogic.go b/app/main/api/internal/logic/pay/paymentchecklogic.go index 20436c2..27a2dfb 100644 --- a/app/main/api/internal/logic/pay/paymentchecklogic.go +++ b/app/main/api/internal/logic/pay/paymentchecklogic.go @@ -151,7 +151,7 @@ func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *type if newPayStatus != yunyinOrder.PayStatus { 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) } else { logx.Infof("成功更新云印签订单支付状态,订单ID: %s, 新支付状态: %d", order.Id, newPayStatus) diff --git a/app/main/api/internal/logic/pay/paymentlogic.go b/app/main/api/internal/logic/pay/paymentlogic.go index 3304963..78cb009 100644 --- a/app/main/api/internal/logic/pay/paymentlogic.go +++ b/app/main/api/internal/logic/pay/paymentlogic.go @@ -17,6 +17,7 @@ import ( "strings" "time" + "github.com/Masterminds/squirrel" "github.com/google/uuid" "github.com/pkg/errors" "github.com/redis/go-redis/v9" @@ -140,8 +141,21 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, // 查询用户是否有未完成的签署(待签署且待支付) var yunYinSignPayResult *service.CreateYunYinSignPayOrderResult - unfinishedOrder, findUnfinishedErr := l.svcCtx.YunyinSignPayOrderModel.FindUnfinishedByUserId(l.ctx, userID) - if findUnfinishedErr == nil && unfinishedOrder != nil { + // 使用 SelectBuilder 查询未完成的签署订单(del_state 由系统自动处理) + 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) @@ -382,7 +396,7 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses Version: 0, } - _, insertYunYinErr := l.svcCtx.YunyinSignPayOrderModel.InsertWithSession(l.ctx, session, &yunyinSignPayOrder) + _, insertYunYinErr := l.svcCtx.YunyinSignPayOrderModel.Insert(l.ctx, session, &yunyinSignPayOrder) if insertYunYinErr != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存云印签订单失败: %+v", insertYunYinErr) } diff --git a/app/main/api/internal/logic/pay/yunyinsignpaycallbacklogic.go b/app/main/api/internal/logic/pay/yunyinsignpaycallbacklogic.go index 998fa6b..45c37bf 100644 --- a/app/main/api/internal/logic/pay/yunyinsignpaycallbacklogic.go +++ b/app/main/api/internal/logic/pay/yunyinsignpaycallbacklogic.go @@ -166,7 +166,7 @@ func (l *YunYinSignPayCallbackLogic) YunYinSignPayCallback(w http.ResponseWriter // 更新支付状态为已支付(1) if 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) } else { logx.Infof("云印签回调,成功更新云印签订单支付状态,订单ID: %s", order.Id) diff --git a/app/main/api/internal/logic/pay/yunyinsignpayrefundlogic.go b/app/main/api/internal/logic/pay/yunyinsignpayrefundlogic.go index 5fae342..3fb251d 100644 --- a/app/main/api/internal/logic/pay/yunyinsignpayrefundlogic.go +++ b/app/main/api/internal/logic/pay/yunyinsignpayrefundlogic.go @@ -100,7 +100,7 @@ func (l *YunYinSignPayRefundLogic) YunYinSignPayRefund(req *types.YunYinSignPayR yunyinOrder.PayStatus = 2 } // 部分退款保持已支付状态(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) } } diff --git a/app/main/model/yunyinSignPayOrderModel.go b/app/main/model/yunyinSignPayOrderModel.go index 32fdf51..dcf4faa 100644 --- a/app/main/model/yunyinSignPayOrderModel.go +++ b/app/main/model/yunyinSignPayOrderModel.go @@ -1,10 +1,6 @@ package model import ( - "context" - "database/sql" - "fmt" - "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlx" ) @@ -16,8 +12,6 @@ type ( // and implement the added methods in customYunyinSignPayOrderModel. YunyinSignPayOrderModel interface { yunyinSignPayOrderModel - FindUnfinishedByUserId(ctx context.Context, userId string) (*YunyinSignPayOrder, error) - InsertWithSession(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) (sql.Result, error) } customYunyinSignPayOrderModel struct { @@ -26,35 +20,8 @@ type ( ) // 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{ - 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) - }) -} diff --git a/app/main/model/yunyinSignPayOrderModel_gen.go b/app/main/model/yunyinSignPayOrderModel_gen.go index e776a90..4eed930 100644 --- a/app/main/model/yunyinSignPayOrderModel_gen.go +++ b/app/main/model/yunyinSignPayOrderModel_gen.go @@ -1,6 +1,4 @@ -// Code generated by goctl. DO NOT EDIT. -// versions: -// goctl version: 1.8.3 +// Code generated by goctl. DO NOT EDIT! package model @@ -9,20 +7,26 @@ import ( "database/sql" "fmt" "strings" + + "reflect" "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/cache" "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stringx" + "jnc-server/common/globalkey" ) var ( yunyinSignPayOrderFieldNames = builder.RawFieldNames(&YunyinSignPayOrder{}) yunyinSignPayOrderRows = strings.Join(yunyinSignPayOrderFieldNames, ",") - yunyinSignPayOrderRowsExpectAutoSet = strings.Join(stringx.Remove(yunyinSignPayOrderFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") - yunyinSignPayOrderRowsWithPlaceHolder = strings.Join(stringx.Remove(yunyinSignPayOrderFieldNames, "`id`", "`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_time`", "`update_time`"), "=?,") + "=?" cacheJncYunyinSignPayOrderIdPrefix = "cache:jnc:yunyinSignPayOrder:id:" cacheJncYunyinSignPayOrderOrderIdPrefix = "cache:jnc:yunyinSignPayOrder:orderId:" @@ -31,12 +35,23 @@ var ( type ( 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) FindOneByOrderId(ctx context.Context, orderId string) (*YunyinSignPayOrder, error) FindOneByTaskId(ctx context.Context, taskId string) (*YunyinSignPayOrder, error) - Update(ctx context.Context, data *YunyinSignPayOrder) error - Delete(ctx context.Context, id string) error + Update(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) (sql.Result, 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 { @@ -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{ - CachedConn: sqlc.NewConn(conn, c, opts...), + CachedConn: sqlc.NewConn(conn, c), table: "`yunyin_sign_pay_order`", } } -func (m *defaultYunyinSignPayOrderModel) Delete(ctx context.Context, id string) error { - data, err := m.FindOne(ctx, id) - if err != nil { - return err - } - - jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, id) +func (m *defaultYunyinSignPayOrderModel) Insert(ctx context.Context, session sqlx.Session, data *YunyinSignPayOrder) (sql.Result, error) { + data.DelState = globalkey.DelStateNo + m.insertUUID(data) + jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, data.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) { - query := fmt.Sprintf("delete from %s where `id` = ?", m.table) - return conn.ExecCtx(ctx, query, id) + 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) + 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) - 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) { jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, id) var resp YunyinSignPayOrder - err := m.QueryRowCtx(ctx, &resp, jncYunyinSignPayOrderIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { - query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", yunyinSignPayOrderRows, m.table) - return conn.QueryRowCtx(ctx, v, query, id) + 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` = ? and del_state = ? limit 1", yunyinSignPayOrderRows, m.table) + return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo) }) switch err { 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) { jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, orderId) var resp YunyinSignPayOrder - err := m.QueryRowIndexCtx(ctx, &resp, jncYunyinSignPayOrderOrderIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { - query := fmt.Sprintf("select %s from %s where `order_id` = ? limit 1", yunyinSignPayOrderRows, m.table) - if err := conn.QueryRowCtx(ctx, &resp, query, orderId); err != nil { + 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` = ? and del_state = ? limit 1", yunyinSignPayOrderRows, m.table) + if err := conn.QueryRowCtx(ctx, &resp, query, orderId, globalkey.DelStateNo); err != nil { return nil, err } 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) { jncYunyinSignPayOrderTaskIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderTaskIdPrefix, taskId) var resp YunyinSignPayOrder - err := m.QueryRowIndexCtx(ctx, &resp, jncYunyinSignPayOrderTaskIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { - query := fmt.Sprintf("select %s from %s where `task_id` = ? limit 1", yunyinSignPayOrderRows, m.table) - if err := conn.QueryRowCtx(ctx, &resp, query, taskId); err != nil { + 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` = ? and del_state = ? limit 1", yunyinSignPayOrderRows, m.table) + if err := conn.QueryRowCtx(ctx, &resp, query, taskId, globalkey.DelStateNo); err != nil { return nil, err } 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) jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, data.OrderId) 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) { - query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, yunyinSignPayOrderRowsExpectAutoSet) - 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) + return 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) + 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) - 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) if err != nil { return err } - jncYunyinSignPayOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, data.Id) jncYunyinSignPayOrderOrderIdKey := fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderOrderIdPrefix, data.OrderId) 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) { - query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, yunyinSignPayOrderRowsWithPlaceHolder) - 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) + query := fmt.Sprintf("delete from %s where `id` = ?", m.table) + if session != nil { + return session.ExecCtx(ctx, query, id) + } + return conn.ExecCtx(ctx, query, id) }, jncYunyinSignPayOrderIdKey, jncYunyinSignPayOrderOrderIdKey, jncYunyinSignPayOrderTaskIdKey) return err } - -func (m *defaultYunyinSignPayOrderModel) formatPrimary(primary any) string { +func (m *defaultYunyinSignPayOrderModel) formatPrimary(primary interface{}) string { return fmt.Sprintf("%s%v", cacheJncYunyinSignPayOrderIdPrefix, primary) } - -func (m *defaultYunyinSignPayOrderModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { - query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", yunyinSignPayOrderRows, m.table) - return conn.QueryRowCtx(ctx, v, query, primary) +func (m *defaultYunyinSignPayOrderModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error { + query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", yunyinSignPayOrderRows, m.table) + return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo) } func (m *defaultYunyinSignPayOrderModel) tableName() string { diff --git a/deploy/script/gen_models.ps1 b/deploy/script/gen_models.ps1 index 9793c18..3e976b4 100644 --- a/deploy/script/gen_models.ps1 +++ b/deploy/script/gen_models.ps1 @@ -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 # 移动生成的文件到目标目录 - # if (Test-Path $OUTPUT_DIR) { - # $sourceFiles = Get-ChildItem -Path $OUTPUT_DIR -File + if (Test-Path $OUTPUT_DIR) { + $sourceFiles = Get-ChildItem -Path $OUTPUT_DIR -File - # foreach ($file in $sourceFiles) { - # $fileName = $file.Name - # $targetPath = Join-Path $TARGET_DIR $fileName - # $sourcePath = $file.FullName + foreach ($file in $sourceFiles) { + $fileName = $file.Name + $targetPath = Join-Path $TARGET_DIR $fileName + $sourcePath = $file.FullName - # # 检查文件类型并决定是否移动 - # $shouldMove = $false - # $shouldOverwrite = $false + # 检查文件类型并决定是否移动 + $shouldMove = $false + $shouldOverwrite = $false - # if ($fileName -eq "vars.go") { - # # vars.go: 如果目标目录不存在才移动 - # if (-not (Test-Path $targetPath)) { - # $shouldMove = $true - # Write-Host " 移动 $fileName (vars.go 不存在于目标目录)" -ForegroundColor Yellow - # } - # else { - # Write-Host " 跳过 $fileName (vars.go 已存在于目标目录,防止覆盖)" -ForegroundColor Cyan - # } - # } - # elseif ($fileName -match "_gen\.go$") { - # # 带 _gen 后缀的文件: 直接覆盖 - # $shouldMove = $true - # $shouldOverwrite = $true - # Write-Host " 移动 $fileName (覆盖 _gen 文件)" -ForegroundColor Yellow - # } - # else { - # # 不带 _gen 后缀的文件: 如果目标目录不存在才移动 - # if (-not (Test-Path $targetPath)) { - # $shouldMove = $true - # Write-Host " 移动 $fileName (非 _gen 文件不存在于目标目录)" -ForegroundColor Yellow - # } - # else { - # Write-Host " 跳过 $fileName (非 _gen 文件已存在于目标目录,防止覆盖)" -ForegroundColor Cyan - # } - # } + if ($fileName -eq "vars.go") { + # vars.go: 如果目标目录不存在才移动 + if (-not (Test-Path $targetPath)) { + $shouldMove = $true + Write-Host " 移动 $fileName (vars.go 不存在于目标目录)" -ForegroundColor Yellow + } + else { + Write-Host " 跳过 $fileName (vars.go 已存在于目标目录,防止覆盖)" -ForegroundColor Cyan + } + } + elseif ($fileName -match "_gen\.go$") { + # 带 _gen 后缀的文件: 直接覆盖 + $shouldMove = $true + $shouldOverwrite = $true + Write-Host " 移动 $fileName (覆盖 _gen 文件)" -ForegroundColor Yellow + } + else { + # 不带 _gen 后缀的文件: 如果目标目录不存在才移动 + if (-not (Test-Path $targetPath)) { + $shouldMove = $true + Write-Host " 移动 $fileName (非 _gen 文件不存在于目标目录)" -ForegroundColor Yellow + } + else { + Write-Host " 跳过 $fileName (非 _gen 文件已存在于目标目录,防止覆盖)" -ForegroundColor Cyan + } + } - # # 执行移动操作 - # if ($shouldMove) { - # # 确保目标目录存在 - # if (-not (Test-Path $TARGET_DIR)) { - # New-Item -ItemType Directory -Path $TARGET_DIR -Force | Out-Null - # } + # 执行移动操作 + if ($shouldMove) { + # 确保目标目录存在 + if (-not (Test-Path $TARGET_DIR)) { + New-Item -ItemType Directory -Path $TARGET_DIR -Force | Out-Null + } - # # 如果目标文件存在且需要覆盖,先删除 - # if ($shouldOverwrite -and (Test-Path $targetPath)) { - # Remove-Item -Path $targetPath -Force - # } + # 如果目标文件存在且需要覆盖,先删除 + if ($shouldOverwrite -and (Test-Path $targetPath)) { + Remove-Item -Path $targetPath -Force + } - # # 移动文件 - # Move-Item -Path $sourcePath -Destination $targetPath -Force - # Write-Host " ✓ 已移动到: $targetPath" -ForegroundColor Green - # } - # } - # } + # 移动文件 + Move-Item -Path $sourcePath -Destination $targetPath -Force + Write-Host " ✓ 已移动到: $targetPath" -ForegroundColor Green + } + } + } } Write-Host "" diff --git a/deploy/script/model/vars.go b/deploy/script/model/vars.go deleted file mode 100644 index 69ca814..0000000 --- a/deploy/script/model/vars.go +++ /dev/null @@ -1,5 +0,0 @@ -package model - -import "github.com/zeromicro/go-zero/core/stores/sqlx" - -var ErrNotFound = sqlx.ErrNotFound