package net.ihypo.work;

import java.sql.ResultSet;
import java.sql.SQLException;

import net.ihypo.db.DbDriver;

import com.mysql.jdbc.Statement;

public class WorkFactory {
	static private int id = -1;
	
	static private int  getNextId() throws ClassNotFoundException, SQLException{
		if(id == -1){
			//调用数据库里的最新ID
			Statement statement = (Statement) new DbDriver().getConnection().createStatement();
			ResultSet set = statement.executeQuery("select max(work_id) from works;");
			
			if(set.next()){
				id = set.getInt("max(work_id)") + 1;
			}else{
				id = 1;
			}
		}
		System.out.println("id = " + id);
		return id;
	}
	
	public Work createWork(String title,int userId,String data,int rank) throws ClassNotFoundException, SQLException{
		Work work =  new Work(getNextId(), title, userId, data,rank);
		id++;
		insertWorkList(work);
		return work;
	}
	
	private void insertWorkList(Work work) throws ClassNotFoundException, SQLException{
		//写入数据库
		Statement statement = (Statement) new DbDriver().getConnection().createStatement();
		
		System.out.println("insert into works values(" + work.getId() + ",'" + work.getTitle() + "',"
				 + work.getUserId() + "," + work.getRank() + "," + work.isFinash() + ");");
		statement.execute("insert into works values(" + work.getId() + ",'" + work.getTitle() + "',"
				 + work.getUserId() + "," + work.getRank() + "," + work.isFinash() + ");");
	}
}

@@ -692,16 +692,32 @@
 int rank
+,int workGroupId
 ) throws
@@ -816,16 +816,28 @@
 ata,rank
+,workGroupId
 );%0A%09%09id+
@@ -1071,184 +1071,8 @@
 %0A%09%09%0A
-%09%09System.out.println(%22insert into works values(%22 + work.getId() + %22,'%22 + work.getTitle() + %22',%22%0A%09%09%09%09 + work.getUserId() + %22,%22 + work.getRank() + %22,%22 + work.isFinash() + %22);%22);%0A
 %09%09st
@@ -1234,16 +1234,43 @@
 nash() +
+ %22, %22 + work.getGroupId() +
  %22);%22);%0A

update Factory method about create and update work