Browse Source

新增所有商品查询、指定商品查询、创建或更新商品、创建项目接口

Ohio-HYF 3 years ago
parent
commit
934059d3b2

+ 17 - 1
db/enterprise.go

@@ -3,9 +3,11 @@ package db
 import (
 	"context"
 	"youngee_b_api/model/gorm_model"
+
+	"gorm.io/gorm"
 )
 
-func CreateEnterprise(ctx context.Context, newEnterprise gorm_model.Enterprise) (*int, error) {
+func CreateEnterprise(ctx context.Context, newEnterprise gorm_model.Enterprise) (*int64, error) {
 	db := GetReadDB(ctx)
 	err := db.Create(&newEnterprise).Error
 	if err != nil {
@@ -13,3 +15,17 @@ func CreateEnterprise(ctx context.Context, newEnterprise gorm_model.Enterprise)
 	}
 	return &newEnterprise.EnterpriseID, nil
 }
+
+func GetEnterpriseByUID(ctx context.Context, userID int64) (*int64, error) {
+	db := GetReadDB(ctx)
+	enterprise := gorm_model.Enterprise{}
+	err := db.Where("user_id = ?", userID).First(&enterprise).Error
+	if err != nil {
+		if err == gorm.ErrRecordNotFound {
+			return nil, nil
+		} else {
+			return nil, err
+		}
+	}
+	return &enterprise.EnterpriseID, nil
+}

+ 50 - 1
db/product.go

@@ -3,9 +3,11 @@ package db
 import (
 	"context"
 	"youngee_b_api/model/gorm_model"
+
+	"gorm.io/gorm"
 )
 
-func CreateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int, error) {
+func CreateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
 	db := GetReadDB(ctx)
 	err := db.Create(&product).Error
 	if err != nil {
@@ -13,3 +15,50 @@ func CreateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*in
 	}
 	return &product.ProductID, nil
 }
+
+func UpdateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
+	db := GetReadDB(ctx)
+	err := db.Model(&product).Updates(product).Error
+	if err != nil {
+		return nil, err
+	}
+	return &product.ProductID, nil
+}
+
+func FindAllProduct(ctx context.Context, enterpriseID int64) ([]gorm_model.YounggeeProduct, error) {
+	db := GetReadDB(ctx)
+	products := []gorm_model.YounggeeProduct{}
+	err := db.Where("enterprise_id = ?", enterpriseID).Find(&products).Error
+	if err != nil {
+		return nil, err
+	}
+	return products, nil
+}
+
+func FindProductByID(ctx context.Context, productID int64) (*gorm_model.YounggeeProduct, error) {
+	db := GetReadDB(ctx)
+	product := &gorm_model.YounggeeProduct{}
+	err := db.First(&product, productID).Error
+	if err != nil {
+		if err == gorm.ErrRecordNotFound {
+			return nil, nil
+		} else {
+			return nil, err
+		}
+	}
+	return product, nil
+}
+
+func FindProductByName(ctx context.Context, brandName string, productName string) (*int64, error) {
+	db := GetReadDB(ctx)
+	product := &gorm_model.YounggeeProduct{}
+	err := db.Where("product_name = ? AND brand_name = ?", productName, brandName).First(&product).Error
+	if err != nil {
+		if err == gorm.ErrRecordNotFound {
+			return nil, nil
+		} else {
+			return nil, err
+		}
+	}
+	return &product.ProductID, nil
+}

+ 19 - 0
db/product_photo.go

@@ -13,3 +13,22 @@ func CreateProductPhoto(ctx context.Context, productPhotos []gorm_model.Younggee
 	}
 	return nil
 }
+
+func FindAllProductPhoto(ctx context.Context, productID int64) ([]gorm_model.YounggeeProductPhoto, error) {
+	db := GetReadDB(ctx)
+	productPhotos := []gorm_model.YounggeeProductPhoto{}
+	err := db.Where("product_id = ?", productID).Find(&productPhotos).Error
+	if err != nil {
+		return nil, err
+	}
+	return productPhotos, nil
+}
+
+func DeletePhoto(ctx context.Context, productID int64) error {
+	db := GetReadDB(ctx)
+	err := db.Where("product_id = ?", productID).Delete(&gorm_model.YounggeeProductPhoto{}).Error
+	if err != nil {
+		return err
+	}
+	return nil
+}

+ 1 - 1
db/project.go

@@ -5,7 +5,7 @@ import (
 	"youngee_b_api/model/gorm_model"
 )
 
-func CreateProject(ctx context.Context, projectInfo gorm_model.ProjectInfo) (*int, error) {
+func CreateProject(ctx context.Context, projectInfo gorm_model.ProjectInfo) (*int64, error) {
 	db := GetReadDB(ctx)
 	err := db.Create(&projectInfo).Error
 	if err != nil {

+ 1 - 1
db/user.go

@@ -7,7 +7,7 @@ import (
 	"gorm.io/gorm"
 )
 
-func CreateUser(ctx context.Context, user gorm_model.User) (*int, error) {
+func CreateUser(ctx context.Context, user gorm_model.User) (*int64, error) {
 	db := GetReadDB(ctx)
 	err := db.Create(&user).Error
 	if err != nil {

+ 2 - 5
handler/Register.go

@@ -42,15 +42,12 @@ func (h *RegisterHandler) getResponse() interface{} {
 func (h *RegisterHandler) run() {
 	data := http_model.RegisterRequest{}
 	data = *h.req
-	// 注册接口处理流程
-	// 1. 在user表中查找phone,判断账号是否存在,若不存在进行下一步,否则返回error,message:账号已存在
-	// 2. 在redis中查找验证码,判断验证码是否正确,若正确进行下一步,否则返回error,message:验证码错误
-	flag, err := service.Register.Register(h.ctx, data.Phone, data.Code)
+	userID, err := service.Register.Register(h.ctx, data.Phone, data.Code)
 	if err != nil {
 		logrus.Errorf("[RegisterHandler] call RegisterUser err:%+v\n", err)
 		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal)
 		return
-	} else if flag != nil {
+	} else if userID != nil {
 		// 3. 先后在user表和enterprise表中增加账号信息
 		res, err := service.Enterprise.CreateEnterpriseUser(h.ctx, data)
 		if err != nil {

+ 1 - 1
handler/password_login.go

@@ -44,7 +44,7 @@ func (h *PasswordLoginHandler) run() {
 	// 2. 在user表中查找phone,判断手机号是否存在,若存在进行下一步,否则返回error,message:账号不存在
 	// 3. 更新user表中登陆时间
 	// 4. 生成tocken
-	// 5. 将userID和tocken存到redis
+	// 5. 将用户信息和tocken存到redis
 	// 6. 返回tocken
 
 	data := http_model.PasswordLoginData{}

+ 27 - 3
handler/product_create.go

@@ -2,6 +2,7 @@ package handler
 
 import (
 	"youngee_b_api/consts"
+	"youngee_b_api/middleware"
 	"youngee_b_api/model/http_model"
 	"youngee_b_api/service"
 	"youngee_b_api/util"
@@ -41,13 +42,36 @@ func (h *CreateProductHandler) getResponse() interface{} {
 func (h *CreateProductHandler) run() {
 	data := http_model.CreateProductRequest{}
 	data = *h.req
-	res, err := service.Product.CreateProduct(h.ctx, data)
+	auth := middleware.GetSessionAuth(h.ctx)
+	enterpriseID := auth.EnterpriseID
+	//根据品牌名和商品名查询商品是否存在,若存在则更新,否则新增
+	productID, err := service.Product.FindByName(h.ctx, data.BrandName, data.ProductName)
 	if err != nil {
-		logrus.Errorf("[CreateProductHandler] call CreateProduct err:%+v\n", err)
+		logrus.Errorf("[CreateProductHandler] call Create err:%+v\n", err)
 		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal)
 		return
+	} else {
+		if productID != nil {
+			// 该商品存在,更新
+			data.ProductId = *productID
+			res, err := service.Product.Update(h.ctx, data, enterpriseID)
+			if err != nil {
+				logrus.Errorf("[CreateProductHandler] call Create err:%+v\n", err)
+				util.HandlerPackErrorResp(h.resp, consts.ErrorInternal)
+				return
+			}
+			h.resp.Data = res
+		} else {
+			// 商品不存在,新增
+			res, err := service.Product.Create(h.ctx, data, enterpriseID)
+			if err != nil {
+				logrus.Errorf("[CreateProductHandler] call Create err:%+v\n", err)
+				util.HandlerPackErrorResp(h.resp, consts.ErrorInternal)
+				return
+			}
+			h.resp.Data = res
+		}
 	}
-	h.resp.Data = res
 }
 func (h *CreateProductHandler) checkParam() error {
 	return nil

+ 55 - 0
handler/product_find.go

@@ -0,0 +1,55 @@
+package handler
+
+import (
+	"youngee_b_api/consts"
+	"youngee_b_api/model/http_model"
+	"youngee_b_api/service"
+	"youngee_b_api/util"
+
+	"github.com/gin-gonic/gin"
+	"github.com/sirupsen/logrus"
+)
+
+func WrapFindProductHandler(ctx *gin.Context) {
+	handler := newFindProductHandler(ctx)
+	baseRun(handler)
+}
+
+func newFindProductHandler(ctx *gin.Context) *FindProductHandler {
+	return &FindProductHandler{
+		req:  http_model.NewFindProductRequest(),
+		resp: http_model.NewFindProductResponse(),
+		ctx:  ctx,
+	}
+}
+
+type FindProductHandler struct {
+	req  *http_model.FindProductRequest
+	resp *http_model.CommonResponse
+	ctx  *gin.Context
+}
+
+func (h *FindProductHandler) getRequest() interface{} {
+	return h.req
+}
+func (h *FindProductHandler) getContext() *gin.Context {
+	return h.ctx
+}
+func (h *FindProductHandler) getResponse() interface{} {
+	return h.resp
+}
+func (h *FindProductHandler) run() {
+	data := *&http_model.FindProductRequest{}
+	data = *h.req
+	res, err := service.Product.FindByID(h.ctx, data.ProductID)
+	if err != nil {
+		// 数据库查询失败,返回5001
+		logrus.Errorf("[FindProductHandler] call Find err:%+v\n", err)
+		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal)
+		return
+	}
+	h.resp.Data = res
+}
+func (h *FindProductHandler) checkParam() error {
+	return nil
+}

+ 56 - 0
handler/product_findAll.go

@@ -0,0 +1,56 @@
+package handler
+
+import (
+	"youngee_b_api/consts"
+	"youngee_b_api/middleware"
+	"youngee_b_api/model/http_model"
+	"youngee_b_api/service"
+	"youngee_b_api/util"
+
+	"github.com/gin-gonic/gin"
+	"github.com/sirupsen/logrus"
+)
+
+func WrapFindAllProductHandler(ctx *gin.Context) {
+	handler := newFindAllProductHandler(ctx)
+	baseRun(handler)
+}
+
+func newFindAllProductHandler(ctx *gin.Context) *FindAllProductHandler {
+	return &FindAllProductHandler{
+		req:  http_model.NewFindAllProductRequest(),
+		resp: http_model.NewFindAllProductResponse(),
+		ctx:  ctx,
+	}
+}
+
+type FindAllProductHandler struct {
+	req  *http_model.FindAllProductRequest
+	resp *http_model.CommonResponse
+	ctx  *gin.Context
+}
+
+func (h *FindAllProductHandler) getRequest() interface{} {
+	return h.req
+}
+func (h *FindAllProductHandler) getContext() *gin.Context {
+	return h.ctx
+}
+func (h *FindAllProductHandler) getResponse() interface{} {
+	return h.resp
+}
+func (h *FindAllProductHandler) run() {
+	auth := middleware.GetSessionAuth(h.ctx)
+	enterpriseID := auth.EnterpriseID
+	res, err := service.Product.FindAll(h.ctx, enterpriseID)
+	if err != nil {
+		// 数据库查询失败,返回5001
+		logrus.Errorf("[FindAllProductHandler] call FindAll err:%+v\n", err)
+		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal)
+		return
+	}
+	h.resp.Data = res
+}
+func (h *FindAllProductHandler) checkParam() error {
+	return nil
+}

+ 4 - 1
handler/project_create.go

@@ -2,6 +2,7 @@ package handler
 
 import (
 	"youngee_b_api/consts"
+	"youngee_b_api/middleware"
 	"youngee_b_api/model/http_model"
 	"youngee_b_api/service"
 	"youngee_b_api/util"
@@ -41,7 +42,9 @@ func (h *CreateProjectHandler) getResponse() interface{} {
 func (h *CreateProjectHandler) run() {
 	data := http_model.CreateProjectRequest{}
 	data = *h.req
-	res, err := service.Project.CreateProject(h.ctx, data)
+	auth := middleware.GetSessionAuth(h.ctx)
+	enterpriseID := auth.EnterpriseID
+	res, err := service.Project.CreateProject(h.ctx, data, enterpriseID)
 	if res != nil {
 		logrus.Errorf("[CreateProjectHandler] call CreateProject err:%+v\n", err)
 		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal)

+ 2 - 0
middleware/login_auth.go

@@ -1,6 +1,7 @@
 package middleware
 
 import (
+	"fmt"
 	"youngee_b_api/consts"
 	"youngee_b_api/model/redis_model"
 	"youngee_b_api/service"
@@ -14,6 +15,7 @@ import (
 func LoginAuthMiddleware(c *gin.Context) {
 	token := c.Request.Header.Get("Authorization")
 	if token != "" {
+		fmt.Println(token)
 		if auth, err := service.LoginAuth.AuthToken(c, token); err == nil {
 			c.Set(consts.SessionAuthSchema, auth)
 			c.Next()

+ 6 - 6
model/gorm_model/enterprise.go

@@ -6,13 +6,13 @@ import (
 )
 
 type Enterprise struct {
-	EnterpriseID     int       `gorm:"column:enterprise_id"` // 企业id
-	Industry         int       `gorm:"column:industry"`                                 // 行业,1-14分别代表能源、化工、材料、机械设备/军工、企业服务/造纸印刷、运输设备、旅游酒店、媒体/信息通信服务、批发/零售、消费品、卫生保健/医疗、金融、建材/建筑/房地产、公共事业
+	EnterpriseID     int64       `gorm:"column:enterprise_id"` // 企业id
+	Industry         int64       `gorm:"column:industry"`                                 // 行业,1-14分别代表能源、化工、材料、机械设备/军工、企业服务/造纸印刷、运输设备、旅游酒店、媒体/信息通信服务、批发/零售、消费品、卫生保健/医疗、金融、建材/建筑/房地产、公共事业
 	BusinessName     string    `gorm:"column:business_name"`                            // 公司或组织名称
-	UserID           int       `gorm:"column:user_id"`                                  // 对应用户id
-	Balance          int       `gorm:"column:balance"`                                  // 账户余额
-	FrozenBalance    int       `gorm:"column:frozen_balance"`                           // 冻结余额
-	AvailableBalance int       `gorm:"column:available_balance"`                        // 可用余额
+	UserID           int64       `gorm:"column:user_id"`                                  // 对应用户id
+	Balance          int64       `gorm:"column:balance"`                                  // 账户余额
+	FrozenBalance    int64       `gorm:"column:frozen_balance"`                           // 冻结余额
+	AvailableBalance int64       `gorm:"column:available_balance"`                        // 可用余额
 	CreatedAt        time.Time `gorm:"column:created_at"`                               // 创建时间
 	UpdatedAt        time.Time `gorm:"column:updated_at"`                               // 更新时间
 }

+ 3 - 3
model/gorm_model/product.go

@@ -6,13 +6,13 @@ import (
 )
 
 type YounggeeProduct struct {
-	ProductID    int       `gorm:"column:product_id;primary_key"` // 商品id
+	ProductID    int64       `gorm:"column:product_id;primary_key"` // 商品id
 	ProductName  string    `gorm:"column:product_name"`           // 商品名称
-	ProductType  int       `gorm:"column:product_type"`           // 商品类型
+	ProductType  int64       `gorm:"column:product_type"`           // 商品类型
 	ShopAddress  string    `gorm:"column:shop_address"`           // 店铺地址,商品类型为线下品牌时需填写
 	ProductPrice int64     `gorm:"column:product_price"`          // 商品价值
 	ProductUrl   string    `gorm:"column:product_url"`            // 商品链接,可为电商网址、公司官网、大众点评的店铺地址等可以说明商品信息或者品牌信息的线上地址;
-	EnterpriseID int       `gorm:"column:enterprise_id"`          // 所属企业id
+	EnterpriseID int64       `gorm:"column:enterprise_id"`          // 所属企业id
 	CreatedAt    time.Time `gorm:"column:created_at"`             // 创建时间
 	UpdatedAt    time.Time `gorm:"column:updated_at"`             // 更新时间
 	BrandName    string    `gorm:"column:brand_name"`

+ 3 - 3
model/gorm_model/product_photo.go

@@ -6,10 +6,10 @@ import (
 )
 
 type YounggeeProductPhoto struct {
-	ProductPhotoID int       `gorm:"column:product_photo_id;primary_key"` // 商品图片id
+	ProductPhotoID int64       `gorm:"column:product_photo_id;primary_key"` // 商品图片id
 	PhotoUrl       string    `gorm:"column:photo_url"`                    // 图片或视频url
-	Symbol         int       `gorm:"column:symbol"`                       // 图片为主图或详情图标志位,1为主图,2为详情图,3为视频
-	ProductID      int       `gorm:"column:product_id"`                   // 所属商品id
+	Symbol         int64       `gorm:"column:symbol"`                       // 图片为主图或详情图标志位,1为主图,2为详情图,3为视频
+	ProductID      int64       `gorm:"column:product_id"`                   // 所属商品id
 	CreatedAt      time.Time `gorm:"column:created_at"`                   // 创建时间
 }
 

+ 9 - 9
model/gorm_model/project.go

@@ -6,18 +6,18 @@ import (
 )
 
 type ProjectInfo struct {
-	ProjectID       int       `gorm:"column:project_id"` // 项目id
+	ProjectID       int64       `gorm:"column:project_id"` // 项目id
 	ProjectName     string    `gorm:"column:project_name"`           // 项目名称
-	ProjectStatus   int       `gorm:"column:project_status"`         // 项目状态,1-7分别代表创建中、待审核、招募中、待支付、失效、执行中、已结案
-	ProjectType     int       `gorm:"column:project_type"`           // 项目类型,1代表全流程项目,2代表专项项目
-	ProjectPlatform int       `gorm:"column:project_platform"`       // 项目平台,1-7分别代表小红书、抖音、微博、快手、b站、大众点评、知乎
-	ProjectForm     int       `gorm:"column:project_form"`           // 项目形式,1-4分别代表实体商品寄拍、虚拟产品测评、线下探店打卡、素材微原创
-	TalentType      int       `gorm:"column:talent_type"`            // 达人类型
+	ProjectStatus   int64       `gorm:"column:project_status"`         // 项目状态,1-7分别代表创建中、待审核、招募中、待支付、失效、执行中、已结案
+	ProjectType     int64       `gorm:"column:project_type"`           // 项目类型,1代表全流程项目,2代表专项项目
+	ProjectPlatform int64       `gorm:"column:project_platform"`       // 项目平台,1-7分别代表小红书、抖音、微博、快手、b站、大众点评、知乎
+	ProjectForm     int64       `gorm:"column:project_form"`           // 项目形式,1-4分别代表实体商品寄拍、虚拟产品测评、线下探店打卡、素材微原创
+	TalentType      int64       `gorm:"column:talent_type"`            // 达人类型
 	RecruitDdl      time.Time `gorm:"column:recruit_ddl"`            // 招募截止时间
-	ContentType     int       `gorm:"column:content_type"`           // 内容形式,1代表图文,2代表视频
+	ContentType     int64       `gorm:"column:content_type"`           // 内容形式,1代表图文,2代表视频
 	ProjectDetail   string    `gorm:"column:project_detail"`         // 项目详情
-	EnterpriseID    int       `gorm:"column:enterprise_id"`          // 所属企业id
-	ProductID       int       `gorm:"column:product_id"`             // 关联商品id
+	EnterpriseID    int64       `gorm:"column:enterprise_id"`          // 所属企业id
+	ProductID       int64       `gorm:"column:product_id"`             // 关联商品id
 	CreatedAt       time.Time `gorm:"column:created_at"`             // 创建时间
 	UpdatedAt       time.Time `gorm:"column:updated_at"`             // 修改时间
 }

+ 2 - 2
model/gorm_model/project_photo.go

@@ -6,9 +6,9 @@ import (
 )
 
 type ProjectPhoto struct {
-	ProjectPhotoID int       `gorm:"column:project_photo_id;primary_key"` // 项目图片id
+	ProjectPhotoID int64       `gorm:"column:project_photo_id;primary_key"` // 项目图片id
 	PhotoUrl       string    `gorm:"column:photo_url"`                    // 图片url
-	ProjectID      int       `gorm:"column:project_id"`                   // 所属项目id
+	ProjectID      int64       `gorm:"column:project_id"`                   // 所属项目id
 	CreatedAt      time.Time `gorm:"column:created_at"`                   // 创建时间
 }
 

+ 8 - 8
model/gorm_model/recruit_strategy.go

@@ -3,14 +3,14 @@ package gorm_model
 
 
 type RecruitStrategy struct {
-	RecruitStrategyID int `gorm:"column:recruit_strategy_id;primary_key"` // 招募策略id
-	FeeForm           int `gorm:"column:fee_form"`                        // 稿费形式,1-3分别代表产品置换、固定稿费、自报价
-	StrategyID        int `gorm:"column:strategy_id"`                     // 策略id
-	FollowersLow      int `gorm:"column:followers_low"`                   // 达人粉丝数下限
-	FollowersUp       int `gorm:"column:followers_up"`                    // 达人粉丝数上限
-	RecruitNumber     int `gorm:"column:recruit_number"`                  // 招募数量
-	Offer             int `gorm:"column:offer"`                           // 报价
-	ProjectID         int `gorm:"column:project_id"`                      // 所属项目id
+	RecruitStrategyID int64 `gorm:"column:recruit_strategy_id;primary_key"` // 招募策略id
+	FeeForm           int64 `gorm:"column:fee_form"`                        // 稿费形式,1-3分别代表产品置换、固定稿费、自报价
+	StrategyID        int64 `gorm:"column:strategy_id"`                     // 策略id
+	FollowersLow      int64 `gorm:"column:followers_low"`                   // 达人粉丝数下限
+	FollowersUp       int64 `gorm:"column:followers_up"`                    // 达人粉丝数上限
+	RecruitNumber     int64 `gorm:"column:recruit_number"`                  // 招募数量
+	Offer             int64 `gorm:"column:offer"`                           // 报价
+	ProjectID         int64 `gorm:"column:project_id"`                      // 所属项目id
 }
 
 func (m *RecruitStrategy) TableName() string {

+ 2 - 2
model/gorm_model/user.go

@@ -6,7 +6,7 @@ import (
 )
 
 type User struct {
-	ID            int       `gorm:"column:id;primary_key;AUTO_INCREMENT"` // 用户表id
+	ID            int64       `gorm:"column:id;primary_key;AUTO_INCREMENT"` // 用户表id
 	User          string    `gorm:"column:user"`                          // 账号
 	Username      string    `gorm:"column:username"`                      // 后台用户名
 	Password      string    `gorm:"column:password"`                      // 用户密码
@@ -14,7 +14,7 @@ type User struct {
 	Role          string    `gorm:"column:role"`                          // 角色 1,超级管理员; 2,管理员;3,企业用户
 	Phone         string    `gorm:"column:phone"`                         // 绑定手机
 	Email         string    `gorm:"column:email"`                         // 电子邮件
-	LastLoginTime time.Time `gorm:"column:last_login_time"`               // 最后一次登录时间
+	LastLogintime time.Time `gorm:"column:last_login_time"`               // 最后一次登录时间
 	UserState     string    `gorm:"column:user_state"`                    // 0,禁用,1,正常
 	CreatedAt     time.Time `gorm:"column:created_at"`                    // 创建时间
 	UpdatedAt     time.Time `gorm:"column:updated_at"`                    // 更新时间

+ 4 - 4
model/http_model/product_create.go

@@ -2,22 +2,22 @@ package http_model
 
 type CreateProductPhoto struct {
 	PhotoUrl string `json:"photo_url"` // 图片或视频url
-	Symbol   int    `json:"symbol"`    // 图片为主图或详情图标志位,1为主图,2为详情图,3为视频
+	Symbol   int64  `json:"symbol"`    // 图片为主图或详情图标志位,1为主图,2为详情图,3为视频
 }
 
 type CreateProductRequest struct {
+	ProductId     int64                `json:"product_id"`
 	ProductName   string               `json:"product_name"`   // 商品名称
-	ProductType   int                  `json:"product_type"`   // 商品类型
+	ProductType   int64                `json:"product_type"`   // 商品类型
 	ShopAddress   string               `json:"shop_address"`   // 店铺地址,商品类型为线下品牌时需填写
 	ProductPrice  int64                `json:"product_price"`  // 商品价值
 	ProductPhotos []CreateProductPhoto `json:"product_photos"` // 商品图片列表
 	ProductUrl    string               `json:"product_url"`    // 商品链接,可为电商网址、公司官网、大众点评的店铺地址等可以说明商品信息或者品牌信息的线上地址;
-	EnterpriseID  int                  `json:"enterprise_id"`  // 所属企业id
 	BrandName     string               `json:"brand_name"`
 }
 
 type CreateProductData struct {
-	ProductID int `json:"product_id"` // 商品id
+	ProductID int64 `json:"product_id"` // 商品id
 }
 
 func NewCreateProductRequest() *CreateProductRequest {

+ 31 - 0
model/http_model/product_find.go

@@ -0,0 +1,31 @@
+package http_model
+
+type FindProductRequest struct {
+	ProductID int64 `json:"product_id"`
+}
+
+type ProductPhoto struct {
+	PhotoUrl string `json:"photo_url"` // 图片或视频url
+	Symbol   int64  `json:"symbol"`    // 图片为主图或详情图标志位,1为主图,2为详情图,3为视频
+}
+
+type FindProductData struct {
+	ProductID     int64          `json:"product_id"`
+	ProductName   string         `json:"product_name"`   // 商品名称
+	ProductType   int64          `json:"product_type"`   // 商品类型
+	ShopAddress   string         `json:"shop_address"`   // 店铺地址,商品类型为线下品牌时需填写
+	ProductPrice  int64          `json:"product_price"`  // 商品价值
+	ProductPhotos []ProductPhoto `json:"product_photos"` // 商品图片列表
+	ProductUrl    string         `json:"product_url"`    // 商品链接,可为电商网址、公司官网、大众点评的店铺地址等可以说明商品信息或者品牌信息的线上地址;
+	EnterpriseID  int64          `json:"enterprise_id"`  // 所属企业id
+	BrandName     string         `json:"brand_name"`     // 品牌名称
+}
+
+func NewFindProductRequest() *FindProductRequest {
+	return new(FindProductRequest)
+}
+func NewFindProductResponse() *CommonResponse {
+	resp := new(CommonResponse)
+	resp.Data = new(FindProductData)
+	return resp
+}

+ 23 - 0
model/http_model/product_findall.go

@@ -0,0 +1,23 @@
+package http_model
+
+type FindAllProductRequest struct {
+}
+
+type ProductInfo struct {
+	ProductID   int64  `json:"product_id"`
+	BrandName   string `json:"brand_name"`
+	ProductName string `json:"product_name"`
+}
+
+type FindAllProductData struct {
+	ProductInfos []ProductInfo `json:"product_infos"`
+}
+
+func NewFindAllProductRequest() *FindAllProductRequest {
+	return new(FindAllProductRequest)
+}
+func NewFindAllProductResponse() *CommonResponse {
+	resp := new(CommonResponse)
+	resp.Data = new(FindAllProductData)
+	return resp
+}

+ 14 - 15
model/http_model/project_create.go

@@ -7,32 +7,31 @@ type CreateProjectPhoto struct {
 }
 
 type CreateRecruitStrategy struct {
-	FeeForm       int `json:"fee_form"`       // 稿费形式,1-3分别代表产品置换、固定稿费、自报价
-	StrategyID    int `json:"strategy_id"`    // 策略id
-	FollowersLow  int `json:"followers_low"`  // 达人粉丝数下限
-	FollowersUp   int `json:"followers_up"`   // 达人粉丝数上限
-	RecruitNumber int `json:"recruit_number"` // 招募数量
-	Offer         int `json:"offer"`          // 报价
+	FeeForm       int64 `json:"fee_form"`       // 稿费形式,1-3分别代表产品置换、固定稿费、自报价
+	StrategyID    int64 `json:"strategy_id"`    // 策略id
+	FollowersLow  int64 `json:"followers_low"`  // 达人粉丝数下限
+	FollowersUp   int64 `json:"followers_up"`   // 达人粉丝数上限
+	RecruitNumber int64 `json:"recruit_number"` // 招募数量
+	Offer         int64 `json:"offer"`          // 报价
 }
 
 type CreateProjectRequest struct {
 	ProjectName      string                  `json:"project_name"`      // 项目名称
-	ProjectStatus    int                     `json:"project_status"`    // 项目状态,1-7分别代表创建中、待审核、招募中、待支付、失效、执行中、已结案
-	ProjectType      int                     `json:"project_type"`      // 项目类型,1代表全流程项目,2代表专项项目
-	ProjectPlatform  int                     `json:"project_platform"`  // 项目平台,1-7分别代表小红书、抖音、微博、快手、b站、大众点评、知乎
-	ProjectForm      int                     `json:"project_form"`      // 项目形式,1-4分别代表实体商品寄拍、虚拟产品测评、线下探店打卡、素材微原创
-	TalentType       int                     `json:"talent_type"`       // 达人类型
+	ProjectStatus    int64                   `json:"project_status"`    // 项目状态,1-7分别代表创建中、待审核、招募中、待支付、失效、执行中、已结案
+	ProjectType      int64                   `json:"project_type"`      // 项目类型,1代表全流程项目,2代表专项项目
+	ProjectPlatform  int64                   `json:"project_platform"`  // 项目平台,1-7分别代表小红书、抖音、微博、快手、b站、大众点评、知乎
+	ProjectForm      int64                   `json:"project_form"`      // 项目形式,1-4分别代表实体商品寄拍、虚拟产品测评、线下探店打卡、素材微原创
+	TalentType       int64                   `json:"talent_type"`       // 达人类型
 	RecruitDdl       time.Time               `json:"recruit_ddl"`       // 招募截止时间
-	ContentType      int                     `json:"content_type"`      // 内容形式,1代表图文,2代表视频
+	ContentType      int64                   `json:"content_type"`      // 内容形式,1代表图文,2代表视频
 	ProjectDetail    string                  `json:"project_detail"`    // 项目详情
 	RecruitStrategys []CreateRecruitStrategy `json:"recruit_strategys"` // 定价策略
 	ProjectPhotos    []CreateProjectPhoto    `json:"project_photos"`    // 项目图片
-	EnterpriseID     int                     `json:"enterprise_id"`     // 所属企业id
-	ProductID        int                     `json:"product_id"`        // 关联商品id
+	ProductID        int64                   `json:"product_id"`        // 关联商品id
 }
 
 type CreateProjectData struct {
-	ProjectID int `json:"Project_id"` // 项目id
+	ProjectID int64 `json:"Project_id"` // 项目id
 }
 
 func NewCreateProjectRequest() *CreateProjectRequest {

+ 5 - 5
model/http_model/register.go

@@ -1,19 +1,19 @@
 package http_model
 
 type RegisterRequest struct {
-	EnterpriseId int    `json:"enterprise_id"` // 企业id
-	UserId       int    `json:"user_id"`       // 对应用户id
+	EnterpriseId int64  `json:"enterprise_id"` // 企业id
+	UserId       int64  `json:"user_id"`       // 对应用户id
 	RealName     string `json:"real_name"`     // 真实姓名
 	Phone        string `json:"phone"`         //手机号
-	Industry     int    `json:"industry"`      // 行业,1-14分别代表能源、化工、材料、机械设备/军工、企业服务/造纸印刷、运输设备、旅游酒店、媒体/信息通信服务、批发/零售、消费品、卫生保健/医疗、金融、建材/建筑/房地产、公共事业
+	Industry     int64  `json:"industry"`      // 行业,1-14分别代表能源、化工、材料、机械设备/军工、企业服务/造纸印刷、运输设备、旅游酒店、媒体/信息通信服务、批发/零售、消费品、卫生保健/医疗、金融、建材/建筑/房地产、公共事业
 	BusinessName string `json:"business_name"` // 公司或组织名称
 	Email        string `json:"email"`         // 电子邮件
 	Code         string `json:"code"`          //验证码
 }
 
 type RegisterData struct {
-	UserID       int `json:"user_id"`       // 用户id
-	EnterpriseId int `json:"enterprise_id"` // 企业id
+	UserID       int64 `json:"user_id"`       // 用户id
+	EnterpriseId int64 `json:"enterprise_id"` // 企业id
 }
 
 func NewRegisterRequest() *RegisterRequest {

+ 9 - 8
model/redis_model/auth.go

@@ -2,12 +2,13 @@ package redis_model
 
 // redis 存放的企业用户信息Session
 type Auth struct {
-	Phone    string `json:"phone"`
-	ID       int    `json:"id"`        // 用户表id
-	User     string `json:"user"`      // 账号
-	Username string `json:"username"`  // 后台用户名
-	RealName string `json:"real_name"` // 真实姓名
-	Role     string `json:"role"`      // 角色 1,超级管理员; 2,管理员;3,企业用户
-	Email    string `json:"email"`     // 电子邮件
-	Token    string `json:"token"`
+	Phone        string `json:"phone"`
+	ID           int64  `json:"id"` // 用户表id
+	EnterpriseID int64  `json:"enterprise_id"`
+	User         string `json:"user"`      // 账号
+	Username     string `json:"username"`  // 后台用户名
+	RealName     string `json:"real_name"` // 真实姓名
+	Role         string `json:"role"`      // 角色 1,超级管理员; 2,管理员;3,企业用户
+	Email        string `json:"email"`     // 电子邮件
+	Token        string `json:"token"`
 }

+ 2 - 0
route/init.go

@@ -39,6 +39,8 @@ func InitRoute(r *gin.Engine) {
 			auth := middleware.GetSessionAuth(c)
 			logrus.Infof("auth:%+v", auth)
 		})
+		m.POST("/product/findall", handler.WrapFindAllProductHandler)
+		m.POST("/product/find", handler.WrapFindProductHandler)
 		m.POST("/project/create", handler.WrapCreateProjectHandler)
 		m.POST("/product/create", handler.WrapCreateProductHandler)
 	}

+ 8 - 9
service/enterprise.go

@@ -2,7 +2,6 @@ package service
 
 import (
 	"context"
-	"time"
 	"youngee_b_api/db"
 	"youngee_b_api/model/gorm_model"
 	"youngee_b_api/model/http_model"
@@ -17,14 +16,14 @@ type enterprise struct {
 
 func (*enterprise) CreateEnterpriseUser(ctx context.Context, newEnterprise http_model.RegisterRequest) (*http_model.RegisterData, error) {
 	user := gorm_model.User{
-		Phone:         newEnterprise.Phone,
-		User:          "1001",
-		Username:      newEnterprise.BusinessName,
-		Password:      "1001",
-		RealName:      newEnterprise.RealName,
-		Role:          "3",
-		Email:         newEnterprise.Email,
-		LastLoginTime: time.Now().UTC().Local(),
+		Phone:    newEnterprise.Phone,
+		User:     "1001",
+		Username: newEnterprise.BusinessName,
+		Password: "1001",
+		RealName: newEnterprise.RealName,
+		Role:     "3",
+		Email:    newEnterprise.Email,
+		// LastLoginTime: time.Now().UTC().Local(),
 	}
 	userId, err := db.CreateUser(ctx, user)
 	if err != nil {

+ 40 - 45
service/login_auth.go

@@ -46,7 +46,6 @@ func (l *loginAuth) AuthToken(ctx context.Context, token string) (*redis_model.A
 		return nil, errors.New("auth failed")
 	}
 	return auth, nil
-
 }
 
 func (l *loginAuth) AuthMSG(ctx context.Context, phone string, vcode string) (string, error) {
@@ -59,66 +58,62 @@ func (l *loginAuth) AuthMSG(ctx context.Context, phone string, vcode string) (st
 	if err != nil {
 		return "", err
 	}
-	if user == nil {
-		fmt.Printf("user == nil\n")
-	}
-	if string(user.Role) != consts.BRole {
-		fmt.Printf("%+v\n", string(user.Role))
-	}
-	if *code != vcode {
-		fmt.Printf("code:%+v, vcode:%+v\n", *code, vcode)
-	}
 	if user == nil || string(user.Role) != consts.BRole || *code != vcode { // 登录失败
 		logrus.Debugf("[AuthPassword] auth fail,phone:%+v", phone)
 		return "", errors.New("auth fail")
 	}
-	token := l.getToken(ctx, phone)
-	auth := &redis_model.Auth{
-		Phone:    phone,
-		ID:       user.ID,
-		User:     user.User,
-		Username: user.Username,
-		RealName: user.RealName,
-		Role:     user.Role,
-		Email:    user.Email,
-		Token:    token,
-	}
-	if err := l.setSession(ctx, phone, auth); err != nil {
-		fmt.Printf("setSession error\n")
-		return "", err
-	}
-	return token, nil
-}
-
-func (l *loginAuth) AuthPassword(ctx context.Context, phone string, password string) (string, error) {
-	// 验证是否存在
-	user, err := db.GetUserByPhone(ctx, phone)
+	enterpriseID, err := db.GetEnterpriseByUID(ctx, user.ID)
 	if err != nil {
 		return "", err
 	}
-	// 验证正确性
-	if user == nil || user.Role != consts.BRole || user.Password != l.encryptPassword(password) {
-		// 登录失败
-		logrus.Debugf("[AuthPassword] auth fail,phone:%+v", phone)
-		return "", errors.New("auth fail")
-	}
 	token := l.getToken(ctx, phone)
 	auth := &redis_model.Auth{
-		Phone:    phone,
-		ID:       user.ID,
-		User:     user.User,
-		Username: user.Username,
-		RealName: user.RealName,
-		Role:     user.Role,
-		Email:    user.Email,
-		Token:    token,
+		Phone:        phone,
+		ID:           user.ID,
+		EnterpriseID: *enterpriseID,
+		User:         user.User,
+		Username:     user.Username,
+		RealName:     user.RealName,
+		Role:         user.Role,
+		Email:        user.Email,
+		Token:        token,
 	}
 	if err := l.setSession(ctx, phone, auth); err != nil {
+		fmt.Printf("setSession error\n")
 		return "", err
 	}
 	return token, nil
 }
 
+// func (l *loginAuth) AuthPassword(ctx context.Context, phone string, password string) (string, error) {
+// 	// 验证是否存在
+// 	user, err := db.GetUserByPhone(ctx, phone)
+// 	if err != nil {
+// 		return "", err
+// 	}
+// 	// 验证正确性
+// 	if user == nil || user.Role != consts.BRole || user.Password != l.encryptPassword(password) {
+// 		// 登录失败
+// 		logrus.Debugf("[AuthPassword] auth fail,phone:%+v", phone)
+// 		return "", errors.New("auth fail")
+// 	}
+// 	token := l.getToken(ctx, phone)
+// 	auth := &redis_model.Auth{
+// 		Phone:    phone,
+// 		ID:       user.ID,
+// 		User:     user.User,
+// 		Username: user.Username,
+// 		RealName: user.RealName,
+// 		Role:     user.Role,
+// 		Email:    user.Email,
+// 		Token:    token,
+// 	}
+// 	if err := l.setSession(ctx, phone, auth); err != nil {
+// 		return "", err
+// 	}
+// 	return token, nil
+// }
+
 func (l *loginAuth) setSession(ctx context.Context, phone string, auth *redis_model.Auth) error {
 	if authJson, err := json.Marshal(auth); err == nil {
 		err = redis.Set(ctx, l.getRedisKey(phone), string(authJson), l.sessionTTL)

+ 97 - 2
service/product.go

@@ -12,14 +12,14 @@ var Product *product
 type product struct {
 }
 
-func (*product) CreateProduct(ctx context.Context, newProduct http_model.CreateProductRequest) (*http_model.CreateProductData, error) {
+func (*product) Create(ctx context.Context, newProduct http_model.CreateProductRequest, enterpriseID int64) (*http_model.CreateProductData, error) {
 	product := gorm_model.YounggeeProduct{
 		ProductName:  newProduct.ProductName,
 		ProductType:  newProduct.ProductType,
 		ShopAddress:  newProduct.ShopAddress,
 		ProductPrice: newProduct.ProductPrice,
 		ProductUrl:   newProduct.ProductUrl,
-		EnterpriseID: newProduct.EnterpriseID,
+		EnterpriseID: enterpriseID,
 		BrandName:    newProduct.BrandName,
 	}
 	productID, err := db.CreateProduct(ctx, product)
@@ -45,3 +45,98 @@ func (*product) CreateProduct(ctx context.Context, newProduct http_model.CreateP
 	}
 	return res, nil
 }
+
+func (*product) Update(ctx context.Context, newProduct http_model.CreateProductRequest, enterpriseID int64) (*http_model.CreateProductData, error) {
+	product := gorm_model.YounggeeProduct{
+		ProductID:    newProduct.ProductId,
+		ProductName:  newProduct.ProductName,
+		ProductType:  newProduct.ProductType,
+		ShopAddress:  newProduct.ShopAddress,
+		ProductPrice: newProduct.ProductPrice,
+		ProductUrl:   newProduct.ProductUrl,
+		EnterpriseID: enterpriseID,
+		BrandName:    newProduct.BrandName,
+	}
+	productID, err := db.UpdateProduct(ctx, product)
+	if err != nil {
+		return nil, err
+	}
+	// 删除该商品之前的所有图片
+	err = db.DeletePhoto(ctx, *productID)
+	if err != nil {
+		return nil, err
+	}
+	// 新增图片
+	productPhotos := []gorm_model.YounggeeProductPhoto{}
+	for _, photo := range newProduct.ProductPhotos {
+		productPhoto := gorm_model.YounggeeProductPhoto{
+			PhotoUrl:  photo.PhotoUrl,
+			Symbol:    photo.Symbol,
+			ProductID: *productID,
+		}
+		productPhotos = append(productPhotos, productPhoto)
+	}
+	err = db.CreateProductPhoto(ctx, productPhotos)
+	if err != nil {
+		return nil, err
+	}
+	res := &http_model.CreateProductData{
+		ProductID: *productID,
+	}
+	return res, nil
+}
+
+func (*product) FindAll(ctx context.Context, enterpriseID int64) (*http_model.FindAllProductData, error) {
+	products, err := db.FindAllProduct(ctx, enterpriseID)
+	if err != nil {
+		// 数据库查询error
+		return nil, err
+	}
+	findAllProductData := http_model.FindAllProductData{}
+	for _, product := range products {
+		productData := http_model.ProductInfo{
+			ProductID:   product.ProductID,
+			BrandName:   product.BrandName,
+			ProductName: product.ProductName,
+		}
+		findAllProductData.ProductInfos = append(findAllProductData.ProductInfos, productData)
+	}
+	return &findAllProductData, nil
+}
+
+func (*product) FindByID(ctx context.Context, productID int64) (*http_model.FindProductData, error) {
+	product, err := db.FindProductByID(ctx, productID)
+	if err != nil {
+		return nil, err
+	}
+	productPhotos, err := db.FindAllProductPhoto(ctx, productID)
+	if err != nil {
+		return nil, err
+	}
+	findProductData := http_model.FindProductData{
+		ProductID:    product.ProductID,
+		ProductName:  product.ProductName,
+		ProductType:  product.ProductType,
+		ShopAddress:  product.ShopAddress,
+		ProductPrice: product.ProductPrice,
+		ProductUrl:   product.ProductUrl,
+		EnterpriseID: product.EnterpriseID,
+		BrandName:    product.BrandName,
+	}
+	for _, photo := range productPhotos {
+		productPhoto := http_model.ProductPhoto{
+			PhotoUrl: photo.PhotoUrl,
+			Symbol:   photo.Symbol,
+		}
+		findProductData.ProductPhotos = append(findProductData.ProductPhotos, productPhoto)
+	}
+	return &findProductData, nil
+}
+
+func (*product) FindByName(ctx context.Context, brandName string, productName string) (*int64, error) {
+	productID, err := db.FindProductByName(ctx, brandName, productName)
+	if err != nil {
+		return nil, err
+	}
+	return productID, nil
+}

+ 2 - 2
service/project.go

@@ -13,7 +13,7 @@ var Project *project
 type project struct {
 }
 
-func (*project) CreateProject(ctx context.Context, newProject http_model.CreateProjectRequest) (*http_model.CreateProjectData, error) {
+func (*project) CreateProject(ctx context.Context, newProject http_model.CreateProjectRequest, enterpriseID int64) (*http_model.CreateProjectData, error) {
 	// build gorm_model.ProjectInfo
 	projectInfo := gorm_model.ProjectInfo{
 		ProjectName:     newProject.ProjectName,
@@ -25,7 +25,7 @@ func (*project) CreateProject(ctx context.Context, newProject http_model.CreateP
 		RecruitDdl:      time.Now().UTC().Local(),
 		ProjectDetail:   newProject.ProjectDetail,
 		ContentType:     newProject.ContentType,
-		EnterpriseID:    newProject.EnterpriseID,
+		EnterpriseID:    enterpriseID,
 		ProductID:       newProject.ProductID,
 	}
 	// db create ProjectInfo

+ 3 - 3
service/register.go

@@ -15,12 +15,12 @@ type register struct {
 	sessionTTL time.Duration
 }
 
-func (r *register) Register(ctx context.Context, phone string, vcode string) (*string, error) {
+func (r *register) Register(ctx context.Context, phone string, vcode string) (*int64, error) {
 	user, err := db.GetUserByPhone(ctx, phone)
 	if err != nil {
 		return nil, err
 	} else if user != nil {
-		return &phone, nil
+		return &user.ID, nil
 	} else {
 		code, err := r.getSession(ctx, phone)
 		if err != nil {
@@ -28,7 +28,7 @@ func (r *register) Register(ctx context.Context, phone string, vcode string) (*s
 		} else {
 			fmt.Printf("%+v", code)
 			if *code != vcode {
-				return &phone, nil
+				return &user.ID, nil
 			} else {
 				return nil, nil
 			}