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

import com.neusoft.smart.pos.card.persistent.domain.CardCapitalChange;
import com.neusoft.smart.pos.card.persistent.domain.CardCapitalChangeExample;
import com.neusoft.smart.pos.card.persistent.domain.CardCapitalChangeExt;
import com.neusoft.smart.pos.card.persistent.domain.CardCapitalChangeExtExample;
import com.neusoft.smart.pos.card.persistent.ext.domain.CardCapitalChangeQuery;
import com.neusoft.smart.pos.card.persistent.mapper.CardCapitalChangeExtMapper;
import com.neusoft.smart.pos.card.persistent.mapper.CardCapitalChangeMapper;
import com.neusoft.smart.pos.framework.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class CardCapitalChangeRepository {
	
	
	@Autowired
	private CardCapitalChangeMapper cardCapitalChangeMapper;
	
	@Autowired
	private CardCapitalChangeExtMapper cardCapitalChangeExtMapper;
	
	
	public CardCapitalChange createChange(CardCapitalChange cardCapitalChange) {
        cardCapitalChangeMapper.insertSelective(cardCapitalChange);
		return cardCapitalChange;
	}

	public Integer queryChangeCount(CardCapitalChangeExample example){
		return cardCapitalChangeMapper.countByExample(example);
	}

    public List<CardCapitalChange> getList(CardCapitalChangeExample example) {
        return cardCapitalChangeMapper.selectByExample(example);
    }
	
	public Integer queryChangeCount(CardCapitalChangeQuery cardCapitalChangeQuery) {
		CardCapitalChangeExample cardRechargeExample = new CardCapitalChangeExample();
        cardRechargeExample.createCriteria().andChangeOrderIdEqualTo(cardCapitalChangeQuery.getChangeOrderId()).
				andChangeTypeEqualTo(cardCapitalChangeQuery.getChangeType());
		return cardCapitalChangeMapper.countByExample(cardRechargeExample);
	}
	
	public List<CardCapitalChangeExt> getList4Canteen(CardCapitalChangeQuery cardCapitalChangeQuery) {
		CardCapitalChangeExtExample cardCapitalChangeExtExample = this.exampleProvider(cardCapitalChangeQuery);
		if (cardCapitalChangeQuery.getIsPage()) {
			cardCapitalChangeExtExample.setLimit(cardCapitalChangeQuery.getLimit());
			cardCapitalChangeExtExample.setOffset(cardCapitalChangeQuery.getOffset());
		}
		return cardCapitalChangeExtMapper.selectByExample4Canteen(cardCapitalChangeExtExample);
	}
	
	public Integer getCount4Canteen(CardCapitalChangeQuery cardCapitalChangeQuery) {
		CardCapitalChangeExtExample cardCapitalChangeExtExample = this.exampleProvider(cardCapitalChangeQuery);
		return cardCapitalChangeExtMapper.countByExample4Canteen(cardCapitalChangeExtExample);
	}
	
	public List<CardCapitalChangeExt> getList(CardCapitalChangeQuery cardCapitalChangeQuery) {
		CardCapitalChangeExtExample cardCapitalChangeExtExample = this.exampleProvider(cardCapitalChangeQuery);
		if (cardCapitalChangeQuery.getIsPage()) {
			cardCapitalChangeExtExample.setLimit(cardCapitalChangeQuery.getLimit());
			cardCapitalChangeExtExample.setOffset(cardCapitalChangeQuery.getOffset());
		}
		return cardCapitalChangeExtMapper.selectByExample(cardCapitalChangeExtExample);
	}
	
	public Integer getCount(CardCapitalChangeQuery cardCapitalChangeQuery) {
		CardCapitalChangeExtExample cardCapitalChangeExtExample = this.exampleProvider(cardCapitalChangeQuery);
		return cardCapitalChangeExtMapper.countByExample(cardCapitalChangeExtExample);
	}
	
	
	private CardCapitalChangeExtExample exampleProvider(CardCapitalChangeQuery cardCapitalChangeQuery) {
		CardCapitalChangeExtExample cardCapitalChangeExtExample = new CardCapitalChangeExtExample();
		cardCapitalChangeExtExample.setOrderByClause("created_time desc");
		CardCapitalChangeExtExample.Criteria criteria = cardCapitalChangeExtExample.createCriteria();
		
		if (!StringUtils.isEmpty(cardCapitalChangeQuery.getDay())) {
			criteria.andChangeTimeBetween(DateUtils.parse(cardCapitalChangeQuery.getDay() + " 00:00:00"),
					DateUtils.parse(cardCapitalChangeQuery.getDay() + " 23:59:59"));
		}
		if (null != cardCapitalChangeQuery.getMemberId()) {
			criteria.andMemberIdEqualTo(cardCapitalChangeQuery.getMemberId());
		}
		if (!StringUtils.isEmpty(cardCapitalChangeQuery.getMemberCardId()) && cardCapitalChangeQuery.getMemberCardId() != 0) {
			criteria.andMemberCardIdEqualTo(cardCapitalChangeQuery.getMemberCardId());
		}
		if (null != cardCapitalChangeQuery.getChangeType()) {
			criteria.andChangeTypeEqualTo(cardCapitalChangeQuery.getChangeType());
		}
		
		if (null != cardCapitalChangeQuery.getChangeTypes() && !cardCapitalChangeQuery.getChangeTypes().isEmpty()) {
			criteria.andChangeTypeIn(cardCapitalChangeQuery.getChangeTypes());
		}
		return cardCapitalChangeExtExample;
	}

	public Integer getChangeCountByOrderId(String orderId, Integer changeType) {
        CardCapitalChangeQuery cardCapitalChangeQuery = new CardCapitalChangeQuery();
        cardCapitalChangeQuery.setChangeOrderId(orderId);
        cardCapitalChangeQuery.setChangeType(changeType);
		return queryChangeCount(cardCapitalChangeQuery);
	}
}
