Browse Source

chang excel output

zjxpcyc 6 years ago
parent
commit
36bddd289b
1 changed files with 55 additions and 78 deletions
  1. 55
    78
      controllers/goods/order.go

+ 55
- 78
controllers/goods/order.go View File

@@ -9,9 +9,6 @@ import (
9 9
 	"spaceofcheng/services/utils"
10 10
 	"strconv"
11 11
 	"time"
12
-
13
-	"github.com/astaxie/beego"
14
-	"github.com/tealeg/xlsx"
15 12
 )
16 13
 
17 14
 // GetOrderList 获取商品订单
@@ -154,94 +151,74 @@ func (c *GoodsController) GetOrdersByRecord() {
154 151
 		c.ResponseError(err)
155 152
 	}
156 153
 
157
-	file := xlsx.NewFile()
158
-	sheet, _ := file.AddSheet("Sheet1")
159
-	row := sheet.AddRow()
160
-	row.SetHeightCM(0.8) //设置每行的高度
161
-	cell := row.AddCell()
162
-	cell.Value = "归属案场"
163
-	cell = row.AddCell()
164
-	cell.Value = "商品分类"
165
-	cell = row.AddCell()
166
-	cell.Value = "商品名称"
167
-	cell = row.AddCell()
168
-	cell.Value = "商品规格"
169
-	cell = row.AddCell()
170
-	cell.Value = "下单人"
171
-	cell = row.AddCell()
172
-	cell.Value = "销售数量"
173
-	cell = row.AddCell()
174
-	cell.Value = "商品单价"
175
-	cell = row.AddCell()
176
-	cell.Value = "商品总价"
177
-	cell = row.AddCell()
178
-	cell.Value = "订单状态"
179
-	cell = row.AddCell()
180
-	cell.Value = "下单时间"
181
-	cell = row.AddCell()
182
-	cell.Value = "订单号"
154
+	excel, err := utils.NewTinyXLSXEngine()
155
+	if err != nil {
156
+		utils.LogError("初始化Excel服务失败: " + err.Error())
157
+		c.ResponseError(errors.New("初始化Excel服务失败"))
158
+	}
159
+
160
+	excel.SetCell(excel.InsertRow(), []string{
161
+		"归属案场",
162
+		"商品分类",
163
+		"商品名称",
164
+		"商品规格",
165
+		"下单人",
166
+		"销售数量",
167
+		"商品单价",
168
+		"商品总价",
169
+		"订单状态",
170
+		"下单时间",
171
+		"订单号",
172
+	})
173
+
183 174
 	var amount float64
184
-	amount = 0
185 175
 	for _, order := range orders {
186 176
 		for _, good := range order.Goods {
187
-			row := sheet.AddRow()
188
-			row.SetHeightCM(0.8) //设置每行的高度
189
-			cell := row.AddCell()
190
-			cell.Value = order.CaseName
191
-			cell = row.AddCell()
192
-			cell.Value = good.TypeName
193
-			cell = row.AddCell()
194
-			cell.Value = good.GoodsName
195
-			cell = row.AddCell()
196
-			cell.Value = good.SpecName
197
-			cell = row.AddCell()
198
-			cell.Value = order.UserName
199
-			cell = row.AddCell()
200
-			cell.Value = strconv.Itoa(good.Number)
201
-			cell = row.AddCell()
202
-			cell.Value = good.Price
203
-			cell = row.AddCell()
177
+			row := excel.InsertRow()
178
+			excel.SetCell(row, []string{
179
+				order.CaseName,
180
+				good.TypeName,
181
+				good.GoodsName,
182
+				good.SpecName,
183
+				order.UserName,
184
+				strconv.Itoa(good.Number),
185
+				good.Price,
186
+				strconv.Itoa(good.Number),
187
+			})
188
+
204 189
 			price, err := strconv.ParseFloat(good.Price, 64)
205 190
 			if err != nil {
206 191
 				c.ResponseError(errors.New("金额类型不正确!"))
207 192
 			}
208
-			num, err := strconv.ParseFloat(strconv.Itoa(good.Number), 64)
209
-			if err != nil {
210
-				c.ResponseError(errors.New("金额类型不正确!"))
211
-			}
212
-			amount = amount + price*num
213
-			cell.Value = strconv.FormatFloat(price*num, 'f', -1, 64)
214
-			cell = row.AddCell()
215
-			var status = "未完成"
193
+			// num, err := strconv.ParseFloat(strconv.Itoa(good.Number), 64)
194
+			// if err != nil {
195
+			// 	c.ResponseError(errors.New("金额类型不正确!"))
196
+			// }
197
+			charge := price * float64(good.Number)
198
+			amount = amount + charge
199
+
200
+			excel.SetCell(row, strconv.FormatFloat(charge, 'f', -1, 64))
201
+
202
+			status := "未完成"
216 203
 			if order.Status == models.STATUS_NORMAL {
217 204
 				status = "已完成"
218 205
 			}
219
-			cell.Value = status
220
-			cell = row.AddCell()
221
-			cell.Value = order.CreateDate.Format("2006-01-02 15:04:05")
222
-			cell = row.AddCell()
223
-			cell.Value = order.OrdersNo
206
+			excel.SetCell(row, status)
207
+			excel.SetCell(row, order.CreateDate.Format("2006-01-02 15:04:05"))
208
+			excel.SetCell(row, order.OrdersNo)
224 209
 		}
225 210
 	}
226
-	row = sheet.AddRow()
227
-	row.SetHeightCM(0.8)
228
-	cell = row.AddCell()
229
-	cell.Value = "总规定次数"
230
-	cell = row.AddCell()
231
-	cell.Value = strconv.Itoa(record.Num)
232
-	cell = row.AddCell()
233
-	cell.Value = "实际使用总数"
234
-	cell = row.AddCell()
235
-	cell.Value = strconv.Itoa(record.ActrualNum)
236
-	cell = row.AddCell()
237
-	cell.Value = "总价"
238
-	cell = row.AddCell()
239
-	cell.Value = strconv.FormatFloat(amount, 'f', -1, 64)
240
-
241
-	excelpath := beego.AppConfig.String("excelpath") + utils.GetGUID() + ".xlsx"
242
-	err = file.Save(excelpath)
243 211
 
244
-	c.ResponseJSON(orders)
212
+	excel.SetCell(excel.InsertRow(), []string{
213
+		"总规定次数",
214
+		strconv.Itoa(record.Num),
215
+		"实际使用总数",
216
+		strconv.Itoa(record.ActrualNum),
217
+		"总价",
218
+		strconv.FormatFloat(amount, 'f', -1, 64),
219
+	})
220
+
221
+	c.SaveToExcel("订单月记录.xlsx", excel)
245 222
 }
246 223
 
247 224
 // GetCustomerOrders 获取用户订单