Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e61988d78f |
@@ -1,121 +0,0 @@
|
|||||||
syntax = "v1"
|
|
||||||
|
|
||||||
info (
|
|
||||||
title: "promo admin API"
|
|
||||||
desc: "API for ppanel"
|
|
||||||
author: "Tension"
|
|
||||||
email: "tension@ppanel.com"
|
|
||||||
version: "0.0.1"
|
|
||||||
)
|
|
||||||
|
|
||||||
import "../types.api"
|
|
||||||
|
|
||||||
type (
|
|
||||||
CreatePromoRuleRequest {
|
|
||||||
Name string `json:"name" validate:"required,max=100"`
|
|
||||||
Type string `json:"type" validate:"required,oneof=new_user inactive_user campaign"`
|
|
||||||
Params map[string]interface{} `json:"params"`
|
|
||||||
Priority int64 `json:"priority" validate:"gte=0"`
|
|
||||||
Enabled *bool `json:"enabled"`
|
|
||||||
StartTime *int64 `json:"start_time"`
|
|
||||||
EndTime *int64 `json:"end_time"`
|
|
||||||
}
|
|
||||||
UpdatePromoRuleRequest {
|
|
||||||
Id int64 `uri:"id" validate:"required,gt=0"`
|
|
||||||
Name string `json:"name" validate:"required,max=100"`
|
|
||||||
Type string `json:"type" validate:"required,oneof=new_user inactive_user campaign"`
|
|
||||||
Params map[string]interface{} `json:"params"`
|
|
||||||
Priority int64 `json:"priority" validate:"gte=0"`
|
|
||||||
Enabled *bool `json:"enabled"`
|
|
||||||
StartTime *int64 `json:"start_time"`
|
|
||||||
EndTime *int64 `json:"end_time"`
|
|
||||||
}
|
|
||||||
GetPromoRuleDetailRequest {
|
|
||||||
Id int64 `uri:"id" validate:"required,gt=0"`
|
|
||||||
}
|
|
||||||
DeletePromoRuleRequest {
|
|
||||||
Id int64 `uri:"id" validate:"required,gt=0"`
|
|
||||||
}
|
|
||||||
GetPromoRuleListRequest {
|
|
||||||
Page int64 `form:"page" validate:"required"`
|
|
||||||
Size int64 `form:"size" validate:"required"`
|
|
||||||
Type string `form:"type" validate:"omitempty,oneof=new_user inactive_user campaign"`
|
|
||||||
Enabled *bool `form:"enabled"`
|
|
||||||
Search string `form:"search,omitempty"`
|
|
||||||
}
|
|
||||||
GetPromoRuleListResponse {
|
|
||||||
Total int64 `json:"total"`
|
|
||||||
List []PromoRule `json:"list"`
|
|
||||||
}
|
|
||||||
SetPromoPriceRequest {
|
|
||||||
PromoRuleId int64 `json:"promo_rule_id" validate:"required,gt=0"`
|
|
||||||
Items []PromoPriceItem `json:"items" validate:"required,dive"`
|
|
||||||
}
|
|
||||||
GetPromoPriceListRequest {
|
|
||||||
PromoRuleId int64 `form:"promo_rule_id" validate:"required,gt=0"`
|
|
||||||
Page int64 `form:"page" validate:"required"`
|
|
||||||
Size int64 `form:"size" validate:"required"`
|
|
||||||
}
|
|
||||||
GetPromoPriceListResponse {
|
|
||||||
Total int64 `json:"total"`
|
|
||||||
List []PromoPrice `json:"list"`
|
|
||||||
}
|
|
||||||
DeletePromoPriceRequest {
|
|
||||||
Id int64 `uri:"id" validate:"required,gt=0"`
|
|
||||||
}
|
|
||||||
GetPromoUsageListRequest {
|
|
||||||
Page int64 `form:"page" validate:"required"`
|
|
||||||
Size int64 `form:"size" validate:"required"`
|
|
||||||
RuleId int64 `form:"rule_id,omitempty"`
|
|
||||||
UserId int64 `form:"user_id,omitempty"`
|
|
||||||
SubscribeId int64 `form:"subscribe_id,omitempty"`
|
|
||||||
OrderNo string `form:"order_no,omitempty"`
|
|
||||||
}
|
|
||||||
GetPromoUsageListResponse {
|
|
||||||
Total int64 `json:"total"`
|
|
||||||
List []PromoUsage `json:"list"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
@server (
|
|
||||||
prefix: v1/admin/promo
|
|
||||||
group: admin/promo
|
|
||||||
middleware: AuthMiddleware
|
|
||||||
)
|
|
||||||
service ppanel {
|
|
||||||
@doc "Create promo rule"
|
|
||||||
@handler CreateRule
|
|
||||||
post /rule (CreatePromoRuleRequest) returns (PromoRule)
|
|
||||||
|
|
||||||
@doc "Get promo rule list"
|
|
||||||
@handler GetRuleList
|
|
||||||
get /rule/list (GetPromoRuleListRequest) returns (GetPromoRuleListResponse)
|
|
||||||
|
|
||||||
@doc "Get promo rule detail"
|
|
||||||
@handler GetRuleDetail
|
|
||||||
get /rule/:id (GetPromoRuleDetailRequest) returns (PromoRule)
|
|
||||||
|
|
||||||
@doc "Update promo rule"
|
|
||||||
@handler UpdateRule
|
|
||||||
put /rule/:id (UpdatePromoRuleRequest) returns (PromoRule)
|
|
||||||
|
|
||||||
@doc "Delete promo rule"
|
|
||||||
@handler DeleteRule
|
|
||||||
delete /rule/:id (DeletePromoRuleRequest)
|
|
||||||
|
|
||||||
@doc "Set promo prices"
|
|
||||||
@handler SetPrice
|
|
||||||
post /price (SetPromoPriceRequest)
|
|
||||||
|
|
||||||
@doc "Get promo price list"
|
|
||||||
@handler GetPriceList
|
|
||||||
get /price/list (GetPromoPriceListRequest) returns (GetPromoPriceListResponse)
|
|
||||||
|
|
||||||
@doc "Delete promo price"
|
|
||||||
@handler DeletePrice
|
|
||||||
delete /price/:id (DeletePromoPriceRequest)
|
|
||||||
|
|
||||||
@doc "Get promo usage list"
|
|
||||||
@handler GetUsageList
|
|
||||||
get /usage/list (GetPromoUsageListRequest) returns (GetPromoUsageListResponse)
|
|
||||||
}
|
|
||||||
@@ -230,41 +230,6 @@ type (
|
|||||||
Discount float64 `json:"discount"`
|
Discount float64 `json:"discount"`
|
||||||
MapApple string `json:"map_apple"`
|
MapApple string `json:"map_apple"`
|
||||||
}
|
}
|
||||||
PromoPrice {
|
|
||||||
Id int64 `json:"id"`
|
|
||||||
SubscribeId int64 `json:"subscribe_id"`
|
|
||||||
PromoRuleId int64 `json:"promo_rule_id"`
|
|
||||||
Quantity int64 `json:"quantity"`
|
|
||||||
PromoPrice int64 `json:"promo_price"`
|
|
||||||
CreatedAt int64 `json:"created_at"`
|
|
||||||
UpdatedAt int64 `json:"updated_at"`
|
|
||||||
}
|
|
||||||
PromoPriceItem {
|
|
||||||
SubscribeId int64 `json:"subscribe_id" validate:"required,gt=0"`
|
|
||||||
Quantity int64 `json:"quantity" validate:"required,gt=0,lte=1000"`
|
|
||||||
PromoPrice int64 `json:"promo_price" validate:"required,gt=0"`
|
|
||||||
}
|
|
||||||
PromoRule {
|
|
||||||
Id int64 `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Params map[string]interface{} `json:"params"`
|
|
||||||
Priority int64 `json:"priority"`
|
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
StartTime *int64 `json:"start_time"`
|
|
||||||
EndTime *int64 `json:"end_time"`
|
|
||||||
CreatedAt int64 `json:"created_at"`
|
|
||||||
UpdatedAt int64 `json:"updated_at"`
|
|
||||||
}
|
|
||||||
PromoUsage {
|
|
||||||
Id int64 `json:"id"`
|
|
||||||
UserId int64 `json:"user_id"`
|
|
||||||
PromoRuleId int64 `json:"promo_rule_id"`
|
|
||||||
SubscribeId int64 `json:"subscribe_id"`
|
|
||||||
OrderNo string `json:"order_no"`
|
|
||||||
PromoPrice int64 `json:"promo_price"`
|
|
||||||
CreatedAt int64 `json:"created_at"`
|
|
||||||
}
|
|
||||||
TrafficLimit {
|
TrafficLimit {
|
||||||
StatType string `json:"stat_type"`
|
StatType string `json:"stat_type"`
|
||||||
StatValue int64 `json:"stat_value"`
|
StatValue int64 `json:"stat_value"`
|
||||||
|
|||||||
+4
@@ -1,3 +1,7 @@
|
|||||||
|
ALTER TABLE `order`
|
||||||
|
DROP COLUMN IF EXISTS `promo_discount`,
|
||||||
|
DROP COLUMN IF EXISTS `promo_rule_id`;
|
||||||
|
|
||||||
DROP TABLE IF EXISTS `promo_usage`;
|
DROP TABLE IF EXISTS `promo_usage`;
|
||||||
DROP TABLE IF EXISTS `subscribe_promo`;
|
DROP TABLE IF EXISTS `subscribe_promo`;
|
||||||
DROP TABLE IF EXISTS `promo_rule`;
|
DROP TABLE IF EXISTS `promo_rule`;
|
||||||
+20
-10
@@ -1,38 +1,36 @@
|
|||||||
CREATE TABLE IF NOT EXISTS `promo_rule` (
|
CREATE TABLE IF NOT EXISTS `promo_rule` (
|
||||||
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
`name` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '规则名称',
|
`name` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '规则名称,如"新客7天优惠"',
|
||||||
`type` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '规则类型:new_user / inactive_user / campaign',
|
`type` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '规则类型:new_user / inactive_user / campaign',
|
||||||
`params` JSON NOT NULL COMMENT '类型专属参数',
|
`params` JSON NOT NULL COMMENT '类型专属参数',
|
||||||
`priority` INT NOT NULL DEFAULT 0 COMMENT '优先级,数值越大越优先匹配',
|
`priority` INT NOT NULL DEFAULT 0 COMMENT '优先级,数值越大越优先匹配',
|
||||||
`enabled` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
|
`enabled` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
|
||||||
`start_time` DATETIME DEFAULT NULL COMMENT '生效开始时间',
|
`start_time` DATETIME DEFAULT NULL COMMENT '生效开始时间,NULL=立即生效',
|
||||||
`end_time` DATETIME DEFAULT NULL COMMENT '生效结束时间',
|
`end_time` DATETIME DEFAULT NULL COMMENT '生效结束时间,NULL=永不过期',
|
||||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
`deleted_at` DATETIME DEFAULT NULL COMMENT '软删除时间',
|
`deleted_at` DATETIME DEFAULT NULL COMMENT '软删除时间',
|
||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
KEY `idx_enabled_priority` (`enabled`, `priority` DESC),
|
KEY `idx_enabled_priority` (`enabled`, `priority` DESC)
|
||||||
KEY `idx_deleted_at` (`deleted_at`)
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='促销规则表';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='促销规则表';
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `subscribe_promo` (
|
CREATE TABLE IF NOT EXISTS `subscribe_promo` (
|
||||||
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
`subscribe_id` BIGINT UNSIGNED NOT NULL COMMENT '套餐规格 ID',
|
`subscribe_id` BIGINT UNSIGNED NOT NULL COMMENT '套餐规格 ID',
|
||||||
`quantity` INT NOT NULL DEFAULT 0 COMMENT '购买数量',
|
|
||||||
`promo_rule_id` BIGINT UNSIGNED NOT NULL COMMENT '促销规则 ID',
|
`promo_rule_id` BIGINT UNSIGNED NOT NULL COMMENT '促销规则 ID',
|
||||||
`promo_price` BIGINT NOT NULL DEFAULT 0 COMMENT '优惠价(分)',
|
`promo_price` BIGINT NOT NULL DEFAULT 0 COMMENT '该规格在此规则下的优惠价(分)',
|
||||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
UNIQUE KEY `idx_subscribe_qty_rule` (`subscribe_id`, `quantity`, `promo_rule_id`),
|
UNIQUE KEY `uk_subscribe_rule` (`subscribe_id`, `promo_rule_id`),
|
||||||
KEY `idx_promo_rule_id` (`promo_rule_id`)
|
KEY `idx_promo_rule_id` (`promo_rule_id`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='规格促销价表';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='规格促销价表';
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `promo_usage` (
|
CREATE TABLE IF NOT EXISTS `promo_usage` (
|
||||||
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
`user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户 ID',
|
`user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户 ID',
|
||||||
`promo_rule_id` BIGINT UNSIGNED NOT NULL COMMENT '促销规则 ID',
|
`promo_rule_id` BIGINT UNSIGNED NOT NULL COMMENT '使用的规则 ID',
|
||||||
`subscribe_id` BIGINT UNSIGNED NOT NULL COMMENT '套餐规格 ID',
|
`subscribe_id` BIGINT UNSIGNED NOT NULL COMMENT '购买的规格 ID',
|
||||||
`order_no` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '关联订单号',
|
`order_no` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '关联订单号',
|
||||||
`promo_price` BIGINT NOT NULL DEFAULT 0 COMMENT '使用时的促销单价(分)',
|
`promo_price` BIGINT NOT NULL DEFAULT 0 COMMENT '使用时的促销单价(分)',
|
||||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
@@ -40,3 +38,15 @@ CREATE TABLE IF NOT EXISTS `promo_usage` (
|
|||||||
KEY `idx_user_rule` (`user_id`, `promo_rule_id`),
|
KEY `idx_user_rule` (`user_id`, `promo_rule_id`),
|
||||||
KEY `idx_order_no` (`order_no`)
|
KEY `idx_order_no` (`order_no`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='促销使用记录表';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='促销使用记录表';
|
||||||
|
|
||||||
|
SET @promo_rule_id_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'order' AND COLUMN_NAME = 'promo_rule_id');
|
||||||
|
SET @promo_rule_id_sql = IF(@promo_rule_id_exists = 0, 'ALTER TABLE `order` ADD COLUMN `promo_rule_id` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT ''促销规则ID, 0=未使用促销'' AFTER `discount`', 'SELECT 1');
|
||||||
|
PREPARE stmt FROM @promo_rule_id_sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
|
SET @promo_discount_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'order' AND COLUMN_NAME = 'promo_discount');
|
||||||
|
SET @promo_discount_sql = IF(@promo_discount_exists = 0, 'ALTER TABLE `order` ADD COLUMN `promo_discount` BIGINT NOT NULL DEFAULT 0 COMMENT ''促销优惠金额(分)'' AFTER `promo_rule_id`', 'SELECT 1');
|
||||||
|
PREPARE stmt FROM @promo_discount_sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/perfect-panel/server/internal/logic/admin/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/result"
|
|
||||||
)
|
|
||||||
|
|
||||||
func CreateRuleHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
var req types.CreatePromoRuleRequest
|
|
||||||
_ = c.ShouldBind(&req)
|
|
||||||
if err := svcCtx.Validate(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l := promo.NewCreateRuleLogic(c.Request.Context(), svcCtx)
|
|
||||||
resp, err := l.CreateRule(&req)
|
|
||||||
result.HttpResult(c, resp, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/perfect-panel/server/internal/logic/admin/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/result"
|
|
||||||
)
|
|
||||||
|
|
||||||
func DeletePriceHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
var req types.DeletePromoPriceRequest
|
|
||||||
if err := c.ShouldBindUri(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := svcCtx.Validate(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l := promo.NewDeletePriceLogic(c.Request.Context(), svcCtx)
|
|
||||||
err := l.DeletePrice(&req)
|
|
||||||
result.HttpResult(c, nil, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/perfect-panel/server/internal/logic/admin/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/result"
|
|
||||||
)
|
|
||||||
|
|
||||||
func DeleteRuleHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
var req types.DeletePromoRuleRequest
|
|
||||||
if err := c.ShouldBindUri(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := svcCtx.Validate(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l := promo.NewDeleteRuleLogic(c.Request.Context(), svcCtx)
|
|
||||||
err := l.DeleteRule(&req)
|
|
||||||
result.HttpResult(c, nil, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/perfect-panel/server/internal/logic/admin/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/result"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetPriceListHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
var req types.GetPromoPriceListRequest
|
|
||||||
_ = c.ShouldBind(&req)
|
|
||||||
if err := svcCtx.Validate(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l := promo.NewGetPriceListLogic(c.Request.Context(), svcCtx)
|
|
||||||
resp, err := l.GetPriceList(&req)
|
|
||||||
result.HttpResult(c, resp, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/perfect-panel/server/internal/logic/admin/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/result"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetRuleDetailHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
var req types.GetPromoRuleDetailRequest
|
|
||||||
if err := c.ShouldBindUri(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := svcCtx.Validate(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l := promo.NewGetRuleDetailLogic(c.Request.Context(), svcCtx)
|
|
||||||
resp, err := l.GetRuleDetail(&req)
|
|
||||||
result.HttpResult(c, resp, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/perfect-panel/server/internal/logic/admin/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/result"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetRuleListHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
var req types.GetPromoRuleListRequest
|
|
||||||
_ = c.ShouldBind(&req)
|
|
||||||
if err := svcCtx.Validate(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l := promo.NewGetRuleListLogic(c.Request.Context(), svcCtx)
|
|
||||||
resp, err := l.GetRuleList(&req)
|
|
||||||
result.HttpResult(c, resp, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/perfect-panel/server/internal/logic/admin/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/result"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetUsageListHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
var req types.GetPromoUsageListRequest
|
|
||||||
_ = c.ShouldBind(&req)
|
|
||||||
if err := svcCtx.Validate(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l := promo.NewGetUsageListLogic(c.Request.Context(), svcCtx)
|
|
||||||
resp, err := l.GetUsageList(&req)
|
|
||||||
result.HttpResult(c, resp, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/perfect-panel/server/internal/logic/admin/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/result"
|
|
||||||
)
|
|
||||||
|
|
||||||
func SetPriceHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
var req types.SetPromoPriceRequest
|
|
||||||
_ = c.ShouldBind(&req)
|
|
||||||
if err := svcCtx.Validate(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l := promo.NewSetPriceLogic(c.Request.Context(), svcCtx)
|
|
||||||
err := l.SetPrice(&req)
|
|
||||||
result.HttpResult(c, nil, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/perfect-panel/server/internal/logic/admin/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/result"
|
|
||||||
)
|
|
||||||
|
|
||||||
func UpdateRuleHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
var req types.UpdatePromoRuleRequest
|
|
||||||
if err := c.ShouldBindUri(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_ = c.ShouldBind(&req)
|
|
||||||
if err := svcCtx.Validate(&req); err != nil {
|
|
||||||
result.ParamErrorResult(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l := promo.NewUpdateRuleLogic(c.Request.Context(), svcCtx)
|
|
||||||
resp, err := l.UpdateRule(&req)
|
|
||||||
result.HttpResult(c, resp, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -17,7 +17,6 @@ import (
|
|||||||
adminMarketing "github.com/perfect-panel/server/internal/handler/admin/marketing"
|
adminMarketing "github.com/perfect-panel/server/internal/handler/admin/marketing"
|
||||||
adminOrder "github.com/perfect-panel/server/internal/handler/admin/order"
|
adminOrder "github.com/perfect-panel/server/internal/handler/admin/order"
|
||||||
adminPayment "github.com/perfect-panel/server/internal/handler/admin/payment"
|
adminPayment "github.com/perfect-panel/server/internal/handler/admin/payment"
|
||||||
adminPromo "github.com/perfect-panel/server/internal/handler/admin/promo"
|
|
||||||
adminRedemption "github.com/perfect-panel/server/internal/handler/admin/redemption"
|
adminRedemption "github.com/perfect-panel/server/internal/handler/admin/redemption"
|
||||||
adminServer "github.com/perfect-panel/server/internal/handler/admin/server"
|
adminServer "github.com/perfect-panel/server/internal/handler/admin/server"
|
||||||
adminSubscribe "github.com/perfect-panel/server/internal/handler/admin/subscribe"
|
adminSubscribe "github.com/perfect-panel/server/internal/handler/admin/subscribe"
|
||||||
@@ -375,38 +374,6 @@ func RegisterHandlers(router *gin.Engine, serverCtx *svc.ServiceContext) {
|
|||||||
adminPaymentGroupRouter.GET("/platform", adminPayment.GetPaymentPlatformHandler(serverCtx))
|
adminPaymentGroupRouter.GET("/platform", adminPayment.GetPaymentPlatformHandler(serverCtx))
|
||||||
}
|
}
|
||||||
|
|
||||||
adminPromoGroupRouter := router.Group("/v1/admin/promo")
|
|
||||||
adminPromoGroupRouter.Use(middleware.AuthMiddleware(serverCtx))
|
|
||||||
|
|
||||||
{
|
|
||||||
// Create promo rule
|
|
||||||
adminPromoGroupRouter.POST("/rule", adminPromo.CreateRuleHandler(serverCtx))
|
|
||||||
|
|
||||||
// Get promo rule list
|
|
||||||
adminPromoGroupRouter.GET("/rule/list", adminPromo.GetRuleListHandler(serverCtx))
|
|
||||||
|
|
||||||
// Get promo rule detail
|
|
||||||
adminPromoGroupRouter.GET("/rule/:id", adminPromo.GetRuleDetailHandler(serverCtx))
|
|
||||||
|
|
||||||
// Update promo rule
|
|
||||||
adminPromoGroupRouter.PUT("/rule/:id", adminPromo.UpdateRuleHandler(serverCtx))
|
|
||||||
|
|
||||||
// Delete promo rule
|
|
||||||
adminPromoGroupRouter.DELETE("/rule/:id", adminPromo.DeleteRuleHandler(serverCtx))
|
|
||||||
|
|
||||||
// Set promo prices
|
|
||||||
adminPromoGroupRouter.POST("/price", adminPromo.SetPriceHandler(serverCtx))
|
|
||||||
|
|
||||||
// Get promo price list
|
|
||||||
adminPromoGroupRouter.GET("/price/list", adminPromo.GetPriceListHandler(serverCtx))
|
|
||||||
|
|
||||||
// Delete promo price
|
|
||||||
adminPromoGroupRouter.DELETE("/price/:id", adminPromo.DeletePriceHandler(serverCtx))
|
|
||||||
|
|
||||||
// Get promo usage list
|
|
||||||
adminPromoGroupRouter.GET("/usage/list", adminPromo.GetUsageListHandler(serverCtx))
|
|
||||||
}
|
|
||||||
|
|
||||||
adminRedemptionGroupRouter := router.Group("/v1/admin/redemption")
|
adminRedemptionGroupRouter := router.Group("/v1/admin/redemption")
|
||||||
adminRedemptionGroupRouter.Use(middleware.AuthMiddleware(serverCtx))
|
adminRedemptionGroupRouter.Use(middleware.AuthMiddleware(serverCtx))
|
||||||
|
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
promomodel "github.com/perfect-panel/server/internal/model/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CreateRuleLogic struct {
|
|
||||||
logger.Logger
|
|
||||||
ctx context.Context
|
|
||||||
svcCtx *svc.ServiceContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCreateRuleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateRuleLogic {
|
|
||||||
return &CreateRuleLogic{
|
|
||||||
Logger: logger.WithContext(ctx),
|
|
||||||
ctx: ctx,
|
|
||||||
svcCtx: svcCtx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *CreateRuleLogic) CreateRule(req *types.CreatePromoRuleRequest) (*types.PromoRule, error) {
|
|
||||||
if err := validateRuleInput(req.Type, req.Params, req.Priority, req.StartTime, req.EndTime); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
params, err := paramsToString(req.Params)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
enabled := true
|
|
||||||
if req.Enabled != nil {
|
|
||||||
enabled = *req.Enabled
|
|
||||||
}
|
|
||||||
rule := &promomodel.Rule{
|
|
||||||
Name: req.Name,
|
|
||||||
Type: req.Type,
|
|
||||||
Params: params,
|
|
||||||
Priority: req.Priority,
|
|
||||||
Enabled: &enabled,
|
|
||||||
StartTime: unixPtrToTimePtr(req.StartTime),
|
|
||||||
EndTime: unixPtrToTimePtr(req.EndTime),
|
|
||||||
}
|
|
||||||
if err := l.svcCtx.PromoModel.InsertRule(l.ctx, rule); err != nil {
|
|
||||||
l.Errorw("[CreatePromoRule] Database Error", logger.Field("error", err.Error()))
|
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "create promo rule error: %v", err.Error())
|
|
||||||
}
|
|
||||||
if err := l.svcCtx.Redis.Del(l.ctx, ruleCacheKey).Err(); err != nil {
|
|
||||||
l.Errorw("[CreatePromoRule] Delete Cache Error", logger.Field("error", err.Error()))
|
|
||||||
}
|
|
||||||
resp := convertRule(rule)
|
|
||||||
return &resp, nil
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type DeletePriceLogic struct {
|
|
||||||
logger.Logger
|
|
||||||
ctx context.Context
|
|
||||||
svcCtx *svc.ServiceContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDeletePriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeletePriceLogic {
|
|
||||||
return &DeletePriceLogic{
|
|
||||||
Logger: logger.WithContext(ctx),
|
|
||||||
ctx: ctx,
|
|
||||||
svcCtx: svcCtx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *DeletePriceLogic) DeletePrice(req *types.DeletePromoPriceRequest) error {
|
|
||||||
price, err := l.svcCtx.PromoModel.FindPrice(l.ctx, req.Id)
|
|
||||||
if err != nil {
|
|
||||||
l.Errorw("[DeletePromoPrice] Find Price Error", logger.Field("error", err.Error()))
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find promo price error: %v", err.Error())
|
|
||||||
}
|
|
||||||
if err := l.svcCtx.PromoModel.DeletePrice(l.ctx, req.Id); err != nil {
|
|
||||||
l.Errorw("[DeletePromoPrice] Database Error", logger.Field("error", err.Error()))
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "delete promo price error: %v", err.Error())
|
|
||||||
}
|
|
||||||
if err := l.svcCtx.Redis.Del(l.ctx, subscribePromoCacheKey(price.SubscribeId)).Err(); err != nil {
|
|
||||||
l.Errorw("[DeletePromoPrice] Delete Cache Error", logger.Field("error", err.Error()))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type DeleteRuleLogic struct {
|
|
||||||
logger.Logger
|
|
||||||
ctx context.Context
|
|
||||||
svcCtx *svc.ServiceContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDeleteRuleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteRuleLogic {
|
|
||||||
return &DeleteRuleLogic{
|
|
||||||
Logger: logger.WithContext(ctx),
|
|
||||||
ctx: ctx,
|
|
||||||
svcCtx: svcCtx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *DeleteRuleLogic) DeleteRule(req *types.DeletePromoRuleRequest) error {
|
|
||||||
if err := l.svcCtx.PromoModel.DeleteRule(l.ctx, req.Id); err != nil {
|
|
||||||
l.Errorw("[DeletePromoRule] Database Error", logger.Field("error", err.Error()))
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "delete promo rule error: %v", err.Error())
|
|
||||||
}
|
|
||||||
if err := l.svcCtx.Redis.Del(l.ctx, ruleCacheKey).Err(); err != nil {
|
|
||||||
l.Errorw("[DeletePromoRule] Delete Cache Error", logger.Field("error", err.Error()))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GetPriceListLogic struct {
|
|
||||||
logger.Logger
|
|
||||||
ctx context.Context
|
|
||||||
svcCtx *svc.ServiceContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGetPriceListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPriceListLogic {
|
|
||||||
return &GetPriceListLogic{
|
|
||||||
Logger: logger.WithContext(ctx),
|
|
||||||
ctx: ctx,
|
|
||||||
svcCtx: svcCtx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *GetPriceListLogic) GetPriceList(req *types.GetPromoPriceListRequest) (*types.GetPromoPriceListResponse, error) {
|
|
||||||
total, list, err := l.svcCtx.PromoModel.QueryPriceList(l.ctx, req.PromoRuleId, int(req.Page), int(req.Size))
|
|
||||||
if err != nil {
|
|
||||||
l.Errorw("[GetPromoPriceList] Database Error", logger.Field("error", err.Error()))
|
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get promo price list error: %v", err.Error())
|
|
||||||
}
|
|
||||||
resp := &types.GetPromoPriceListResponse{
|
|
||||||
Total: total,
|
|
||||||
List: make([]types.PromoPrice, 0, len(list)),
|
|
||||||
}
|
|
||||||
for _, item := range list {
|
|
||||||
resp.List = append(resp.List, convertPrice(item))
|
|
||||||
}
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GetRuleDetailLogic struct {
|
|
||||||
logger.Logger
|
|
||||||
ctx context.Context
|
|
||||||
svcCtx *svc.ServiceContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGetRuleDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRuleDetailLogic {
|
|
||||||
return &GetRuleDetailLogic{
|
|
||||||
Logger: logger.WithContext(ctx),
|
|
||||||
ctx: ctx,
|
|
||||||
svcCtx: svcCtx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *GetRuleDetailLogic) GetRuleDetail(req *types.GetPromoRuleDetailRequest) (*types.PromoRule, error) {
|
|
||||||
rule, err := l.svcCtx.PromoModel.FindRule(l.ctx, req.Id)
|
|
||||||
if err != nil {
|
|
||||||
l.Errorw("[GetPromoRuleDetail] Database Error", logger.Field("error", err.Error()))
|
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get promo rule detail error: %v", err.Error())
|
|
||||||
}
|
|
||||||
resp := convertRule(rule)
|
|
||||||
return &resp, nil
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GetRuleListLogic struct {
|
|
||||||
logger.Logger
|
|
||||||
ctx context.Context
|
|
||||||
svcCtx *svc.ServiceContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGetRuleListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRuleListLogic {
|
|
||||||
return &GetRuleListLogic{
|
|
||||||
Logger: logger.WithContext(ctx),
|
|
||||||
ctx: ctx,
|
|
||||||
svcCtx: svcCtx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *GetRuleListLogic) GetRuleList(req *types.GetPromoRuleListRequest) (*types.GetPromoRuleListResponse, error) {
|
|
||||||
total, list, err := l.svcCtx.PromoModel.QueryRuleList(l.ctx, int(req.Page), int(req.Size), req.Type, req.Enabled, req.Search)
|
|
||||||
if err != nil {
|
|
||||||
l.Errorw("[GetPromoRuleList] Database Error", logger.Field("error", err.Error()))
|
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get promo rule list error: %v", err.Error())
|
|
||||||
}
|
|
||||||
resp := &types.GetPromoRuleListResponse{
|
|
||||||
Total: total,
|
|
||||||
List: make([]types.PromoRule, 0, len(list)),
|
|
||||||
}
|
|
||||||
for _, item := range list {
|
|
||||||
resp.List = append(resp.List, convertRule(item))
|
|
||||||
}
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
promomodel "github.com/perfect-panel/server/internal/model/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GetUsageListLogic struct {
|
|
||||||
logger.Logger
|
|
||||||
ctx context.Context
|
|
||||||
svcCtx *svc.ServiceContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGetUsageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUsageListLogic {
|
|
||||||
return &GetUsageListLogic{
|
|
||||||
Logger: logger.WithContext(ctx),
|
|
||||||
ctx: ctx,
|
|
||||||
svcCtx: svcCtx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *GetUsageListLogic) GetUsageList(req *types.GetPromoUsageListRequest) (*types.GetPromoUsageListResponse, error) {
|
|
||||||
total, list, err := l.svcCtx.PromoModel.QueryUsageList(l.ctx, promomodel.UsageFilter{
|
|
||||||
Page: int(req.Page),
|
|
||||||
Size: int(req.Size),
|
|
||||||
RuleId: req.RuleId,
|
|
||||||
UserId: req.UserId,
|
|
||||||
SubscribeId: req.SubscribeId,
|
|
||||||
OrderNo: req.OrderNo,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
l.Errorw("[GetPromoUsageList] Database Error", logger.Field("error", err.Error()))
|
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get promo usage list error: %v", err.Error())
|
|
||||||
}
|
|
||||||
resp := &types.GetPromoUsageListResponse{
|
|
||||||
Total: total,
|
|
||||||
List: make([]types.PromoUsage, 0, len(list)),
|
|
||||||
}
|
|
||||||
for _, item := range list {
|
|
||||||
resp.List = append(resp.List, convertUsage(item))
|
|
||||||
}
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
promomodel "github.com/perfect-panel/server/internal/model/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SetPriceLogic struct {
|
|
||||||
logger.Logger
|
|
||||||
ctx context.Context
|
|
||||||
svcCtx *svc.ServiceContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSetPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetPriceLogic {
|
|
||||||
return &SetPriceLogic{
|
|
||||||
Logger: logger.WithContext(ctx),
|
|
||||||
ctx: ctx,
|
|
||||||
svcCtx: svcCtx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *SetPriceLogic) SetPrice(req *types.SetPromoPriceRequest) error {
|
|
||||||
if _, err := l.svcCtx.PromoModel.FindRule(l.ctx, req.PromoRuleId); err != nil {
|
|
||||||
l.Errorw("[SetPromoPrice] Find Rule Error", logger.Field("error", err.Error()))
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find promo rule error: %v", err.Error())
|
|
||||||
}
|
|
||||||
items := make([]*promomodel.SubscribePromo, 0, len(req.Items))
|
|
||||||
cacheKeys := make([]string, 0, len(req.Items))
|
|
||||||
for _, item := range req.Items {
|
|
||||||
sub, err := l.svcCtx.SubscribeModel.FindOne(l.ctx, item.SubscribeId)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "subscribe plan not found")
|
|
||||||
}
|
|
||||||
l.Errorw("[SetPromoPrice] Find Subscribe Error", logger.Field("error", err.Error()))
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find subscribe error: %v", err.Error())
|
|
||||||
}
|
|
||||||
originPrice := sub.UnitPrice * item.Quantity
|
|
||||||
if item.PromoPrice >= originPrice {
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "promo_price must be less than unit_price * quantity")
|
|
||||||
}
|
|
||||||
items = append(items, &promomodel.SubscribePromo{
|
|
||||||
SubscribeId: item.SubscribeId,
|
|
||||||
PromoRuleId: req.PromoRuleId,
|
|
||||||
Quantity: item.Quantity,
|
|
||||||
PromoPrice: item.PromoPrice,
|
|
||||||
})
|
|
||||||
cacheKeys = append(cacheKeys, subscribePromoCacheKey(item.SubscribeId))
|
|
||||||
}
|
|
||||||
if err := l.svcCtx.PromoModel.UpsertPrices(l.ctx, req.PromoRuleId, items); err != nil {
|
|
||||||
l.Errorw("[SetPromoPrice] Database Error", logger.Field("error", err.Error()))
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "set promo price error: %v", err.Error())
|
|
||||||
}
|
|
||||||
if len(cacheKeys) > 0 {
|
|
||||||
if err := l.svcCtx.Redis.Del(l.ctx, cacheKeys...).Err(); err != nil {
|
|
||||||
l.Errorw("[SetPromoPrice] Delete Cache Error", logger.Field("error", err.Error()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,166 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
promomodel "github.com/perfect-panel/server/internal/model/promo"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
ruleCacheKey = "promo:rules:enabled"
|
|
||||||
subscribeCachePref = "promo:subscribe:"
|
|
||||||
)
|
|
||||||
|
|
||||||
func validateRuleInput(ruleType string, params map[string]interface{}, priority int64, startTime, endTime *int64) error {
|
|
||||||
if priority < 0 {
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "priority must be greater than or equal to 0")
|
|
||||||
}
|
|
||||||
if startTime != nil && endTime != nil && *startTime >= *endTime {
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "start_time must be less than end_time")
|
|
||||||
}
|
|
||||||
switch ruleType {
|
|
||||||
case "new_user":
|
|
||||||
windowHours, ok := numberParam(params, "window_hours")
|
|
||||||
if !ok || windowHours <= 0 {
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "params.window_hours must be greater than 0")
|
|
||||||
}
|
|
||||||
case "inactive_user":
|
|
||||||
inactiveMonths, ok := numberParam(params, "inactive_months")
|
|
||||||
if !ok || inactiveMonths <= 0 {
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "params.inactive_months must be greater than 0")
|
|
||||||
}
|
|
||||||
case "campaign":
|
|
||||||
if params == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "unsupported promo rule type")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func numberParam(params map[string]interface{}, key string) (int64, bool) {
|
|
||||||
if params == nil {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
value, ok := params[key]
|
|
||||||
if !ok {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
switch v := value.(type) {
|
|
||||||
case float64:
|
|
||||||
if math.Trunc(v) != v {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
return int64(v), true
|
|
||||||
case int64:
|
|
||||||
return v, true
|
|
||||||
case int:
|
|
||||||
return int64(v), true
|
|
||||||
case json.Number:
|
|
||||||
n, err := v.Int64()
|
|
||||||
return n, err == nil
|
|
||||||
default:
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func paramsToString(params map[string]interface{}) (string, error) {
|
|
||||||
if params == nil {
|
|
||||||
params = map[string]interface{}{}
|
|
||||||
}
|
|
||||||
b, err := json.Marshal(params)
|
|
||||||
if err != nil {
|
|
||||||
return "", errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "invalid params")
|
|
||||||
}
|
|
||||||
return string(b), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseParams(data string) map[string]interface{} {
|
|
||||||
if data == "" {
|
|
||||||
return map[string]interface{}{}
|
|
||||||
}
|
|
||||||
var params map[string]interface{}
|
|
||||||
if err := json.Unmarshal([]byte(data), ¶ms); err != nil {
|
|
||||||
return map[string]interface{}{}
|
|
||||||
}
|
|
||||||
return params
|
|
||||||
}
|
|
||||||
|
|
||||||
func unixPtrToTimePtr(ts *int64) *time.Time {
|
|
||||||
if ts == nil || *ts == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
t := time.Unix(*ts, 0)
|
|
||||||
return &t
|
|
||||||
}
|
|
||||||
|
|
||||||
func timePtrToUnixPtr(t *time.Time) *int64 {
|
|
||||||
if t == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ts := t.Unix()
|
|
||||||
return &ts
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertRule(item *promomodel.Rule) types.PromoRule {
|
|
||||||
if item == nil {
|
|
||||||
return types.PromoRule{}
|
|
||||||
}
|
|
||||||
enabled := false
|
|
||||||
if item.Enabled != nil {
|
|
||||||
enabled = *item.Enabled
|
|
||||||
}
|
|
||||||
return types.PromoRule{
|
|
||||||
Id: item.Id,
|
|
||||||
Name: item.Name,
|
|
||||||
Type: item.Type,
|
|
||||||
Params: parseParams(item.Params),
|
|
||||||
Priority: item.Priority,
|
|
||||||
Enabled: enabled,
|
|
||||||
StartTime: timePtrToUnixPtr(item.StartTime),
|
|
||||||
EndTime: timePtrToUnixPtr(item.EndTime),
|
|
||||||
CreatedAt: item.CreatedAt.Unix(),
|
|
||||||
UpdatedAt: item.UpdatedAt.Unix(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertPrice(item *promomodel.SubscribePromo) types.PromoPrice {
|
|
||||||
if item == nil {
|
|
||||||
return types.PromoPrice{}
|
|
||||||
}
|
|
||||||
return types.PromoPrice{
|
|
||||||
Id: item.Id,
|
|
||||||
SubscribeId: item.SubscribeId,
|
|
||||||
PromoRuleId: item.PromoRuleId,
|
|
||||||
Quantity: item.Quantity,
|
|
||||||
PromoPrice: item.PromoPrice,
|
|
||||||
CreatedAt: item.CreatedAt.Unix(),
|
|
||||||
UpdatedAt: item.UpdatedAt.Unix(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertUsage(item *promomodel.Usage) types.PromoUsage {
|
|
||||||
if item == nil {
|
|
||||||
return types.PromoUsage{}
|
|
||||||
}
|
|
||||||
return types.PromoUsage{
|
|
||||||
Id: item.Id,
|
|
||||||
UserId: item.UserId,
|
|
||||||
PromoRuleId: item.PromoRuleId,
|
|
||||||
SubscribeId: item.SubscribeId,
|
|
||||||
OrderNo: item.OrderNo,
|
|
||||||
PromoPrice: item.PromoPrice,
|
|
||||||
CreatedAt: item.CreatedAt.Unix(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func subscribePromoCacheKey(subscribeId int64) string {
|
|
||||||
return fmt.Sprintf("%s%d", subscribeCachePref, subscribeId)
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
|
||||||
"github.com/perfect-panel/server/internal/types"
|
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type UpdateRuleLogic struct {
|
|
||||||
logger.Logger
|
|
||||||
ctx context.Context
|
|
||||||
svcCtx *svc.ServiceContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewUpdateRuleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateRuleLogic {
|
|
||||||
return &UpdateRuleLogic{
|
|
||||||
Logger: logger.WithContext(ctx),
|
|
||||||
ctx: ctx,
|
|
||||||
svcCtx: svcCtx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *UpdateRuleLogic) UpdateRule(req *types.UpdatePromoRuleRequest) (*types.PromoRule, error) {
|
|
||||||
if err := validateRuleInput(req.Type, req.Params, req.Priority, req.StartTime, req.EndTime); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rule, err := l.svcCtx.PromoModel.FindRule(l.ctx, req.Id)
|
|
||||||
if err != nil {
|
|
||||||
l.Errorw("[UpdatePromoRule] Find Rule Error", logger.Field("error", err.Error()))
|
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find promo rule error: %v", err.Error())
|
|
||||||
}
|
|
||||||
params, err := paramsToString(req.Params)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
enabled := true
|
|
||||||
if rule.Enabled != nil {
|
|
||||||
enabled = *rule.Enabled
|
|
||||||
}
|
|
||||||
if req.Enabled != nil {
|
|
||||||
enabled = *req.Enabled
|
|
||||||
}
|
|
||||||
rule.Name = req.Name
|
|
||||||
rule.Type = req.Type
|
|
||||||
rule.Params = params
|
|
||||||
rule.Priority = req.Priority
|
|
||||||
rule.Enabled = &enabled
|
|
||||||
rule.StartTime = unixPtrToTimePtr(req.StartTime)
|
|
||||||
rule.EndTime = unixPtrToTimePtr(req.EndTime)
|
|
||||||
if err := l.svcCtx.PromoModel.UpdateRule(l.ctx, rule); err != nil {
|
|
||||||
l.Errorw("[UpdatePromoRule] Database Error", logger.Field("error", err.Error()))
|
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update promo rule error: %v", err.Error())
|
|
||||||
}
|
|
||||||
if err := l.svcCtx.Redis.Del(l.ctx, ruleCacheKey).Err(); err != nil {
|
|
||||||
l.Errorw("[UpdatePromoRule] Delete Cache Error", logger.Field("error", err.Error()))
|
|
||||||
}
|
|
||||||
resp := convertRule(rule)
|
|
||||||
return &resp, nil
|
|
||||||
}
|
|
||||||
@@ -22,6 +22,8 @@ type Details struct {
|
|||||||
Price int64 `gorm:"type:int;not null;default:0;comment:Original price"`
|
Price int64 `gorm:"type:int;not null;default:0;comment:Original price"`
|
||||||
Amount int64 `gorm:"type:int;not null;default:0;comment:Order Amount"`
|
Amount int64 `gorm:"type:int;not null;default:0;comment:Order Amount"`
|
||||||
Discount int64 `gorm:"type:int;not null;default:0;comment:Order Discount"`
|
Discount int64 `gorm:"type:int;not null;default:0;comment:Order Discount"`
|
||||||
|
PromoRuleID int64 `gorm:"type:bigint unsigned;not null;default:0;comment:Promo Rule ID, 0 means no promo"`
|
||||||
|
PromoDiscount int64 `gorm:"type:bigint;not null;default:0;comment:Promo Discount Amount"`
|
||||||
Coupon string `gorm:"type:varchar(255);default:null;comment:Coupon"`
|
Coupon string `gorm:"type:varchar(255);default:null;comment:Coupon"`
|
||||||
CouponDiscount int64 `gorm:"type:int;not null;default:0;comment:Coupon Discount"`
|
CouponDiscount int64 `gorm:"type:int;not null;default:0;comment:Coupon Discount"`
|
||||||
PaymentId int64 `gorm:"type:bigint;not null;default:0;comment:Payment Id"`
|
PaymentId int64 `gorm:"type:bigint;not null;default:0;comment:Payment Id"`
|
||||||
|
|||||||
@@ -3,32 +3,34 @@ package order
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Order struct {
|
type Order struct {
|
||||||
Id int64 `gorm:"primaryKey"`
|
Id int64 `gorm:"primaryKey"`
|
||||||
ParentId int64 `gorm:"type:bigint;default:null;comment:Parent Order Id"`
|
ParentId int64 `gorm:"type:bigint;default:null;comment:Parent Order Id"`
|
||||||
UserId int64 `gorm:"type:bigint;not null;default:0;comment:User Id"`
|
UserId int64 `gorm:"type:bigint;not null;default:0;comment:User Id"`
|
||||||
SubscriptionUserId int64 `gorm:"type:bigint;not null;default:0;comment:Target user ID for subscription (0=same as UserId)"`
|
SubscriptionUserId int64 `gorm:"type:bigint;not null;default:0;comment:Target user ID for subscription (0=same as UserId)"`
|
||||||
OrderNo string `gorm:"type:varchar(255);not null;default:'';unique;comment:Order No"`
|
OrderNo string `gorm:"type:varchar(255);not null;default:'';unique;comment:Order No"`
|
||||||
Type uint8 `gorm:"type:tinyint(1);not null;default:1;comment:Order Type: 1: Subscribe, 2: Renewal, 3: ResetTraffic, 4: Recharge"`
|
Type uint8 `gorm:"type:tinyint(1);not null;default:1;comment:Order Type: 1: Subscribe, 2: Renewal, 3: ResetTraffic, 4: Recharge"`
|
||||||
Quantity int64 `gorm:"type:bigint;not null;default:1;comment:Quantity"`
|
Quantity int64 `gorm:"type:bigint;not null;default:1;comment:Quantity"`
|
||||||
Price int64 `gorm:"type:int;not null;default:0;comment:Original price"`
|
Price int64 `gorm:"type:int;not null;default:0;comment:Original price"`
|
||||||
Amount int64 `gorm:"type:int;not null;default:0;comment:Order Amount"`
|
Amount int64 `gorm:"type:int;not null;default:0;comment:Order Amount"`
|
||||||
GiftAmount int64 `gorm:"type:int;not null;default:0;comment:User Gift Amount"`
|
GiftAmount int64 `gorm:"type:int;not null;default:0;comment:User Gift Amount"`
|
||||||
Discount int64 `gorm:"type:int;not null;default:0;comment:Discount Amount"`
|
Discount int64 `gorm:"type:int;not null;default:0;comment:Discount Amount"`
|
||||||
Coupon string `gorm:"type:varchar(255);default:null;comment:Coupon"`
|
PromoRuleID int64 `gorm:"type:bigint unsigned;not null;default:0;comment:Promo Rule ID, 0 means no promo"`
|
||||||
CouponDiscount int64 `gorm:"type:int;not null;default:0;comment:Coupon Discount Amount"`
|
PromoDiscount int64 `gorm:"type:bigint;not null;default:0;comment:Promo Discount Amount"`
|
||||||
Commission int64 `gorm:"type:int;not null;default:0;comment:Order Commission"`
|
Coupon string `gorm:"type:varchar(255);default:null;comment:Coupon"`
|
||||||
PaymentId int64 `gorm:"type:bigint;not null;default:0;comment:Payment Method Id"`
|
CouponDiscount int64 `gorm:"type:int;not null;default:0;comment:Coupon Discount Amount"`
|
||||||
Method string `gorm:"type:varchar(255);not null;default:'';comment:Payment Method"`
|
Commission int64 `gorm:"type:int;not null;default:0;comment:Order Commission"`
|
||||||
FeeAmount int64 `gorm:"type:int;not null;default:0;comment:Fee Amount"`
|
PaymentId int64 `gorm:"type:bigint;not null;default:0;comment:Payment Method Id"`
|
||||||
TradeNo string `gorm:"type:varchar(255);default:null;comment:Trade No"`
|
Method string `gorm:"type:varchar(255);not null;default:'';comment:Payment Method"`
|
||||||
Status uint8 `gorm:"type:tinyint(1);not null;default:1;comment:Order Status: 1: Pending, 2: Paid, 3:Close, 4: Failed, 5:Finished;"`
|
FeeAmount int64 `gorm:"type:int;not null;default:0;comment:Fee Amount"`
|
||||||
SubscribeId int64 `gorm:"type:bigint;not null;default:0;comment:Subscribe Id"`
|
TradeNo string `gorm:"type:varchar(255);default:null;comment:Trade No"`
|
||||||
SubscribeToken string `gorm:"type:varchar(255);default:null;comment:Renewal Subscribe Token"`
|
Status uint8 `gorm:"type:tinyint(1);not null;default:1;comment:Order Status: 1: Pending, 2: Paid, 3:Close, 4: Failed, 5:Finished;"`
|
||||||
AppAccountToken string `gorm:"type:varchar(36);default:null;comment:Apple IAP App Account Token (UUID)"`
|
SubscribeId int64 `gorm:"type:bigint;not null;default:0;comment:Subscribe Id"`
|
||||||
ActivationContext string `gorm:"type:text;default:null;comment:Activation context JSON (guest/redemption info for DB fallback)"`
|
SubscribeToken string `gorm:"type:varchar(255);default:null;comment:Renewal Subscribe Token"`
|
||||||
IsNew bool `gorm:"type:tinyint(1);not null;default:0;comment:Is New Order"`
|
AppAccountToken string `gorm:"type:varchar(36);default:null;comment:Apple IAP App Account Token (UUID)"`
|
||||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
ActivationContext string `gorm:"type:text;default:null;comment:Activation context JSON (guest/redemption info for DB fallback)"`
|
||||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
IsNew bool `gorm:"type:tinyint(1);not null;default:0;comment:Is New Order"`
|
||||||
|
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||||
|
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type OrdersTotal struct {
|
type OrdersTotal struct {
|
||||||
|
|||||||
@@ -1,239 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/perfect-panel/server/pkg/cache"
|
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
cachePromoRuleIdPrefix = "cache:promo_rule:id:"
|
|
||||||
cacheSubscribePromoIdPrefix = "cache:subscribe_promo:id:"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Model interface {
|
|
||||||
InsertRule(ctx context.Context, data *Rule) error
|
|
||||||
FindRule(ctx context.Context, id int64) (*Rule, error)
|
|
||||||
UpdateRule(ctx context.Context, data *Rule) error
|
|
||||||
DeleteRule(ctx context.Context, id int64) error
|
|
||||||
QueryRuleList(ctx context.Context, page, size int, ruleType string, enabled *bool, search string) (int64, []*Rule, error)
|
|
||||||
UpsertPrices(ctx context.Context, ruleId int64, items []*SubscribePromo) error
|
|
||||||
FindPrice(ctx context.Context, id int64) (*SubscribePromo, error)
|
|
||||||
DeletePrice(ctx context.Context, id int64) error
|
|
||||||
QueryPriceList(ctx context.Context, ruleId int64, page, size int) (int64, []*SubscribePromo, error)
|
|
||||||
QueryUsageList(ctx context.Context, params UsageFilter) (int64, []*Usage, error)
|
|
||||||
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type UsageFilter struct {
|
|
||||||
Page int
|
|
||||||
Size int
|
|
||||||
RuleId int64
|
|
||||||
UserId int64
|
|
||||||
SubscribeId int64
|
|
||||||
OrderNo string
|
|
||||||
}
|
|
||||||
|
|
||||||
type customPromoModel struct {
|
|
||||||
cache.CachedConn
|
|
||||||
table string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewModel(db *gorm.DB, c *redis.Client) Model {
|
|
||||||
return &customPromoModel{
|
|
||||||
CachedConn: cache.NewConn(db, c),
|
|
||||||
table: "`promo_rule`",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) ruleCacheKey(id int64) string {
|
|
||||||
return fmt.Sprintf("%s%d", cachePromoRuleIdPrefix, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) priceCacheKey(id int64) string {
|
|
||||||
return fmt.Sprintf("%s%d", cacheSubscribePromoIdPrefix, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) InsertRule(ctx context.Context, data *Rule) error {
|
|
||||||
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
|
||||||
return conn.Create(data).Error
|
|
||||||
}, m.ruleCacheKey(data.Id))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) FindRule(ctx context.Context, id int64) (*Rule, error) {
|
|
||||||
var resp Rule
|
|
||||||
err := m.QueryCtx(ctx, &resp, m.ruleCacheKey(id), func(conn *gorm.DB, v interface{}) error {
|
|
||||||
return conn.Model(&Rule{}).Where("id = ?", id).First(v).Error
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) UpdateRule(ctx context.Context, data *Rule) error {
|
|
||||||
old, err := m.FindRule(ctx, data.Id)
|
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
keys := []string{m.ruleCacheKey(data.Id)}
|
|
||||||
if old != nil {
|
|
||||||
keys = append(keys, m.ruleCacheKey(old.Id))
|
|
||||||
}
|
|
||||||
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
|
||||||
return conn.Save(data).Error
|
|
||||||
}, keys...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) DeleteRule(ctx context.Context, id int64) error {
|
|
||||||
data, err := m.FindRule(ctx, id)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
|
||||||
return conn.Delete(&Rule{}, id).Error
|
|
||||||
}, m.ruleCacheKey(data.Id))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) QueryRuleList(ctx context.Context, page, size int, ruleType string, enabled *bool, search string) (int64, []*Rule, error) {
|
|
||||||
if page <= 0 {
|
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
if size <= 0 {
|
|
||||||
size = 10
|
|
||||||
}
|
|
||||||
var total int64
|
|
||||||
var list []*Rule
|
|
||||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
|
||||||
db := conn.Model(&Rule{})
|
|
||||||
if ruleType != "" {
|
|
||||||
db = db.Where("type = ?", ruleType)
|
|
||||||
}
|
|
||||||
if enabled != nil {
|
|
||||||
db = db.Where("enabled = ?", *enabled)
|
|
||||||
}
|
|
||||||
if search != "" {
|
|
||||||
db = db.Where("name LIKE ?", "%"+search+"%")
|
|
||||||
}
|
|
||||||
if err := db.Count(&total).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return db.Order("priority DESC").Order("id DESC").Limit(size).Offset((page - 1) * size).Find(v).Error
|
|
||||||
})
|
|
||||||
return total, list, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) UpsertPrices(ctx context.Context, ruleId int64, items []*SubscribePromo) error {
|
|
||||||
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
|
||||||
for _, item := range items {
|
|
||||||
if item == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
item.PromoRuleId = ruleId
|
|
||||||
var existing SubscribePromo
|
|
||||||
err := conn.Model(&SubscribePromo{}).
|
|
||||||
Where("subscribe_id = ? AND quantity = ? AND promo_rule_id = ?", item.SubscribeId, item.Quantity, ruleId).
|
|
||||||
First(&existing).Error
|
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
if err := conn.Create(item).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
existing.Quantity = item.Quantity
|
|
||||||
existing.PromoPrice = item.PromoPrice
|
|
||||||
if err := conn.Save(&existing).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) FindPrice(ctx context.Context, id int64) (*SubscribePromo, error) {
|
|
||||||
var resp SubscribePromo
|
|
||||||
err := m.QueryCtx(ctx, &resp, m.priceCacheKey(id), func(conn *gorm.DB, v interface{}) error {
|
|
||||||
return conn.Model(&SubscribePromo{}).Where("id = ?", id).First(v).Error
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) DeletePrice(ctx context.Context, id int64) error {
|
|
||||||
data, err := m.FindPrice(ctx, id)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
|
||||||
return conn.Delete(&SubscribePromo{}, id).Error
|
|
||||||
}, m.priceCacheKey(data.Id))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) QueryPriceList(ctx context.Context, ruleId int64, page, size int) (int64, []*SubscribePromo, error) {
|
|
||||||
if page <= 0 {
|
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
if size <= 0 {
|
|
||||||
size = 10
|
|
||||||
}
|
|
||||||
var total int64
|
|
||||||
var list []*SubscribePromo
|
|
||||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
|
||||||
db := conn.Model(&SubscribePromo{}).Where("promo_rule_id = ?", ruleId)
|
|
||||||
if err := db.Count(&total).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return db.Order("id DESC").Limit(size).Offset((page - 1) * size).Find(v).Error
|
|
||||||
})
|
|
||||||
return total, list, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) QueryUsageList(ctx context.Context, params UsageFilter) (int64, []*Usage, error) {
|
|
||||||
if params.Page <= 0 {
|
|
||||||
params.Page = 1
|
|
||||||
}
|
|
||||||
if params.Size <= 0 {
|
|
||||||
params.Size = 10
|
|
||||||
}
|
|
||||||
var total int64
|
|
||||||
var list []*Usage
|
|
||||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
|
||||||
db := conn.Model(&Usage{})
|
|
||||||
if params.RuleId > 0 {
|
|
||||||
db = db.Where("promo_rule_id = ?", params.RuleId)
|
|
||||||
}
|
|
||||||
if params.UserId > 0 {
|
|
||||||
db = db.Where("user_id = ?", params.UserId)
|
|
||||||
}
|
|
||||||
if params.SubscribeId > 0 {
|
|
||||||
db = db.Where("subscribe_id = ?", params.SubscribeId)
|
|
||||||
}
|
|
||||||
if params.OrderNo != "" {
|
|
||||||
db = db.Where("order_no = ?", params.OrderNo)
|
|
||||||
}
|
|
||||||
if err := db.Count(&total).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return db.Order("id DESC").Limit(params.Size).Offset((params.Page - 1) * params.Size).Find(v).Error
|
|
||||||
})
|
|
||||||
return total, list, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *customPromoModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
|
||||||
return m.TransactCtx(ctx, fn)
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Rule struct {
|
|
||||||
Id int64 `gorm:"primaryKey"`
|
|
||||||
Name string `gorm:"type:varchar(100);not null;default:'';comment:Promo Rule Name"`
|
|
||||||
Type string `gorm:"type:varchar(32);not null;default:'';comment:Promo Rule Type"`
|
|
||||||
Params string `gorm:"type:json;not null;comment:Rule Params"`
|
|
||||||
Priority int64 `gorm:"type:int;not null;default:0;comment:Priority"`
|
|
||||||
Enabled *bool `gorm:"type:tinyint(1);not null;default:1;comment:Enabled"`
|
|
||||||
StartTime *time.Time `gorm:"default:null;comment:Start Time"`
|
|
||||||
EndTime *time.Time `gorm:"default:null;comment:End Time"`
|
|
||||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
|
||||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index;comment:Delete Time"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*Rule) TableName() string {
|
|
||||||
return "promo_rule"
|
|
||||||
}
|
|
||||||
|
|
||||||
type SubscribePromo struct {
|
|
||||||
Id int64 `gorm:"primaryKey"`
|
|
||||||
SubscribeId int64 `gorm:"not null;index:idx_subscribe_qty_rule,unique;comment:Subscribe ID"`
|
|
||||||
Quantity int64 `gorm:"not null;default:0;index:idx_subscribe_qty_rule,unique;comment:Quantity"`
|
|
||||||
PromoRuleId int64 `gorm:"not null;index:idx_subscribe_qty_rule,unique;index:idx_promo_rule_id;comment:Promo Rule ID"`
|
|
||||||
PromoPrice int64 `gorm:"type:bigint;not null;default:0;comment:Promo Price"`
|
|
||||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
|
||||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*SubscribePromo) TableName() string {
|
|
||||||
return "subscribe_promo"
|
|
||||||
}
|
|
||||||
|
|
||||||
type Usage struct {
|
|
||||||
Id int64 `gorm:"primaryKey"`
|
|
||||||
UserId int64 `gorm:"not null;index:idx_user_rule;comment:User ID"`
|
|
||||||
PromoRuleId int64 `gorm:"not null;index:idx_user_rule;comment:Promo Rule ID"`
|
|
||||||
SubscribeId int64 `gorm:"not null;comment:Subscribe ID"`
|
|
||||||
OrderNo string `gorm:"type:varchar(255);not null;default:'';index:idx_order_no;comment:Order No"`
|
|
||||||
PromoPrice int64 `gorm:"type:bigint;not null;default:0;comment:Promo Price"`
|
|
||||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*Usage) TableName() string {
|
|
||||||
return "promo_usage"
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package promo_rule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/perfect-panel/server/pkg/cache"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ Model = (*customPromoRuleModel)(nil)
|
||||||
|
|
||||||
|
var cachePromoRuleIdPrefix = "cache:promo_rule:id:"
|
||||||
|
|
||||||
|
type (
|
||||||
|
Model interface {
|
||||||
|
promoRuleModel
|
||||||
|
customPromoRuleLogicModel
|
||||||
|
}
|
||||||
|
|
||||||
|
promoRuleModel interface {
|
||||||
|
Insert(ctx context.Context, data *PromoRule, tx ...*gorm.DB) error
|
||||||
|
FindOne(ctx context.Context, id int64) (*PromoRule, error)
|
||||||
|
Update(ctx context.Context, data *PromoRule, tx ...*gorm.DB) error
|
||||||
|
Delete(ctx context.Context, id int64, tx ...*gorm.DB) error
|
||||||
|
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||||
|
}
|
||||||
|
|
||||||
|
customPromoRuleModel struct {
|
||||||
|
*defaultPromoRuleModel
|
||||||
|
}
|
||||||
|
defaultPromoRuleModel struct {
|
||||||
|
cache.CachedConn
|
||||||
|
table string
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func newPromoRuleModel(db *gorm.DB, c *redis.Client) *defaultPromoRuleModel {
|
||||||
|
return &defaultPromoRuleModel{
|
||||||
|
CachedConn: cache.NewConn(db, c),
|
||||||
|
table: "`promo_rule`",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoRuleModel) getCacheKeys(data *PromoRule) []string {
|
||||||
|
if data == nil {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
return []string{fmt.Sprintf("%s%v", cachePromoRuleIdPrefix, data.Id)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoRuleModel) Insert(ctx context.Context, data *PromoRule, tx ...*gorm.DB) error {
|
||||||
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||||
|
if len(tx) > 0 {
|
||||||
|
conn = tx[0]
|
||||||
|
}
|
||||||
|
return conn.Create(data).Error
|
||||||
|
}, m.getCacheKeys(data)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoRuleModel) FindOne(ctx context.Context, id int64) (*PromoRule, error) {
|
||||||
|
key := fmt.Sprintf("%s%v", cachePromoRuleIdPrefix, id)
|
||||||
|
var resp PromoRule
|
||||||
|
err := m.QueryCtx(ctx, &resp, key, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Model(&PromoRule{}).Where("`id` = ?", id).First(v).Error
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoRuleModel) Update(ctx context.Context, data *PromoRule, tx ...*gorm.DB) error {
|
||||||
|
old, err := m.FindOne(ctx, data.Id)
|
||||||
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||||
|
if len(tx) > 0 {
|
||||||
|
conn = tx[0]
|
||||||
|
}
|
||||||
|
return conn.Save(data).Error
|
||||||
|
}, m.getCacheKeys(old)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoRuleModel) Delete(ctx context.Context, id int64, tx ...*gorm.DB) error {
|
||||||
|
data, err := m.FindOne(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||||
|
if len(tx) > 0 {
|
||||||
|
conn = tx[0]
|
||||||
|
}
|
||||||
|
return conn.Delete(&PromoRule{}, id).Error
|
||||||
|
}, m.getCacheKeys(data)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoRuleModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
||||||
|
return m.TransactCtx(ctx, fn)
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package promo_rule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type customPromoRuleLogicModel interface {
|
||||||
|
QueryEnabledByPriority(ctx context.Context) ([]*PromoRule, error)
|
||||||
|
QueryAvailableByPriority(ctx context.Context, now time.Time) ([]*PromoRule, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewModel(conn *gorm.DB, c *redis.Client) Model {
|
||||||
|
return &customPromoRuleModel{
|
||||||
|
defaultPromoRuleModel: newPromoRuleModel(conn, c),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *customPromoRuleModel) QueryEnabledByPriority(ctx context.Context) ([]*PromoRule, error) {
|
||||||
|
var list []*PromoRule
|
||||||
|
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Model(&PromoRule{}).
|
||||||
|
Where("enabled = ?", true).
|
||||||
|
Order("priority DESC, id DESC").
|
||||||
|
Find(v).Error
|
||||||
|
})
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *customPromoRuleModel) QueryAvailableByPriority(ctx context.Context, now time.Time) ([]*PromoRule, error) {
|
||||||
|
var list []*PromoRule
|
||||||
|
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Model(&PromoRule{}).
|
||||||
|
Where("enabled = ?", true).
|
||||||
|
Where("(start_time IS NULL OR start_time <= ?)", now).
|
||||||
|
Where("(end_time IS NULL OR end_time >= ?)", now).
|
||||||
|
Order("priority DESC, id DESC").
|
||||||
|
Find(v).Error
|
||||||
|
})
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package promo_rule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JSONRawMessage json.RawMessage
|
||||||
|
|
||||||
|
func (j *JSONRawMessage) Scan(value interface{}) error {
|
||||||
|
if value == nil {
|
||||||
|
*j = JSONRawMessage([]byte("{}"))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch v := value.(type) {
|
||||||
|
case []byte:
|
||||||
|
*j = JSONRawMessage(v)
|
||||||
|
case string:
|
||||||
|
*j = JSONRawMessage([]byte(v))
|
||||||
|
default:
|
||||||
|
*j = JSONRawMessage([]byte("{}"))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j JSONRawMessage) Value() (driver.Value, error) {
|
||||||
|
if len(j) == 0 {
|
||||||
|
return "{}", nil
|
||||||
|
}
|
||||||
|
data, err := json.RawMessage(j).MarshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return string(data), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j JSONRawMessage) MarshalJSON() ([]byte, error) {
|
||||||
|
if len(j) == 0 {
|
||||||
|
return []byte("{}"), nil
|
||||||
|
}
|
||||||
|
return json.RawMessage(j).MarshalJSON()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JSONRawMessage) UnmarshalJSON(data []byte) error {
|
||||||
|
if len(data) == 0 {
|
||||||
|
*j = JSONRawMessage([]byte("{}"))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
*j = JSONRawMessage(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type PromoRule struct {
|
||||||
|
Id int64 `gorm:"type:bigint unsigned;primaryKey"`
|
||||||
|
Name string `gorm:"type:varchar(100);not null;default:'';comment:Rule Name"`
|
||||||
|
Type string `gorm:"type:varchar(32);not null;default:'';comment:Rule Type: new_user, inactive_user, campaign"`
|
||||||
|
Params JSONRawMessage `gorm:"type:json;not null;comment:Rule Params"`
|
||||||
|
Priority int64 `gorm:"type:int;not null;default:0;index:idx_enabled_priority,priority:2,sort:desc;comment:Priority"`
|
||||||
|
Enabled bool `gorm:"type:tinyint(1);not null;default:1;index:idx_enabled_priority,priority:1;comment:Enabled"`
|
||||||
|
StartTime *time.Time `gorm:"type:datetime;default:null;comment:Start Time"`
|
||||||
|
EndTime *time.Time `gorm:"type:datetime;default:null;comment:End Time"`
|
||||||
|
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||||
|
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||||
|
DeletedAt gorm.DeletedAt `gorm:"index;comment:Delete Time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (PromoRule) TableName() string {
|
||||||
|
return "promo_rule"
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package promo_usage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/perfect-panel/server/pkg/cache"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ Model = (*customPromoUsageModel)(nil)
|
||||||
|
|
||||||
|
var cachePromoUsageIdPrefix = "cache:promo_usage:id:"
|
||||||
|
|
||||||
|
type (
|
||||||
|
Model interface {
|
||||||
|
promoUsageModel
|
||||||
|
customPromoUsageLogicModel
|
||||||
|
}
|
||||||
|
|
||||||
|
promoUsageModel interface {
|
||||||
|
Insert(ctx context.Context, data *PromoUsage, tx ...*gorm.DB) error
|
||||||
|
FindOne(ctx context.Context, id int64) (*PromoUsage, error)
|
||||||
|
Update(ctx context.Context, data *PromoUsage, tx ...*gorm.DB) error
|
||||||
|
Delete(ctx context.Context, id int64, tx ...*gorm.DB) error
|
||||||
|
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||||
|
}
|
||||||
|
|
||||||
|
customPromoUsageModel struct {
|
||||||
|
*defaultPromoUsageModel
|
||||||
|
}
|
||||||
|
defaultPromoUsageModel struct {
|
||||||
|
cache.CachedConn
|
||||||
|
table string
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func newPromoUsageModel(db *gorm.DB, c *redis.Client) *defaultPromoUsageModel {
|
||||||
|
return &defaultPromoUsageModel{
|
||||||
|
CachedConn: cache.NewConn(db, c),
|
||||||
|
table: "`promo_usage`",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoUsageModel) getCacheKeys(data *PromoUsage) []string {
|
||||||
|
if data == nil {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
return []string{fmt.Sprintf("%s%v", cachePromoUsageIdPrefix, data.Id)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoUsageModel) Insert(ctx context.Context, data *PromoUsage, tx ...*gorm.DB) error {
|
||||||
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||||
|
if len(tx) > 0 {
|
||||||
|
conn = tx[0]
|
||||||
|
}
|
||||||
|
return conn.Create(data).Error
|
||||||
|
}, m.getCacheKeys(data)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoUsageModel) FindOne(ctx context.Context, id int64) (*PromoUsage, error) {
|
||||||
|
key := fmt.Sprintf("%s%v", cachePromoUsageIdPrefix, id)
|
||||||
|
var resp PromoUsage
|
||||||
|
err := m.QueryCtx(ctx, &resp, key, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Model(&PromoUsage{}).Where("`id` = ?", id).First(v).Error
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoUsageModel) Update(ctx context.Context, data *PromoUsage, tx ...*gorm.DB) error {
|
||||||
|
old, err := m.FindOne(ctx, data.Id)
|
||||||
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||||
|
if len(tx) > 0 {
|
||||||
|
conn = tx[0]
|
||||||
|
}
|
||||||
|
return conn.Save(data).Error
|
||||||
|
}, m.getCacheKeys(old)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoUsageModel) Delete(ctx context.Context, id int64, tx ...*gorm.DB) error {
|
||||||
|
data, err := m.FindOne(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||||
|
if len(tx) > 0 {
|
||||||
|
conn = tx[0]
|
||||||
|
}
|
||||||
|
return conn.Delete(&PromoUsage{}, id).Error
|
||||||
|
}, m.getCacheKeys(data)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultPromoUsageModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
||||||
|
return m.TransactCtx(ctx, fn)
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package promo_usage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type customPromoUsageLogicModel interface {
|
||||||
|
FindByOrderNo(ctx context.Context, orderNo string) ([]*PromoUsage, error)
|
||||||
|
FindByUserAndRule(ctx context.Context, userId, promoRuleId int64) ([]*PromoUsage, error)
|
||||||
|
QueryListByPage(ctx context.Context, page, size int, userId, promoRuleId, subscribeId int64) (int64, []*PromoUsage, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewModel(conn *gorm.DB, c *redis.Client) Model {
|
||||||
|
return &customPromoUsageModel{
|
||||||
|
defaultPromoUsageModel: newPromoUsageModel(conn, c),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *customPromoUsageModel) FindByOrderNo(ctx context.Context, orderNo string) ([]*PromoUsage, error) {
|
||||||
|
var list []*PromoUsage
|
||||||
|
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Model(&PromoUsage{}).
|
||||||
|
Where("order_no = ?", orderNo).
|
||||||
|
Order("id DESC").
|
||||||
|
Find(v).Error
|
||||||
|
})
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *customPromoUsageModel) FindByUserAndRule(ctx context.Context, userId, promoRuleId int64) ([]*PromoUsage, error) {
|
||||||
|
var list []*PromoUsage
|
||||||
|
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Model(&PromoUsage{}).
|
||||||
|
Where("user_id = ? AND promo_rule_id = ?", userId, promoRuleId).
|
||||||
|
Order("id DESC").
|
||||||
|
Find(v).Error
|
||||||
|
})
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *customPromoUsageModel) QueryListByPage(ctx context.Context, page, size int, userId, promoRuleId, subscribeId int64) (int64, []*PromoUsage, error) {
|
||||||
|
var list []*PromoUsage
|
||||||
|
var total int64
|
||||||
|
if page <= 0 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
if size <= 0 {
|
||||||
|
size = 20
|
||||||
|
}
|
||||||
|
|
||||||
|
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
db := conn.Model(&PromoUsage{})
|
||||||
|
if userId > 0 {
|
||||||
|
db = db.Where("user_id = ?", userId)
|
||||||
|
}
|
||||||
|
if promoRuleId > 0 {
|
||||||
|
db = db.Where("promo_rule_id = ?", promoRuleId)
|
||||||
|
}
|
||||||
|
if subscribeId > 0 {
|
||||||
|
db = db.Where("subscribe_id = ?", subscribeId)
|
||||||
|
}
|
||||||
|
if err := db.Count(&total).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return db.Order("id DESC").Limit(size).Offset((page - 1) * size).Find(v).Error
|
||||||
|
})
|
||||||
|
return total, list, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package promo_usage
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type PromoUsage struct {
|
||||||
|
Id int64 `gorm:"type:bigint unsigned;primaryKey"`
|
||||||
|
UserId int64 `gorm:"type:bigint unsigned;not null;index:idx_user_rule,priority:1;comment:User Id"`
|
||||||
|
PromoRuleId int64 `gorm:"type:bigint unsigned;not null;index:idx_user_rule,priority:2;comment:Promo Rule Id"`
|
||||||
|
SubscribeId int64 `gorm:"type:bigint unsigned;not null;comment:Subscribe Id"`
|
||||||
|
OrderNo string `gorm:"type:varchar(255);not null;default:'';index:idx_order_no;comment:Order No"`
|
||||||
|
PromoPrice int64 `gorm:"type:bigint;not null;default:0;comment:Promo Price"`
|
||||||
|
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (PromoUsage) TableName() string {
|
||||||
|
return "promo_usage"
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package subscribe_promo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/perfect-panel/server/pkg/cache"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ Model = (*customSubscribePromoModel)(nil)
|
||||||
|
|
||||||
|
var cacheSubscribePromoIdPrefix = "cache:subscribe_promo:id:"
|
||||||
|
|
||||||
|
type (
|
||||||
|
Model interface {
|
||||||
|
subscribePromoModel
|
||||||
|
customSubscribePromoLogicModel
|
||||||
|
}
|
||||||
|
|
||||||
|
subscribePromoModel interface {
|
||||||
|
Insert(ctx context.Context, data *SubscribePromo, tx ...*gorm.DB) error
|
||||||
|
FindOne(ctx context.Context, id int64) (*SubscribePromo, error)
|
||||||
|
Update(ctx context.Context, data *SubscribePromo, tx ...*gorm.DB) error
|
||||||
|
Delete(ctx context.Context, id int64, tx ...*gorm.DB) error
|
||||||
|
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||||
|
}
|
||||||
|
|
||||||
|
customSubscribePromoModel struct {
|
||||||
|
*defaultSubscribePromoModel
|
||||||
|
}
|
||||||
|
defaultSubscribePromoModel struct {
|
||||||
|
cache.CachedConn
|
||||||
|
table string
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func newSubscribePromoModel(db *gorm.DB, c *redis.Client) *defaultSubscribePromoModel {
|
||||||
|
return &defaultSubscribePromoModel{
|
||||||
|
CachedConn: cache.NewConn(db, c),
|
||||||
|
table: "`subscribe_promo`",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultSubscribePromoModel) getCacheKeys(data *SubscribePromo) []string {
|
||||||
|
if data == nil {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
return []string{fmt.Sprintf("%s%v", cacheSubscribePromoIdPrefix, data.Id)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultSubscribePromoModel) Insert(ctx context.Context, data *SubscribePromo, tx ...*gorm.DB) error {
|
||||||
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||||
|
if len(tx) > 0 {
|
||||||
|
conn = tx[0]
|
||||||
|
}
|
||||||
|
return conn.Create(data).Error
|
||||||
|
}, m.getCacheKeys(data)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultSubscribePromoModel) FindOne(ctx context.Context, id int64) (*SubscribePromo, error) {
|
||||||
|
key := fmt.Sprintf("%s%v", cacheSubscribePromoIdPrefix, id)
|
||||||
|
var resp SubscribePromo
|
||||||
|
err := m.QueryCtx(ctx, &resp, key, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Model(&SubscribePromo{}).Where("`id` = ?", id).First(v).Error
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultSubscribePromoModel) Update(ctx context.Context, data *SubscribePromo, tx ...*gorm.DB) error {
|
||||||
|
old, err := m.FindOne(ctx, data.Id)
|
||||||
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||||
|
if len(tx) > 0 {
|
||||||
|
conn = tx[0]
|
||||||
|
}
|
||||||
|
return conn.Save(data).Error
|
||||||
|
}, m.getCacheKeys(old)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultSubscribePromoModel) Delete(ctx context.Context, id int64, tx ...*gorm.DB) error {
|
||||||
|
data, err := m.FindOne(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||||
|
if len(tx) > 0 {
|
||||||
|
conn = tx[0]
|
||||||
|
}
|
||||||
|
return conn.Delete(&SubscribePromo{}, id).Error
|
||||||
|
}, m.getCacheKeys(data)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultSubscribePromoModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
||||||
|
return m.TransactCtx(ctx, fn)
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package subscribe_promo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/perfect-panel/server/internal/model/promo_rule"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RulePromo struct {
|
||||||
|
Id int64
|
||||||
|
SubscribeId int64
|
||||||
|
PromoRuleId int64
|
||||||
|
PromoPrice int64
|
||||||
|
RuleName string
|
||||||
|
RuleType string
|
||||||
|
Params promo_rule.JSONRawMessage
|
||||||
|
Priority int64
|
||||||
|
StartTime *time.Time
|
||||||
|
EndTime *time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type customSubscribePromoLogicModel interface {
|
||||||
|
FindOneBySubscribeAndRule(ctx context.Context, subscribeId, promoRuleId int64) (*SubscribePromo, error)
|
||||||
|
FindBySubscribeId(ctx context.Context, subscribeId int64) ([]*SubscribePromo, error)
|
||||||
|
FindByPromoRuleId(ctx context.Context, promoRuleId int64) ([]*SubscribePromo, error)
|
||||||
|
QueryEnabledBySubscribeId(ctx context.Context, subscribeId int64) ([]*RulePromo, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewModel(conn *gorm.DB, c *redis.Client) Model {
|
||||||
|
return &customSubscribePromoModel{
|
||||||
|
defaultSubscribePromoModel: newSubscribePromoModel(conn, c),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *customSubscribePromoModel) FindOneBySubscribeAndRule(ctx context.Context, subscribeId, promoRuleId int64) (*SubscribePromo, error) {
|
||||||
|
var resp SubscribePromo
|
||||||
|
err := m.QueryNoCacheCtx(ctx, &resp, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Model(&SubscribePromo{}).
|
||||||
|
Where("subscribe_id = ? AND promo_rule_id = ?", subscribeId, promoRuleId).
|
||||||
|
First(v).Error
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *customSubscribePromoModel) FindBySubscribeId(ctx context.Context, subscribeId int64) ([]*SubscribePromo, error) {
|
||||||
|
var list []*SubscribePromo
|
||||||
|
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Model(&SubscribePromo{}).
|
||||||
|
Where("subscribe_id = ?", subscribeId).
|
||||||
|
Order("id DESC").
|
||||||
|
Find(v).Error
|
||||||
|
})
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *customSubscribePromoModel) FindByPromoRuleId(ctx context.Context, promoRuleId int64) ([]*SubscribePromo, error) {
|
||||||
|
var list []*SubscribePromo
|
||||||
|
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Model(&SubscribePromo{}).
|
||||||
|
Where("promo_rule_id = ?", promoRuleId).
|
||||||
|
Order("id DESC").
|
||||||
|
Find(v).Error
|
||||||
|
})
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *customSubscribePromoModel) QueryEnabledBySubscribeId(ctx context.Context, subscribeId int64) ([]*RulePromo, error) {
|
||||||
|
var list []*RulePromo
|
||||||
|
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||||
|
return conn.Table("subscribe_promo AS sp").
|
||||||
|
Select("sp.id, sp.subscribe_id, sp.promo_rule_id, sp.promo_price, pr.name AS rule_name, pr.type AS rule_type, pr.params, pr.priority, pr.start_time, pr.end_time").
|
||||||
|
Joins("JOIN promo_rule AS pr ON pr.id = sp.promo_rule_id AND pr.deleted_at IS NULL").
|
||||||
|
Where("sp.subscribe_id = ? AND pr.enabled = ?", subscribeId, true).
|
||||||
|
Order("pr.priority DESC, pr.id DESC").
|
||||||
|
Scan(v).Error
|
||||||
|
})
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package subscribe_promo
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type SubscribePromo struct {
|
||||||
|
Id int64 `gorm:"type:bigint unsigned;primaryKey"`
|
||||||
|
SubscribeId int64 `gorm:"type:bigint unsigned;not null;index:uk_subscribe_rule,unique,priority:1;comment:Subscribe Id"`
|
||||||
|
PromoRuleId int64 `gorm:"type:bigint unsigned;not null;index:uk_subscribe_rule,unique,priority:2;index:idx_promo_rule_id;comment:Promo Rule Id"`
|
||||||
|
PromoPrice int64 `gorm:"type:bigint;not null;default:0;comment:Promo Price"`
|
||||||
|
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||||
|
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (SubscribePromo) TableName() string {
|
||||||
|
return "subscribe_promo"
|
||||||
|
}
|
||||||
@@ -21,8 +21,10 @@ import (
|
|||||||
logmessage "github.com/perfect-panel/server/internal/model/logmessage"
|
logmessage "github.com/perfect-panel/server/internal/model/logmessage"
|
||||||
"github.com/perfect-panel/server/internal/model/order"
|
"github.com/perfect-panel/server/internal/model/order"
|
||||||
"github.com/perfect-panel/server/internal/model/payment"
|
"github.com/perfect-panel/server/internal/model/payment"
|
||||||
"github.com/perfect-panel/server/internal/model/promo"
|
"github.com/perfect-panel/server/internal/model/promo_rule"
|
||||||
|
"github.com/perfect-panel/server/internal/model/promo_usage"
|
||||||
"github.com/perfect-panel/server/internal/model/subscribe"
|
"github.com/perfect-panel/server/internal/model/subscribe"
|
||||||
|
"github.com/perfect-panel/server/internal/model/subscribe_promo"
|
||||||
"github.com/perfect-panel/server/internal/model/system"
|
"github.com/perfect-panel/server/internal/model/system"
|
||||||
"github.com/perfect-panel/server/internal/model/ticket"
|
"github.com/perfect-panel/server/internal/model/ticket"
|
||||||
"github.com/perfect-panel/server/internal/model/traffic"
|
"github.com/perfect-panel/server/internal/model/traffic"
|
||||||
@@ -64,9 +66,11 @@ type ServiceContext struct {
|
|||||||
RedemptionCodeModel redemption.RedemptionCodeModel
|
RedemptionCodeModel redemption.RedemptionCodeModel
|
||||||
RedemptionRecordModel redemption.RedemptionRecordModel
|
RedemptionRecordModel redemption.RedemptionRecordModel
|
||||||
PaymentModel payment.Model
|
PaymentModel payment.Model
|
||||||
PromoModel promo.Model
|
|
||||||
DocumentModel document.Model
|
DocumentModel document.Model
|
||||||
SubscribeModel subscribe.Model
|
SubscribeModel subscribe.Model
|
||||||
|
PromoRuleModel promo_rule.Model
|
||||||
|
SubscribePromoModel subscribe_promo.Model
|
||||||
|
PromoUsageModel promo_usage.Model
|
||||||
TrafficLogModel traffic.Model
|
TrafficLogModel traffic.Model
|
||||||
AnnouncementModel announcement.Model
|
AnnouncementModel announcement.Model
|
||||||
IAPAppleTransactionModel iapapple.Model
|
IAPAppleTransactionModel iapapple.Model
|
||||||
@@ -140,9 +144,11 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
|||||||
RedemptionCodeModel: redemption.NewRedemptionCodeModel(db, rds),
|
RedemptionCodeModel: redemption.NewRedemptionCodeModel(db, rds),
|
||||||
RedemptionRecordModel: redemption.NewRedemptionRecordModel(db, rds),
|
RedemptionRecordModel: redemption.NewRedemptionRecordModel(db, rds),
|
||||||
PaymentModel: payment.NewModel(db, rds),
|
PaymentModel: payment.NewModel(db, rds),
|
||||||
PromoModel: promo.NewModel(db, rds),
|
|
||||||
DocumentModel: document.NewModel(db, rds),
|
DocumentModel: document.NewModel(db, rds),
|
||||||
SubscribeModel: subscribe.NewModel(db, rds),
|
SubscribeModel: subscribe.NewModel(db, rds),
|
||||||
|
PromoRuleModel: promo_rule.NewModel(db, rds),
|
||||||
|
SubscribePromoModel: subscribe_promo.NewModel(db, rds),
|
||||||
|
PromoUsageModel: promo_usage.NewModel(db, rds),
|
||||||
TrafficLogModel: traffic.NewModel(db),
|
TrafficLogModel: traffic.NewModel(db),
|
||||||
AnnouncementModel: announcement.NewModel(db, rds),
|
AnnouncementModel: announcement.NewModel(db, rds),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -316,45 +316,6 @@ type ContactRequest struct {
|
|||||||
Notes string `json:"notes" validate:"max=2000"`
|
Notes string `json:"notes" validate:"max=2000"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PromoPrice struct {
|
|
||||||
Id int64 `json:"id"`
|
|
||||||
SubscribeId int64 `json:"subscribe_id"`
|
|
||||||
PromoRuleId int64 `json:"promo_rule_id"`
|
|
||||||
Quantity int64 `json:"quantity"`
|
|
||||||
PromoPrice int64 `json:"promo_price"`
|
|
||||||
CreatedAt int64 `json:"created_at"`
|
|
||||||
UpdatedAt int64 `json:"updated_at"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PromoPriceItem struct {
|
|
||||||
SubscribeId int64 `json:"subscribe_id" validate:"required,gt=0"`
|
|
||||||
Quantity int64 `json:"quantity" validate:"required,gt=0,lte=1000"`
|
|
||||||
PromoPrice int64 `json:"promo_price" validate:"required,gt=0"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PromoRule struct {
|
|
||||||
Id int64 `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Params map[string]interface{} `json:"params"`
|
|
||||||
Priority int64 `json:"priority"`
|
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
StartTime *int64 `json:"start_time"`
|
|
||||||
EndTime *int64 `json:"end_time"`
|
|
||||||
CreatedAt int64 `json:"created_at"`
|
|
||||||
UpdatedAt int64 `json:"updated_at"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PromoUsage struct {
|
|
||||||
Id int64 `json:"id"`
|
|
||||||
UserId int64 `json:"user_id"`
|
|
||||||
PromoRuleId int64 `json:"promo_rule_id"`
|
|
||||||
SubscribeId int64 `json:"subscribe_id"`
|
|
||||||
OrderNo string `json:"order_no"`
|
|
||||||
PromoPrice int64 `json:"promo_price"`
|
|
||||||
CreatedAt int64 `json:"created_at"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Coupon struct {
|
type Coupon struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -414,16 +375,6 @@ type CreateCouponRequest struct {
|
|||||||
Enable *bool `json:"enable,omitempty"`
|
Enable *bool `json:"enable,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreatePromoRuleRequest struct {
|
|
||||||
Name string `json:"name" validate:"required,max=100"`
|
|
||||||
Type string `json:"type" validate:"required,oneof=new_user inactive_user campaign"`
|
|
||||||
Params map[string]interface{} `json:"params"`
|
|
||||||
Priority int64 `json:"priority" validate:"gte=0"`
|
|
||||||
Enabled *bool `json:"enabled"`
|
|
||||||
StartTime *int64 `json:"start_time"`
|
|
||||||
EndTime *int64 `json:"end_time"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type CreateDocumentRequest struct {
|
type CreateDocumentRequest struct {
|
||||||
Title string `json:"title" validate:"required"`
|
Title string `json:"title" validate:"required"`
|
||||||
Content string `json:"content" validate:"required"`
|
Content string `json:"content" validate:"required"`
|
||||||
@@ -1156,48 +1107,6 @@ type GetCouponListResponse struct {
|
|||||||
List []Coupon `json:"list"`
|
List []Coupon `json:"list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetPromoPriceListRequest struct {
|
|
||||||
PromoRuleId int64 `form:"promo_rule_id" validate:"required,gt=0"`
|
|
||||||
Page int64 `form:"page" validate:"required"`
|
|
||||||
Size int64 `form:"size" validate:"required"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetPromoPriceListResponse struct {
|
|
||||||
Total int64 `json:"total"`
|
|
||||||
List []PromoPrice `json:"list"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetPromoRuleDetailRequest struct {
|
|
||||||
Id int64 `uri:"id" validate:"required,gt=0"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetPromoRuleListRequest struct {
|
|
||||||
Page int64 `form:"page" validate:"required"`
|
|
||||||
Size int64 `form:"size" validate:"required"`
|
|
||||||
Type string `form:"type" validate:"omitempty,oneof=new_user inactive_user campaign"`
|
|
||||||
Enabled *bool `form:"enabled"`
|
|
||||||
Search string `form:"search,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetPromoRuleListResponse struct {
|
|
||||||
Total int64 `json:"total"`
|
|
||||||
List []PromoRule `json:"list"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetPromoUsageListRequest struct {
|
|
||||||
Page int64 `form:"page" validate:"required"`
|
|
||||||
Size int64 `form:"size" validate:"required"`
|
|
||||||
RuleId int64 `form:"rule_id,omitempty"`
|
|
||||||
UserId int64 `form:"user_id,omitempty"`
|
|
||||||
SubscribeId int64 `form:"subscribe_id,omitempty"`
|
|
||||||
OrderNo string `form:"order_no,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetPromoUsageListResponse struct {
|
|
||||||
Total int64 `json:"total"`
|
|
||||||
List []PromoUsage `json:"list"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetDetailRequest struct {
|
type GetDetailRequest struct {
|
||||||
Id int64 `form:"id" validate:"required"`
|
Id int64 `form:"id" validate:"required"`
|
||||||
}
|
}
|
||||||
@@ -3219,30 +3128,6 @@ type UpdateCouponRequest struct {
|
|||||||
Enable *bool `json:"enable,omitempty"`
|
Enable *bool `json:"enable,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SetPromoPriceRequest struct {
|
|
||||||
PromoRuleId int64 `json:"promo_rule_id" validate:"required,gt=0"`
|
|
||||||
Items []PromoPriceItem `json:"items" validate:"required,dive"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type DeletePromoPriceRequest struct {
|
|
||||||
Id int64 `uri:"id" validate:"required,gt=0"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type DeletePromoRuleRequest struct {
|
|
||||||
Id int64 `uri:"id" validate:"required,gt=0"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UpdatePromoRuleRequest struct {
|
|
||||||
Id int64 `uri:"id" validate:"required,gt=0"`
|
|
||||||
Name string `json:"name" validate:"required,max=100"`
|
|
||||||
Type string `json:"type" validate:"required,oneof=new_user inactive_user campaign"`
|
|
||||||
Params map[string]interface{} `json:"params"`
|
|
||||||
Priority int64 `json:"priority" validate:"gte=0"`
|
|
||||||
Enabled *bool `json:"enabled"`
|
|
||||||
StartTime *int64 `json:"start_time"`
|
|
||||||
EndTime *int64 `json:"end_time"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UpdateDocumentRequest struct {
|
type UpdateDocumentRequest struct {
|
||||||
Id int64 `json:"id" validate:"required"`
|
Id int64 `json:"id" validate:"required"`
|
||||||
Title string `json:"title" validate:"required"`
|
Title string `json:"title" validate:"required"`
|
||||||
|
|||||||
Reference in New Issue
Block a user