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

import com.neusoft.smart.pos.card.persistent.domain.Card;
import com.neusoft.smart.pos.card.persistent.domain.CardExample;
import com.neusoft.smart.pos.card.persistent.mapper.CardMapper;
import com.neusoft.smart.pos.exchangeStrategy.persistent.domain.ExchangeStrategy;
import com.neusoft.smart.pos.exchangeStrategy.persistent.domain.ExchangeStrategyExample;
import com.neusoft.smart.pos.exchangeStrategy.persistent.mapper.ExchangeStrategyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class ExchangeStrategyRepository {
	
	@Resource
	private ExchangeStrategyMapper exchangeStrategyMapper;

	public void insertExchangeStrategy(Integer cardId, Integer strategyTypeId, String strategyCategory, String params) {
		ExchangeStrategy exchangeStrategy = new ExchangeStrategy();
		exchangeStrategy.setCardId(cardId);
		exchangeStrategy.setStrategyTypeId(strategyTypeId);
		exchangeStrategy.setStrategyCategory(strategyCategory);
		exchangeStrategy.setParams(params);
		exchangeStrategyMapper.insert(exchangeStrategy);
	}

	public void insertExchangeStrategy(Integer cardId, Integer strategyTypeId, String strategyCategory, List<Double> params) {
		ExchangeStrategy exchangeStrategy = new ExchangeStrategy();
		exchangeStrategy.setCardId(cardId);
		exchangeStrategy.setStrategyTypeId(strategyTypeId);
		exchangeStrategy.setStrategyCategory(strategyCategory);
		String p = "";
		for (Double param:params) {
			p = p.concat(param+"");
			p = p.concat(";");
		}
		p = p.substring(0,p.length()-2);
		exchangeStrategy.setParams(p);
		exchangeStrategyMapper.insert(exchangeStrategy);
	}
	public ExchangeStrategy getExchangeStrategy(Integer cardId, String strategyCategory) {
		ExchangeStrategyExample example = new ExchangeStrategyExample();
		example.createCriteria().andCardIdEqualTo(cardId).andStrategyCategoryEqualTo(strategyCategory);
		return exchangeStrategyMapper.selectByExample(example).stream().findAny().orElse(null);
	}

	
}
