import (
	'epur/model'
	'epur/pkg/decorator'
	'epur/pkg/logger'
	'epur/pkg/struct/adminStruct'
	'epur/pkg/utils'
	'fmt'
	'github.com/360EntSecGroup-Skylar/excelize'
	'reflect'
)

// 导出采购单Excel
func PostPurchaseListExport(a *decorator.ApiBase, data *adminStruct.ExportPurchaseOrderRequest) error {
	logger.AccessLogger.Info('PostPurchaseListExport....')
	res := adminStruct.ExportPurchaseOrderInfoResponse{}
	res.ResponseCommon = a.NewSuccessResponseCommon()

	var poids []int64
	for _, i := range data.Poid {
		poids = append(poids, i)
	}

	query := a.Ts.Table('purchase_order_info a').
		Select('a.arrival_status,a.reason,a.remark,s.sname,whi.wname,a.poid,u.user_name,'+
			'a.paid,p.logistics_id,a.verify_status,a.ctime,a.order_no,a.arrival_num,a.sum_num,'+
			'a.goods_sum_amt,a.pur_amt,a.other_amt,a.sum_amt,a.remark,whi.addr,whi.phone,whi.contacts').
		Joins('left join warehouse_info whi on a.wid = whi.wid').
		Joins('left join purchase_order_list b on a.poid=b.poid').
		Joins(`left join goods_base g on g.gid=b.gid`).
		Joins(`left join goods_sku gs on gs.gsid = b.skuid`).
		Joins(`left join supplier_base s on s.sid=a.sid`).
		Joins(`left join user_base u on u.uid=a.cuid`).
		Joins('left join purchase_waybill p on p.poid=a.poid').
		Group('a.poid').
		Where('a.poid in ? ', poids)

	utils.Error(query.Limit(a.Size).
		Offset(a.Offset).
		Find(&res.Data).Error)

	g := []adminStruct.GoodsInfo2{}
	utils.Error(a.Ts.Table('purchase_order_list b').
		Select('b.poid,gs.sku,b.price,b.num,g.gname,g.image,b.polid,gs.specs').
		Joins(`left join goods_base g on g.gid=b.gid`).
		Joins(`left join goods_sku gs on gs.gsid = b.skuid`).
		Where('b.poid in ? and b.status=?', poids, model.Normal).
		Limit(a.Size).
		Offset(a.Offset).
		Find(&g).Error)
	for i := range res.Data {
		p := &res.Data[i]
		for _, i2 := range g {
			if i2.Poid == p.Poid {
				p.Detail = append(p.Detail, i2)
			}
		}
	}

	w := []adminStruct.PurchaseWaybillList{}
	utils.Error(a.Ts.Table('purchase_waybill').
		Select('poid,logistics_firm,logistics_id').
		Where('poid in ?', poids).
		Find(&w).Error)
	for i := range res.Data {
		p := &res.Data[i]
		for _, i2 := range w {
			if i2.Poid == p.Poid {
				p.WayDetail = append(p.WayDetail, i2)
			}
		}
	}

	for index, item := range res.Data {
		res.Data[index].StatusText = GetStatusText(item.VerifyStatus, item.ArrivalStatus)
	}

	// 定义Excel表格列名和对应的字段名
	var columns = []string{'采购单号', '状态文本', '供货商', '仓库', '贷款(元)', '运费(元)', '其他费用(元)', '应付(应付=货款+运费+其他)', '已付(元)', '商品SKU', '商品图片',
		'规格', '单价(元)', '采购数量', '到货数量', '小计', '运单信息', '收货地址', '采购备注', '创建时间', '采购员'}
	var fields = []string{'OrderNo', 'StatusText', 'Sname', 'Wname', 'GoodsSumAmt', 'PurAmt', 'OtherAmt', 'SumAmt', 'Paid', 'Sku', 'Image', 'Specs', 'Price', 'Num', 'ArrivalNum', 'Amt', 'SourcePlatform,LogisticsId,LogisticsFirm', 'Contacts,Phone,Addr', 'Remark,Reason', 'Ctime', 'UserName'}

	// 创建Excel表格并设置列名
	file := excelize.NewFile()
	sheetName := '采购单列表'
	index := file.NewSheet(sheetName)
	file.SetActiveSheet(index)
	for i, col := range columns {
		char := 'A' + i
		file.SetCellValue(sheetName, fmt.Sprintf('%c1', char), col)
	}

	// 将采购单数据写入Excel表格
	for i, item := range res.Data {
		rowIndex := i + 2
		for j, field := range fields {
			char := 'A' + j
			if field == 'SourcePlatform,LogisticsId,LogisticsFirm' {
				for _, info2 := range item.WayDetail {
					sourcePlatform := item.SourcePlatform
					logisticsId := info2.LogisticsId
					logisticsFirm := info2.LogisticsFirm
					value := fmt.Sprintf('%s(%s)(%s)', sourcePlatform, logisticsId, logisticsFirm)
					file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)
					rowIndex++
				}
			} else if field == 'Contacts,Phone,Addr' {
				contacts := item.Contacts
				phone := item.Phone
				addr := item.Addr
				value := fmt.Sprintf('%s(%s)(%s)', contacts, phone, addr)
				file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)
			} else if field == 'Remark,Reason' {
				remark := item.Remark
				reason := item.Reason
				value := fmt.Sprintf('%s(%s)', remark, reason)
				file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)

			} else if field == 'Sku' {
				// 遍历商品详情列表,将每个商品的SKU写入Excel表格
				for _, info2 := range item.Detail {
					value := info2.Sku
					file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)
					rowIndex++
				}
			} else if field == 'Image' {
				// 遍历商品详情列表,将每个商品的图片写入Excel表格
				for _, info2 := range item.Detail {
					value := info2.Image
					file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)

				}
			} else if field == 'Specs' {
				// 遍历商品详情列表,将每个商品的图片写入Excel表格
				for _, info2 := range item.Detail {
					value := info2.Specs
					file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)

				}
			} else if field == 'Price' {
				// 遍历商品详情列表,将每个商品的图片写入Excel表格
				for _, info2 := range item.Detail {
					value := info2.Price
					file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)

				}
			} else if field == 'Num' {
				// 遍历商品详情列表,将每个商品的图片写入Excel表格
				for _, info2 := range item.Detail {
					value := info2.Num
					file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)

				}
			} else if field == 'Amt' {
				// 遍历商品详情列表,将每个商品的图片写入Excel表格
				for _, info2 := range item.Detail {
					value := info2.Amt
					file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)

				}
			} else {
				// 其他字段直接获取值并写入Excel表格
				//if field == 'Sku' || field == 'Image' || field == 'Specs' || field == 'Price' || field == 'Num' || field == 'Amt' {
				//	for _, info2 := range item.Detail {
				//		sku := info2.Sku
				//		image := info2.Image
				//		specs := info2.Specs
				//		price := info2.Price
				//		num := info2.Num
				//		value := fmt.Sprintf('%s(%s)(%s)(%d)(%d)', sku, image, specs, price, num)
				//		//file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), sku)
				//		//file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), image)
				//		//file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), specs)
				//		//file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), price)
				//		//file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), num)
				//		file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)
				//		rowIndex++
				//	}
				//
				//}

				value := reflect.ValueOf(item).FieldByName(field).Interface()
				file.SetCellValue(sheetName, fmt.Sprintf('%c%d', char, rowIndex), value)

			}
		}
	}

	// 保存Excel文件
	err := file.SaveAs('采购单列表.xlsx')
	if err != nil {
		return err
	}

	return nil
}

导出excel

**内容:**该函数实现了根据传入的采购单号列表,将采购单数据导出为Excel表格的功能。具体实现过程如下:

  1. 从请求参数中获取采购单号列表,构造查询条件。

  2. 使用GORM进行查询,获取采购单数据。

  3. 使用GORM进行查询,获取采购单对应的商品详情列表。

  4. 使用GORM进行查询,获取采购单对应的运单信息。

  5. 将查询到的数据写入Excel表格中,最终保存为文件。

在写入Excel表格时,需要根据不同的字段进行特殊处理,例如将多个字段拼接在一起,或者将多个商品详情写入同一行。

采购单导出功能:将采购单数据导出为Excel表格

原文地址: https://www.cveoy.top/t/topic/nj2J 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录