You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.1 KiB

package supply
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
"path/filepath"
"recook/internal/libs/bean"
"recook/internal/static_path"
"recook/internal/v2/lib/back"
"recook/internal/v2/lib/excel"
"recook/internal/v2/logic/third"
manage "recook/internal/v2/model/third"
"recook/tools"
"strconv"
)
type Sku struct {
}
type argsSupplyLists struct {
third.SupplySearch
bean.Page
}
// Lists @Title 供应链商品列表
func (s *Sku) Lists(c *gin.Context) {
args := argsSupplyLists{}
err := tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
lists, total := third.SupplyLogic.Lists(args.SupplySearch, args.Page)
back.Suc(c, "操作成功", bean.ResultLists{
List: lists,
Total: int(total),
})
}
type argsSupplyDetail struct {
SupplySkuId uint `binding:"required"`
}
// Detail @Title 商品详情
func (s *Sku) Detail(c *gin.Context) {
args := argsSupplyDetail{}
err := tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
detail, err := third.SupplyLogic.Detail(args.SupplySkuId)
if err != nil {
back.Fail(c, err.Error())
return
}
back.Suc(c, "操作成功", detail)
}
// Export @Title 导出商品清单
func (s *Sku) Export(c *gin.Context) {
lists, _ := third.SupplyLogic.Lists(third.SupplySearch{
Status: 1,
IsExport: true,
}, bean.Page{})
file := excelize.NewFile()
// 导出excel处理
sheetName := file.GetSheetName(0)
// 设置表头
excel.Excel.SetRow(file, sheetName, 1, []excel.Row{
{Value: "供应商Sku编码"},
{Value: "商品名称"},
{Value: "供应商状态"},
{Value: "分类"},
{Value: "品牌"},
{Value: "采购价"},
{Value: "官方指导价"},
{Value: "导入售价"},
{Value: "导入商品券"},
})
for key, list := range lists {
status := "上架"
if list.SupplyStatus == manage.RecookThirdPartySupplySupplyStatusDown {
status = "下架"
}
excel.Excel.SetRow(file, sheetName, key+2, []excel.Row{
{Value: list.SupplySkuId},
{Value: list.Name},
{Value: status},
{Value: fmt.Sprintf("%s>%s>%s", list.FirstCategoryName, list.SecondCategoryName, list.ThirdCategoryName)},
{Value: list.BrandName},
{Value: list.Price},
{Value: list.GuidePrice},
})
}
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename="+"result.xlsx")
c.Header("Content-Transfer-Encoding", "binary")
_ = file.Write(c.Writer)
}
type argsSupplyImport struct {
Path string `binding:"required"`
}
type errItem struct {
Col int `json:"col"`
Msg string `json:"msg"`
}
// Import @Title 导入处理结果
func (s *Sku) Import(c *gin.Context) {
args := argsSupplyImport{}
err := tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
file, err := excelize.OpenFile(filepath.Join(static_path.Dir.Root, args.Path))
if err != nil {
back.Fail(c, "上传文件错误")
return
}
rows, err := file.GetRows(file.GetSheetName(0))
if err != nil {
back.Fail(c, "上传文件错误")
return
}
if len(rows) <= 1 {
back.Fail(c, "上传文件为空")
return
}
result := []errItem{}
for i, row := range rows {
if len(row) < 9 {
result = append(result, errItem{
Col: i + 1,
Msg: "无数据",
})
continue
}
supplySkuId, err := strconv.Atoi(row[0])
if err != nil {
result = append(result, errItem{
Col: i + 1,
Msg: "供应商Sku编码错误",
})
continue
}
discountPrice, err := decimal.NewFromString(row[7])
if err != nil {
result = append(result, errItem{
Col: i + 1,
Msg: "售价错误",
})
continue
}
coupon, err := decimal.NewFromString(row[8])
if err != nil {
result = append(result, errItem{
Col: i + 1,
Msg: "商品券错误",
})
continue
}
err = third.SupplyLogic.Adopt(third.AdoptSkuInfo{
SupplySkuId: uint(supplySkuId),
DiscountPrice: discountPrice,
Coupon: coupon,
})
if err != nil {
result = append(result, errItem{
Col: i + 1,
Msg: err.Error(),
})
continue
}
}
back.Suc(c, "操作成功", result)
}