package com.wlps.www.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.wlps.www.pojo.Bussiness;
public class BussinessDao extends BaseDao {
//定义Connection
private Connection con = null;
//定义自身的一个变量
private static BussinessDao bussinessDao = null;
//定义自身私有的构造函数
private BussinessDao(Connection con) {
this.con = con;
}
//通过一个简单的单例模式将LoginDao返回到客户端
public synchronized static BussinessDao getbussinessDao(Connection con) {
if (bussinessDao != null) {
return bussinessDao;
}
bussinessDao = new BussinessDao(con);
return bussinessDao;
}
/**
* 往数据库中添加业务信息,添加成功返回1
* @param cus
* @return
*/
public int add(Bussiness bussiness){
Connection conn = null;
conn = this.con;
PreparedStatement pstmt = null;
int i = 0;
String sql = "insert into bussiness values (null,?,?,?,?,?,?,?,?,?,?,?,?,?)";
try{
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bussiness.getCustomerid());
pstmt.setDouble(2, bussiness.getSendpiece());
pstmt.setInt(3, bussiness.getSendset());
pstmt.setDouble(4, bussiness.getReceivepiece());
pstmt.setInt(5, bussiness.getReceiveset());
pstmt.setInt(6,bussiness.getBreakteacup());
pstmt.setInt(7,bussiness.getBreakdish());
pstmt.setInt(8,bussiness.getBreakbowl());
pstmt.setInt(9,bussiness.getBreakscoop());
pstmt.setInt(10,bussiness.getBreakglass());
pstmt.setInt(11, bussiness.getBreakchopsticks());
Date regdate = new Date(bussiness.getRegdate().getTime());
pstmt.setDate(12, regdate);
pstmt.setString(13, bussiness.getNote());
i = pstmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
return i;
}
/**
* 修改数据库中的业务信息。
* @param cus
* @return
*/
public int update(Bussiness bussiness){
Connection conn = null;
conn = this.con;
PreparedStatement pstmt = null;
int i = 0;
String sql = "update bussiness set customerid = ?,sendpiece = ?,sendset = ?,"
+ "receivepiece = ? ,receiveset = ? ,breakteacup = ? ,"
+ "breakdish = ? ,breakbowl = ? ,breakscoop = ? ,"
+ "breakglass = ?,breakchopsticks = ?,regdate = ? ,"
+ "note = ? where bussid = ? ";
try{
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bussiness.getCustomerid());
pstmt.setDouble(2, bussiness.getSendpiece());
pstmt.setInt(3, bussiness.getSendset());
pstmt.setDouble(4, bussiness.getReceivepiece());
pstmt.setInt(5, bussiness.getReceiveset());
pstmt.setInt(6,bussiness.getBreakteacup());
pstmt.setInt(7,bussiness.getBreakdish());
pstmt.setInt(8,bussiness.getBreakbowl());
pstmt.setInt(9,bussiness.getBreakscoop());
pstmt.setInt(10,bussiness.getBreakglass());
pstmt.setInt(11, bussiness.getBreakchopsticks());
Date regdate = new Date(bussiness.getRegdate().getTime());
pstmt.setDate(12, regdate);
pstmt.setString(13, bussiness.getNote());
pstmt.setInt(14, bussiness.getBussid());
i = pstmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
return i;
}
/**
* 删除数据库中的业务信息
* @param cusid
* @return
*/
public int del(int bussid){
Connection conn = null;
conn = this.con;
PreparedStatement pstmt = null;
int i = 0;
String sql = "delete from bussiness where bussid = ? ";
try{
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,bussid);
i = pstmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
return i;
}
/**
* 如果bussid的值是0,则查询数据库中的业务信息,如果bussid的值不为0,则查询数据库中单个的业务信息
* 此处可以考虑用JDK1.5的新特性 传参为Object...objects
* @param bussid
* @return
*/
public List<Bussiness> select(int bussid,int cusid,String startTime,String endTime){
List<Bussiness> bussList = new ArrayList<Bussiness>();
Connection conn = null;
conn = this.con;
PreparedStatement pstmt = null;
String sql = "select bussid,b.customerid,c.customer,sendpiece,sendset," +
"receivepiece,receiveset,breakteacup,breakdish,breakbowl," +
"breakscoop,breakglass,breakchopsticks,regdate,b.note " +
"from bussiness b,customer c where b.customerid = c.customerid";
String sql_context1 = "";
String sql_context2 = "";
String sql_context3 = "";
if(bussid>0){
sql_context1 = " and bussid=?";
}
if(cusid>0){
sql_context2 = " and b.customerid=?";
}
if(startTime!="" && endTime!=""){
sql_context3 = " and b.regdate BETWEEN ? and ?";
}
try {
//预编译Sql返回一个PrePareStatement对象
pstmt = conn.prepareStatement(sql+sql_context1+sql_context2+sql_context3);
int i = 0;
if(!sql_context1.equals("")){
i++;
pstmt.setInt(i, bussid);
}
if(!sql_context2.equals("")){
i++;
pstmt.setInt(i, cusid);
}
if(!sql_context3.equals("")){
i++;
pstmt.setString(i, startTime);
i++;
pstmt.setString(i, endTime);
}
//执行Sql并返回结果集
ResultSet rs = pstmt.executeQuery();
//循环遍历结果集
while(rs.next()) {
Bussiness buss = new Bussiness();
buss.setBussid(rs.getInt(1));
buss.setCustomerid(rs.getInt(2));
buss.setCustomerName(rs.getString(3));
buss.setSendpiece(rs.getInt(4));
buss.setSendset(rs.getInt(5));
buss.setReceivepiece(rs.getInt(6));
buss.setReceiveset(rs.getInt(7));
buss.setBreakteacup(rs.getInt(8));
buss.setBreakdish(rs.getInt(9));
buss.setBreakbowl(rs.getInt(10));
buss.setBreakscoop(rs.getInt(11));
buss.setBreakglass(rs.getInt(12));
buss.setBreakchopsticks(rs.getInt(13));
java.util.Date regdate = new java.util.Date(rs.getDate(14).getTime());
buss.setRegdate(regdate);
buss.setNote(rs.getString(15));
bussList.add(buss);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return bussList;
}
// /**
// * 显示某客户某段时间内的交易记录(针对客户)
// * sendpiece-receivepiece计算的是当前余件
// * sendset-receiveset计算的是当前余套
// * @return
// */
// public List cusBussLog(String startTime,String endTime,int cusid){
// List<Bussiness> bussList = new ArrayList<Bussiness>();
// Connection conn = null;
// conn = this.con;
// PreparedStatement pstmt = null;
// String sql = "select bussid,sendpiece,sendset,receivepiece,receiveset," +
// "sendpiece-receivepiece as remainpiece,sendset-receiveset as remainset," +
// "breakteacup,breakdish,breakbowl,breakscoop,breakglass,breakchopsticks " +
// "from bussiness b,customer c where b.customerid = c.customerid " +
// "and regdate BETWEEN ? and ? and b.customerid=?";
// try {
// //预编译Sql返回一个PrePareStatement对象
// pstmt = conn.prepareStatement(sql);
// pstmt.setString(1, startTime);
// pstmt.setString(2, endTime);
// pstmt.setInt(3, cusid);
// //执行Sql并返回结果集
// ResultSet rs = pstmt.executeQuery();
// //循环遍历结果集
// while(rs.next()) {
// Bussiness buss = new Bussiness();
// buss.setBussid(rs.getInt(1));
// buss.setSendpiece(rs.getInt(2));
// buss.setSendset(rs.getInt(3));
// buss.setReceivepiece(rs.getInt(4));
// buss.setReceiveset(rs.getInt(5));
// buss.setRemainpiece(rs.getInt(6));
// buss.setRemainset(rs.getInt(7));
// buss.setBreakteacup(rs.getInt(8));
// buss.setBreakdish(rs.getInt(9));
// buss.setBreakbowl(rs.getInt(10));
// buss.setBreakscoop(rs.getInt(11));
// buss.setBreakglass(rs.getInt(12));
// buss.setBreakchopsticks(rs.getInt(13));
// bussList.add(buss);
// }
// } catch (Exception ex) {
// ex.printStackTrace();
// }
// return bussList;
// }
/**
* 统计某段时间内的客户的业务数据(汇总统计)
* @return
*/
public List bussTotal(String startTime,String endTime){
List<Bussiness> bussList = new ArrayList<Bussiness