package com.iamberry.wechat.service.cart;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.PaperBean;
import com.iamberry.wechat.core.entity.admin.ShopSystemRule;
import com.iamberry.wechat.core.entity.cart.CartDto;
import com.iamberry.wechat.core.entity.cart.ShopOrderAndItemDto;
import com.iamberry.wechat.core.entity.cart.ShopOrderItemDto;
import com.iamberry.wechat.core.entity.cart.ShopSalesOrderDto;
import com.iamberry.wechat.core.entity.coupon.CouponItem;
import com.iamberry.wechat.core.entity.coupon.CouponType;
import com.iamberry.wechat.core.entity.mq.MQMessage;
import com.iamberry.wechat.core.entity.order.Order;
import com.iamberry.wechat.core.entity.order.OrderItem;
import com.iamberry.wechat.face.cart.CartService;
import com.iamberry.wechat.face.cart.ProductInfoService;
import com.iamberry.wechat.face.coupon.CouponItemService;
import com.iamberry.wechat.face.coupon.CouponTypeService;
import com.iamberry.wechat.face.member.CashLogService;
import com.iamberry.wechat.face.reback.RebackServices;
import com.iamberry.wechat.service.cart.dao.CartDao;
import com.iamberry.wechat.service.order.dao.AdminOrderDao;

@Service
public class CartServiceImpl implements CartService {

	@Autowired
	private CartDao cartDao;
	
	@Autowired
	private AdminOrderDao adminOrderDao;
	@Autowired
	CouponItemService couponItemService;
	@Autowired
	CouponTypeService couponTypeService;
	@Autowired
	CashLogService cashLogService;
	@Autowired
	private RebackServices rebackServices;
	@Autowired
	ProductInfoService productInfoService;
	
	
	@Override
	public List<CartDto> selectCartByOpenId(String openId) {
		// TODO Auto-generated method stub
		return cartDao.selectCartByOpenId(openId);
	}

	@Override
	public Integer getCartNumByCartOpenId(String openId) {
		// TODO Auto-generated method stub
		return cartDao.getCartNumByCartOpenId(openId);
	}

	@Override
	public Integer updateCartByOpenId(CartDto cartDto) {
		// TODO Auto-generated method stub
		return cartDao.updateCartByOpenId(cartDto);
	}

	@Override
	public Integer getCartNumByOpenId(CartDto cartDto) {
		// TODO Auto-generated method stub
		return cartDao.getCartNumByOpenId(cartDto);
	}

	@Override
	public CartDto getCartByCartProductId(CartDto cartDto) {
		// TODO Auto-generated method stub
		return cartDao.getCartByCartProductId(cartDto);
	}

	@Override
	public Integer addProductToCart(CartDto cartDto) {
		// TODO Auto-generated method stub
		Integer num = cartDao.addProductToCart(cartDto);
		if (num > 0) {
			return cartDto.getCartId();
		} else {
			return num;
		}
	}

	@Override
	public Integer deleteCartByCartId(CartDto cartDto) {
		// TODO Auto-generated method stub
		return cartDao.deleteCartByCartId(cartDto);
	}

	@Override
	public List<ShopOrderAndItemDto> selectShopOrder(PaperBean paperBean) {
		// TODO Auto-generated method stub
		return cartDao.selectShopOrder(paperBean);
	}

	@Override
	public List<ShopOrderItemDto> selectShopOrderItem(String itemSalesOrderid) {
		// TODO Auto-generated method stub
		return cartDao.selectShopOrderItem(itemSalesOrderid);
	}

	@Override
	public ShopSalesOrderDto getShopOrderByOrderId(String salesOrderId,
			String salesOpenId) {
		// TODO Auto-generated method stub
		return cartDao.getShopOrderByOrderId(salesOrderId, salesOpenId);
	}

	@Override
	public List<CartDto> selectCartItemByListId(Integer[] carts) {
		// TODO Auto-generated method stub
		return cartDao.selectCartItemByListId(carts);
	}

	@Override
	public boolean addOrderAndSaveOrderItem(Order order, List<OrderItem> items, List<Integer> cartIds) throws Exception {
		// TODO Auto-generated method stub
		int count = cartDao.insertOrderInfo(order);
		if (count < 1) {
			throw new Exception();
		}
		count = cartDao.insertOrderItemList(items);
		if (count < 1) {
			throw new Exception();
		}
		// 删除购物车项目
		cartDao.deleteBatchCartListById(cartIds);
		return true;
	}

	@Override
	public Integer updateOrderStatus(Order order) {
		// TODO Auto-generated method stub
		return cartDao.updateOrderStatus(order);
	}

	@Override
	public Integer selectOrderStatusByOrderId(String orderId) {
		// TODO Auto-generated method stub
		return cartDao.selectOrderStatusByOrderId(orderId);
	}

	@Override
	public ShopSalesOrderDto getOpenIdAndStatus(String salesOrderid) {
		// TODO Auto-generated method stub
		return cartDao.getOpenIdAndStatus(salesOrderid);
	}

	@Override
	public Integer updateOrderByOrderId(Order order) {
		// TODO Auto-generated method stub
		return cartDao.updateOrderByOrderId(order);
	}

	@Override
	public Order selectOrderInfoById(Order order) {
		// TODO Auto-generated method stub
		return cartDao.selectOrderInfoById(order);
	}

	@Override
	public Integer getOrderNumByOpenId(PaperBean paperBean) {
		// TODO Auto-generated method stub
		return cartDao.getOrderNumByOpenId(paperBean);
	}

	@Override
	public ShopSystemRule getReluByRid(Integer rId) {
		// TODO Auto-generated method stub
		return adminOrderDao.getReluByRid(rId);
	}
	
	/**
	 * 根据单号查询该订单的产品总数 
	 * 2016年5月18日
	 * @param orderid
	 * @return
	 */
	public Integer getItemNumByOrderId(String orderid){
		return cartDao.getItemNumByOrderId(orderid);
	}
	
	/**
	 * 用于支付后的修改优惠券状态 及其订单状态状态的回调函数
	 * @author 欧阳明
	 * @return
	 * 
	 */
	public Boolean supdateStateByOrderId(MQMessage QMessage){
		Order order=new Order();
		order.setSalesOrderid(QMessage.getServiceToMessage());
//System.out.println("order is for search============");
		order=cartDao.selectOrderInfoById(order);
		/*order.setSalesTransactionId(QMessage.getServiceOtherMessage());
		updateOrderByOrderId(order);*/
		int i=0;
		
		/*//修改优惠券状态
		String couponId=order.getCouponId();
		if(couponId!=null && !couponId.endsWith("")){
			CouponItem couponItem=new CouponItem();
			couponItem.setCouponItemId(couponId);
			couponItem.setCouponUseStatus(2);
			couponItem.setCouponUseDate(nowDate);
			couponItemService.insertCouponItem(couponItem);
		}
		//更新账户余额
		int balancePayAmount=order.getBalancePayAmount();
		if(balancePayAmount>0){
			Member member=new Member();
			member.setUserOpenid(order.getSalesOpenid());
			member.setUserIncome(-balancePayAmount);
			memberService.updateUserIncomeByOpenId(member);
			
			//现金使用记录
			CashLog log=new CashLog();
			log.setCashLogsIntroduction("订单支出");
			log.setCashLogsNum(balancePayAmount);
			log.setCashLogsOpenid(member.getUserOpenid());
			log.setCashLogsType(2);
			log.setCashLogsResType(3);
			log.setCashLogsOrderid(order.getSalesOrderid());
			log.setCashLogsCreateDate(nowDate);
			i=cashLogService.addCashLog(log);
		}*/
		productInfoService.updateProductNumList(QMessage.getServiceToMessage());
		//添加到返利表
		i=rebackServices.loadSingleOrderReward(order);
		return i>0;
	}

	@Override
	public CouponType getCouponReduceByItemId(CouponItem couponItem) {
		// TODO Auto-generated method stub
		return cartDao.getCouponReduceByItemId(couponItem);
	}

	@Override
	public List<ShopSalesOrderDto> getOrderList(
			ShopSalesOrderDto shopSalesOrderDto) {
		// TODO Auto-generated method stub
		return cartDao.getOrderList(shopSalesOrderDto);
	}
	
	public Order getOrderRecentAddress(String openid){
		return cartDao.getRecentOrderAddress(openid);
	}

	@Override
	public Integer selectOrderCount(ShopSalesOrderDto shopSalesOrderDto) {
		// TODO Auto-generated method stub
		return cartDao.selectOrderCount(shopSalesOrderDto);
	}
	
	
}
