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


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.payorder.persistent.domain.TradeMchPayChannel;
import com.neusoft.smart.pos.payorder.persistent.domain.TradeMchPayChannelExample;
import com.neusoft.smart.pos.payorder.persistent.mapper.TradeMchPayChannelMapper;
import lombok.val;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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

/**
 * @author zcf
 * @date 2017-08-13 10：43
 * @description 商户支付渠道管理使用Repository
 */
@Component
public class PayChannelRepository {

	@Resource
	private TradeMchPayChannelMapper tradeMchPayChannelMapper;
	
	/**
	 * 商户支付渠道信息查询方法
	 * @param orgId：集团编号
	 * @param shopId：门店编号
	 * @return
	 */
	public List<TradeMchPayChannel> queryShopPayChannels(Integer orgId, Integer shopId, String isEnable, String activeStatus){
		val example = new TradeMchPayChannelExample();
		example.createCriteria()
				.andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId);
		if(!StringUtils.isEmpty(isEnable)){
			example.getOredCriteria().get(0).andIsEnableEqualTo(isEnable);
		}
		if(!StringUtils.isEmpty(activeStatus)){
			example.getOredCriteria().get(0).andActiveStatusEqualTo(activeStatus);
		}
		example.setOrderByClause("priority desc,created_at asc");
		return tradeMchPayChannelMapper.selectByExample(example);
	}
	
	/**
	 * 商户支付渠道信息查询方法
	 * @param orgId：集团编号
	 * @return
	 */
	public List<String> queryOrgPayTypeList(Integer orgId){
		return queryOrgPayTypeList(orgId,null);
	}
	
	/**
	 * 商户支付渠道信息查询方法
	 * @param orgId：集团编号
	 * @return
	 */
	public List<String> queryOrgPayTypeList(Integer orgId,String isEnable){
		val example = new TradeMchPayChannelExample();
		example.createCriteria()
				.andOrgIdEqualTo(orgId);
		if(!StringUtils.isEmpty(isEnable)){
			example.getOredCriteria().get(0).andIsEnableEqualTo(isEnable);
		}
		example.setOrderByClause("priority desc,created_at asc");
		List<TradeMchPayChannel> tradeMchPayChannelList = tradeMchPayChannelMapper.selectByExample(example);
		List<String> payTypeList = new ArrayList<>();
		tradeMchPayChannelList.forEach(tradeMchPayChannel -> {
			payTypeList.add(tradeMchPayChannel.getPayType());
		});
		HashSet payTypeListSet = new HashSet(payTypeList);
		payTypeList.clear();
		payTypeList.addAll(payTypeListSet);
		Collections.sort(payTypeList, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				if(Integer.parseInt(o1) >= Integer.parseInt(o2)){
					return 1;
				}else{
					return -1;
				}
			}
		});
		return payTypeList;
	}
	
	/**
	 * 商户支付渠道信息查询方法
	 * @param orgId：集团编号
	 * @param shopId：门店编号
	 * @return
	 */
	public TradeMchPayChannel queryShopPayChannel(Integer orgId, Integer shopId,String payType){
		val example = new TradeMchPayChannelExample();
		example.createCriteria()
				.andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId)
				.andPayTypeEqualTo(payType);
		val tradeMchPayChannels = tradeMchPayChannelMapper.selectByExample(example);
		return tradeMchPayChannels.size() > 0?tradeMchPayChannels.get(0):null;
	}

	/**
	 * 商户支付渠道信息查询方法
	 * @param orgId：集团编号
	 * @param shopId：门店编号
	 * @return
	 */
	public List<TradeMchPayChannel> queryShopPayChannels(Integer orgId, Integer shopId){
		val example = new TradeMchPayChannelExample();
		example.createCriteria()
				.andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId);
		val tradeMchPayChannels = tradeMchPayChannelMapper.selectByExample(example);
		return tradeMchPayChannels;
	}

	/**
	 * 商户支付渠道信息查询方法
	 * @param shopId：门店编号
	 * @return
	 */
	public TradeMchPayChannel queryShopPayChannel(Integer shopId,String payType){
		val example = new TradeMchPayChannelExample();
		example.createCriteria()
				.andShopIdEqualTo(shopId)
				.andPayTypeEqualTo(payType);
		val tradeMchPayChannels = tradeMchPayChannelMapper.selectByExample(example);
		return tradeMchPayChannels.size() > 0?tradeMchPayChannels.get(0):null;
	}

	/**
	 * 商户支付渠道信息查询方法
	 * @param orgId：集团编号
	 * @param shopId：门店编号
	 * @param payType：付款方式编号
	 * @1:微信支付
	 * @2:支付宝支付
	 * @3:银行卡支付
	 * @4:微信转账
	 * @:5支付宝转账
	 * @6:现金支付
	 * @7:会员卡支付
	 * @8:当面资金授权支付
	 * @9:银联二维码支付
	 * @10:微信商户主动扫码支付
	 * @return
	 */
	public TradeMchPayChannel queryShopPayChannelByPayType(Integer orgId, Integer shopId,String payType){
		val example = new TradeMchPayChannelExample();
		example.createCriteria()
				.andIsEnableEqualTo(DictionaryConstant.IS_ENABLE_QY.getCode())
				.andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId)
				.andPayTypeEqualTo(payType);
		val tradeMchPayChannels = tradeMchPayChannelMapper.selectByExample(example);
		return tradeMchPayChannels.size()>0?tradeMchPayChannels.get(0):null;
	}

	/**
	 * 商户支付渠道信息保存方法
	 * @param tradeMchPayChannel：支付渠道信息
	 * @return
	 */
	public Boolean savePayChannel(TradeMchPayChannel tradeMchPayChannel){
		val count = tradeMchPayChannelMapper.insertSelective(tradeMchPayChannel);
		return count>0?true:false;
	}

	/**
	 * 商户支付渠道信息更新方法
	 * @param tradeMchPayChannel：支付渠道信息
	 * @return
	 */
	public Boolean updatePayChannel(TradeMchPayChannel tradeMchPayChannel){
		val example = new TradeMchPayChannelExample();
		example.createCriteria()
				.andOrgIdEqualTo(tradeMchPayChannel.getOrgId())
				.andShopIdEqualTo(tradeMchPayChannel.getShopId())
				.andPayTypeEqualTo(tradeMchPayChannel.getPayType());
		val count = tradeMchPayChannelMapper.updateByExampleSelective(tradeMchPayChannel,example);
		return count>0?true:false;
	}

	/**
	 * 商户支付渠道信息更新方法
	 * @return
	 */
	public Boolean deletePayChannel(Integer orgId,Integer shopId,String payType){
		val example = new TradeMchPayChannelExample();
		example.createCriteria()
				.andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId)
				.andPayTypeEqualTo(payType);
		val count = tradeMchPayChannelMapper.deleteByExample(example);
		return count>0?true:false;
	}
}
