2022-11-28 16:38:30 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crm/models"
|
|
|
|
|
"crm/response"
|
|
|
|
|
"crm/service"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ProductApi struct {
|
|
|
|
|
productService *service.ProductService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewProductApi() *ProductApi {
|
|
|
|
|
productApi := ProductApi{
|
|
|
|
|
productService: &service.ProductService{},
|
|
|
|
|
}
|
|
|
|
|
return &productApi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建产品
|
|
|
|
|
func (p *ProductApi) Create(context *gin.Context) {
|
|
|
|
|
var param models.ProductCreateParam
|
|
|
|
|
uid, _ := strconv.Atoi(context.Request.Header.Get("uid"))
|
|
|
|
|
err := context.ShouldBind(¶m)
|
|
|
|
|
if uid <= 0 || err != nil {
|
|
|
|
|
response.Result(response.ErrCodeParamInvalid, nil, context)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
param.Creator = int64(uid)
|
|
|
|
|
errCode := p.productService.Create(¶m)
|
|
|
|
|
response.Result(errCode, nil, context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新产品
|
|
|
|
|
func (p *ProductApi) Update(context *gin.Context) {
|
|
|
|
|
var param models.ProductUpdateParam
|
|
|
|
|
if err := context.ShouldBind(¶m); err != nil {
|
|
|
|
|
response.Result(response.ErrCodeParamInvalid, nil, context)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
errCode := p.productService.Update(¶m)
|
|
|
|
|
response.Result(errCode, nil, context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除产品
|
|
|
|
|
func (p *ProductApi) Delete(context *gin.Context) {
|
|
|
|
|
var param models.ProductDeleteParam
|
|
|
|
|
if err := context.ShouldBind(¶m); err != nil {
|
|
|
|
|
response.Result(response.ErrCodeParamInvalid, nil, context)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
errCode := p.productService.Delete(¶m)
|
|
|
|
|
response.Result(errCode, nil, context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询产品列表
|
2023-01-25 16:10:16 +08:00
|
|
|
func (p *ProductApi) GetList(context *gin.Context) {
|
2022-11-28 16:38:30 +08:00
|
|
|
var param models.ProductQueryParam
|
|
|
|
|
uid, _ := strconv.Atoi(context.Request.Header.Get("uid"))
|
|
|
|
|
err := context.ShouldBind(¶m)
|
|
|
|
|
if uid <= 0 || err != nil {
|
|
|
|
|
response.Result(response.ErrCodeParamInvalid, nil, context)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
param.Creator = int64(uid)
|
2023-01-25 16:10:16 +08:00
|
|
|
productList, rows, errCode := p.productService.GetList(¶m)
|
2022-11-28 16:38:30 +08:00
|
|
|
response.PageResult(errCode, productList, rows, context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询产品信息
|
2023-01-25 16:10:16 +08:00
|
|
|
func (p *ProductApi) GetInfo(context *gin.Context) {
|
2022-11-28 16:38:30 +08:00
|
|
|
var param models.ProductQueryParam
|
|
|
|
|
if err := context.ShouldBind(¶m); err != nil {
|
|
|
|
|
response.Result(response.ErrCodeParamInvalid, nil, context)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-25 16:10:16 +08:00
|
|
|
productInfo, errCode := p.productService.GetInfo(¶m)
|
2022-11-28 16:38:30 +08:00
|
|
|
response.Result(errCode, productInfo, context)
|
|
|
|
|
}
|
2022-12-27 19:18:58 +08:00
|
|
|
|
|
|
|
|
// 导出Excel文件
|
|
|
|
|
func (p *ProductApi) Export(context *gin.Context) {
|
|
|
|
|
uid, _ := strconv.Atoi(context.Request.Header.Get("uid"))
|
|
|
|
|
if uid <= 0 {
|
|
|
|
|
response.Result(response.ErrCodeParamInvalid, nil, context)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
file, errCode := p.productService.Export(int64(uid))
|
|
|
|
|
if len(file) >= 0 && errCode != 0 {
|
|
|
|
|
response.Result(errCode, nil, context)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
context.File(file)
|
|
|
|
|
}
|