first commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
Nacos NacosConf
|
||||
Mysql MysqlConf
|
||||
}
|
||||
|
||||
type NacosConf struct {
|
||||
Hosts []string
|
||||
NamespaceId string `json:",optional"`
|
||||
Group string `json:",optional"`
|
||||
RegisterIP string `json:",optional"`
|
||||
}
|
||||
|
||||
type MysqlConf struct {
|
||||
Host string
|
||||
Port int
|
||||
User string
|
||||
Password string
|
||||
Database string
|
||||
Charset string `json:",default=utf8mb4"`
|
||||
Prefix string `json:",optional"`
|
||||
|
||||
ReadHost string `json:",optional"`
|
||||
ReadPort int `json:",optional"`
|
||||
ReadUser string `json:",optional"`
|
||||
ReadPassword string `json:",optional"`
|
||||
ReadDatabase string `json:",optional"`
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package dao
|
||||
|
||||
import "time"
|
||||
|
||||
// 产品类型
|
||||
const (
|
||||
ProductTypeNormal = 1 // 普通产品
|
||||
ProductFirstTry = 2 // 首次试用
|
||||
ProductTry = 3 // 再次试用
|
||||
ProductFirstTryName = "首次试用"
|
||||
ProductTryName = "多次试用"
|
||||
)
|
||||
|
||||
// 销售模式
|
||||
const (
|
||||
ProductSalesModelOffline = 1 // 线下
|
||||
ProductSalesModelOnline = 2 // 线上
|
||||
)
|
||||
|
||||
// 产品状态 / 审核状态(与老库约定一致)
|
||||
const (
|
||||
ProductStatusNormal uint8 = 1 // 正常
|
||||
ProductStatusDisabled uint8 = 2 // 禁用
|
||||
ProductStatusUnpublished uint8 = 3 // 未发布
|
||||
|
||||
VerifyStatusPass uint8 = 1 // 通过
|
||||
VerifyStatusRefuse uint8 = 2 // 驳回
|
||||
VerifyStatusChecking uint8 = 3 // 审核中
|
||||
VerifyStatusSecond uint8 = 4 // 二审中
|
||||
)
|
||||
|
||||
type Product struct {
|
||||
Id int `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
Name string `gorm:"column:name;type:varchar(256);not null"`
|
||||
Subhead string `gorm:"column:subhead;type:varchar(255)"`
|
||||
SalesModel uint8 `gorm:"column:sales_model;type:tinyint(1);default:1"`
|
||||
Content string `gorm:"column:content;type:text;not null"`
|
||||
ModelCode string `gorm:"column:model_code;type:varchar(255)"`
|
||||
Price float32 `gorm:"column:price;type:decimal(10,2);not null"`
|
||||
StorePrice float32 `gorm:"column:store_price;type:decimal(10,2)"`
|
||||
SalePrice float32 `gorm:"column:sale_price;type:decimal(10,2)"`
|
||||
SharePrice float32 `gorm:"column:share_price;type:decimal(10,2)"`
|
||||
Waybill string `gorm:"column:waybill;type:varchar(255)"`
|
||||
AgentPrice float32 `gorm:"column:agent_price;type:decimal(10,2)"`
|
||||
SaleReward string `gorm:"column:sale_reward;type:varchar(255)"`
|
||||
Images string `gorm:"column:images;type:varchar(512);not null"`
|
||||
Weight float32 `gorm:"column:weight;type:double"`
|
||||
Cubage string `gorm:"column:cubage;type:varchar(255)"`
|
||||
IsIndex uint8 `gorm:"column:is_index;type:tinyint(4);default:2"`
|
||||
IndexImage string `gorm:"column:index_image;type:varchar(255)"`
|
||||
Label string `gorm:"column:label;type:varchar(255)"`
|
||||
PeriodValidity int16 `gorm:"column:period_validity;type:smallint(4)"`
|
||||
PublishTime time.Time `gorm:"column:publish_time;type:datetime"`
|
||||
Sort uint16 `gorm:"column:sort;type:smallint(6) unsigned"`
|
||||
IsBuy uint8 `gorm:"column:is_buy;type:tinyint(1);default:2"`
|
||||
Type uint8 `gorm:"column:type;type:tinyint(1);default:1;not null"`
|
||||
NormsNumber uint8 `gorm:"column:norms_number;type:smallint(4);default:1;not null"`
|
||||
BoxNumber float64 `gorm:"column:box_number;type:smallint(6) unsigned;default:0"`
|
||||
Number uint `gorm:"column:number;type:int(10) unsigned;default:0;not null"`
|
||||
TryNumber uint8 `gorm:"column:try_number;type:tinyint(2);default:1"`
|
||||
Status uint8 `gorm:"column:status;type:tinyint(1);default:3;not null"`
|
||||
VerifyStatus uint8 `gorm:"column:verify_status;type:tinyint(1);default:3"`
|
||||
VerifyId int `gorm:"column:verify_id;type:int(11)"`
|
||||
VerifyName string `gorm:"column:verify_name;type:varchar(50)"`
|
||||
VerifySecondId int `gorm:"column:verify_second_id;type:int(11)"`
|
||||
VerifySecondName string `gorm:"column:verify_second_name;type:varchar(255)"`
|
||||
Reason string `gorm:"column:reason;type:varchar(255)"`
|
||||
AdminName string `gorm:"column:admin_name;type:varchar(50);not null"`
|
||||
AdminId int `gorm:"column:admin_id;type:int(11);not null"`
|
||||
}
|
||||
|
||||
type ProductCreate struct {
|
||||
Id int `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
Name string `gorm:"column:name"`
|
||||
Subhead string `gorm:"column:subhead"`
|
||||
SalesModel uint8 `gorm:"column:sales_model"`
|
||||
Content string `gorm:"column:content"`
|
||||
ModelCode string `gorm:"column:model_code"`
|
||||
Price float32 `gorm:"column:price"`
|
||||
StorePrice float32 `gorm:"column:store_price"`
|
||||
SalePrice float32 `gorm:"column:sale_price"`
|
||||
SharePrice float32 `gorm:"column:share_price"`
|
||||
Waybill string `gorm:"column:waybill"`
|
||||
AgentPrice float32 `gorm:"column:agent_price"`
|
||||
SaleReward string `gorm:"column:sale_reward"`
|
||||
Images string `gorm:"column:images"`
|
||||
Weight float32 `gorm:"column:weight"`
|
||||
Cubage string `gorm:"column:cubage"`
|
||||
IsIndex uint8 `gorm:"column:is_index"`
|
||||
IndexImage string `gorm:"column:index_image"`
|
||||
Label string `gorm:"column:label"`
|
||||
PeriodValidity int16 `gorm:"column:period_validity"`
|
||||
PublishTime time.Time `gorm:"column:publish_time"`
|
||||
Sort uint16 `gorm:"column:sort"`
|
||||
IsBuy uint8 `gorm:"column:is_buy"`
|
||||
Type uint8 `gorm:"column:type"`
|
||||
NormsNumber uint8 `gorm:"column:norms_number"`
|
||||
BoxNumber float64 `gorm:"column:box_number"`
|
||||
Number uint `gorm:"column:number"`
|
||||
TryNumber uint8 `gorm:"column:try_number"`
|
||||
AdminName string `gorm:"column:admin_name"`
|
||||
AdminId int `gorm:"column:admin_id"`
|
||||
}
|
||||
|
||||
type ProductCheckExist struct {
|
||||
Id int `gorm:"column:id"`
|
||||
Name string `gorm:"column:name"`
|
||||
ModelCode string `gorm:"column:model_code"`
|
||||
Status uint8 `gorm:"column:status"`
|
||||
}
|
||||
|
||||
type ProductVerify struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name,omitempty"`
|
||||
ModelCode string `json:"model_code"`
|
||||
Price float32 `json:"price"`
|
||||
StorePrice float32 `json:"store_price"`
|
||||
SalePrice float32 `json:"sale_price"`
|
||||
SharePrice float32 `json:"share_price"`
|
||||
AgentPrice float32 `json:"agent_price"`
|
||||
SaleReward string `json:"sale_reward"`
|
||||
SalesModel uint8 `json:"sales_model,omitempty"`
|
||||
Type uint8 `json:"type"`
|
||||
NormsNumber uint8 `json:"norms_number"`
|
||||
BoxNumber float64 `json:"box_number"`
|
||||
AdminName string `json:"admin_name"`
|
||||
AdminId int `json:"admin_id"`
|
||||
IsIndex uint8 `json:"is_index,omitempty"`
|
||||
IndexImage string `json:"index_image,omitempty"`
|
||||
}
|
||||
|
||||
type ProductEditBase struct {
|
||||
Id int `gorm:"column:id;primaryKey"`
|
||||
Name string `gorm:"column:name"`
|
||||
Subhead string `gorm:"column:subhead"`
|
||||
Content string `gorm:"column:content"`
|
||||
Images string `gorm:"column:images"`
|
||||
Weight float32 `gorm:"column:weight"`
|
||||
Cubage string `gorm:"column:cubage"`
|
||||
Waybill string `gorm:"column:waybill"`
|
||||
Label string `gorm:"column:label"`
|
||||
IsIndex uint8 `gorm:"column:is_index"`
|
||||
IndexImage string `gorm:"column:index_image"`
|
||||
PeriodValidity int16 `gorm:"column:period_validity"`
|
||||
IsBuy uint8 `gorm:"column:is_buy"`
|
||||
PublishTime time.Time `gorm:"column:publish_time"`
|
||||
AdminName string `gorm:"column:admin_name"`
|
||||
AdminId int `gorm:"column:admin_id"`
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package dao
|
||||
|
||||
import "time"
|
||||
|
||||
// 审核类型
|
||||
const (
|
||||
VerifyTypeProduct uint8 = 1 // 产品
|
||||
VerifyTypeTryProduct uint8 = 2 // 试用产品
|
||||
VerifyTypeAccount uint8 = 3 // 结算
|
||||
VerifyTypeCompensate uint8 = 4 // 补发货
|
||||
VerifyTypeRefund uint8 = 5 // 退款
|
||||
)
|
||||
|
||||
// Verify 审核表完整字段映射
|
||||
type Verify struct {
|
||||
Id int `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
VerifyId string `gorm:"column:verify_id;type:varchar(150);default:0"`
|
||||
AdminId int `gorm:"column:admin_id;type:int(11)"`
|
||||
AdminName string `gorm:"column:admin_name;type:varchar(50)"`
|
||||
AdminSecondId int `gorm:"column:admin_second_id;type:int(11)"`
|
||||
AdminSecondName string `gorm:"column:admin_second_name;type:varchar(255)"`
|
||||
Content string `gorm:"column:content;type:text;not null"`
|
||||
Type uint8 `gorm:"column:type;type:tinyint(2);not null"`
|
||||
VerifyStatus uint8 `gorm:"column:verify_status;type:tinyint(1);default:3;not null"`
|
||||
VerifyReason string `gorm:"column:verify_reason;type:varchar(256)"`
|
||||
VerifyTime time.Time `gorm:"column:verify_time;type:datetime"`
|
||||
}
|
||||
|
||||
// VerifyCreate 创建审核记录
|
||||
type VerifyCreate struct {
|
||||
Id int `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
VerifyId string `gorm:"column:verify_id"`
|
||||
Content string `gorm:"column:content"`
|
||||
Type uint8 `gorm:"column:type"`
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"product/internal/dao"
|
||||
"product/internal/model"
|
||||
"product/internal/svc"
|
||||
"product/product"
|
||||
|
||||
"pkg.local/modelbase"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AddLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
|
||||
return &AddLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AddLogic) Add(in *product.AddReq) (*product.AddResp, error) {
|
||||
if in.GetType() == dao.ProductTypeNormal {
|
||||
if in.GetAgentPrice() == 0 || in.GetStorePrice() == 0 ||
|
||||
in.GetSalePrice() == 0 || in.GetSharePrice() == 0 ||
|
||||
in.GetSaleReward() == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "缺少相关价格")
|
||||
}
|
||||
}
|
||||
|
||||
publishTime := time.Now()
|
||||
if len(in.GetPublishTime()) > 1 {
|
||||
t, err := parsePublishTime(in.GetPublishTime())
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "时间错误")
|
||||
}
|
||||
publishTime = t
|
||||
}
|
||||
|
||||
var check dao.ProductCheckExist
|
||||
err := model.ProductModel{}.Init().GetOne(modelbase.Params{
|
||||
Eq: map[string]string{"name": in.GetName()},
|
||||
}, &check)
|
||||
if err != nil {
|
||||
l.Errorf("check product name: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询失败")
|
||||
}
|
||||
if check.Id > 0 {
|
||||
return nil, status.Error(codes.AlreadyExists, "数据已存在")
|
||||
}
|
||||
|
||||
// 管理员信息后续从 ctx / metadata 取
|
||||
adminId := 0
|
||||
adminName := ""
|
||||
|
||||
data := dao.ProductCreate{
|
||||
Name: in.GetName(),
|
||||
Subhead: in.GetSubhead(),
|
||||
Content: in.GetContent(),
|
||||
ModelCode: in.GetModelCode(),
|
||||
Price: float32(in.GetPrice()),
|
||||
StorePrice: float32(in.GetStorePrice()),
|
||||
SalePrice: float32(in.GetSalePrice()),
|
||||
SharePrice: float32(in.GetSharePrice()),
|
||||
AgentPrice: float32(in.GetAgentPrice()),
|
||||
SaleReward: in.GetSaleReward(),
|
||||
Waybill: in.GetWaybill(),
|
||||
SalesModel: uint8(in.GetSalesModel()),
|
||||
Weight: float32(in.GetWeight()),
|
||||
Images: in.GetImages(),
|
||||
PeriodValidity: int16(in.GetPeriodValidity()),
|
||||
Type: uint8(in.GetType()),
|
||||
NormsNumber: uint8(in.GetNormsNumber()),
|
||||
Number: uint(in.GetNumber()),
|
||||
Sort: 1,
|
||||
Label: in.GetLabel(),
|
||||
Cubage: in.GetCubage(),
|
||||
BoxNumber: in.GetBoxNumber(),
|
||||
IsIndex: uint8(in.GetIsIndex()),
|
||||
IndexImage: in.GetIndexImage(),
|
||||
IsBuy: uint8(in.GetIsBuy()),
|
||||
PublishTime: publishTime,
|
||||
TryNumber: uint8(in.GetTryNumber()),
|
||||
AdminName: adminName,
|
||||
AdminId: adminId,
|
||||
}
|
||||
if data.IsIndex == 0 {
|
||||
data.IsIndex = 2
|
||||
}
|
||||
if data.IsBuy == 0 {
|
||||
data.IsBuy = 2
|
||||
}
|
||||
if data.Type == 0 {
|
||||
data.Type = dao.ProductTypeNormal
|
||||
}
|
||||
if data.NormsNumber == 0 {
|
||||
data.NormsNumber = 1
|
||||
}
|
||||
if data.TryNumber == 0 {
|
||||
data.TryNumber = 1
|
||||
}
|
||||
if data.SalesModel == 0 {
|
||||
data.SalesModel = dao.ProductSalesModelOffline
|
||||
}
|
||||
|
||||
err = l.svcCtx.DB.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := (model.ProductModel{}.Init().WithTX(tx).Create(&data)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
content := dao.ProductVerify{
|
||||
Id: data.Id,
|
||||
ModelCode: data.ModelCode,
|
||||
Price: data.Price,
|
||||
StorePrice: data.StorePrice,
|
||||
SalePrice: data.SalePrice,
|
||||
SharePrice: data.SharePrice,
|
||||
AgentPrice: data.AgentPrice,
|
||||
SaleReward: data.SaleReward,
|
||||
Type: data.Type,
|
||||
BoxNumber: data.BoxNumber,
|
||||
NormsNumber: data.NormsNumber,
|
||||
AdminName: data.AdminName,
|
||||
AdminId: data.AdminId,
|
||||
}
|
||||
contentBytes, err := json.Marshal(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
verify := dao.VerifyCreate{
|
||||
VerifyId: strconv.Itoa(data.Id),
|
||||
Content: string(contentBytes),
|
||||
Type: dao.VerifyTypeProduct,
|
||||
}
|
||||
return model.VerifyModel{}.Init().WithTX(tx).Create(&verify)
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("create product: %v", err)
|
||||
return nil, status.Error(codes.Internal, "创建失败")
|
||||
}
|
||||
|
||||
return &product.AddResp{}, nil
|
||||
}
|
||||
|
||||
func parsePublishTime(s string) (time.Time, error) {
|
||||
for _, layout := range []string{time.DateTime, time.DateOnly} {
|
||||
if t, err := time.ParseInLocation(layout, s, time.Local); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
return time.Time{}, status.Error(codes.InvalidArgument, "invalid time format")
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"product/internal/svc"
|
||||
"product/product"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type EditLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditLogic {
|
||||
return &EditLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *EditLogic) Edit(in *product.EditReq) (*product.EditResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &product.EditResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"product/internal/dao"
|
||||
|
||||
"pkg.local/modelbase"
|
||||
)
|
||||
|
||||
type ProductModel struct {
|
||||
modelbase.Base
|
||||
}
|
||||
|
||||
func (m ProductModel) TableName() string {
|
||||
return modelbase.Prefix() + "product"
|
||||
}
|
||||
|
||||
func (m ProductModel) Init() ProductModel {
|
||||
m.Table = m.TableName()
|
||||
return m
|
||||
}
|
||||
|
||||
func (m ProductModel) Create(data *dao.ProductCreate) error {
|
||||
return m.Base.Create(data)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"product/internal/dao"
|
||||
|
||||
"pkg.local/modelbase"
|
||||
)
|
||||
|
||||
type VerifyModel struct {
|
||||
modelbase.Base
|
||||
}
|
||||
|
||||
func (m VerifyModel) TableName() string {
|
||||
return modelbase.Prefix() + "verify"
|
||||
}
|
||||
|
||||
func (m VerifyModel) Init() VerifyModel {
|
||||
m.Table = m.TableName()
|
||||
return m
|
||||
}
|
||||
|
||||
func (m VerifyModel) Create(data *dao.VerifyCreate) error {
|
||||
return m.Base.Create(data)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
// Source: product.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"product/internal/logic"
|
||||
"product/internal/svc"
|
||||
"product/product"
|
||||
)
|
||||
|
||||
type ProductServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
product.UnimplementedProductServer
|
||||
}
|
||||
|
||||
func NewProductServer(svcCtx *svc.ServiceContext) *ProductServer {
|
||||
return &ProductServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ProductServer) Add(ctx context.Context, in *product.AddReq) (*product.AddResp, error) {
|
||||
l := logic.NewAddLogic(ctx, s.svcCtx)
|
||||
return l.Add(in)
|
||||
}
|
||||
|
||||
func (s *ProductServer) Edit(ctx context.Context, in *product.EditReq) (*product.EditResp, error) {
|
||||
l := logic.NewEditLogic(ctx, s.svcCtx)
|
||||
return l.Edit(in)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"product/internal/config"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
Prefix string
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
Prefix: c.Mysql.Prefix,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user