From ac656a41256dd410167f8abcdc9b88d1bfe087ae Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Wed, 1 Jan 2025 02:00:06 +0800 Subject: [PATCH] =?UTF-8?q?v1.1.0=20=20=E6=96=B0=E5=A2=9E=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E3=80=81=E6=9A=82=E6=97=B6=E6=80=A72=E6=8A=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/user/cmd/api/desc/user.api | 12 + app/user/cmd/api/desc/user/user.api | 17 + .../notification/getnotificationshandler.go | 17 + app/user/cmd/api/internal/handler/routes.go | 13 + .../notification/getnotificationslogic.go | 56 +++ .../api/internal/logic/pay/paymentlogic.go | 25 +- .../cmd/api/internal/svc/servicecontext.go | 78 ++-- app/user/cmd/api/internal/types/types.go | 15 + app/user/model/globalNotificationsModel.go | 27 ++ .../model/globalNotificationsModel_gen.go | 375 ++++++++++++++++++ 10 files changed, 595 insertions(+), 40 deletions(-) create mode 100644 app/user/cmd/api/internal/handler/notification/getnotificationshandler.go create mode 100644 app/user/cmd/api/internal/logic/notification/getnotificationslogic.go create mode 100644 app/user/model/globalNotificationsModel.go create mode 100644 app/user/model/globalNotificationsModel_gen.go diff --git a/app/user/cmd/api/desc/user.api b/app/user/cmd/api/desc/user.api index 07442dc..eb68c83 100644 --- a/app/user/cmd/api/desc/user.api +++ b/app/user/cmd/api/desc/user.api @@ -65,3 +65,15 @@ service main { post /auth/sendSms (sendSmsReq) } + + +//============================> notification v1 <============================ +@server ( + prefix: api/v1 + group: notification +) +service main { + @doc "get notifications" + @handler getNotifications + get /notification/list returns (GetNotificationsResp) +} \ No newline at end of file diff --git a/app/user/cmd/api/desc/user/user.api b/app/user/cmd/api/desc/user/user.api index 4976297..f7f8791 100644 --- a/app/user/cmd/api/desc/user/user.api +++ b/app/user/cmd/api/desc/user/user.api @@ -78,3 +78,20 @@ type ( } ) +//============================> notification v1 <============================ +type Notification { + Title string `json:"title"` // 通知标题 + Content string `json:"content"` // 通知内容 (富文本) + NotificationPage string `json:"notificationPage"` // 通知页面 + StartDate string `json:"startDate"` // 通知开始日期,格式 "YYYY-MM-DD" + EndDate string `json:"endDate"` // 通知结束日期,格式 "YYYY-MM-DD" + StartTime string `json:"startTime"` // 每天通知开始时间,格式 "HH:MM:SS" + EndTime string `json:"endTime"` // 每天通知结束时间,格式 "HH:MM:SS" +} +type ( + // 获取通知响应体(分页) + GetNotificationsResp { + Notifications []Notification `json:"notifications"` // 通知列表 + Total int64 `json:"total"` // 总记录数 + } +) \ No newline at end of file diff --git a/app/user/cmd/api/internal/handler/notification/getnotificationshandler.go b/app/user/cmd/api/internal/handler/notification/getnotificationshandler.go new file mode 100644 index 0000000..6dc313f --- /dev/null +++ b/app/user/cmd/api/internal/handler/notification/getnotificationshandler.go @@ -0,0 +1,17 @@ +package notification + +import ( + "net/http" + + "qnc-server/app/user/cmd/api/internal/logic/notification" + "qnc-server/app/user/cmd/api/internal/svc" + "qnc-server/common/result" +) + +func GetNotificationsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + l := notification.NewGetNotificationsLogic(r.Context(), svcCtx) + resp, err := l.GetNotifications() + result.HttpResult(r, w, resp, err) + } +} diff --git a/app/user/cmd/api/internal/handler/routes.go b/app/user/cmd/api/internal/handler/routes.go index c74110b..46d00d1 100644 --- a/app/user/cmd/api/internal/handler/routes.go +++ b/app/user/cmd/api/internal/handler/routes.go @@ -5,6 +5,7 @@ import ( "net/http" auth "qnc-server/app/user/cmd/api/internal/handler/auth" + notification "qnc-server/app/user/cmd/api/internal/handler/notification" pay "qnc-server/app/user/cmd/api/internal/handler/pay" product "qnc-server/app/user/cmd/api/internal/handler/product" query "qnc-server/app/user/cmd/api/internal/handler/query" @@ -27,6 +28,18 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { rest.WithPrefix("/api/v1"), ) + server.AddRoutes( + []rest.Route{ + { + // get notifications + Method: http.MethodGet, + Path: "/notification/list", + Handler: notification.GetNotificationsHandler(serverCtx), + }, + }, + rest.WithPrefix("/api/v1"), + ) + server.AddRoutes( []rest.Route{ { diff --git a/app/user/cmd/api/internal/logic/notification/getnotificationslogic.go b/app/user/cmd/api/internal/logic/notification/getnotificationslogic.go new file mode 100644 index 0000000..92ffd33 --- /dev/null +++ b/app/user/cmd/api/internal/logic/notification/getnotificationslogic.go @@ -0,0 +1,56 @@ +package notification + +import ( + "context" + "github.com/jinzhu/copier" + "github.com/pkg/errors" + "qnc-server/common/xerr" + "time" + + "qnc-server/app/user/cmd/api/internal/svc" + "qnc-server/app/user/cmd/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetNotificationsLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetNotificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetNotificationsLogic { + return &GetNotificationsLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetNotificationsLogic) GetNotifications() (resp *types.GetNotificationsResp, err error) { + // 获取今天的日期 + now := time.Now() + + // 获取开始和结束日期的时间戳 + todayStart := now.Format("2006-01-02") + " 00:00:00" + todayEnd := now.Format("2006-01-02") + " 23:59:59" + + // 构建查询条件 + builder := l.svcCtx.GlobalNotificationsModel.SelectBuilder(). + Where("status = ?", "active"). + Where("(start_date IS NULL OR start_date <= ?)", todayEnd). // start_date 是 NULL 或者小于等于今天结束时间 + Where("(end_date IS NULL OR end_date >= ?)", todayStart) // end_date 是 NULL 或者大于等于今天开始时间 + + notificationsModelList, findErr := l.svcCtx.GlobalNotificationsModel.FindAll(l.ctx, builder, "") + if findErr != nil { + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "全局通知, 查找通知失败, err:%+v", findErr) + } + + var notifications []types.Notification + copyErr := copier.Copy(¬ifications, ¬ificationsModelList) + if copyErr != nil { + return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "全局通知, 复制结构体失败, err:%+v", copyErr) + } + + return &types.GetNotificationsResp{Notifications: notifications}, nil +} diff --git a/app/user/cmd/api/internal/logic/pay/paymentlogic.go b/app/user/cmd/api/internal/logic/pay/paymentlogic.go index 3003c02..baa3504 100644 --- a/app/user/cmd/api/internal/logic/pay/paymentlogic.go +++ b/app/user/cmd/api/internal/logic/pay/paymentlogic.go @@ -8,12 +8,14 @@ import ( "github.com/pkg/errors" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/sqlx" + "math" "qnc-server/app/user/cmd/api/internal/svc" "qnc-server/app/user/cmd/api/internal/types" "qnc-server/app/user/model" "qnc-server/common/ctxdata" "qnc-server/common/xerr" "qnc-server/pkg/lzkit/crypto" + "time" ) type PaymentLogic struct { @@ -70,10 +72,29 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 获取用户信息失败: %+v", err) } + + currentTime := time.Now() + + var mhour int if user.Inside == 1 { - amount = 0.01 + mhour = 0 } else { - amount = product.SellPrice + mhour = 12 + } + // 设置时间范围:2025年1月1日中午12点到2025年1月2日中午12点 + startTime := time.Date(2025, 1, 1, mhour, 0, 0, 0, time.Local) + endTime := time.Date(2025, 1, 2, 12, 0, 0, 0, time.Local) + + if currentTime.After(startTime) && currentTime.Before(endTime) { + // 打两折 + amount = product.SellPrice * 0.2 + amount = math.Round(amount*100) / 100 + } else { + if user.Inside == 1 { + amount = 0.01 + } else { + amount = product.SellPrice + } } var createOrderErr error if req.PayMethod == "wechatpay" { diff --git a/app/user/cmd/api/internal/svc/servicecontext.go b/app/user/cmd/api/internal/svc/servicecontext.go index 02a3652..90c11ca 100644 --- a/app/user/cmd/api/internal/svc/servicecontext.go +++ b/app/user/cmd/api/internal/svc/servicecontext.go @@ -13,25 +13,26 @@ import ( ) type ServiceContext struct { - Config config.Config - Redis *redis.Redis - SourceInterceptor rest.Middleware - UserModel model.UserModel - UserAuthModel model.UserAuthModel - ProductModel model.ProductModel - FeatureModel model.FeatureModel - ProductFeatureModel model.ProductFeatureModel - OrderModel model.OrderModel - QueryModel model.QueryModel - AlipayService *service.AliPayService - WechatPayService *service.WechatPayService - ApplePayService *service.ApplePayService - WestDexService *service.WestDexService - YushanService *service.YushanService - ApiRequestService *service.ApiRequestService - AsynqServer *asynq.Server // 服务端 - AsynqService *service.AsynqService // 客户端 - VerificationService *service.VerificationService + Config config.Config + Redis *redis.Redis + SourceInterceptor rest.Middleware + UserModel model.UserModel + UserAuthModel model.UserAuthModel + ProductModel model.ProductModel + FeatureModel model.FeatureModel + ProductFeatureModel model.ProductFeatureModel + OrderModel model.OrderModel + QueryModel model.QueryModel + GlobalNotificationsModel model.GlobalNotificationsModel + AlipayService *service.AliPayService + WechatPayService *service.WechatPayService + ApplePayService *service.ApplePayService + WestDexService *service.WestDexService + YushanService *service.YushanService + ApiRequestService *service.ApiRequestService + AsynqServer *asynq.Server // 服务端 + AsynqService *service.AsynqService // 客户端 + VerificationService *service.VerificationService } func NewServiceContext(c config.Config) *ServiceContext { @@ -56,25 +57,26 @@ func NewServiceContext(c config.Config) *ServiceContext { productFeatureModel := model.NewProductFeatureModel(db, c.CacheRedis) featureModel := model.NewFeatureModel(db, c.CacheRedis) return &ServiceContext{ - Config: c, - Redis: redis.MustNewRedis(redisConf), - SourceInterceptor: middleware.NewSourceInterceptorMiddleware().Handle, - AlipayService: service.NewAliPayService(c), - WechatPayService: service.NewWechatPayService(c), - ApplePayService: service.NewApplePayService(c), - WestDexService: westDexService, - YushanService: yushanService, - VerificationService: service.NewVerificationService(c, westDexService), - AsynqServer: asynqServer, - ApiRequestService: service.NewApiRequestService(c, westDexService, yushanService, featureModel, productFeatureModel), - AsynqService: service.NewAsynqService(c), - UserModel: model.NewUserModel(db, c.CacheRedis), - UserAuthModel: model.NewUserAuthModel(db, c.CacheRedis), - ProductModel: model.NewProductModel(db, c.CacheRedis), - OrderModel: model.NewOrderModel(db, c.CacheRedis), - QueryModel: model.NewQueryModel(db, c.CacheRedis), - FeatureModel: featureModel, - ProductFeatureModel: productFeatureModel, + Config: c, + Redis: redis.MustNewRedis(redisConf), + SourceInterceptor: middleware.NewSourceInterceptorMiddleware().Handle, + AlipayService: service.NewAliPayService(c), + WechatPayService: service.NewWechatPayService(c), + ApplePayService: service.NewApplePayService(c), + WestDexService: westDexService, + YushanService: yushanService, + VerificationService: service.NewVerificationService(c, westDexService), + AsynqServer: asynqServer, + ApiRequestService: service.NewApiRequestService(c, westDexService, yushanService, featureModel, productFeatureModel), + AsynqService: service.NewAsynqService(c), + UserModel: model.NewUserModel(db, c.CacheRedis), + UserAuthModel: model.NewUserAuthModel(db, c.CacheRedis), + ProductModel: model.NewProductModel(db, c.CacheRedis), + OrderModel: model.NewOrderModel(db, c.CacheRedis), + QueryModel: model.NewQueryModel(db, c.CacheRedis), + GlobalNotificationsModel: model.NewGlobalNotificationsModel(db, c.CacheRedis), + FeatureModel: featureModel, + ProductFeatureModel: productFeatureModel, } } func (s *ServiceContext) Close() { diff --git a/app/user/cmd/api/internal/types/types.go b/app/user/cmd/api/internal/types/types.go index dab2dfc..e117771 100644 --- a/app/user/cmd/api/internal/types/types.go +++ b/app/user/cmd/api/internal/types/types.go @@ -7,6 +7,11 @@ type Feature struct { Name string `json:"name"` // 功能描述 } +type GetNotificationsResp struct { + Notifications []Notification `json:"notifications"` // 通知列表 + Total int64 `json:"total"` // 总记录数 +} + type GetProductByEnRequest struct { ProductEn string `path:"product_en"` } @@ -42,6 +47,16 @@ type MobileLoginResp struct { RefreshAfter int64 `json:"refreshAfter"` } +type Notification struct { + Title string `json:"title"` // 通知标题 + Content string `json:"content"` // 通知内容 (富文本) + NotificationPage string `json:"notificationPage"` // 通知页面 + StartDate string `json:"startDate"` // 通知开始日期,格式 "YYYY-MM-DD" + EndDate string `json:"endDate"` // 通知结束日期,格式 "YYYY-MM-DD" + StartTime string `json:"startTime"` // 每天通知开始时间,格式 "HH:MM:SS" + EndTime string `json:"endTime"` // 每天通知结束时间,格式 "HH:MM:SS" +} + type PaymentReq struct { Id string `json:"id"` PayMethod string `json:"pay_method"` diff --git a/app/user/model/globalNotificationsModel.go b/app/user/model/globalNotificationsModel.go new file mode 100644 index 0000000..059bdff --- /dev/null +++ b/app/user/model/globalNotificationsModel.go @@ -0,0 +1,27 @@ +package model + +import ( + "github.com/zeromicro/go-zero/core/stores/cache" + "github.com/zeromicro/go-zero/core/stores/sqlx" +) + +var _ GlobalNotificationsModel = (*customGlobalNotificationsModel)(nil) + +type ( + // GlobalNotificationsModel is an interface to be customized, add more methods here, + // and implement the added methods in customGlobalNotificationsModel. + GlobalNotificationsModel interface { + globalNotificationsModel + } + + customGlobalNotificationsModel struct { + *defaultGlobalNotificationsModel + } +) + +// NewGlobalNotificationsModel returns a model for the database table. +func NewGlobalNotificationsModel(conn sqlx.SqlConn, c cache.CacheConf) GlobalNotificationsModel { + return &customGlobalNotificationsModel{ + defaultGlobalNotificationsModel: newGlobalNotificationsModel(conn, c), + } +} diff --git a/app/user/model/globalNotificationsModel_gen.go b/app/user/model/globalNotificationsModel_gen.go new file mode 100644 index 0000000..1045450 --- /dev/null +++ b/app/user/model/globalNotificationsModel_gen.go @@ -0,0 +1,375 @@ +// Code generated by goctl. DO NOT EDIT! + +package model + +import ( + "context" + "database/sql" + "fmt" + model2 "qnc-server/deploy/script/model" + "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 ( + globalNotificationsFieldNames = builder.RawFieldNames(&GlobalNotifications{}) + globalNotificationsRows = strings.Join(globalNotificationsFieldNames, ",") + globalNotificationsRowsExpectAutoSet = strings.Join(stringx.Remove(globalNotificationsFieldNames, "`id`", "`create_time`", "`update_time`"), ",") + globalNotificationsRowsWithPlaceHolder = strings.Join(stringx.Remove(globalNotificationsFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?" + + cacheQncGlobalNotificationsIdPrefix = "cache:qnc:globalNotifications:id:" +) + +type ( + globalNotificationsModel interface { + Insert(ctx context.Context, session sqlx.Session, data *GlobalNotifications) (sql.Result, error) + FindOne(ctx context.Context, id int64) (*GlobalNotifications, error) + Update(ctx context.Context, session sqlx.Session, data *GlobalNotifications) (sql.Result, error) + UpdateWithVersion(ctx context.Context, session sqlx.Session, data *GlobalNotifications) 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 *GlobalNotifications) 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) ([]*GlobalNotifications, error) + FindPageListByPage(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*GlobalNotifications, error) + FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*GlobalNotifications, int64, error) + FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*GlobalNotifications, error) + FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*GlobalNotifications, error) + Delete(ctx context.Context, session sqlx.Session, id int64) error + } + + defaultGlobalNotificationsModel struct { + sqlc.CachedConn + table string + } + + GlobalNotifications struct { + Id int64 `db:"id"` + Title string `db:"title"` + Content string `db:"content"` + NotificationPage string `db:"notification_page"` + StartDate sql.NullTime `db:"start_date"` + EndDate sql.NullTime `db:"end_date"` + StartTime string `db:"start_time"` + EndTime string `db:"end_time"` + CreatedAt time.Time `db:"created_at"` + UpdatedAt time.Time `db:"updated_at"` + Status string `db:"status"` + DelState int64 `db:"del_state"` // 删除状态 + Version int64 `db:"version"` // 版本号 + DeleteTime sql.NullTime `db:"delete_time"` // 删除时间 + } +) + +func newGlobalNotificationsModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultGlobalNotificationsModel { + return &defaultGlobalNotificationsModel{ + CachedConn: sqlc.NewConn(conn, c), + table: "`global_notifications`", + } +} + +func (m *defaultGlobalNotificationsModel) Insert(ctx context.Context, session sqlx.Session, data *GlobalNotifications) (sql.Result, error) { + data.DelState = globalkey.DelStateNo + qncGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheQncGlobalNotificationsIdPrefix, data.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, globalNotificationsRowsExpectAutoSet) + if session != nil { + return session.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime) + } + return conn.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime) + }, qncGlobalNotificationsIdKey) +} + +func (m *defaultGlobalNotificationsModel) FindOne(ctx context.Context, id int64) (*GlobalNotifications, error) { + qncGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheQncGlobalNotificationsIdPrefix, id) + var resp GlobalNotifications + err := m.QueryRowCtx(ctx, &resp, qncGlobalNotificationsIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error { + query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", globalNotificationsRows, m.table) + return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo) + }) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, model2.ErrNotFound + default: + return nil, err + } +} + +func (m *defaultGlobalNotificationsModel) Update(ctx context.Context, session sqlx.Session, data *GlobalNotifications) (sql.Result, error) { + qncGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheQncGlobalNotificationsIdPrefix, data.Id) + 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, globalNotificationsRowsWithPlaceHolder) + if session != nil { + return session.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime, data.Id) + } + return conn.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime, data.Id) + }, qncGlobalNotificationsIdKey) +} + +func (m *defaultGlobalNotificationsModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, data *GlobalNotifications) error { + + oldVersion := data.Version + data.Version += 1 + + var sqlResult sql.Result + var err error + + qncGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheQncGlobalNotificationsIdPrefix, data.Id) + 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, globalNotificationsRowsWithPlaceHolder) + if session != nil { + return session.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime, data.Id, oldVersion) + } + return conn.ExecCtx(ctx, query, data.Title, data.Content, data.NotificationPage, data.StartDate, data.EndDate, data.StartTime, data.EndTime, data.Status, data.DelState, data.Version, data.DeleteTime, data.Id, oldVersion) + }, qncGlobalNotificationsIdKey) + if err != nil { + return err + } + updateCount, err := sqlResult.RowsAffected() + if err != nil { + return err + } + if updateCount == 0 { + return model2.ErrNoRowsUpdate + } + + return nil +} + +func (m *defaultGlobalNotificationsModel) DeleteSoft(ctx context.Context, session sqlx.Session, data *GlobalNotifications) 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 "), "GlobalNotificationsModel delete err : %+v", err) + } + return nil +} + +func (m *defaultGlobalNotificationsModel) 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 *defaultGlobalNotificationsModel) 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 *defaultGlobalNotificationsModel) FindAll(ctx context.Context, builder squirrel.SelectBuilder, orderBy string) ([]*GlobalNotifications, error) { + + builder = builder.Columns(globalNotificationsRows) + + 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 []*GlobalNotifications + err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...) + switch err { + case nil: + return resp, nil + default: + return nil, err + } +} + +func (m *defaultGlobalNotificationsModel) FindPageListByPage(ctx context.Context, builder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*GlobalNotifications, error) { + + builder = builder.Columns(globalNotificationsRows) + + 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 []*GlobalNotifications + err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...) + switch err { + case nil: + return resp, nil + default: + return nil, err + } +} + +func (m *defaultGlobalNotificationsModel) FindPageListByPageWithTotal(ctx context.Context, builder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*GlobalNotifications, int64, error) { + + total, err := m.FindCount(ctx, builder, "id") + if err != nil { + return nil, 0, err + } + + builder = builder.Columns(globalNotificationsRows) + + 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 []*GlobalNotifications + err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...) + switch err { + case nil: + return resp, total, nil + default: + return nil, total, err + } +} + +func (m *defaultGlobalNotificationsModel) FindPageListByIdDESC(ctx context.Context, builder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*GlobalNotifications, error) { + + builder = builder.Columns(globalNotificationsRows) + + 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 []*GlobalNotifications + err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...) + switch err { + case nil: + return resp, nil + default: + return nil, err + } +} + +func (m *defaultGlobalNotificationsModel) FindPageListByIdASC(ctx context.Context, builder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*GlobalNotifications, error) { + + builder = builder.Columns(globalNotificationsRows) + + 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 []*GlobalNotifications + err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...) + switch err { + case nil: + return resp, nil + default: + return nil, err + } +} + +func (m *defaultGlobalNotificationsModel) 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 *defaultGlobalNotificationsModel) SelectBuilder() squirrel.SelectBuilder { + return squirrel.Select().From(m.table) +} +func (m *defaultGlobalNotificationsModel) Delete(ctx context.Context, session sqlx.Session, id int64) error { + qncGlobalNotificationsIdKey := fmt.Sprintf("%s%v", cacheQncGlobalNotificationsIdPrefix, id) + _, 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) + }, qncGlobalNotificationsIdKey) + return err +} +func (m *defaultGlobalNotificationsModel) formatPrimary(primary interface{}) string { + return fmt.Sprintf("%s%v", cacheQncGlobalNotificationsIdPrefix, primary) +} +func (m *defaultGlobalNotificationsModel) 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", globalNotificationsRows, m.table) + return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo) +} + +func (m *defaultGlobalNotificationsModel) tableName() string { + return m.table +}