package com.iamberry.wechat.service.lease;

import java.util.List;

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

import com.iamberry.wechat.core.entity.lease.LeaseOrder;
import com.iamberry.wechat.core.entity.machine.Machine;
import com.iamberry.wechat.core.entity.rent.RentApply;
import com.iamberry.wechat.face.lease.LeaseOrderService;
import com.iamberry.wechat.service.cart.dao.CartDao;
import com.iamberry.wechat.service.mapper.LeaseMachineMapper;
import com.iamberry.wechat.service.mapper.LeaseOrderMapper;

@Service
public class LeaseOrderServiceImpl implements LeaseOrderService {

	@Autowired
	private LeaseOrderMapper leaseOrderMapper;
	
	@Autowired
	private CartDao cartDao;
	
	@Autowired
	private LeaseMachineMapper leaseMachineMapper;
	
	@Override
	public boolean saveLeaseOrder(LeaseOrder leaseOrder, List<Integer> cartIds) throws Exception {
		// TODO Auto-generated method stub
		int num = leaseOrderMapper.saveLeaseOrder(leaseOrder);
		if (num < 1) {
			throw new Exception();
		}
		// 删除购物车项目
		cartDao.deleteBatchCartListById(cartIds);
		return true;
	}

	@Override
	public Integer updateLeaseOrderInfo(LeaseOrder leaseOrder, Machine machine) {
		// TODO Auto-generated method stub
		int num = leaseOrderMapper.updateLeaseOrderInfo(leaseOrder);
		int count = leaseMachineMapper.insertMachineInfo(machine);
		if (num > 0 && count > 0) {
			return 1;
		}
		return 0;
	}

	@Override
	public List<LeaseOrder> selectLeaseOrderInfo(String openId) {
		// TODO Auto-generated method stub
		return leaseOrderMapper.selectLeaseOrderInfo(openId);
	}

	@Override
	public Integer updateLeaseOrderStatus(LeaseOrder leaseOrder) {
		// TODO Auto-generated method stub
		return leaseOrderMapper.updateLeaseOrderStatus(leaseOrder);
	}

	@Override
	public LeaseOrder selectDepositByOrderId(String orderId) {
		// TODO Auto-generated method stub
		return leaseOrderMapper.selectDepositByOrderId(orderId);
	}

	@Override
	public Integer addApply(RentApply apply) {
		// TODO Auto-generated method stub
		leaseOrderMapper.addApply(apply);
		return apply.getApplyId();
	}

	@Override
	public Integer updateApply(RentApply apply) {
		// TODO Auto-generated method stub
		return leaseOrderMapper.updateApply(apply);
	}
	
}
