l_excel_export/README.md

5.3 KiB

安装

$ go get  gitea.cdlsxd.cn/self-tools/l_excel_export

使用

	func (s *GoodsService) ExportGoodsV2(ctx http.Context) error {
var (
pbRequest     pb.GetGoodsReqs
goodsIds      []int32
goodsMediaMap = make(map[int32][]string)
data          []map[string]interface{}
)
//fmt.Printf("begin----%s", time.Now().Format(time.TimeOnly))
request := ctx.Request().Body
body, err := io.ReadAll(request)
if err != nil {
return errors.New("参数解析失败")
}
err = sonic.Unmarshal(body, &pbRequest)
if err != nil {
return errors.New("参数解析失败")
}
reply, err := s.GoodsBiz.Range(ctx, &pbRequest)
if err != nil {
return err
}
if len(reply.Data) == 0 {
return errors.New("未获取到商品数据")
}
for _, v := range reply.Data {
goodsIds = append(goodsIds, v.Id)
}

bestGoodsSupplierMap, err := goods.NewGoodsSupplierRelationBiz(types.ToTmplConf(s.c)).GetGoodsBestSupplierInfo(ctx, goodsIds, 0, false)

mediaList, err := goods.NewGoodsMediaBiz(types.ToTmplConf(s.c)).Range(ctx, &pb.GetGoodsMediaListReqs{
Search: &pb.GoodsMediaSearch{GoodsIds: goodsIds},
})
if err != nil {
return err
}
for _, media := range mediaList.Data {

goodsMediaMap[media.GoodsId] = append(goodsMediaMap[media.GoodsId], media.Url)
}
//获取分类信息

cateMap, err := goods.NewGoodsCateGoryRelationBiz(types.ToTmplConf(s.c)).GoodsRelationMap(ctx, goodsIds)
if err != nil {
return err
}
cateNameMap := make(map[int32][]string, len(cateMap))
for k, v := range cateMap {
for _, cate := range v {
cateNameMap[k] = append(cateNameMap[k], cate.Name)
}
}

// 循环生成记录
for _, v := range reply.Data {
var (
cate      string
media     string
supplier  string
wareHouse string
costPrice float64
)
if _, exist := cateNameMap[v.Id]; exist {
cate = strings.Join(cateNameMap[v.Id], ",")
}
if _, exist := goodsMediaMap[v.Id]; exist {
media = strings.Join(goodsMediaMap[v.Id], ",")
}
if _, exist := bestGoodsSupplierMap[v.Id]; exist {
supplier = bestGoodsSupplierMap[v.Id].SupplierName
wareHouse = bestGoodsSupplierMap[v.Id].WarehouseName
costPrice = bestGoodsSupplierMap[v.Id].CostPrice
}
data = append(data, map[string]interface{}{
"0":  v.GoodsBarCode,
"1":  cate,
"2":  v.Title,
"3":  v.GoodsNum,
"4":  v.GoodsCode,
"5":  v.Brand,
"6":  v.Unit,
"7":  v.GoodsAttributes,
"8":  v.Introduction,
"9":  fmt.Sprintf("%d", v.SellByDate),
"10": v.SellByDateUnit,
"11": v.ExternalUrl,
"12": media,
"13": fmt.Sprintf("%.2f", v.ExternalPrice),
"14": fmt.Sprintf("%.2f", v.SalesPrice),
"15": fmt.Sprintf("%.2f", costPrice),
"16": fmt.Sprintf("%.2f", v.TaxRate),
"17": supplier,
"18": wareHouse,
"19": v.Remark,
"20": fmt.Sprintf("%.2f", v.Length),
"21": fmt.Sprintf("%.2f", v.Width),
"22": fmt.Sprintf("%.2f", v.Height),
"23": fmt.Sprintf("%.2f", v.Weight),
})
}
taskId, err := excel_export.NewExport(excel_export.WithJobName("goods_export")).Run(&excel_export.Config{FileName: "商品导出",
Data: data,
Ext:  excel_export.Xlsx,
Head: []*excel_export.FiledMapping{
{FieldName: "0", ColName: "条码"},
{FieldName: "1", ColName: "分类名称"},
{FieldName: "2", ColName: "货品名称"},
{FieldName: "3", ColName: "货品编号 "},
{FieldName: "4", ColName: "商品货号"},
{FieldName: "5", ColName: "品牌"},
{FieldName: "6", ColName: "单位 "},
{FieldName: "7", ColName: "规格参数"},
{FieldName: "8", ColName: "货品说明"},
{FieldName: "9", ColName: "保质期 "},
{FieldName: "10", ColName: "保质期单位"},
{FieldName: "11", ColName: "链接"},
{FieldName: "12", ColName: "货品图片 "},
{FieldName: "13", ColName: "电商销售价格"},
{FieldName: "14", ColName: "销售价"},
{FieldName: "15", ColName: "供应商报价 "},
{FieldName: "16", ColName: "税率"},
{FieldName: "17", ColName: "默认供应商"},
{FieldName: "18", ColName: "默认存放仓库 "},
{FieldName: "19", ColName: "备注"},
{FieldName: "20", ColName: "长"},
{FieldName: "21", ColName: "宽 "},
{FieldName: "22", ColName: "高"},
{FieldName: "23", ColName: "重量"},
},
SavePath: "./log/export/excel",
})

if err != nil {
return err
}
//fmt.Println(time.Now().Format(time.TimeOnly))
return ctx.Result(200, map[string]interface{}{
"task_id": taskId,
})
}

func (s *GoodsService) ExportTaskInfo(ctx http.Context) error {
job_name := ctx.Query().Get("job_name")
taskId := ctx.Query().Get("task_id")
if taskId == "" {
return errors.New("任务id不能为空")
}
if job_name == "" {
job_name = "goods_export"
}
info, err := excel_export.NewExport(excel_export.WithJobName(job_name)).TaskInfo(taskId)
if err != nil {
return err
}
return ctx.Result(200, info)
}

func (s *GoodsService) ExportTaskHIs(ctx http.Context) error {
job_name := ctx.Query().Get("job_name")
page := ctx.Query().Get("page")
limit := ctx.Query().Get("limit")
pageInt, err := strconv.ParseInt(page, 10, 64)
numInt, err := strconv.ParseInt(limit, 10, 64)
if err != nil {
return err
}
if job_name == "" {
job_name = "goods_export"
}
res, err := excel_export.NewExport(excel_export.WithJobName(job_name)).TaskHis(int(pageInt), int(numInt))
if err != nil {
return err
}
return ctx.Result(200, res)
}

func (s *GoodsService) ExportDownloadExcel(ctx http.Context) error {
job_name := ctx.Query().Get("job_name")
taskId := ctx.Query().Get("task_id")
if taskId == "" {
return errors.New("任务id不能为空")
}
if job_name == "" {
job_name = "goods_export"
}
return excel_export.NewExport(excel_export.WithJobName(job_name)).DownloadExcel(ctx.Response(), taskId)
}