package net.shopxx.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import net.shopxx.dao.ProductCategoryDao;
import net.shopxx.entity.ProductCategory;
import util.DBPoolConnection;


public class ProductCategoryImpl implements ProductCategoryDao{
	Connection conn;
	PreparedStatement ps;
	PreparedStatement ps1;
	ResultSet rs;
	ResultSet rs1;
	@Override
	/*
	 * 获得所有大的商品分类(grade=0)
	 */
	public List<ProductCategory> getAllProductCategory() {
		List<ProductCategory> list=new ArrayList<>();
		try {
			String sql="select * from xx_product_category where grade=0";
			conn=DBPoolConnection.getConnection();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				ProductCategory productCategory=new ProductCategory();
				productCategory.setName(rs.getString("name"));
				productCategory.setOrders(rs.getShort("orders"));
				productCategory.setId(rs.getInt("id"));
				list.add(productCategory);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBPoolConnection.close(conn, ps, rs);
		}
		
		return list;	
	}
	
	/*
	 * 获得所有小的商品分类
	 */
	public List<ProductCategory> getAllProductCategorySmall() {
		List<ProductCategory> list=new ArrayList<>();
		try {
			String sql="select * from xx_product_category where grade=0";
			int grade;
			conn=DBPoolConnection.getConnection();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				ProductCategory productCategory=new ProductCategory();
				productCategory.setName(rs.getString("name"));
				productCategory.setOrders(rs.getShort("orders"));
				grade=rs.getShort("orders");
				String sql1="select * from xx_product_category where parent="+grade;
				ps1=conn.prepareStatement(sql1);
				rs1=ps1.executeQuery();
				while(rs1.next()){
					ProductCategory productCategory1=new ProductCategory();
					productCategory1.setName(rs1.getString("name"));
					productCategory1.setOrders(rs1.getShort("orders"));
					productCategory1.setParent(grade);
					productCategory1.setId(rs1.getInt("id"));
					list.add(productCategory1);
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBPoolConnection.close(conn, ps, rs);
		}
		
		return list;	
	}
	
	
	@Override
	/*
	 * 添加商品分类
	 */
	public void addProductCategory(ProductCategory productCategory) {
		try {
			String sql;
			if(productCategory.getParent()==0){//他是顶级分类
				sql="insert into xx_product_category (create_date,modify_date,grade,name,tree_path,parent) values (now(),now(),0,'"+productCategory.getName()+"',',',null)";
			}else{//他不是顶级分类
				sql="insert into xx_product_category (create_date,modify_date,grade,name,tree_path,parent) values (now(),now(),1,'"+productCategory.getName()+"','"+productCategory.getParent()+"','"+productCategory.getParent()+"')";
			}
			conn=DBPoolConnection.getConnection();
			ps=conn.prepareStatement(sql);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBPoolConnection.close(conn, ps, rs);
		}
	}


}
最近下载更多
abcdljw  LV20 2022年12月16日
lulululuping  LV1 2022年11月8日
liweb 010928  LV1 2022年10月31日
cqbscxh  LV11 2022年9月19日
ming_123_9715  LV23 2022年5月3日
李田所  LV1 2021年12月20日
儿童游戏机  LV9 2021年6月25日
928477320  LV1 2020年12月30日
dengge123  LV13 2020年9月14日
张信哲  LV8 2020年6月27日
最近浏览更多
cab123123  LV8 4月9日
LHLHBBA 3月19日
暂无贡献等级
微信网友_6786215447367680  LV4 2023年12月20日
2385649653  LV7 2023年12月12日
qxzcxas  LV1 2023年11月26日
zhaozhiqi  LV5 2023年10月21日
EFWAGGFAWGR 2023年10月19日
暂无贡献等级
Chuiwaitim  LV2 2023年10月18日
微信网友_6680567232876544  LV8 2023年10月10日
2036495585  LV9 2023年9月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友