package com.iamberry.wechat.service.order.dao.impl;

import java.util.List;

import com.iamberry.wechat.core.entity.cart.ShopSalesOrderDto;
import com.iamberry.wechat.core.entity.coupon.CouponItem;
import com.iamberry.wechat.core.entity.eo.GiftPool;
import com.iamberry.wechat.core.entity.eo.ItemGift;
import com.iamberry.wechat.core.entity.order.NumberBacklogDto;
import com.iamberry.wechat.face.eo.GiftPoolService;
import com.iamberry.wechat.face.ism.IntegralLogService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.service.mapper.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.iamberry.wechat.core.entity.admin.ShopSystemRule;
import com.iamberry.wechat.core.entity.drp.PlaceInfo;
import com.iamberry.wechat.core.entity.drp.PlaceLogs;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.order.Order;
import com.iamberry.wechat.core.entity.order.OrderItem;
import com.iamberry.wechat.core.entity.order.OrderRebackMoney;
import com.iamberry.wechat.service.order.dao.AdminOrderDao;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class AdminOrderDaoImpl implements AdminOrderDao {
	Logger logger = LoggerFactory.getLogger(AdminOrderDaoImpl.class);
	
	@Autowired
	private AdminOrderMapper adminOrderMapper;
	@Autowired
	private PlaceInfoMapper placeInfoMapper;
	@Autowired
	private PlaceLogsMapper placeLogsMapper;
	@Autowired
	private CartMapper cartMapper;
	@Autowired
	private ItemGiftMapper itemGiftMapper;
	@Autowired
	private GiftPoolService giftPoolService;
	@Autowired
	private IntegralLogService integralLogService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private CouponItemMapper couponItemMapper;

	
	@Override
	public List<OrderRebackMoney> selectOrderRebackByOrderId(String orderId) {
		// TODO Auto-generated method stub
		return adminOrderMapper.selectOrderRebackByOrderId(orderId);
	}
	
	
	@Override
	public List<Order> selectShopOrder(Order order) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.selectShopOrder(order);
	}

	@Override
	public List<Order> selectShopOrderByOrderId(Order order) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.selectShopOrderByOrderId(order);
	}


	@Override
	public Order getShopOrderByOrderId(String orderId) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.getShopOrderByOrderId(orderId);
	}

	@Override
	public List<OrderItem> getShopOrderItemByOrderId(String orderId) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.getShopOrderItemByOrderId(orderId);
	}

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

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

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

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

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

	@Override
	public List<Order> selectOrderByIntegral(Order order) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.selectOrderByIntegral(order);
	}

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

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

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

	@Override
	public Member getUserByOpenId(String openId) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.getUserByOpenId(openId);
	}

	@Override
	public Member getUserByUserRefereeid(Integer userId) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.getUserByUserRefereeid(userId);
	}

	@Override
	public Integer updateUserByUserId(Member member) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.updateUserByUserId(member);
	}

	@Override
	public ShopSystemRule getReluByRid(Integer rId) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.getReluByRid(rId);
	}

	@Override
	public ShopSystemRule getReluByRtype(Integer ruleType) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.getReluByRtype(ruleType);
	}

	@Override
	public PlaceInfo getOnePlaceInfo(int id) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.getPlaceInfoByPlaceId(id);
	}

	@Override
	public int insertPlaceLogs(PlaceLogs placeLogs) {
		// TODO Auto-generated method stub
		return this.placeLogsMapper.insert(placeLogs);
	}

	@Override
	public Integer updatePlaceInfo(PlaceInfo placeInfo) {
		// TODO Auto-generated method stub
		return this.adminOrderMapper.updatePlaceInfo(placeInfo);
	}

	@Override
	public String selectSalesOpenIdByOrderId(String orderId) {
		// TODO Auto-generated method stub
		return adminOrderMapper.selectSalesOpenIdByOrderId(orderId);
	}

	@Override
	public List<Order> getOrderByCreateTime(Order order) {
		// TODO Auto-generated method stub
		return adminOrderMapper.getOrderByCreateTime(order);
	}
	
	@Override
	public List<Order> getOrderByCreateTimeAndFiveMinutes(Order order) {
		// TODO Auto-generated method stub
		return adminOrderMapper.getOrderByCreateTimeAndFiveMinutes(order);
	}
	
	
	
	@Override
	public Integer updateOrderByOrderId(List<String> list) {
		// TODO Auto-generated method stub
		return adminOrderMapper.updateOrderByOrderId(list);
	}

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

	@Override
	public List<OrderItem> getItemsNumByOrderId(String orderId) {
		// TODO Auto-generated method stub
		return adminOrderMapper.getItemsNumByOrderId(orderId);
	}

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

	@Override
	public List<Order> getOrderList(Order order) {
		// TODO Auto-generated method stub
		return adminOrderMapper.getOrderList(order);
	}

	@Override
	public Integer updateOrderListStatus(List<Order> list) {
		// TODO Auto-generated method stub
		return adminOrderMapper.updateOrderListStatus(list);
	}


	@Override
	public PlaceLogs selectPlaceLogInfo(PlaceLogs PlaceLogs) {
		// TODO Auto-generated method stub
		return placeLogsMapper.selectPlaceLogInfo(PlaceLogs);
	}

	@Override
	public Integer updatePlaceLogs(PlaceLogs placeLogs) {
		// TODO Auto-generated method stub
		return placeLogsMapper.updatePlaceLogs(placeLogs);
	}

	@Override
	public NumberBacklogDto selectNumberBacklog() {
		return adminOrderMapper.selectNumberBacklog();
	}
	@Override
	public List<Order> getOrderListToExcel(Order order) {
		return adminOrderMapper.getOrderListToExcel(order);
	}

	@Override
	public List<Order> cancelOrderList(Order order) {
		return adminOrderMapper.cancelOrderList(order);
	}

	@Override
	@Transactional
	public Integer cancelOrder(String salesOrderid) {
		Integer flag = 0;
		ShopSalesOrderDto ssod  = cartMapper.getOpenIdAndStatus(salesOrderid);
		Order order = new Order();
		order.setSalesOrderid(salesOrderid);
		order.setSalesStatus(0);	//订单取消
		flag = cartMapper.updateOrderByOrder(order);
		if(flag < 1){
			throw new RuntimeException("修改订单失败");
		}

		//恢复赠品的库存
		List<OrderItem> orderItemList = adminOrderMapper.getShopOrderItemByOrderId(salesOrderid);
		for (OrderItem orderItem:orderItemList) {
			ItemGift itemGift = new ItemGift();
			itemGift.setItemId(orderItem.getItemId());
			List<ItemGift> itemGiftList = itemGiftMapper.getItemGiftList(itemGift);
			for (ItemGift ig:itemGiftList) {
				GiftPool gp = new GiftPool();
				gp.setGiftPoolId(ig.getGiftPoolId());
				gp.setGiftPoolStock(ig.getItemGiftNum());
				giftPoolService.updateAddStock(gp);
			}
		}

		//恢复优惠券
		if (null != ssod.getSalesCouponReceiveId() && !"".equals(ssod.getSalesCouponReceiveId())) {
			CouponItem couponItem = new CouponItem();
			couponItem.setCouponItemId(ssod.getSalesCouponReceiveId());
			couponItem.setCouponItemUseropenid(ssod.getSalesOpenid());
			couponItem.setCouponUseStatus(1);
			couponItem.setOldCouponUseStatus(7);
			flag = couponItemMapper.updateCouponItemById(couponItem);
			if(flag < 1){
				throw new RuntimeException("恢复优惠券失败");
			}
		}

		//恢复积分
		flag = integralLogService.cancelSurplusIntegral(salesOrderid);
		if(flag < 1){
			throw new RuntimeException("恢复积分失败");
		}
		return flag;
	}
}
