package com.qst.dms.service;

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

import com.qst.dms.db.DBUtil;
import com.qst.dms.entity.*;

public class TransportService {
	// 物流数据采集
	public Transport inputTransport() {
		Transport trans = null;

		// 建立一个从键盘接收数据的扫描器
		Scanner scanner = new Scanner(System.in);
		try {
			// 提示用户输入ID标识
			System.out.println("请输入ID标识：");
			// 接收键盘输入的整数
			int id = scanner.nextInt();
			// 获取当前系统时间
			Date nowDate = new Date();
			// 提示用户输入地址
			System.out.println("请输入地址：");
			// 接收键盘输入的字符串信息
			String address = scanner.next();
			// 数据状态是“采集”
			int type = DataBase.GATHER;

			// 提示用户输入登录用户名
			System.out.println("请输入货物经手人：");
			// 接收键盘输入的字符串信息
			String handler = scanner.next();
			// 提示用户输入主机IP
			System.out.println("请输入 收货人:");
			// 接收键盘输入的字符串信息
			String reciver = scanner.next();
			// 提示用于输入物流状态
			System.out.println("请输入物流状态：1发货中，2送货中，3已签收");
			// 接收物流状态
			int transportType = scanner.nextInt();
			// 创建物流信息对象
			trans = new Transport(id, nowDate, address, type, handler, reciver,
					transportType);
		} catch (Exception e) {
			System.out.println("采集的日志信息不合法");
		}
		// 返回物流对象
		return trans;
	}

	// 物流信息输出
	public void showTransport(Transport... transports) {
		for (Transport e : transports) {
			if (e != null) {
				System.out.println(e.toString());
			}
		}
	}

	// 匹配的物流信息输出，可变参数
	public void showMatchTransport(MatchedTransport... matchTrans) {
		for (MatchedTransport e : matchTrans) {
			if (e != null) {
				System.out.println(e.toString());
			}
		}
	}

	// 匹配的物流信息输出，参数是集合
	public void showMatchTransport(ArrayList<MatchedTransport> matchTrans) {
		for (MatchedTransport e : matchTrans) {
			if (e != null) {
				System.out.println(e.toString());
			}
		}
	}
	
	public ArrayList<MatchedTransport>readMatchedTransportFromDB() throws SQLException
	{
		ArrayList<MatchedTransport>matchedTransports = new ArrayList<>();
		DBUtil db = new DBUtil();
		try
		{
			db.getConnection();
			String sql = "select s.id,s.time,s.address,s.type,s.handler,s.reciver,s.transporttype,"+
					"t.id,t.time,t.address,t.type,t.handler,t.reciver,t.transporttype,"+
					"r.id,r.time,r.address,r.type,r.handler,r.reciver,r.transporttype "+
					"from matched_transport m,gather_transport s,gather_transport t,gather_transport r " +
					"where m.sendid=s.id AND m.transid=t.id AND m.receiveid=r.id;";
			ResultSet resultSet = db.executeQuery(sql,null);
			while(resultSet.next())
			{
				Object[] temp = new Object[7];
				for(int i=1;i<=7;i++)
					temp[i-1] = resultSet.getObject(i);
				Transport send = Transport.get_Transport(temp);
				for(int i=8;i<=14;i++)
					temp[i-8] = resultSet.getObject(i);
				Transport trans = Transport.get_Transport(temp);
				for(int i=15;i<=21;i++)
					temp[i-15] = resultSet.getObject(i);
				Transport receive = Transport.get_Transport(temp);
				MatchedTransport matchedTransport = new MatchedTransport(send,trans,receive);
				matchedTransports.add(matchedTransport);
			}
			db.closeAll();
		}
		catch(SQLException e)
		{
			throw e;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return matchedTransports;
    }
	// 匹配物流信息保存，参数是集合
	public void saveMatchedTransport(ArrayList<MatchedTransport> matchTrans) {
		// 创建一个ObjectOutputStream对象输出流，并连接文件输出流
		// 以可追加的方式创建文件输出流，数据保存到MatchedTransports.txt文件中
		try (ObjectOutputStream obs = new ObjectOutputStream(
				new FileOutputStream("MatchedTransports.txt", true))) {
			// 循环保存对象数据
			for (MatchedTransport e : matchTrans) {
				if (e != null) {
					// 把对象写入到文件中
					obs.writeObject(e);
					obs.flush();
				}
			}
			// 文件末尾保存一个null对象，代表文件结束
			obs.writeObject(null);
			obs.flush();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void saveMatchTransportToDB(ArrayList<MatchedTransport> matchTrans)
	{
		DBUtil db = new DBUtil();
		try
		{
			db.getConnection();
			for(MatchedTransport matchedTransport:matchTrans)
			{
				Transport send =matchedTransport.getSend(),
						trans =matchedTransport.getTrans(),
						receive = matchedTransport.getReceive();
				String sql = "insert into gather_transport(id,time,address,type,handler,reciver,transporttype) VALUES(?,?,?,?,?,?,?);";
				db.executeUpdate(sql, send.get_DB_param());
				db.executeUpdate(sql, trans.get_DB_param());
				db.executeUpdate(sql,receive.get_DB_param());
				sql = "insert into matched_transport(sendid,transid,receiveid) VALUES(?,?,?);";
				db.executeUpdate(sql,new Object[]{send.getId(),trans.getId(),receive.getId()});
			}
			db.closeAll();
		}
		catch(SQLIntegrityConstraintViolationException e)
		{;}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	public void saveAndAppendTransport(ArrayList<MatchedTransport>matchTrans)
	{
		if (matchTrans == null)
			return;
		try
		{
			File file = new File("MatchedTransports.txt");
			AppendObjectOutputStream.file = file;
			AppendObjectOutputStream AOOStream = new AppendObjectOutputStream();
			for(MatchedTransport tran:matchTrans)
			{
				if(tran==null)
					continue;
				AOOStream.writeObject(tran);
				AOOStream.flush();
			}
			//null 代表结尾
			//AOOStream.writeObject(null);
			AOOStream.close();
		}
		catch (Exception Ex)
		{
			Ex.printStackTrace();
		}
	}
	// 读匹配物流信息保存，参数是集合
	public ArrayList<MatchedTransport> readMatchedTransport() {
		ArrayList<MatchedTransport> matchTrans = new ArrayList<>();
		// 创建一个ObjectInputStream对象输入流，并连接文件输入流，读MatchedTransports.txt文件中
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				"MatchedTransports.txt"))) {
			MatchedTransport matchTran;
			// 循环读文件中的对象
			while ((matchTran = (MatchedTransport) ois.readObject()) != null) {
				// 将对象添加到泛型集合中
				matchTrans.add(matchTran);
			}
		}
		catch(FileNotFoundException ex)
		{
			;
		}
		catch(EOFException ex)
		{
			;
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return matchTrans;
	}

	
}







