feat(main): fix url
This commit is contained in:
parent
761796bd35
commit
6a5eab11ea
@ -1,29 +1,31 @@
|
|||||||
syntax = "v1"
|
syntax = "v1"
|
||||||
|
|
||||||
info (
|
info (
|
||||||
title: "代理服务"
|
title: "代理服务"
|
||||||
desc: "代理服务接口"
|
desc: "代理服务接口"
|
||||||
author: "Liangzai"
|
author: "Liangzai"
|
||||||
email: "2440983361@qq.com"
|
email: "2440983361@qq.com"
|
||||||
version: "v1"
|
version: "v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"agent/agent.api"
|
"agent/agent.api"
|
||||||
)
|
)
|
||||||
|
|
||||||
@server (
|
@server (
|
||||||
prefix: api/v1
|
prefix: api/v1/agent
|
||||||
group: agent
|
group: agent
|
||||||
)
|
)
|
||||||
|
|
||||||
service main {
|
service main {
|
||||||
// 提交代理申请
|
@handler GetAgentInfo
|
||||||
@handler ApplyForAgent
|
get /info returns (AgentInfoResp)
|
||||||
post /agent/apply (AgentApplyReq) returns (AgentApplyResp)
|
|
||||||
|
|
||||||
// 查询代理申请状态
|
// 提交代理申请
|
||||||
@handler GetAgentAuditStatus
|
@handler ApplyForAgent
|
||||||
get /agent/audit/status (AgentAuditStatusReq) returns (AgentAuditStatusResp)
|
post /apply (AgentApplyReq) returns (AgentApplyResp)
|
||||||
|
|
||||||
|
// 查询代理申请状态
|
||||||
|
@handler GetAgentAuditStatus
|
||||||
|
get /audit/status (AgentAuditStatusReq) returns (AgentAuditStatusResp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,13 @@ info (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
AgentInfoResp {
|
||||||
|
isAgent bool `json:"is_agent"`
|
||||||
|
region string `json:"region"`
|
||||||
|
mobile string `json:"mobile"`
|
||||||
|
wechatID string `json:"wechat_id"`
|
||||||
|
}
|
||||||
|
|
||||||
// 代理申请请求参数
|
// 代理申请请求参数
|
||||||
AgentApplyReq {
|
AgentApplyReq {
|
||||||
UserID int64 `json:"user_id"`
|
UserID int64 `json:"user_id"`
|
||||||
|
@ -53,9 +53,9 @@ service main {
|
|||||||
@handler detail
|
@handler detail
|
||||||
get /user/detail returns (UserInfoResp)
|
get /user/detail returns (UserInfoResp)
|
||||||
|
|
||||||
@doc "get new token"
|
@doc "get new token"
|
||||||
@handler getToken
|
@handler getToken
|
||||||
post /user/getToken returns (MobileCodeLoginResp)
|
post /user/getToken returns (MobileCodeLoginResp)
|
||||||
}
|
}
|
||||||
|
|
||||||
//============================> auth v1 <============================
|
//============================> auth v1 <============================
|
||||||
|
@ -35,8 +35,8 @@ Alipay:
|
|||||||
PrivateKey: "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCvo8TmTNnVguKwMYrX0z01PfUlSS+AqwwCH1+/P39q6No+09yO1bjhy4LowtDfwKX8F+IZLl5Cx06f1M8KzPvjQliBAfMJ1FuTEOOXPmtE1YLAYIUMLbltR6Crnp16T28eF41Uo0PUo2ple/oSjWhYgsTJjyBMXY04f6HM2uZlHOhG2aOUz2CDNbvkEhNddazuPMgo8Xl7vwENGDFTPa85HmSSoDppFowLdYfAw2Jl1ilKNg4sOPc6d507nXdgpMlUguwZxol6OQ2hBS6v9OjII5cJ1tyR/klJRapnIPmFaPvhDozYwjU6Z3jMvcbrByl0qNpLrbEFS4pn9hfRo4YrAgMBAAECggEAZAi0Ri6TCqXnEk7FMzMec0p8auYJ5hCFYFgaIkS5/1vroUjtH3TePcu5HXSHnkiMwM2hepIMIaB+SU3dNduVwtOwsJk5oOmP1m0SErv8QFISjBrs7AjGyVS4T8ahDl5bfRoQ5pmuMld4a6B2x0Y+ndqs1ddsn9HQctNOhexOuFsSjX1N3PZxFzfRTmGzxf7kwZGlXMN7G1r3Rp4koylYHVpqjXIK34Anc8SVljxFVkFOk62QvBdYcCucepymHNl0pYwPDCCylPH8OpIXOErmPldTflmoLRc+ywu9rw4I8UvkoPgTe+16he+jdi/N7c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xg7p8AYXzYPEHQr1tl7W+M4zv9wO9GKZCxIqMA8U3RP5npPfRaCfIRGzXzCqFEEUvWuidOB7frsvN4jiPD07qpL2Bi9LM1X/ee2kC/oM8Uhd7ERZhG8MbZfijZKxgrsDKBcwIDAQAB"
|
||||||
IsProduction: true
|
IsProduction: true
|
||||||
NotifyUrl: "https://www.quannengcha.com/api/v1/pay/alipay/callback"
|
NotifyUrl: "https://www.tianyuancha.cn/api/v1/pay/alipay/callback"
|
||||||
ReturnURL: "https://www.quannengcha.com/report"
|
ReturnURL: "https://www.tianyuancha.cn/report"
|
||||||
Wxpay:
|
Wxpay:
|
||||||
AppID: "wxba8424db4771cc18"
|
AppID: "wxba8424db4771cc18"
|
||||||
AppSecret: "89646203d7f76eb7aef0d926b9efffaa"
|
AppSecret: "89646203d7f76eb7aef0d926b9efffaa"
|
||||||
@ -44,8 +44,8 @@ Wxpay:
|
|||||||
MchCertificateSerialNumber: "5369B8AEEBDCF7AF274510252E6A8C0659C30F61"
|
MchCertificateSerialNumber: "5369B8AEEBDCF7AF274510252E6A8C0659C30F61"
|
||||||
MchApiv3Key: "e3ea4cf0765f1e71b01bb387dfcdbc9f"
|
MchApiv3Key: "e3ea4cf0765f1e71b01bb387dfcdbc9f"
|
||||||
MchPrivateKeyPath: "etc/merchant/apiclient_key.pem"
|
MchPrivateKeyPath: "etc/merchant/apiclient_key.pem"
|
||||||
NotifyUrl: "https://www.quannengcha.com/api/v1/pay/wechat/callback"
|
NotifyUrl: "https://www.tianyuancha.cn/api/v1/pay/wechat/callback"
|
||||||
RefundNotifyUrl: "https://www.quannengcha.com/api/v1/wechat/refund_callback"
|
RefundNotifyUrl: "https://www.tianyuancha.cn/api/v1/wechat/refund_callback"
|
||||||
Applepay:
|
Applepay:
|
||||||
ProductionVerifyURL: "https://api.storekit.itunes.apple.com/inApps/v1/transactions/receipt"
|
ProductionVerifyURL: "https://api.storekit.itunes.apple.com/inApps/v1/transactions/receipt"
|
||||||
SandboxVerifyURL: "https://api.storekit-sandbox.itunes.apple.com/inApps/v1/transactions/receipt"
|
SandboxVerifyURL: "https://api.storekit-sandbox.itunes.apple.com/inApps/v1/transactions/receipt"
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
package agent
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"qnc-server/app/user/cmd/api/internal/logic/agent"
|
||||||
|
"qnc-server/app/user/cmd/api/internal/svc"
|
||||||
|
"qnc-server/common/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetAgentInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
l := agent.NewGetAgentInfoLogic(r.Context(), svcCtx)
|
||||||
|
resp, err := l.GetAgentInfo()
|
||||||
|
result.HttpResult(r, w, resp, err)
|
||||||
|
}
|
||||||
|
}
|
@ -21,16 +21,21 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||||||
[]rest.Route{
|
[]rest.Route{
|
||||||
{
|
{
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
Path: "/agent/apply",
|
Path: "/apply",
|
||||||
Handler: agent.ApplyForAgentHandler(serverCtx),
|
Handler: agent.ApplyForAgentHandler(serverCtx),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Method: http.MethodGet,
|
Method: http.MethodGet,
|
||||||
Path: "/agent/audit/status",
|
Path: "/audit/status",
|
||||||
Handler: agent.GetAgentAuditStatusHandler(serverCtx),
|
Handler: agent.GetAgentAuditStatusHandler(serverCtx),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/info",
|
||||||
|
Handler: agent.GetAgentInfoHandler(serverCtx),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
rest.WithPrefix("/api/v1"),
|
rest.WithPrefix("/api/v1/agent"),
|
||||||
)
|
)
|
||||||
|
|
||||||
server.AddRoutes(
|
server.AddRoutes(
|
||||||
|
48
app/user/cmd/api/internal/logic/agent/getagentinfologic.go
Normal file
48
app/user/cmd/api/internal/logic/agent/getagentinfologic.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package agent
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"github.com/jinzhu/copier"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"qnc-server/common/ctxdata"
|
||||||
|
"qnc-server/common/xerr"
|
||||||
|
|
||||||
|
"qnc-server/app/user/cmd/api/internal/svc"
|
||||||
|
"qnc-server/app/user/cmd/api/internal/types"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GetAgentInfoLogic struct {
|
||||||
|
logx.Logger
|
||||||
|
ctx context.Context
|
||||||
|
svcCtx *svc.ServiceContext
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGetAgentInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAgentInfoLogic {
|
||||||
|
return &GetAgentInfoLogic{
|
||||||
|
Logger: logx.WithContext(ctx),
|
||||||
|
ctx: ctx,
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *GetAgentInfoLogic) GetAgentInfo() (resp *types.AgentInfoResp, err error) {
|
||||||
|
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取代理信息, %+v", err)
|
||||||
|
}
|
||||||
|
agent, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return &types.AgentInfoResp{
|
||||||
|
IsAgent: false,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取代理信息, %+v", err)
|
||||||
|
}
|
||||||
|
copier.Copy(&resp, agent)
|
||||||
|
resp.IsAgent = true
|
||||||
|
return resp, nil
|
||||||
|
}
|
@ -61,10 +61,13 @@ func (a *AliPayService) CreateAlipayAppOrder(amount float64, subject string, out
|
|||||||
// CreateAlipayH5Order 创建支付宝H5支付订单
|
// CreateAlipayH5Order 创建支付宝H5支付订单
|
||||||
func (a *AliPayService) CreateAlipayH5Order(amount float64, subject string, outTradeNo string, brand string) (string, error) {
|
func (a *AliPayService) CreateAlipayH5Order(amount float64, subject string, outTradeNo string, brand string) (string, error) {
|
||||||
var returnURL string
|
var returnURL string
|
||||||
|
var notifyURL string
|
||||||
if brand == "tyc" {
|
if brand == "tyc" {
|
||||||
returnURL = "https://www.tianyuancha.com/report"
|
returnURL = "https://www.tianyuancha.com/report"
|
||||||
|
notifyURL = "https://www.tianyuancha.com/api/v1/pay/wechat/callback"
|
||||||
} else {
|
} else {
|
||||||
returnURL = a.config.ReturnURL
|
returnURL = a.config.ReturnURL
|
||||||
|
notifyURL = a.config.NotifyUrl
|
||||||
}
|
}
|
||||||
client := a.AlipayClient
|
client := a.AlipayClient
|
||||||
totalAmount := lzUtils.ToAlipayAmount(amount)
|
totalAmount := lzUtils.ToAlipayAmount(amount)
|
||||||
@ -74,8 +77,8 @@ func (a *AliPayService) CreateAlipayH5Order(amount float64, subject string, outT
|
|||||||
Subject: subject,
|
Subject: subject,
|
||||||
OutTradeNo: outTradeNo,
|
OutTradeNo: outTradeNo,
|
||||||
TotalAmount: totalAmount,
|
TotalAmount: totalAmount,
|
||||||
ProductCode: "QUICK_WAP_PAY", // H5支付专用产品码
|
ProductCode: "QUICK_WAP_PAY", // H5支付专用产品码
|
||||||
NotifyURL: a.config.NotifyUrl, // 异步回调通知地址
|
NotifyURL: notifyURL, // 异步回调通知地址
|
||||||
ReturnURL: returnURL,
|
ReturnURL: returnURL,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,13 @@ type AgentAuditStatusResp struct {
|
|||||||
AuditReason string `json:"audit_reason"`
|
AuditReason string `json:"audit_reason"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AgentInfoResp struct {
|
||||||
|
IsAgent bool `json:"is_agent"`
|
||||||
|
Region string `json:"region"`
|
||||||
|
Mobile string `json:"mobile"`
|
||||||
|
WechatID string `json:"wechat_id"`
|
||||||
|
}
|
||||||
|
|
||||||
type Feature struct {
|
type Feature struct {
|
||||||
ID int64 `json:"id"` // 功能ID
|
ID int64 `json:"id"` // 功能ID
|
||||||
ApiID string `json:"api_id"` // API标识
|
ApiID string `json:"api_id"` // API标识
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"qnc-server/deploy/script/model"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"time"
|
"time"
|
||||||
@ -27,13 +26,15 @@ var (
|
|||||||
agentRowsExpectAutoSet = strings.Join(stringx.Remove(agentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
agentRowsExpectAutoSet = strings.Join(stringx.Remove(agentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||||
agentRowsWithPlaceHolder = strings.Join(stringx.Remove(agentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
agentRowsWithPlaceHolder = strings.Join(stringx.Remove(agentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||||
|
|
||||||
cacheQncAgentIdPrefix = "cache:qnc:agent:id:"
|
cacheQncAgentIdPrefix = "cache:qnc:agent:id:"
|
||||||
|
cacheQncAgentUserIdPrefix = "cache:qnc:agent:userId:"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
agentModel interface {
|
agentModel interface {
|
||||||
Insert(ctx context.Context, session sqlx.Session, data *Agent) (sql.Result, error)
|
Insert(ctx context.Context, session sqlx.Session, data *Agent) (sql.Result, error)
|
||||||
FindOne(ctx context.Context, id int64) (*Agent, error)
|
FindOne(ctx context.Context, id int64) (*Agent, error)
|
||||||
|
FindOneByUserId(ctx context.Context, userId int64) (*Agent, error)
|
||||||
Update(ctx context.Context, session sqlx.Session, data *Agent) (sql.Result, error)
|
Update(ctx context.Context, session sqlx.Session, data *Agent) (sql.Result, error)
|
||||||
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *Agent) error
|
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *Agent) error
|
||||||
Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
|
Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
|
||||||
@ -78,59 +79,90 @@ func newAgentModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultAgentModel {
|
|||||||
func (m *defaultAgentModel) Insert(ctx context.Context, session sqlx.Session, data *Agent) (sql.Result, error) {
|
func (m *defaultAgentModel) Insert(ctx context.Context, session sqlx.Session, data *Agent) (sql.Result, error) {
|
||||||
data.DelState = globalkey.DelStateNo
|
data.DelState = globalkey.DelStateNo
|
||||||
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, data.Id)
|
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, data.Id)
|
||||||
|
qncAgentUserIdKey := fmt.Sprintf("%s%v", cacheQncAgentUserIdPrefix, data.UserId)
|
||||||
return 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, agentRowsExpectAutoSet)
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, agentRowsExpectAutoSet)
|
||||||
if session != nil {
|
if session != nil {
|
||||||
return session.ExecCtx(ctx, query, data.UserId, data.Region, data.Mobile, data.WechatId, data.DeleteTime, data.DelState, data.Version)
|
return session.ExecCtx(ctx, query, data.UserId, data.Region, data.Mobile, data.WechatId, data.DeleteTime, data.DelState, data.Version)
|
||||||
}
|
}
|
||||||
return conn.ExecCtx(ctx, query, data.UserId, data.Region, data.Mobile, data.WechatId, data.DeleteTime, data.DelState, data.Version)
|
return conn.ExecCtx(ctx, query, data.UserId, data.Region, data.Mobile, data.WechatId, data.DeleteTime, data.DelState, data.Version)
|
||||||
}, qncAgentIdKey)
|
}, qncAgentIdKey, qncAgentUserIdKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *defaultAgentModel) FindOne(ctx context.Context, id int64) (*Agent, error) {
|
func (m *defaultAgentModel) FindOne(ctx context.Context, id int64) (*Agent, error) {
|
||||||
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, id)
|
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, id)
|
||||||
var resp Agent
|
var resp Agent
|
||||||
err := m.QueryRowCtx(ctx, &resp, qncAgentIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
err := m.QueryRowCtx(ctx, &resp, qncAgentIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||||
query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? AND del_state = ? limit 1", agentRows, m.table)
|
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", agentRows, m.table)
|
||||||
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
|
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
|
||||||
})
|
})
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
case sqlc.ErrNotFound:
|
case sqlc.ErrNotFound:
|
||||||
return nil, model.ErrNotFound
|
return nil, ErrNotFound
|
||||||
default:
|
default:
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *defaultAgentModel) Update(ctx context.Context, session sqlx.Session, data *Agent) (sql.Result, error) {
|
func (m *defaultAgentModel) FindOneByUserId(ctx context.Context, userId int64) (*Agent, error) {
|
||||||
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, data.Id)
|
qncAgentUserIdKey := fmt.Sprintf("%s%v", cacheQncAgentUserIdPrefix, userId)
|
||||||
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
var resp Agent
|
||||||
query := fmt.Sprintf("UPDATE %s SET %s WHERE `id` = ?", m.table, agentRowsWithPlaceHolder)
|
err := m.QueryRowIndexCtx(ctx, &resp, qncAgentUserIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||||
if session != nil {
|
query := fmt.Sprintf("select %s from %s where `user_id` = ? and del_state = ? limit 1", agentRows, m.table)
|
||||||
return session.ExecCtx(ctx, query, data.UserId, data.Region, data.Mobile, data.WechatId, data.DeleteTime, data.DelState, data.Version, data.Id)
|
if err := conn.QueryRowCtx(ctx, &resp, query, userId, globalkey.DelStateNo); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
return conn.ExecCtx(ctx, query, data.UserId, data.Region, data.Mobile, data.WechatId, data.DeleteTime, data.DelState, data.Version, data.Id)
|
return resp.Id, nil
|
||||||
}, qncAgentIdKey)
|
}, m.queryPrimary)
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
return &resp, nil
|
||||||
|
case sqlc.ErrNotFound:
|
||||||
|
return nil, ErrNotFound
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *defaultAgentModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, data *Agent) error {
|
func (m *defaultAgentModel) Update(ctx context.Context, session sqlx.Session, newData *Agent) (sql.Result, error) {
|
||||||
|
data, err := m.FindOne(ctx, newData.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, data.Id)
|
||||||
|
qncAgentUserIdKey := fmt.Sprintf("%s%v", cacheQncAgentUserIdPrefix, data.UserId)
|
||||||
|
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, agentRowsWithPlaceHolder)
|
||||||
|
if session != nil {
|
||||||
|
return session.ExecCtx(ctx, query, newData.UserId, newData.Region, newData.Mobile, newData.WechatId, newData.DeleteTime, newData.DelState, newData.Version, newData.Id)
|
||||||
|
}
|
||||||
|
return conn.ExecCtx(ctx, query, newData.UserId, newData.Region, newData.Mobile, newData.WechatId, newData.DeleteTime, newData.DelState, newData.Version, newData.Id)
|
||||||
|
}, qncAgentIdKey, qncAgentUserIdKey)
|
||||||
|
}
|
||||||
|
|
||||||
oldVersion := data.Version
|
func (m *defaultAgentModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *Agent) error {
|
||||||
data.Version += 1
|
|
||||||
|
oldVersion := newData.Version
|
||||||
|
newData.Version += 1
|
||||||
|
|
||||||
var sqlResult sql.Result
|
var sqlResult sql.Result
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
data, err := m.FindOne(ctx, newData.Id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, data.Id)
|
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, data.Id)
|
||||||
|
qncAgentUserIdKey := fmt.Sprintf("%s%v", cacheQncAgentUserIdPrefix, data.UserId)
|
||||||
sqlResult, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
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, agentRowsWithPlaceHolder)
|
query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, agentRowsWithPlaceHolder)
|
||||||
if session != nil {
|
if session != nil {
|
||||||
return session.ExecCtx(ctx, query, data.UserId, data.Region, data.Mobile, data.WechatId, data.DeleteTime, data.DelState, data.Version, data.Id, oldVersion)
|
return session.ExecCtx(ctx, query, newData.UserId, newData.Region, newData.Mobile, newData.WechatId, newData.DeleteTime, newData.DelState, newData.Version, newData.Id, oldVersion)
|
||||||
}
|
}
|
||||||
return conn.ExecCtx(ctx, query, data.UserId, data.Region, data.Mobile, data.WechatId, data.DeleteTime, data.DelState, data.Version, data.Id, oldVersion)
|
return conn.ExecCtx(ctx, query, newData.UserId, newData.Region, newData.Mobile, newData.WechatId, newData.DeleteTime, newData.DelState, newData.Version, newData.Id, oldVersion)
|
||||||
}, qncAgentIdKey)
|
}, qncAgentIdKey, qncAgentUserIdKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -139,7 +171,7 @@ func (m *defaultAgentModel) UpdateWithVersion(ctx context.Context, session sqlx.
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if updateCount == 0 {
|
if updateCount == 0 {
|
||||||
return model.ErrNoRowsUpdate
|
return ErrNoRowsUpdate
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -348,21 +380,27 @@ func (m *defaultAgentModel) SelectBuilder() squirrel.SelectBuilder {
|
|||||||
return squirrel.Select().From(m.table)
|
return squirrel.Select().From(m.table)
|
||||||
}
|
}
|
||||||
func (m *defaultAgentModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
|
func (m *defaultAgentModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
|
||||||
|
data, err := m.FindOne(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, id)
|
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, id)
|
||||||
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
qncAgentUserIdKey := fmt.Sprintf("%s%v", cacheQncAgentUserIdPrefix, data.UserId)
|
||||||
query := fmt.Sprintf("DELETE FROM %s WHERE `id` = ?", m.table)
|
_, 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)
|
||||||
if session != nil {
|
if session != nil {
|
||||||
return session.ExecCtx(ctx, query, id)
|
return session.ExecCtx(ctx, query, id)
|
||||||
}
|
}
|
||||||
return conn.ExecCtx(ctx, query, id)
|
return conn.ExecCtx(ctx, query, id)
|
||||||
}, qncAgentIdKey)
|
}, qncAgentIdKey, qncAgentUserIdKey)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
func (m *defaultAgentModel) formatPrimary(primary interface{}) string {
|
func (m *defaultAgentModel) formatPrimary(primary interface{}) string {
|
||||||
return fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, primary)
|
return fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, primary)
|
||||||
}
|
}
|
||||||
func (m *defaultAgentModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
|
func (m *defaultAgentModel) 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", agentRows, m.table)
|
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", agentRows, m.table)
|
||||||
return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo)
|
return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
27
deploy/script/model/agentModel.go
Normal file
27
deploy/script/model/agentModel.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||||
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ AgentModel = (*customAgentModel)(nil)
|
||||||
|
|
||||||
|
type (
|
||||||
|
// AgentModel is an interface to be customized, add more methods here,
|
||||||
|
// and implement the added methods in customAgentModel.
|
||||||
|
AgentModel interface {
|
||||||
|
agentModel
|
||||||
|
}
|
||||||
|
|
||||||
|
customAgentModel struct {
|
||||||
|
*defaultAgentModel
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewAgentModel returns a model for the database table.
|
||||||
|
func NewAgentModel(conn sqlx.SqlConn, c cache.CacheConf) AgentModel {
|
||||||
|
return &customAgentModel{
|
||||||
|
defaultAgentModel: newAgentModel(conn, c),
|
||||||
|
}
|
||||||
|
}
|
409
deploy/script/model/agentModel_gen.go
Normal file
409
deploy/script/model/agentModel_gen.go
Normal file
@ -0,0 +1,409 @@
|
|||||||
|
// Code generated by goctl. DO NOT EDIT!
|
||||||
|
|
||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Masterminds/squirrel"
|
||||||
|
"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"
|
||||||
|
"qnc-server/common/globalkey"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
agentFieldNames = builder.RawFieldNames(&Agent{})
|
||||||
|
agentRows = strings.Join(agentFieldNames, ",")
|
||||||
|
agentRowsExpectAutoSet = strings.Join(stringx.Remove(agentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||||
|
agentRowsWithPlaceHolder = strings.Join(stringx.Remove(agentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||||
|
|
||||||
|
cacheQncAgentIdPrefix = "cache:qnc:agent:id:"
|
||||||
|
cacheQncAgentUserIdPrefix = "cache:qnc:agent:userId:"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
agentModel interface {
|
||||||
|
Insert(ctx context.Context, session sqlx.Session, data *Agent) (sql.Result, error)
|
||||||
|
FindOne(ctx context.Context, id int64) (*Agent, error)
|
||||||
|
FindOneByUserId(ctx context.Context, userId int64) (*Agent, error)
|
||||||
|
Update(ctx context.Context, session sqlx.Session, data *Agent) (sql.Result, error)
|
||||||
|
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *Agent) 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 *Agent) 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) ([]*Agent, error)
|
||||||
|
FindPageListByPage(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*Agent, error)
|
||||||
|
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*Agent, int64, error)
|
||||||
|
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*Agent, error)
|
||||||
|
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*Agent, error)
|
||||||
|
Delete(ctx context.Context, session sqlx.Session, id int64) error
|
||||||
|
}
|
||||||
|
|
||||||
|
defaultAgentModel struct {
|
||||||
|
sqlc.CachedConn
|
||||||
|
table string
|
||||||
|
}
|
||||||
|
|
||||||
|
Agent struct {
|
||||||
|
Id int64 `db:"id"`
|
||||||
|
UserId int64 `db:"user_id"`
|
||||||
|
Region string `db:"region"`
|
||||||
|
Mobile string `db:"mobile"`
|
||||||
|
WechatId sql.NullString `db:"wechat_id"`
|
||||||
|
CreateTime time.Time `db:"create_time"`
|
||||||
|
UpdateTime time.Time `db:"update_time"`
|
||||||
|
DeleteTime sql.NullTime `db:"delete_time"` // 删除时间
|
||||||
|
DelState int64 `db:"del_state"` // 删除状态
|
||||||
|
Version int64 `db:"version"` // 版本号
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func newAgentModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultAgentModel {
|
||||||
|
return &defaultAgentModel{
|
||||||
|
CachedConn: sqlc.NewConn(conn, c),
|
||||||
|
table: "`agent`",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) Insert(ctx context.Context, session sqlx.Session, data *Agent) (sql.Result, error) {
|
||||||
|
data.DelState = globalkey.DelStateNo
|
||||||
|
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, data.Id)
|
||||||
|
qncAgentUserIdKey := fmt.Sprintf("%s%v", cacheQncAgentUserIdPrefix, data.UserId)
|
||||||
|
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, agentRowsExpectAutoSet)
|
||||||
|
if session != nil {
|
||||||
|
return session.ExecCtx(ctx, query, data.UserId, data.Region, data.Mobile, data.WechatId, data.DeleteTime, data.DelState, data.Version)
|
||||||
|
}
|
||||||
|
return conn.ExecCtx(ctx, query, data.UserId, data.Region, data.Mobile, data.WechatId, data.DeleteTime, data.DelState, data.Version)
|
||||||
|
}, qncAgentIdKey, qncAgentUserIdKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) FindOne(ctx context.Context, id int64) (*Agent, error) {
|
||||||
|
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, id)
|
||||||
|
var resp Agent
|
||||||
|
err := m.QueryRowCtx(ctx, &resp, qncAgentIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||||
|
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", agentRows, m.table)
|
||||||
|
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
|
||||||
|
})
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
return &resp, nil
|
||||||
|
case sqlc.ErrNotFound:
|
||||||
|
return nil, ErrNotFound
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) FindOneByUserId(ctx context.Context, userId int64) (*Agent, error) {
|
||||||
|
qncAgentUserIdKey := fmt.Sprintf("%s%v", cacheQncAgentUserIdPrefix, userId)
|
||||||
|
var resp Agent
|
||||||
|
err := m.QueryRowIndexCtx(ctx, &resp, qncAgentUserIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||||
|
query := fmt.Sprintf("select %s from %s where `user_id` = ? and del_state = ? limit 1", agentRows, m.table)
|
||||||
|
if err := conn.QueryRowCtx(ctx, &resp, query, userId, globalkey.DelStateNo); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return resp.Id, nil
|
||||||
|
}, m.queryPrimary)
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
return &resp, nil
|
||||||
|
case sqlc.ErrNotFound:
|
||||||
|
return nil, ErrNotFound
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) Update(ctx context.Context, session sqlx.Session, newData *Agent) (sql.Result, error) {
|
||||||
|
data, err := m.FindOne(ctx, newData.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, data.Id)
|
||||||
|
qncAgentUserIdKey := fmt.Sprintf("%s%v", cacheQncAgentUserIdPrefix, data.UserId)
|
||||||
|
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, agentRowsWithPlaceHolder)
|
||||||
|
if session != nil {
|
||||||
|
return session.ExecCtx(ctx, query, newData.UserId, newData.Region, newData.Mobile, newData.WechatId, newData.DeleteTime, newData.DelState, newData.Version, newData.Id)
|
||||||
|
}
|
||||||
|
return conn.ExecCtx(ctx, query, newData.UserId, newData.Region, newData.Mobile, newData.WechatId, newData.DeleteTime, newData.DelState, newData.Version, newData.Id)
|
||||||
|
}, qncAgentIdKey, qncAgentUserIdKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *Agent) 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
|
||||||
|
}
|
||||||
|
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, data.Id)
|
||||||
|
qncAgentUserIdKey := fmt.Sprintf("%s%v", cacheQncAgentUserIdPrefix, data.UserId)
|
||||||
|
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, agentRowsWithPlaceHolder)
|
||||||
|
if session != nil {
|
||||||
|
return session.ExecCtx(ctx, query, newData.UserId, newData.Region, newData.Mobile, newData.WechatId, newData.DeleteTime, newData.DelState, newData.Version, newData.Id, oldVersion)
|
||||||
|
}
|
||||||
|
return conn.ExecCtx(ctx, query, newData.UserId, newData.Region, newData.Mobile, newData.WechatId, newData.DeleteTime, newData.DelState, newData.Version, newData.Id, oldVersion)
|
||||||
|
}, qncAgentIdKey, qncAgentUserIdKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
updateCount, err := sqlResult.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if updateCount == 0 {
|
||||||
|
return ErrNoRowsUpdate
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) DeleteSoft(ctx context.Context, session sqlx.Session, data *Agent) 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 "), "AgentModel delete err : %+v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) 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 *defaultAgentModel) 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 *defaultAgentModel) FindAll(ctx context.Context, builder squirrel.SelectBuilder, orderBy string) ([]*Agent, error) {
|
||||||
|
|
||||||
|
builder = builder.Columns(agentRows)
|
||||||
|
|
||||||
|
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 []*Agent
|
||||||
|
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
return resp, nil
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) FindPageListByPage(ctx context.Context, builder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*Agent, error) {
|
||||||
|
|
||||||
|
builder = builder.Columns(agentRows)
|
||||||
|
|
||||||
|
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 []*Agent
|
||||||
|
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
return resp, nil
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) FindPageListByPageWithTotal(ctx context.Context, builder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*Agent, int64, error) {
|
||||||
|
|
||||||
|
total, err := m.FindCount(ctx, builder, "id")
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
builder = builder.Columns(agentRows)
|
||||||
|
|
||||||
|
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 []*Agent
|
||||||
|
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
return resp, total, nil
|
||||||
|
default:
|
||||||
|
return nil, total, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) FindPageListByIdDESC(ctx context.Context, builder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*Agent, error) {
|
||||||
|
|
||||||
|
builder = builder.Columns(agentRows)
|
||||||
|
|
||||||
|
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 []*Agent
|
||||||
|
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
return resp, nil
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) FindPageListByIdASC(ctx context.Context, builder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*Agent, error) {
|
||||||
|
|
||||||
|
builder = builder.Columns(agentRows)
|
||||||
|
|
||||||
|
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 []*Agent
|
||||||
|
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
return resp, nil
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) 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 *defaultAgentModel) SelectBuilder() squirrel.SelectBuilder {
|
||||||
|
return squirrel.Select().From(m.table)
|
||||||
|
}
|
||||||
|
func (m *defaultAgentModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
|
||||||
|
data, err := m.FindOne(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
qncAgentIdKey := fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, id)
|
||||||
|
qncAgentUserIdKey := fmt.Sprintf("%s%v", cacheQncAgentUserIdPrefix, data.UserId)
|
||||||
|
_, 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)
|
||||||
|
if session != nil {
|
||||||
|
return session.ExecCtx(ctx, query, id)
|
||||||
|
}
|
||||||
|
return conn.ExecCtx(ctx, query, id)
|
||||||
|
}, qncAgentIdKey, qncAgentUserIdKey)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
func (m *defaultAgentModel) formatPrimary(primary interface{}) string {
|
||||||
|
return fmt.Sprintf("%s%v", cacheQncAgentIdPrefix, primary)
|
||||||
|
}
|
||||||
|
func (m *defaultAgentModel) 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", agentRows, m.table)
|
||||||
|
return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultAgentModel) tableName() string {
|
||||||
|
return m.table
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user