package com.neusoft.smart.pos.payorder.repository;


import com.neusoft.smart.pos.payorder.persistent.domain.TradeSettlementInfo;
import com.neusoft.smart.pos.payorder.persistent.domain.TradeSettlementInfoExample;
import com.neusoft.smart.pos.payorder.persistent.mapper.TradeSettlementInfoMapper;
import lombok.val;
import org.springframework.stereotype.Component;

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

/**
 * @author zcf
 * @date 2018-02-23 10：43
 * @description 商户支结算信息管理使用Repository
 */
@Component
public class TradeSettlementRepository {

	@Resource
	private TradeSettlementInfoMapper tradeSettlementInfoMapper;
	
	/**
	 * 商户结算信息查询方法
	 * @param orgId：集团编号
	 * @param shopId：门店编号
	 * @return
	 */
	public TradeSettlementInfo queryTradeSettlementInfo(Integer orgId, Integer shopId){
		val example = new TradeSettlementInfoExample();
		example.createCriteria()
				.andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId);
		val tradeSettlementInfos = tradeSettlementInfoMapper.selectByExample(example);
		return tradeSettlementInfos.size()>0?tradeSettlementInfos.get(0):null;
	}

	/**
	 * 商户结算信息查询方法
	 * @param shopId：门店编号
	 * @return
	 */
	public TradeSettlementInfo queryTradeSettlementInfo(Integer shopId){
		val example = new TradeSettlementInfoExample();
		example.createCriteria()
				.andShopIdEqualTo(shopId);
		val tradeSettlementInfos = tradeSettlementInfoMapper.selectByExample(example);
		return tradeSettlementInfos.size()>0?tradeSettlementInfos.get(0):null;
	}

	/**
	 * 商户结算信息查询方法
	 * @param mcode：商户号
	 * @return
	 */
	public TradeSettlementInfo queryTradeSettlementInfoByMcode(String mcode){
		val example = new TradeSettlementInfoExample();
		example.createCriteria()
				.andMcodeEqualTo(mcode);
		val tradeSettlementInfos = tradeSettlementInfoMapper.selectByExample(example);
		return tradeSettlementInfos.size()>0?tradeSettlementInfos.get(0):null;
	}

	/**
	 * 商户支付渠道信息保存方法
	 * @param tradeSettlementInfo：商户结算信息
	 * @return
	 */
	public Boolean saveTradeSettlementInfo(TradeSettlementInfo tradeSettlementInfo){
		val count = tradeSettlementInfoMapper.insertSelective(tradeSettlementInfo);
		return count>0?true:false;
	}

	/**
	 * 商户支付渠道信息保存方法
	 * @param tradeSettlementInfo：商户结算信息
	 * @return
	 */
	public Boolean updateTradeSettlementInfo(TradeSettlementInfo tradeSettlementInfo){
		val example = new TradeSettlementInfoExample();
		example.createCriteria()
				.andOrgIdEqualTo(tradeSettlementInfo.getOrgId())
				.andShopIdEqualTo(tradeSettlementInfo.getShopId());
		tradeSettlementInfo.setUpdatedAt(new Date());
		val count = tradeSettlementInfoMapper.updateByExampleSelective(tradeSettlementInfo,example);
		return count>0?true:false;
	}

}
