package com.neusoft.smart.pos.card.persistent.repository;

import com.neusoft.smart.pos.card.persistent.domain.CardOrder;
import com.neusoft.smart.pos.card.persistent.domain.CardOrderExample;
import com.neusoft.smart.pos.card.persistent.mapper.CardOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CardOrderRepository {
	
	@Autowired
	private CardOrderMapper cardOrderMapper;

	public Integer getOrderCountByOrderId(String outOrderId, String orderType) {
		CardOrderExample cardOrderExample = new CardOrderExample();
		cardOrderExample.createCriteria()
                .andOutTradeIdEqualTo(outOrderId)
                .andOrderTypeEqualTo(orderType);
		return cardOrderMapper.countByExample(cardOrderExample);
	}

	public void createOrder(CardOrder cardOrder) {
        cardOrderMapper.insertSelective(cardOrder);
    }

    public CardOrder getCardOrderByOutTradeId(String outTradeId) {
        CardOrderExample cardOrderExample = new CardOrderExample();
        cardOrderExample.createCriteria().andOutTradeIdEqualTo(outTradeId);
        return cardOrderMapper.selectByExample(cardOrderExample).stream().findAny().orElse(null);
    }

	public CardOrder getCardOrderByCardTradeNum(String cardTradeNem) {
		CardOrderExample cardOrderExample = new CardOrderExample();
		cardOrderExample.createCriteria().andCardTradeNumEqualTo(cardTradeNem);
		return cardOrderMapper.selectByExample(cardOrderExample).stream().findAny().orElse(null);
	}

	public void updateByPrimarykey(CardOrder cardOrder) {
		cardOrderMapper.updateByPrimaryKey(cardOrder);
	}

	public List<CardOrder> getCardOrderByCardId(int cardId) {
		CardOrderExample cardOrderExample = new CardOrderExample();
		cardOrderExample.createCriteria().andBuyerCardIdEqualTo(cardId);
		return cardOrderMapper.selectByExample(cardOrderExample);
	}
}
