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
+43 -1
View File
@@ -1,9 +1,11 @@
package service
import (
"crm/common"
"crm/global"
"crm/models"
"crm/response"
"strconv"
"time"
)
@@ -97,7 +99,7 @@ func (c *ContractService) QueryInfo(param *models.ContractQueryParam) (*models.C
}
// 在编辑合同中,添加产品后,返回已添加的产品列表
func (p *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*models.Products, int) {
func (c *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*models.Products, int) {
if param.Id == 0 {
products := make([]*models.Products, 0)
if err := global.Db.Table(PRODUCT).Find(&products, param.Pids).Error; err != nil {
@@ -141,3 +143,43 @@ func (p *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*model
addedProductList = append(addedProductList, products...)
return addedProductList, response.ErrCodeSuccess
}
// 导出Excel文件
func (c *ContractService) Export(uid int64) (string, int) {
contracts := make([]models.ContractList, 0)
s := "contract.id, contract.name, contract.amount, contract.begin_time, contract.over_time, customer.name as cname, contract.remarks, contract.status, contract.created, contract.updated"
j := "left join customer on contract.cid = customer.id and contract.creator = ?"
err := global.Db.Table(CONTRACT).Select(s).Joins(j, uid).Scan(&contracts).Error
if err != nil {
return StringNull, response.ErrCodeFailed
}
excelRows := make([]models.ContractExcelRow, 0)
var row models.ContractExcelRow
for _, c := range contracts {
row.Name = c.Name
row.Cname = c.Cname
row.Amount = c.Amount
row.BeginTime = c.BeginTime
row.OverTime = c.OverTime
row.Remarks = c.Remarks
if c.Status == 1 {
row.Status = "已签约"
}
if c.Status == 2 {
row.Status = "未签约"
}
row.Created = time.Unix(c.Created, 0).Format("2006-01-02")
if c.Updated != 0 {
row.Updated = time.Unix(c.Updated, 0).Format("2006-01-02")
}
excelRows = append(excelRows, row)
}
sheet := "合同信息"
columns := []string{"合同名称", "客户名称", "合同金额", "合同开始时间", "合同结束时间", "备注", "签约状态", "创建时间", "更新时间"}
fileName := "contract_" + strconv.FormatInt(uid, 10)
file, err := common.GenExcelFile(sheet, columns, excelRows, fileName)
if err != nil {
return StringNull, response.ErrCodeFailed
}
return file, response.ErrCodeSuccess
}
+43
View File
@@ -1,9 +1,11 @@
package service
import (
"crm/common"
"crm/global"
"crm/models"
"crm/response"
"strconv"
"time"
)
@@ -102,3 +104,44 @@ func (c *CustomerService) QueryOption(uid int64) ([]*models.CustomerOption, int)
}
return option, response.ErrCodeSuccess
}
// 导出Excel文件
func (c *CustomerService) Export(uid int64) (string, int) {
customers := make([]models.Customer, 0)
err := global.Db.Where("creator = ?", uid).Find(&customers).Error
if err != nil {
return StringNull, response.ErrCodeFailed
}
excelRows := make([]models.CustomerExcelRow, 0)
var row models.CustomerExcelRow
for _, c := range customers {
row.Name = c.Name
row.Source = c.Source
row.Phone = c.Phone
row.Email = c.Email
row.Industry = c.Industry
row.Level = c.Level
row.Remarks = c.Remarks
row.Region = c.Region
row.Address = c.Address
if c.Status == 1 {
row.Status = "已签约"
}
if c.Status == 2 {
row.Status = "未签约"
}
row.Created = time.Unix(c.Created, 0).Format("2006-01-02")
if c.Updated != 0 {
row.Updated = time.Unix(c.Updated, 0).Format("2006-01-02")
}
excelRows = append(excelRows, row)
}
sheet := "客户信息"
columns := []string{"名称", "客户来源", "手机号", "邮箱", "客户行业", "客户级别", "备注", "地区", "详细地址", "成交状态", "创建时间", "更新时间"}
fileName := "customer_" + strconv.FormatInt(uid, 10)
file, err := common.GenExcelFile(sheet, columns, excelRows, fileName)
if err != nil {
return StringNull, response.ErrCodeFailed
}
return file, response.ErrCodeSuccess
}
+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
}