feat: export excel file

This commit is contained in:
zchengo
2022-12-27 19:18:58 +08:00
parent f11313797b
commit 2235516232
18 changed files with 418 additions and 46 deletions
+42
View File
@@ -1,9 +1,11 @@
package service
import (
"crm/common"
"crm/global"
"crm/models"
"crm/response"
"strconv"
"time"
)
@@ -85,3 +87,43 @@ func (p *ProductService) QueryInfo(param *models.ProductQueryParam) (*models.Pro
}
return &productInfo, response.ErrCodeSuccess
}
// 导出Excel文件
func (p *ProductService) Export(uid int64) (string, int) {
products := make([]models.Product, 0)
err := global.Db.Where("creator = ?", uid).Find(&products).Error
if err != nil {
return StringNull, response.ErrCodeFileExportFailed
}
excelRows := make([]models.ProductExcelRow, 0)
var row models.ProductExcelRow
for _, p := range products {
row.Name = p.Name
if p.Status == 1 {
row.Status = "已上架"
}
if p.Status == 2 {
row.Status = "已下架"
}
if p.Type == 1 {
row.Type = "默认"
}
row.Unit = p.Unit
row.Code = p.Code
row.Price = p.Price
row.Description = p.Description
row.Created = time.Unix(p.Created, 0).Format("2006-01-02")
if p.Updated != 0 {
row.Updated = time.Unix(p.Updated, 0).Format("2006-01-02")
}
excelRows = append(excelRows, row)
}
sheet := "产品信息"
columns := []string{"名称", "是否上下架", "类型", "单位", "编码", "价格", "描述", "创建时间", "更新时间"}
fileName := "product_" + strconv.FormatInt(uid, 10)
file, err := common.GenExcelFile(sheet, columns, excelRows, fileName)
if err != nil {
return StringNull, response.ErrCodeFileExportFailed
}
return file, response.ErrCodeSuccess
}