package com.lgx8.gateway.service.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.transaction.annotation.Transactional;
import com.lgx8.common.PageArgument;
import com.lgx8.common.PageList;
import com.lgx8.common.dao.impl.BaseDao;
import com.lgx8.gateway.dao.IOrderDao;
import com.lgx8.gateway.dao.IOrderProductDao;
import com.lgx8.gateway.dao.IProductDao;
import com.lgx8.gateway.entities.OrderProduct;
import com.lgx8.gateway.entities.Orders;
import com.lgx8.gateway.entities.Product;
import com.lgx8.gateway.service.IOrderService;
import com.lgx8.right.dao.IUserDao;
import com.lgx8.right.entities.User;
public class OrdersService implements IOrderService {
IOrderDao orderDao;
IOrderProductDao orderProductDao;
IProductDao productDao;
IUserDao userDao;
public PageList queryOrders(String hql, Object[] values,
PageArgument argment) {
if (argment == null)
return new PageList();
return orderDao.findByPage4Report(hql, values, argment.getCurPage(),
argment.getPageSize());
}
public void setOrderDao(IOrderDao orderDao) {
this.orderDao = orderDao;
}
public List<Orders> queryOrders(String hql, Object[] values) {
return orderDao.findOrders(hql, values);
}
public void setOrderProductDao(IOrderProductDao orderProductDao) {
this.orderProductDao = orderProductDao;
}
public void setProductDao(IProductDao productDao) {
this.productDao = productDao;
}
@Transactional
public boolean updateOrder(Orders order) {
try {
this.orderDao.updateOrders(order);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Transactional
public void createOrders(Orders order) {
orderDao.createOrders(order);
}
@Transactional
public Orders findCartByUserId(long userid) {
User user = userDao.findUserById(userid);
if (user == null) {
return null;
}
return orderDao.findCartByUserId(user);
}
public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
@Transactional
public void createOrderProduct(OrderProduct op) {
orderDao.createOrderProduct(op);
}
/**
* 根据用户id找到用户的购物车,如果不存在,创建一个购物车,将用户所选商品加入购物车
*/
@Transactional
public Orders createOrderProduct(long userid, long productid, int count,
String color, String clothesize, String shoesize) {
User user = userDao.findUserById(userid);
if (user == null) {
return null;
}
Orders order = orderDao.findCartByUserId(user);
Product product = productDao.getProductById(productid);
OrderProduct op = new OrderProduct();
op.setOrders(order);
op.setProduct(product);
op.setName(product.getName());
op.setPrice(product.getPrice());
op.setAmount(count);
op.setColor(color);
op.setClothessize(clothesize);
op.setShoesize(shoesize);
op.setDescription(product.getDescription());
op.setBrand(product.getBrand() == null ? "" : product.getBrand()
.getName());
op.setImage(product.getImage().getLargeURL());
op.setAreacategoryname(product.getAreaCategory().getName());
op.setCategoryname(product.getCategory().getName());
op.setUrl(product.getUrl());
// 有无类似能合并的商品
boolean flag = false;
// 判断有无合并的商品
for (OrderProduct old : order.getOrderProducts()) {
if (old.isOneProduct(op)) {
flag = true;
old.mergeOrderProduct(op);
orderDao.updateOrderProduct(old);
break;
}
}
if (!flag) {
order.addOrderProduct(op);
orderDao.createOrderProduct(op);
}
return order;
}
@Transactional
public void deleteOrderProduct(long id) {
orderDao.deleteOrderProduct(id);
}
@Transactional
public void updateOrderProduct(OrderProduct op) {
orderDao.updateOrderProduct(op);
}
public Orders queryOrderById(long id) {
List<Orders> result = orderDao.findOrders("from Orders o where o.id="
+ id, new Object[] {});
if (null == result || result.size() <= 0)
return null;
return result.get(0);
}
public void delOrders(String[] ids) {
orderDao.deleteOrdersByCondition(ids);
}
}