2023-11-08 03:51:33 +00:00
|
|
|
|
package basic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-11-10 09:49:44 +00:00
|
|
|
|
"go-admin/app/admin/middleware"
|
2023-11-08 03:51:33 +00:00
|
|
|
|
"go-admin/app/admin/models"
|
|
|
|
|
orm "go-admin/common/global"
|
|
|
|
|
"go-admin/tools/app"
|
2023-11-09 10:26:19 +00:00
|
|
|
|
"io"
|
2023-11-08 03:51:33 +00:00
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CommodityCreateRequest struct {
|
2024-01-06 08:48:35 +00:00
|
|
|
|
Name string `json:"name" binding:"required"` // 商品名称
|
|
|
|
|
ErpCategoryId uint32 `json:"erp_category_id" binding:"required"` // 商品分类id
|
|
|
|
|
IsIMEI uint32 `json:"is_imei" binding:"required"` // 是否串码:1-串码类 2-非串码
|
|
|
|
|
ErpBarcode string `json:"erp_barcode"` // 商品条码
|
|
|
|
|
IMEIType uint32 `json:"imei_type"` // 系统生成串码: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 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)"` // 备注
|
2023-11-08 03:51:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CommodityCreate 新增商品
|
|
|
|
|
// @Summary 新增商品
|
|
|
|
|
// @Tags 商品资料
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Param request body CommodityCreateRequest true "商品新增模型"
|
|
|
|
|
// @Success 200 {object} models.ErpCommodity
|
|
|
|
|
// @Router /api/v1/commodity/create [post]
|
2023-11-16 10:09:11 +00:00
|
|
|
|
// 规则:商品名称/商品编号相同则为同一商品,创建的时候由于商品编号不重复,无需判断
|
2023-11-08 03:51:33 +00:00
|
|
|
|
func CommodityCreate(c *gin.Context) {
|
|
|
|
|
var req = new(CommodityCreateRequest)
|
|
|
|
|
|
2024-01-06 08:48:35 +00:00
|
|
|
|
err := c.ShouldBindJSON(&req)
|
|
|
|
|
if err != nil {
|
2023-11-08 03:51:33 +00:00
|
|
|
|
fmt.Println(err.Error())
|
2024-01-06 08:48:35 +00:00
|
|
|
|
app.Error(c, http.StatusBadRequest, errors.New("param err"), "参数错误:"+err.Error())
|
2023-11-08 03:51:33 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-11-17 10:07:01 +00:00
|
|
|
|
|
2024-01-06 08:48:35 +00:00
|
|
|
|
//brokerage1Float, err := models.StringToFloat(req.Brokerage1)
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// //logger.Error("brokerage1 err:", err)
|
|
|
|
|
// app.Error(c, http.StatusBadRequest, errors.New("param err"), "参数错误")
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//brokerage2Float, err := models.StringToFloat(req.Brokerage2)
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// //logger.Error("brokerage1 err:", err)
|
|
|
|
|
// app.Error(c, http.StatusBadRequest, errors.New("param err"), "参数错误")
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//memberDiscountFloat, err := models.StringToFloat(req.MemberDiscount)
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// //logger.Error("brokerage1 err:", err)
|
|
|
|
|
// app.Error(c, http.StatusBadRequest, errors.New("param err"), "参数错误")
|
|
|
|
|
// return
|
|
|
|
|
//}
|
2023-11-17 10:07:01 +00:00
|
|
|
|
|
2024-01-06 08:48:35 +00:00
|
|
|
|
barCode := ""
|
|
|
|
|
if req.ErpBarcode != "" { // 条码不为空则校验
|
|
|
|
|
barCode, err = models.CheckAndConvertBarcode(req.ErpBarcode)
|
|
|
|
|
if err != nil {
|
|
|
|
|
app.Error(c, http.StatusBadRequest, errors.New("param err"), "参数错误:"+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else { // 条码为空则自动生成
|
|
|
|
|
barCode, err = models.GenerateBarcode(req.ErpCategoryId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
app.Error(c, http.StatusBadRequest, errors.New("gen barcode err"), err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-11-08 03:51:33 +00:00
|
|
|
|
}
|
2023-11-17 10:07:01 +00:00
|
|
|
|
|
2024-01-06 08:48:35 +00:00
|
|
|
|
brokerage1Float := req.Brokerage1
|
|
|
|
|
brokerage2Float := req.Brokerage2
|
|
|
|
|
memberDiscountFloat := req.MemberDiscount
|
2023-11-17 10:07:01 +00:00
|
|
|
|
|
2023-12-21 09:36:13 +00:00
|
|
|
|
if models.IsExistingProduct(req.Name) {
|
|
|
|
|
app.Error(c, http.StatusBadRequest, errors.New("param err"), "商品名称已存在,不能重复")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-24 09:09:24 +00:00
|
|
|
|
if req.IsIMEI == 2 { // 是否串码:1-串码类 2-非串码
|
|
|
|
|
req.IMEIType = 1 // 系统生成串码:2-是(系统生成) 3-否(手动添加) 1表示非串码
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 03:51:33 +00:00
|
|
|
|
commodity := &models.ErpCommodity{
|
|
|
|
|
Number: 1,
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
ErpCategoryId: req.ErpCategoryId,
|
|
|
|
|
ErpCategoryName: "",
|
2024-01-06 08:48:35 +00:00
|
|
|
|
ErpBarcode: barCode,
|
2023-11-08 03:51:33 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
2024-01-06 08:48:35 +00:00
|
|
|
|
err = commodity.IdInit()
|
|
|
|
|
if err != nil {
|
|
|
|
|
app.Error(c, http.StatusInternalServerError, err, "创建失败:"+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-11-08 03:51:33 +00:00
|
|
|
|
|
2023-11-10 09:49:44 +00:00
|
|
|
|
commodity.SerialNumber, err = models.GenerateSerialNumber(req.ErpCategoryId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
app.Error(c, http.StatusInternalServerError, err, err.Error())
|
2023-11-08 03:51:33 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = orm.Eloquent.Create(commodity).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
//logger.Error("create commodity err:", err)
|
|
|
|
|
app.Error(c, http.StatusInternalServerError, err, "创建失败")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 10:07:01 +00:00
|
|
|
|
app.OK(c, commodity, "OK")
|
2023-11-08 03:51:33 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-24 09:09:24 +00:00
|
|
|
|
if commodity.IMEIType == 1 { //无串码
|
|
|
|
|
commodity.IsIMEI = 2 // 非串码
|
|
|
|
|
} else {
|
|
|
|
|
commodity.IsIMEI = 1 // 串码
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 03:51:33 +00:00
|
|
|
|
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
|
2024-01-06 08:48:35 +00:00
|
|
|
|
ErpBarcode string `json:"erp_barcode"` // 商品条码
|
2023-11-09 10:26:19 +00:00
|
|
|
|
IMEIType uint32 `json:"imei_type" binding:"required"` // 1-无串码 2-串码(系统生成) 3-串码(手动添加)
|
2023-11-08 03:51:33 +00:00
|
|
|
|
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"` // 指导采购价
|
2023-11-09 10:26:19 +00:00
|
|
|
|
Brokerage1 float64 `json:"brokerage_1"` // 销售毛利提成
|
2023-11-08 03:51:33 +00:00
|
|
|
|
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)
|
|
|
|
|
|
2024-01-06 08:48:35 +00:00
|
|
|
|
err := c.ShouldBindJSON(&req)
|
|
|
|
|
if err != nil {
|
2023-11-08 03:51:33 +00:00
|
|
|
|
//logger.Error(err)
|
2024-01-06 08:48:35 +00:00
|
|
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误:"+err.Error())
|
2023-11-08 03:51:33 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-01-06 08:48:35 +00:00
|
|
|
|
barCode := ""
|
|
|
|
|
if req.ErpBarcode != "" { // 条码不为空则校验
|
|
|
|
|
barCode, err = models.CheckAndConvertBarcode(req.ErpBarcode)
|
|
|
|
|
if err != nil {
|
|
|
|
|
app.Error(c, http.StatusBadRequest, errors.New("param err"), "参数错误:"+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else { // 条码为空则自动生成
|
|
|
|
|
barCode, err = models.GenerateBarcode(req.ErpCategoryId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
app.Error(c, http.StatusBadRequest, errors.New("gen barcode err"), err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 03:51:33 +00:00
|
|
|
|
brokerage1Float := req.Brokerage1
|
|
|
|
|
brokerage2Float := req.Brokerage2
|
|
|
|
|
memberDiscountFloat := req.MemberDiscount
|
|
|
|
|
|
|
|
|
|
commodity := &models.ErpCommodity{
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
ErpCategoryId: req.ErpCategoryId,
|
|
|
|
|
ErpCategoryName: "",
|
2024-01-06 08:48:35 +00:00
|
|
|
|
ErpBarcode: barCode,
|
2023-11-08 03:51:33 +00:00
|
|
|
|
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
|
2024-01-06 08:48:35 +00:00
|
|
|
|
err = commodity.SetErpCategory()
|
2023-11-08 03:51:33 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 01:48:27 +00:00
|
|
|
|
// todo 同步更新库存表和库存商品表的"指导零售价"和"最低零售价"
|
|
|
|
|
|
2023-11-08 03:51:33 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
2023-11-09 10:26:19 +00:00
|
|
|
|
|
2023-11-10 09:49:44 +00:00
|
|
|
|
// CommodityImportView 导入商品资料预览
|
2023-11-09 10:26:19 +00:00
|
|
|
|
// @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
|
|
|
|
|
}
|
2023-11-10 09:49:44 +00:00
|
|
|
|
|
|
|
|
|
// CommodityImport 导入商品资料
|
|
|
|
|
// @Summary 导入商品资料
|
|
|
|
|
// @Tags 商品资料
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Param file body string true "上传excel文件"
|
|
|
|
|
// @Success 200 {object} app.Response
|
|
|
|
|
// @Router /api/v1/commodity/import_commodity [post]
|
|
|
|
|
func CommodityImport(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.FileExcelImport([]byte(readAll), nil, 2)
|
|
|
|
|
if err != nil {
|
|
|
|
|
//logger.Error("file excel reader err:", err)
|
|
|
|
|
app.Error(c, http.StatusInternalServerError, err, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("colsMap:", colsMap)
|
|
|
|
|
if len(colsMap) != 0 {
|
|
|
|
|
colsMap = colsMap[1:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = models.ImportCommodityData(colsMap, middleware.GetCooperativeBusinessId(c))
|
|
|
|
|
if err != nil {
|
|
|
|
|
//logger.Error("file excel reader err:", err)
|
|
|
|
|
app.Error(c, http.StatusInternalServerError, err, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.OK(c, nil, "导入成功")
|
|
|
|
|
return
|
|
|
|
|
}
|