package com.mpshop.dao.goods;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.csuinfo.until.DBUtil;
import com.mpshop.util.DBManager;
import com.mpshop.util.PageInfo;
import com.mpshop.vo.GoodsVo;
import com.mpshop.vo.ProCondition;
public class GoodsDao {
public List<GoodsVo> getMainPageGoods() {
Connection conn = DBManager.getConnection();
String sql = "select goods_id,img_uri,this_price,goods_name,descript,show_title from (select rownum r,goods.* from goods) t where r<19";
String[] fields = new String[] { "goodsId", "imgUri", "realPrice",
"goodsName", "descript", "showTitle" };
List<GoodsVo> l = DBUtil.getVoList(GoodsVo.class, conn, sql, null,
fields);
DBManager.close(conn);
return l;
}
public GoodsVo getById(int id) {
Connection conn = DBManager.getConnection();
String sql = "select g.goods_id,g.class_id,g.brand_id,g.market_price,g.this_price,"
+ "g.look_times,g.sold_num,g.ori_makeup,g.sele_makeup,g.capability,"
+ "g.img_uri,g.goods_state,g.goods_num,g.descript,g.show_title,"
+ "g.goods_size,c.class_name,b.brand_name,"
+ "b.img_url from goods g,goods_class c,goods_brand b "
+ "where g.goods_id="
+ id
+ " and g.class_id=c.class_id "
+ " and g.brand_id=b.brand_id";
String[] fields = new String[] { "goodsId", "classId", "brandId",
"marketPrice", "realPrice", "lookTimes", "soldNumber",
"oriMakeup", "seleMakeup", "capability", "imgUri",
"goodsState", "goodsNumber", "descript", "showTitle",
"goodsSize", "className", "brandName", "brandImgUri" };
GoodsVo goods = DBUtil
.getVoList(GoodsVo.class, conn, sql, null, fields).get(0);
DBManager.close(conn);
return goods;
}
public boolean updateLookTimes(int id) {
Connection conn = DBManager.getConnection();
String sql = "update goods set look_times=look_times+1 where goods_id="
+ id;
PreparedStatement ps = null;
int count = 0;
try {
ps = conn.prepareStatement(sql);
count = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
DBManager.close(conn);
return count > 0;
}
public PageInfo<GoodsVo> splitQuery(ProCondition cd) {
PageInfo<GoodsVo> page = new PageInfo<GoodsVo>();
String bb="select a.*,gc.class_name,gbr.brand_name from Goods a,goods_class gc,goods_brand gbr where a.class_id=gc.class_id and a.brand_id=gbr.brand_id ";
int currentPage=cd.getCurrentPage();
StringBuilder builder=new StringBuilder();
builder.append(bb);
if(cd.getBrandId()!=0){
builder.append(" and gbr.brand_Id="+cd.getBrandId());
}
if(cd.getClassId()!=0){
builder.append(" and gc.class_Id="+cd.getClassId());
}
if(cd.getCapability()!=0){
builder.append(" and a.capability="+cd.getCapability());
}
if(cd.getGoodSize()!=0){
builder.append(" and a.goods_size="+cd.getGoodSize());
}
if(cd.getMinPrice()!=0){
builder.append(" and a.this_price>="+cd.getMinPrice());
}
if(cd.getMaxPrice()!=0){
builder.append(" and a.this_price<="+cd.getMaxPrice());
}
String base=builder.toString();
if(cd.getParamId()!=0){
base="select base.*,gp.param_id,gp.param_value,p.param_name,p.param_type from ("+base+") base,goods_params gp,params p where base.goods_id=gp.goods_id and gp.param_id=p.param_id and p.param_id="+cd.getParamId();
}
String sql1="select count(*) from ("+base+")";
Connection conn = DBManager.getConnection();
PreparedStatement ps=null;
ResultSet rs=null;
int totalSize=0;
try {
ps=conn.prepareStatement(sql1);
rs=ps.executeQuery();
rs.next();
totalSize=rs.getInt(1);
int pageSize=16;
page.setPageSize(pageSize);
int temp=totalSize/page.getPageSize();
int totalPage=(totalSize%page.getPageSize()>0?temp+1:temp);
page.setCurrentPage(currentPage);
page.setTotalCount(totalSize);
page.setTotalPage(totalPage);
String sql ="select rownum r,d.* from ("+base+") d";
String sql3="select t.goods_num,t.sold_num, t.goods_id,t.img_uri,t.this_price,t.goods_name,t.descript,t.show_title,t.class_name,t.brand_name from ("+sql+") t where t.r>"+(currentPage-1)*16+" and t.r<="+currentPage*16;
String[] fields = new String[] {"goodsNumber","soldNumber", "goodsId", "imgUri", "realPrice",
"goodsName", "descript", "showTitle","className","brandName" };
List<GoodsVo> l=DBUtil.getVoList(GoodsVo.class,conn,sql3,null,fields);
page.setLstPage(l);
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBManager.close(conn, ps, rs);
}
return page;
}
public PageInfo<GoodsVo> getMpGoods(int classId1,int classId2){
PageInfo<GoodsVo> page = new PageInfo<GoodsVo>();
Connection conn=DBManager.getConnection();
String sql1="select count(*) from goods";
PreparedStatement ps=null;
ResultSet rs=null;
int totalCount=0;
try {
ps=conn.prepareStatement(sql1);
rs=ps.executeQuery();
rs.next();
totalCount=rs.getInt(1);
int pageSize=16;
int temp=totalCount/page.getPageSize();
int totalPage=(totalCount%page.getPageSize()>0?temp+1:temp);
page.setCurrentPage(1);
page.setPageSize(pageSize);
page.setTotalCount(totalCount);
page.setTotalPage(totalPage);
String sql2="select goods_id,img_uri,this_price,goods_name,descript,show_title from (select rownum r,goods.* from goods where class_id="+classId1+" or class_id="+classId2+") t where r<="+16;
String[] fields = new String[] { "goodsId", "imgUri", "realPrice",
"goodsName", "descript", "showTitle" };
List<GoodsVo> l=DBUtil.getVoList(GoodsVo.class,conn,sql2,null,fields);
page.setLstPage(l);
} catch (SQLException e) {
e.printStackTrace();
}
return page;
}
public PageInfo<GoodsVo> getMpGoods(int classId1){
PageInfo<GoodsVo> page = new PageInfo<GoodsVo>();
Connection conn=DBManager.getConnection();
String sql1="select count(*) from goods";
PreparedStatement ps=null;
ResultSet rs=null;
int totalCount=0;
try {
ps=conn.prepareStatement(sql1);
rs=ps.executeQuery();
rs.next();
totalCount=rs.getInt(1);
int pageSize=16;
int temp=totalCount/page.getPageSize();
int totalPage=(totalCount%page.getPageSize()>0?temp+1:temp);
page.setCurrentPage(1);
page.setPageSize(pageSize);
page.setTotalCount(totalCount);
page.setTotalPage(totalPage);
String sql2="select goods_id,img_uri,this_price,goods_name,descript,show_title from (select rownum r,goods.* from goods where class_id="+classId1+") t where r<="+16;
String[] fields = new String[] { "goodsId", "imgUri", "realPrice",
"goodsName", "descript", "showTitle" };
List<GoodsVo> l=DBUtil.getVoList(GoodsVo.class,conn,sql2,null,fields);
page.setLstPage(l);
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBManager.close(conn, ps);
DBManager.close(rs);
}
return page;
}
public boolean insert(GoodsVo goods){
boolean flag=false;
Connection conn=DBManager.getConnection();
String sql="insert into goods values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
String sql2="insert into goods_params values(?,?,?)";
// String[] fields1 = new String[] { "goodsId", "goodsName","classId","brandId",
// "marketPrice", "realPrice", "lookTimes", "soldNumber",
// "oriMakeup", "seleMakeup", "capability","goodsSize", "imgUri",
// "goodsState", "goodsNumber", "showTitle","descript"
// };
String[] fields1 = new String[] { "goodsId","classId","brandId",
"marketPrice", "realPrice", "lookTimes", "soldNumber",
"oriMakeup", "seleMakeup", "capability", "imgUri",
"goodsState", "goodsNumber","descript","goodsName", "showTitle","goodsSize"
};
String[] fields2=new String[]{"goodsId","paramId","paramValue"};
try {
conn.setAutoCommit(false);
DBUtil.insert