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

import com.neusoft.smart.pos.framework.utils.DateUtils;
import com.neusoft.smart.pos.member.persistent.domain.MemberCapitalChange;
import com.neusoft.smart.pos.member.persistent.domain.MemberCapitalChangeExample;
import com.neusoft.smart.pos.member.persistent.domain.MemberCapitalChangeExt;
import com.neusoft.smart.pos.member.persistent.domain.MemberCapitalChangeExtExample;
import com.neusoft.smart.pos.member.persistent.ext.domain.MemberCapitalChangeQuery;
import com.neusoft.smart.pos.member.persistent.mapper.MemberCapitalChangeExtMapper;
import com.neusoft.smart.pos.member.persistent.mapper.MemberCapitalChangeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class MemberCapitalChangeRepository {
	
	
	@Autowired
	private MemberCapitalChangeMapper memberCapitalChangeMapper;
	
	@Autowired
	private MemberCapitalChangeExtMapper memberCapitalChangeExtMapper;
	
	
	public MemberCapitalChange createChange(MemberCapitalChange memberCapitalChange) {
		memberCapitalChangeMapper.insertSelective(memberCapitalChange);
		return memberCapitalChange;
	}
	
	public Integer queryChangeCount(MemberCapitalChangeQuery memberCapitalChangeQuery) {
		MemberCapitalChangeExample memberRechargeExample = new MemberCapitalChangeExample();
		memberRechargeExample.createCriteria().andChangeOrderIdEqualTo(memberCapitalChangeQuery.getChangeOrderId()).
				andChangeTypeEqualTo(memberCapitalChangeQuery.getChangeType());
		return memberCapitalChangeMapper.countByExample(memberRechargeExample);
	}
	
	public List<MemberCapitalChangeExt> getList4Canteen(MemberCapitalChangeQuery memberCapitalChangeQuery) {
		MemberCapitalChangeExtExample memberCapitalChangeExtExample = this.exampleProvider(memberCapitalChangeQuery);
		if (memberCapitalChangeQuery.getIsPage()) {
			memberCapitalChangeExtExample.setLimit(memberCapitalChangeQuery.getLimit());
			memberCapitalChangeExtExample.setOffset(memberCapitalChangeQuery.getOffset());
		}
		return memberCapitalChangeExtMapper.selectByExample4Canteen(memberCapitalChangeExtExample);
	}
	
	public Integer getCount4Canteen(MemberCapitalChangeQuery memberCapitalChangeQuery) {
		MemberCapitalChangeExtExample memberCapitalChangeExtExample = this.exampleProvider(memberCapitalChangeQuery);
		return memberCapitalChangeExtMapper.countByExample4Canteen(memberCapitalChangeExtExample);
	}
	
	public List<MemberCapitalChangeExt> getList(MemberCapitalChangeQuery memberCapitalChangeQuery) {
		MemberCapitalChangeExtExample memberCapitalChangeExtExample = this.exampleProvider(memberCapitalChangeQuery);
		if (memberCapitalChangeQuery.getIsPage()) {
			memberCapitalChangeExtExample.setLimit(memberCapitalChangeQuery.getLimit());
			memberCapitalChangeExtExample.setOffset(memberCapitalChangeQuery.getOffset());
		}
		return memberCapitalChangeExtMapper.selectByExample(memberCapitalChangeExtExample);
	}
	
	public Integer getCount(MemberCapitalChangeQuery memberCapitalChangeQuery) {
		MemberCapitalChangeExtExample memberCapitalChangeExtExample = this.exampleProvider(memberCapitalChangeQuery);
		return memberCapitalChangeExtMapper.countByExample(memberCapitalChangeExtExample);
	}
	
	
	private MemberCapitalChangeExtExample exampleProvider(MemberCapitalChangeQuery memberCapitalChangeQuery) {
		MemberCapitalChangeExtExample memberCapitalChangeExtExample = new MemberCapitalChangeExtExample();
		memberCapitalChangeExtExample.setOrderByClause("mcc.created_time desc");
		MemberCapitalChangeExtExample.Criteria criteria = memberCapitalChangeExtExample.createCriteria();
		
		if (!StringUtils.isEmpty(memberCapitalChangeQuery.getDay())) {
			criteria.andChangeTimeBetween(DateUtils.parse(memberCapitalChangeQuery.getDay() + " 00:00:00"),
					DateUtils.parse(memberCapitalChangeQuery.getDay() + " 23:59:59"));
		}
		if (null != memberCapitalChangeQuery.getMemberId()) {
			criteria.andMemberIdEqualTo(memberCapitalChangeQuery.getMemberId());
		}
		if (!StringUtils.isEmpty(memberCapitalChangeQuery.getMemberCardId())) {
			criteria.andMemberCardIdEqualTo(memberCapitalChangeQuery.getMemberCardId());
		}
		if (null != memberCapitalChangeQuery.getChangeType()) {
			criteria.andChangeTypeEqualTo(memberCapitalChangeQuery.getChangeType());
		}
		
		if (null != memberCapitalChangeQuery.getChangeTypes() && !memberCapitalChangeQuery.getChangeTypes().isEmpty()) {
			criteria.andChangeTypeIn(memberCapitalChangeQuery.getChangeTypes());
		}
		return memberCapitalChangeExtExample;
	}
}
