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


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.payment.persistent.domain.MchPayChannel;
import com.neusoft.smart.pos.payment.persistent.domain.MchPayChannelExample;
import com.neusoft.smart.pos.payment.persistent.mapper.MchPayChannelMapper;
import lombok.val;
import org.springframework.stereotype.Component;

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

/**
 * @author zcf
 * @date 2018-01-09 10：43
 * @description 支付管理使用Repository
 */
@Component
public class MchPayChannelRepository {

	@Resource
	private MchPayChannelMapper mchPayChannelMapper;

	/**
	 * 支付方式查询方法
	 * @param mcode：商户号
	 * @return
	 */
	public MchPayChannel queryMchPayChannelByMcode(String mcode, String payType){
		val example = new MchPayChannelExample();
		example.createCriteria()
				.andMcodeEqualTo(mcode)
				.andPayTypeEqualTo(payType);
		val mchPayChannels = mchPayChannelMapper.selectByExample(example);
		return mchPayChannels.size()>0?mchPayChannels.get(0):null;
	}

	/**
	 * 支付方式查询方法
	 * @param mcode：商户号
	 * @return
	 */
	public MchPayChannel queryMchPayChannelByMcodeWithBlob(String mcode, String payType){
		val example = new MchPayChannelExample();
		example.createCriteria()
				.andMcodeEqualTo(mcode)
				.andPayTypeEqualTo(payType);
		val mchPayChannels = mchPayChannelMapper.selectByExampleWithBLOBs(example);
		return mchPayChannels.size()>0?mchPayChannels.get(0):null;
	}

	/**
	 * 支付方式查询方法
	 * @param mcode：商户号
	 * @return
	 */
	public MchPayChannel queryMchPayChannelEnableByMcode(String mcode, String payType){
		val example = new MchPayChannelExample();
		example.createCriteria()
				.andMcodeEqualTo(mcode)
				.andIsEnableEqualTo(DictionaryConstant.IS_ENABLE_QY.getCode())
				.andPayTypeEqualTo(payType);
		val mchPayChannels = mchPayChannelMapper.selectByExample(example);
		return mchPayChannels.size()>0?mchPayChannels.get(0):null;
	}

	/**
	 * 支付方式查询方法
	 * @param mchId：商户号
	 * @param payType：支付方式
	 * @return
	 */
	public MchPayChannel queryMchPayChannelByMchId(String mchId,String payType){
		val example = new MchPayChannelExample();
		example.createCriteria()
				.andMchIdEqualTo(mchId)
				.andPayTypeEqualTo(payType);
		val mchPayChannels = mchPayChannelMapper.selectByExample(example);
		return mchPayChannels.size()>0?mchPayChannels.get(0):null;
	}

	/**
	 * 开通商户支付渠道方法
	 * @param mchPayChannel
	 * @return
	 */
	public MchPayChannel createMchPayChannel(MchPayChannel mchPayChannel){
		mchPayChannel.setIsEnable(DictionaryConstant.IS_ENABLE_QY.getCode());	//新建后默认为启用状态
		mchPayChannelMapper.insertSelective(mchPayChannel);
		return mchPayChannel;
	}

	/**
	 * 支付方式查询方法
	 * @param mcode：商户号
	 * @return
	 */
	public Boolean deleteMchPayChannelByMcode(String mcode, String payType){
		val example = new MchPayChannelExample();
		example.createCriteria()
				.andMcodeEqualTo(mcode)
				.andPayTypeEqualTo(payType);
		val count = mchPayChannelMapper.deleteByExample(example);
		return count>0?true:false;
	}

	/**
	 * 支付方式更新方法
	 * @return
	 */
	public Boolean updateMchPayChannelByMcode(MchPayChannel mchPayChannel){
		val example = new MchPayChannelExample();
		example.createCriteria()
				.andMcodeEqualTo(mchPayChannel.getMcode())
				.andPayTypeEqualTo(mchPayChannel.getPayType());
		mchPayChannel.setUpdatedAt(new Date());
//		val count = mchPayChannelMapper.updateByExampleSelective(mchPayChannel,example);
		val count = mchPayChannelMapper.updateByExampleWithBLOBs(mchPayChannel,example);
		return count>0?true:false;
	}
}
