357 lines
12 KiB
Go
357 lines
12 KiB
Go
package basic
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"go-admin/app/admin/models"
|
|
orm "go-admin/common/global"
|
|
"go-admin/tools/app"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type CommodityCreateRequest struct {
|
|
Name string `json:"name" binding:"required"` // 商品名称
|
|
ErpCategoryId uint32 `json:"erp_category_id" binding:"required"` // 商品分类id
|
|
IMEIType uint32 `json:"imei_type" binding:"required"` // 1-无串码 2-串码(系统生成) 3-串码(手动添加)
|
|
ErpSupplierId uint32 `json:"erp_supplier_id" binding:"required"` // 主供应商
|
|
RetailPrice uint32 `json:"retail_price" binding:"required"` // 指导零售价
|
|
MinRetailPrice uint32 `json:"min_retail_price" binding:"required"` // 最低零售价
|
|
StaffCostPrice uint32 `json:"staff_cost_price" binding:"required"` // 员工成本价加价
|
|
WholesalePrice uint32 `json:"wholesale_price" binding:"required"` // 指导采购价
|
|
Brokerage1 string `json:"brokerage_1" binding:"required"` // 销售毛利提成
|
|
Brokerage2 string `json:"brokerage_2"` // 员工毛利提成
|
|
MemberDiscount string `json:"member_discount"` // 会员优惠
|
|
Origin string `json:"origin"` // 产地
|
|
Remark string `json:"remark" gorm:"type:varchar(512)"` // 备注
|
|
}
|
|
|
|
// CommodityCreate 新增商品
|
|
// @Summary 新增商品
|
|
// @Tags 商品资料
|
|
// @Produce json
|
|
// @Accept json
|
|
// @Param request body CommodityCreateRequest true "商品新增模型"
|
|
// @Success 200 {object} models.ErpCommodity
|
|
// @Router /api/v1/commodity/create [post]
|
|
func CommodityCreate(c *gin.Context) {
|
|
var req = new(CommodityCreateRequest)
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
//logger.Error(err)
|
|
fmt.Println(err.Error())
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
brokerage1Float, err := strconv.ParseFloat(req.Brokerage1, 64)
|
|
if err != nil {
|
|
//logger.Error("brokerage1 err:", err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
brokerage2Float, err := strconv.ParseFloat(req.Brokerage2, 64)
|
|
if err != nil {
|
|
//logger.Error("brokerage2 err:", err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
memberDiscountFloat, err := strconv.ParseFloat(req.MemberDiscount, 64)
|
|
if err != nil {
|
|
//logger.Error("member discount err:", err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
commodity := &models.ErpCommodity{
|
|
Number: 1,
|
|
Name: req.Name,
|
|
ErpCategoryId: req.ErpCategoryId,
|
|
ErpCategoryName: "",
|
|
IMEIType: req.IMEIType,
|
|
ErpSupplierId: req.ErpSupplierId,
|
|
ErpSupplierName: "",
|
|
RetailPrice: req.RetailPrice,
|
|
MinRetailPrice: req.MinRetailPrice,
|
|
StaffCostPrice: req.StaffCostPrice + req.WholesalePrice,
|
|
WholesalePrice: req.WholesalePrice,
|
|
Brokerage1: brokerage1Float,
|
|
Brokerage2: brokerage2Float,
|
|
MemberDiscount: memberDiscountFloat,
|
|
Origin: req.Origin,
|
|
Remark: req.Remark,
|
|
}
|
|
err = commodity.SetErpCategory()
|
|
if err != nil {
|
|
//logger.Error("set erp category err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "创建失败")
|
|
return
|
|
}
|
|
commodity.IdInit()
|
|
|
|
var catCommodity models.ErpCommodity
|
|
err = orm.Eloquent.Table("erp_commodity").Where("erp_category_id=?", req.ErpCategoryId).
|
|
Order("id DESC").Limit(1).Find(&catCommodity).Error
|
|
if err != nil && err != models.RecordNotFound {
|
|
//logger.Error("cat erp commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "创建失败")
|
|
return
|
|
}
|
|
if catCommodity.Number != 0 {
|
|
commodity.Number = catCommodity.Number + 1
|
|
}
|
|
commodity.SerialNumber = fmt.Sprintf("%06d%04d", commodity.ErpCategory.FullNum, commodity.Number)
|
|
|
|
err = orm.Eloquent.Create(commodity).Error
|
|
if err != nil {
|
|
//logger.Error("create commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "创建失败")
|
|
return
|
|
}
|
|
|
|
app.OK(c, commodity, "")
|
|
return
|
|
}
|
|
|
|
// CommodityList 商品列表
|
|
// @Summary 商品列表
|
|
// @Tags 商品资料
|
|
// @Produce json
|
|
// @Accept json
|
|
// @Param request body models.ErpCommodityListReq true "商品列表模型"
|
|
// @Success 200 {object} models.ErpCommodityListResp
|
|
// @Router /api/v1/commodity/list [post]
|
|
func CommodityList(c *gin.Context) {
|
|
req := &models.ErpCommodityListReq{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
//logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
|
|
resp, err := req.List()
|
|
if err != nil {
|
|
//logger.Error("erp commodity list err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "获取失败")
|
|
return
|
|
}
|
|
|
|
app.OK(c, resp, "")
|
|
return
|
|
}
|
|
|
|
type CommodityDetailRequest struct {
|
|
ErpCommodityId uint32 `json:"erp_commodity_id"` // 商品id
|
|
SerialNumber string `json:"serial_number"` // 商品编号
|
|
}
|
|
|
|
// CommodityDetail 商品详情
|
|
// @Summary 商品详情
|
|
// @Tags 商品资料
|
|
// @Produce json
|
|
// @Accept json
|
|
// @Param request body CommodityDetailRequest true "商品详情模型"
|
|
// @Success 200 {object} models.ErpCommodity
|
|
// @Router /api/v1/commodity/detail [post]
|
|
func CommodityDetail(c *gin.Context) {
|
|
var req = new(CommodityDetailRequest)
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
//logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
|
|
var commodity models.ErpCommodity
|
|
qs := orm.Eloquent.Table("erp_commodity")
|
|
if req.ErpCommodityId != 0 {
|
|
qs = qs.Where("id=?", req.ErpCommodityId)
|
|
}
|
|
if req.SerialNumber != "" {
|
|
qs = qs.Where("serial_number=?", req.SerialNumber)
|
|
}
|
|
err := qs.Find(&commodity).Error
|
|
if err != nil {
|
|
//logger.Error("erp commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "获取失败")
|
|
return
|
|
}
|
|
|
|
app.OK(c, commodity, "")
|
|
return
|
|
}
|
|
|
|
type CommodityEditRequest struct {
|
|
Id uint32 `json:"id" binding:"required"` // 商品id
|
|
Name string `json:"name" binding:"required"` // 商品名称
|
|
ErpCategoryId uint32 `json:"erp_category_id" binding:"required"` // 商品分类id
|
|
IMEIType uint32 `json:"imei_type" binding:"required"` // 1-无串码 2-串码(系统生成) 3-串码(手动添加)
|
|
ErpSupplierId uint32 `json:"erp_supplier_id" binding:"required"` // 主供应商id
|
|
RetailPrice uint32 `json:"retail_price" binding:"required"` // 指导零售价
|
|
MinRetailPrice uint32 `json:"min_retail_price" binding:"required"` // 最低零售价
|
|
StaffCostPrice uint32 `json:"staff_cost_price" binding:"required"` // 员工成本价加价
|
|
WholesalePrice uint32 `json:"wholesale_price" binding:"required"` // 指导采购价
|
|
Brokerage1 float64 `json:"brokerage_1"` // 销售毛利提成
|
|
Brokerage2 float64 `json:"brokerage_2"` // 员工毛利提成
|
|
MemberDiscount float64 `json:"member_discount"` // 会员优惠
|
|
Origin string `json:"origin"` // 产地
|
|
Remark string `json:"remark" gorm:"type:varchar(512)"` // 备注
|
|
}
|
|
|
|
// CommodityEdit 编辑商品
|
|
// @Summary 编辑商品
|
|
// @Tags 商品资料
|
|
// @Produce json
|
|
// @Accept json
|
|
// @Param request body CommodityEditRequest true "编辑商品模型"
|
|
// @Success 200 {object} models.ErpCommodity
|
|
// @Router /api/v1/commodity/edit [post]
|
|
func CommodityEdit(c *gin.Context) {
|
|
var req = new(CommodityEditRequest)
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
//logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
brokerage1Float := req.Brokerage1
|
|
brokerage2Float := req.Brokerage2
|
|
memberDiscountFloat := req.MemberDiscount
|
|
//brokerage1Float, err := strconv.ParseFloat(req.Brokerage1, 64)
|
|
//if err != nil {
|
|
// logger.Error("brokerage1 err:", err)
|
|
// app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
// return
|
|
//}
|
|
//brokerage2Float, err := strconv.ParseFloat(req.Brokerage2, 64)
|
|
//if err != nil {
|
|
// logger.Error("brokerage2 err:", err)
|
|
// app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
// return
|
|
//}
|
|
//memberDiscountFloat, err := strconv.ParseFloat(req.MemberDiscount, 64)
|
|
//if err != nil {
|
|
// logger.Error("member discount err:", err)
|
|
// app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
// return
|
|
//}
|
|
|
|
commodity := &models.ErpCommodity{
|
|
Name: req.Name,
|
|
ErpCategoryId: req.ErpCategoryId,
|
|
ErpCategoryName: "",
|
|
IMEIType: req.IMEIType,
|
|
ErpSupplierId: req.ErpSupplierId,
|
|
ErpSupplierName: "",
|
|
RetailPrice: req.RetailPrice,
|
|
MinRetailPrice: req.MinRetailPrice,
|
|
StaffCostPrice: req.StaffCostPrice + req.WholesalePrice,
|
|
WholesalePrice: req.WholesalePrice,
|
|
Brokerage1: brokerage1Float,
|
|
Brokerage2: brokerage2Float,
|
|
MemberDiscount: memberDiscountFloat,
|
|
Origin: req.Origin,
|
|
Remark: req.Remark,
|
|
}
|
|
commodity.ID = req.Id
|
|
err := commodity.SetErpCategory()
|
|
if err != nil {
|
|
//logger.Error("set erp category err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
commodity.IdInit()
|
|
|
|
var catCommodity models.ErpCommodity
|
|
//err = orm.Eloquent.Table("erp_commodity").Where("erp_category_id=?", req.Id).Limit(1).Find(&catCommodity).Error
|
|
err = orm.Eloquent.Table("erp_commodity").Where("id=?", req.Id).Find(&catCommodity).Error
|
|
if err != nil {
|
|
//logger.Error("cat erp commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
commodity.Number = catCommodity.Number
|
|
commodity.SerialNumber = catCommodity.SerialNumber
|
|
|
|
err = orm.Eloquent.Save(commodity).Error
|
|
if err != nil {
|
|
//logger.Error("create commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
|
|
app.OK(c, commodity, "")
|
|
return
|
|
}
|
|
|
|
type CommodityDelRequest struct {
|
|
ErpCommodityId uint32 `json:"erp_commodity_id" binding:"required"` // 商品id
|
|
}
|
|
|
|
// CommodityDel 删除商品
|
|
// @Summary 删除商品
|
|
// @Tags 商品资料
|
|
// @Produce json
|
|
// @Accept json
|
|
// @Param request body CommodityDelRequest true "删除商品模型"
|
|
// @Success 200 {object} app.Response
|
|
// @Router /api/v1/commodity/delete [post]
|
|
func CommodityDel(c *gin.Context) {
|
|
var req = new(CommodityDelRequest)
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
//logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
|
|
err := orm.Eloquent.Table("erp_commodity").Where("id=?", req.ErpCommodityId).Delete(&models.ErpCommodity{}).Error
|
|
if err != nil {
|
|
//logger.Error("erp commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "获取失败")
|
|
return
|
|
}
|
|
|
|
app.OK(c, nil, "删除成功")
|
|
return
|
|
}
|
|
|
|
// BatchImportView 导入商品资料预览
|
|
// @Summary 导入商品资料预览
|
|
// @Tags 商品资料
|
|
// @Produce json
|
|
// @Accept json
|
|
// @Param file body string true "上传excel文件"
|
|
// @Success 200 {array} models.CommodityExcel
|
|
// @Router /api/v1/commodity/import_commodity_view [post]
|
|
func CommodityImportView(c *gin.Context) {
|
|
file, header, err := c.Request.FormFile("file")
|
|
if err != nil {
|
|
//logger.Error("form file err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "预览失败")
|
|
return
|
|
}
|
|
|
|
readAll, err := io.ReadAll(file)
|
|
if err != nil {
|
|
//logger.Error("read all err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "预览失败")
|
|
return
|
|
}
|
|
|
|
fmt.Println("header:", header.Filename)
|
|
_, colsMap, err := models.FileExcelReader([]byte(readAll), nil)
|
|
if err != nil {
|
|
//logger.Error("file excel reader err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "预览失败")
|
|
return
|
|
}
|
|
fmt.Println("colsMap:", colsMap)
|
|
if len(colsMap) != 0 {
|
|
colsMap = colsMap[1:]
|
|
}
|
|
|
|
app.OK(c, &colsMap, "")
|
|
return
|
|
}
|