package com.obigo.dao.daoimpl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.obigo.bean.Brand;
import com.obigo.bean.BrandType;
import com.obigo.bean.Color;
import com.obigo.bean.Product;
import com.obigo.bean.Pt1;
import com.obigo.bean.Pt2;
import com.obigo.bean.Pt3;
import com.obigo.bean.Shop;
import com.obigo.bean.Size;
import com.obigo.dao.ProductDao;
import com.obigo.util.DBUtil;
public class ProductDaoImpl implements ProductDao {
//连接数据库
Connection con = DBUtil.getCon();
//查询四大分类的前八个
@Override
public List<Product> SelectPt1(Integer g) {
List<Product> list = new ArrayList();
try {
//预编译sql
Statement stm = con.createStatement();
//sql语句
String sql =" SELECT * FROM (SELECT * FROM(SELECT * FROM(SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT pt1.pt1_id FROM pt1 WHERE pt1.pt1_id="+g+") a LEFT JOIN pt2 ON a.pt1_id=pt2.pt2_pt1id) a LEFT JOIN pt3 ON a.pt2_id=pt3.pt3_pt2id) a LEFT JOIN ptypepro ON a.pt3_id=ptypepro.p_typeid) a LEFT JOIN product ON a.p_pid=product.p_id) a LIMIT 0,8) a LEFT JOIN color ON a.p_color=color.c_id) a LEFT JOIN size ON a.p_sid=size.s_id) a LEFT JOIN shop ON a.p_shopid=shop.shop_id) a LEFT JOIN brand ON a.p_bid=brand.b_id) a LEFT JOIN brandtype ON a.b_btid=brandtype.bt_id";
//查询
ResultSet rs = stm.executeQuery(sql);
while(rs.next()) {
Product p = new Product();
p.setPAn(rs.getString("p_an"));
p.setPId(rs.getInt("p_id"));
p.setPImg(rs.getString("p_img"));
p.setPName(rs.getString("p_name"));
p.setPPrice(rs.getDouble("p_price"));
p.setPWeight(rs.getString("p_weight"));
Brand b = new Brand();
b.setBName(rs.getString("b_name"));
b.setBId(rs.getInt("b_id"));
b.setBImg(rs.getString("b_img"));
b.setBLogo(rs.getString("b_logo"));
BrandType bt = new BrandType();
bt.setBTId(rs.getInt("bt_id"));
bt.setBTName(rs.getString("bt_name"));
Color c = new Color();
c.setCId(rs.getInt("c_id"));
c.setCName(rs.getString("c_name"));
Shop s = new Shop();
s.setSHOPId(rs.getInt("shop_id"));
s.setSHOPName(rs.getString("shop_name"));
Size si = new Size();
si.setSId(rs.getInt("s_id"));
si.setSName(rs.getString("s_name"));
p.setPSid(si);
p.setPShopid(s);
p.setPColor(c);
b.setBBTid(bt);
p.setPBid(b);
list.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//查询pt1
@Override
public List<Pt1> Selectpt1() {
List<Pt1> pt1list = new ArrayList();
try {
//预编译sql
Statement stm = con.createStatement();
//sql语句
String sql ="select * from pt1";
//查询
ResultSet rs = stm.executeQuery(sql);
while(rs.next()) {
Pt1 pt1 = new Pt1();
pt1.setPT1Id(rs.getInt("pt1_id"));
pt1.setPT1Name(rs.getString("pt1_name"));
pt1list.add(pt1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pt1list;
}
//查询pt2
@Override
public List<Pt2> Selectpt2() {
List<Pt2> pt2list = new ArrayList();
try {
//预编译sql
Statement stm = con.createStatement();
//sql语句
String sql ="select * from pt2";
//查询
ResultSet rs = stm.executeQuery(sql);
while(rs.next()) {
Pt2 pt2 = new Pt2();
pt2.setPT2Id(rs.getInt("pt2_id"));
pt2.setPT2Name(rs.getString("pt2_name"));
Pt1 pt1 =new Pt1();
pt1.setPT1Id(rs.getInt("pt2_pt1id"));
pt2.setPT2Pt1id(pt1);
pt2list.add(pt2);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pt2list;
}
//查询pt3
@Override
public List<Pt3> Selectpt3() {
List<Pt3> pt3list = new ArrayList();
try {
//预编译sql
Statement stm = con.createStatement();
//sql语句
String sql ="select * from pt3";
//查询
ResultSet rs = stm.executeQuery(sql);
while(rs.next()) {
Pt3 pt3 = new Pt3();
pt3.setPT3Id(rs.getInt("pt3_id"));
pt3.setPT3Name(rs.getString("pt3_name"));
Pt2 pt2 =new Pt2();
pt2.setPT2Id(rs.getInt("pt3_pt2id"));
pt3.setPT3Pt2id(pt2);
pt3list.add(pt3);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pt3list;
}
//查询全部商品
@Override
public List<Product> SelectAll() {
//新建集合
List<Product> list = new ArrayList();
try {
//预编译sql
Statement stm = con.createStatement();
//sql语句
String sql ="SELECT * FROM color,(SELECT * FROM brandtype,(SELECT * FROM brand,(SELECT * FROM shop,(SELECT * FROM size,(SELECT * FROM product) a WHERE size.s_id=a.p_sid) a WHERE shop.shop_id=a.p_shopid) a WHERE brand.b_id = a.p_bid) a WHERE brandtype.bt_id=a.b_btid) a WHERE color.c_id=a.p_color";
//查询
ResultSet rs = stm.executeQuery(sql);
while(rs.next()) {
Product p = new Product();
p.setPAn(rs.getString("p_an"));
p.setPId(rs.getInt("p_id"));
p.setPImg(rs.getString("p_img"));
p.setPName(rs.getString("p_name"));
p.setPPrice(rs.getDouble("p_price"));
p.setPWeight(rs.getString("p_weight"));
Brand b = new Brand();
b.setBName(rs.getString("b_name"));
b.setBId(rs.getInt("b_id"));
b.setBImg(rs.getString("b_img"));
b.setBLogo(rs.getString("b_logo"));
BrandType bt = new BrandType();
bt.setBTId(rs.getInt("bt_id"));
bt.setBTName(rs.getString("bt_name"));
Color c = new Color();
c.setCId(rs.getInt("c_id"));
c.setCName(rs.getString("c_name"));
Shop s = new Shop();
s.setSHOPId(rs.getInt("shop_id"));
s.setSHOPName(rs.getString("shop_name"));
Size si = new Size();
si.setSId(rs.getInt("s_id"));
si.setSName(rs.getString("s_name"));
p.setPSid(si);
p.setPShopid(s);
p.setPColor(c);
b.setBBTid(bt);
p.setPBid(b);
list.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//按一大类查询
@Override
public List<Product> SelectByType1(Integer i) {
//新建集合
List<Product> list = new ArrayList();
try {
//预编译sql
Statement stm = con.createStatement();
//sql语句
String sql ="SELECT * FROM color ,(SELECT * FROM brandtype,(SELECT * FROM brand,(SELECT * FROM shop,(SELECT * FROM size,(SELECT * FROM product,(SELECT * FROM ptypepro,(SELECT * FROM pt3,(SELECT * FROM pt2 ,(SELECT * FROM pt1 WHERE pt1.pt1_id="+i+" ) a WHERE pt2.pt2_pt1id=a.pt1_id) a WHERE pt3.pt3_pt2id=a.pt2_id) a WHERE ptypepro.p_typeid=a.pt3_id) a WHERE product.p_id=a.p_pid) a WHERE size.s_id=a.p_sid) a WHERE shop.shop_id=a.p_shopid) a WHERE brand.b_id=a.p_bid) a WHERE brandtype.bt_id=a.b_btid) a WHERE color.c_id=a.p_color";
//查询
ResultSet rs = stm.executeQuery(sql);
while(rs.next()) {
Product p = new Product();
p.setPAn(rs.getString("p_an"));
p.setPId(rs.getInt("p_id"));
p.setPImg(rs.getString("p_img"));
p.setPName(rs.get