package com.xmomen.module.export.service;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xmomen.module.base.service.ExpressService;
import com.xmomen.module.order.model.OrderModel;
import com.xmomen.module.order.model.OrderQuery;
import com.xmomen.module.order.model.PurchaseModel;
import com.xmomen.module.order.service.PurchaseService;


@Service
public class ExportService {
	@Autowired
	PurchaseService purchaseService;
	@Autowired
	ExpressService expressService;

	// 导出采购单Excel
	public XSSFWorkbook exportPurchaseExcel(String templatePath){
		XSSFWorkbook workbook = new XSSFWorkbook();
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();     
		try {
			// 读取模板文件
			workbook = new XSSFWorkbook(new FileInputStream(templatePath));
			// 获取第一个sheet
			XSSFSheet sheet = workbook.getSheet("Sheet1");
			 Map param = new HashMap();
	         param.put("purchaseStatus", 0);
			// 获取采购单列表
			List<PurchaseModel> purchaseModels  = purchaseService.getPurchaseList(param);
			// 遍历采购单列表
			for(int i=0,size=purchaseModels.size();i<size;i++){
				PurchaseModel purchaseModel =purchaseModels.get(i);
				// 创建新行
				XSSFRow row = sheet.createRow(i+1);
				// 创建采购单号单元格
				XSSFCell purchaseNoCell = row.createCell(0);
				// 设置采购单号
				purchaseNoCell.setCellValue(purchaseModel.getPurchaseCode());
				// 创建商品名称单元格
				XSSFCell itemNameCell = row.createCell(1);;
				// 设置商品名称
				itemNameCell.setCellValue(purchaseModel.getItemName());
				// 创建商品数量单元格
				XSSFCell totalItemQtyCell = row.createCell(2);;
				// 设置商品数量
				totalItemQtyCell.setCellValue(purchaseModel.getTotalItemQty()+"");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(byteArrayOut!=null)
					byteArrayOut.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	
		return workbook;
	}

	// 导出取货单Excel
	public XSSFWorkbook exportTakeDeliveryExcel(String templatePath,OrderQuery query){
		XSSFWorkbook workbook = new XSSFWorkbook();
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();     
		try {
			// 读取模板文件
			workbook = new XSSFWorkbook(new FileInputStream(templatePath));
			// 获取第一个sheet
			XSSFSheet sheet = workbook.getSheet("Sheet1");
			// 获取订单列表
			List<OrderModel> takeDeliverys  =  expressService.getTakeDeliveryList(query);
			// 遍历订单列表
			for(int i=0,size=takeDeliverys.size();i<size;i++){
				OrderModel orderModel =takeDeliverys.get(i);
				// 创建新行
				XSSFRow row = sheet.createRow(i+1);
				// 创建订单号单元格
				XSSFCell orderNoCell = row.createCell(0);
				// 设置订单号
				orderNoCell.setCellValue(orderModel.getOrderNo());
				// 创建快递名称单元格
				XSSFCell expressNameCell = row.createCell(1);
				// 设置快递名称
				expressNameCell.setCellValue(orderModel.getExpressName());
				// 创建收货人姓名单元格
				XSSFCell consigneeNameCell = row.createCell(2);
				// 设置收货人姓名
				consigneeNameCell.setCellValue(orderModel.getConsigneeName());
				// 创建收货人电话单元格
				XSSFCell consigneePhoneCell = row.createCell(3);
				// 设置收货人电话
				consigneePhoneCell.setCellValue(orderModel.getConsigneePhone());
				// 创建收货人地址单元格
				XSSFCell consigneeAddressCell = row.createCell(4);
				// 设置收货人地址
				consigneeAddressCell.setCellValue(orderModel.getConsigneeAddress());
				// 创建预约时间单元格
				XSSFCell appointmentTimeCell = row.createCell(5);
				// 设置预约时间
				SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
				appointmentTimeCell.setCellValue(sFormat.format(orderModel.getAppointmentTime()));
				// 创建备注单元格
				XSSFCell remarkCell = row.createCell(6);
				// 设置备注
				remarkCell.setCellValue(orderModel.getRemark());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(byteArrayOut!=null)
					byteArrayOut.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	
		return workbook;
	}
}
