package com.neusoft.smart.pos.trade.service;


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.trade.request.DeletePayChannelRequest;
import com.neusoft.smart.pos.dto.trade.request.GetPayChannelRequest;
import com.neusoft.smart.pos.dto.trade.request.PostPayChannelRequest;
import com.neusoft.smart.pos.dto.trade.request.PutPayChannelRequest;
import com.neusoft.smart.pos.dto.trade.response.*;
import com.neusoft.smart.pos.framework.constants.CommonErrorCode;
import com.neusoft.smart.pos.framework.constants.RoleConstant;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.framework.utils.ListUtils;
import com.neusoft.smart.pos.order.constants.TradeErrorCode;
import com.neusoft.smart.pos.order.repository.OrderMasterRepository;
import com.neusoft.smart.pos.payorder.persistent.domain.PayTypeDict;
import com.neusoft.smart.pos.payorder.persistent.domain.TradeMchPayChannel;
import com.neusoft.smart.pos.payorder.persistent.domain.TradeMchPayDetail;
import com.neusoft.smart.pos.payorder.repository.PayChannelRepository;
import com.neusoft.smart.pos.payorder.repository.PayOrderRepository;
import com.neusoft.smart.pos.payorder.repository.TradeMchPayDetailRepository;
import com.neusoft.smart.pos.payorder.repository.TradeSettlementRepository;
import com.neusoft.smart.pos.trade.repository.PayTypeRepository;
import com.neusoft.smart.pos.utils.PosDeviceRedisUtils;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service("payChannelService")
public class PayChannelService {

	@Autowired
	private OrderMasterRepository orderMasterRepository;

	@Autowired
    private PayChannelRepository payChannelRepository;

	@Autowired
    private TradeSettlementRepository settlementRepository;

	@Autowired
	private PayTypeRepository payTypeRepository;

	@Autowired
	private PayOrderRepository payOrderRepository;
	
	@Autowired
	private TradeMchPayDetailRepository tradeMchPayDetailRepository;


	/**
	 * POS支付渠道列表查询方法
	 * @return
	 */
	public GetPayChannelsResponse queryPayChannels(String serialNo){
		val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(serialNo);
		if(null == deviceInfo){
			throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
		}
		val orgId = deviceInfo.getOrganizationId();
		val shopId = deviceInfo.getStoreId();
		if(null == orgId || null == shopId){
			throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
		}
		GetPayChannelsResponse payChannelsResponse = queryPayChannels(orgId,shopId, DictionaryConstant.IS_ENABLE_QY.getCode(),null);
		for(int i=0;i<payChannelsResponse.getPayChannels().size();i++){
			if(payChannelsResponse.getPayChannels().get(i).getPayType().equals(DictionaryConstant.PAY_TYPE_WXZF_SERVICE.getCode())){
				payChannelsResponse.getPayChannels().remove(i);
				break;
			}
		}
		return payChannelsResponse;
	}

	/**
	 * 商户支付渠道列表查询方法
	 * @return
	 */
	public GetPayChannelsResponse queryPayChannels(Integer orgId, Integer shopId, String isEnable, String activeStatus) {
		Integer finalShopId = shopId;
		//1.查询商户结算类型
		val tradeSettlementInfo = settlementRepository.queryTradeSettlementInfo(orgId,shopId);
		if(null == tradeSettlementInfo){
			throw new BusinessCommonException(TradeErrorCode.TRADE_SETTLEMENT_INFO_EMPTY);
		}

		//2.根据结算类型查询当前门店支付渠道信息
		if(!DictionaryConstant.SETTLEMENT_TYPE_MD.getCode().equals(tradeSettlementInfo.getSettlementType())){
			shopId = 0;
		}
		val payChannels = payChannelRepository.queryShopPayChannels(orgId,shopId,isEnable,activeStatus);
		val payTypesResponse = new GetPayChannelsResponse();
		payTypesResponse.setPayChannels(ListUtils.convertListToList(payChannels, GetPayChannelsResponse.PayChannel.class));

		payTypesResponse.getPayChannels().forEach(payChannel->{
			PayTypeDict tradePayTypeDict = payTypeRepository.queryPayType(payChannel.getPayType());
			payChannel.setName(tradePayTypeDict.getName());
			payChannel.setPayImageUrl(tradePayTypeDict.getImageUrl());
			payChannel.setPayImageUrlThumb(tradePayTypeDict.getImageUrlThumb());
			payChannel.setCategoryType(tradePayTypeDict.getCategoryType());
			if(!payChannel.getPayType().equals(DictionaryConstant.PAY_TYPE_WXZFYS.getCode()) &&
					!payChannel.getPayType().equals(DictionaryConstant.PAY_TYPE_ZFBYS.getCode())){
				payChannel.setAppid("");
				payChannel.setMchId("");
				payChannel.setSecretKey("");
			}
		});

		return payTypesResponse;
	}

	/**
	 * 商户支付渠道保存方法
	 * @return
	 */
	public PostPayChannelResponse savePayChannel(PostPayChannelRequest payChannelRequest) {
		Integer orgId = RequestHeaderUtil.getOrgId();
		Integer shopId = RequestHeaderUtil.getStoreIdByRole(payChannelRequest.getShopId());
		return this.savePayChannel(payChannelRequest, orgId, shopId);
	}
	
	
	public PostPayChannelResponse savePayChannel(PostPayChannelRequest payChannelRequest, Integer orgId, Integer shopId) {
		if(null == orgId || null == shopId){
			throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
		}
		payChannelRequest.setShopId(shopId);
		
		//1.查询商户结算类型
		val tradeSettlementInfo = settlementRepository.queryTradeSettlementInfo(orgId,shopId);
		if(null == tradeSettlementInfo){
			throw new BusinessCommonException(TradeErrorCode.TRADE_SETTLEMENT_INFO_EMPTY);
		}
		//校验
		validateOperationRole(tradeSettlementInfo.getSettlementType(), RequestHeaderUtil.getRole());
		
		//2.根据支付类型校验参数信息
		TradeMchPayChannel tradeMchPayChannel = new TradeMchPayChannel();
		BeanUtils.copyProperties(payChannelRequest,tradeMchPayChannel);
		tradeMchPayChannel.setOrgId(orgId);
		
		if(tradeMchPayChannel.getPayType().equals(DictionaryConstant.PAY_TYPE_WXEWM.getCode())){
			if(null == tradeMchPayChannel.getCert() || tradeMchPayChannel.getCert().length == 0){
				throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_CERT_EMPTY);
			}
		}
		
		validateParamsByPayType(tradeMchPayChannel);
		
		//3.判断该支付渠道是否已经添加
		val tradeMchPayChannelTemp = payChannelRepository.queryShopPayChannel(orgId,shopId,payChannelRequest.getPayType());
		if(null != tradeMchPayChannelTemp){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_IS_EXISTS);
		}

		PayTypeDict tradePayTypeDict = payTypeRepository.queryEnabledPayType(payChannelRequest.getPayType());
		
		//4.根据支付方式判断是否需要配置第三方支付平台支付信息
		if(!StringUtils.isEmpty(tradePayTypeDict.getPayPlatformType())){
			//4.1 根据业务系统支付方式查询聚合支付平台支付信息
			//4.2 添加聚合支付平台渠道信息
			Boolean bool = payOrderRepository.savePayChannel(tradeMchPayChannel,tradeSettlementInfo,tradePayTypeDict);
			if(bool){
				//4.3 保存本地支付渠道信息
				return new PostPayChannelResponse(payChannelRepository.savePayChannel(tradeMchPayChannel));
			}else{
				throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_SAVE_ERROR);
			}
		}else{
			//保存本地支付渠道信息
			return new PostPayChannelResponse(payChannelRepository.savePayChannel(tradeMchPayChannel));
		}
	}

	/**
	 * 商户支付渠道更新方法
	 * @return
	 */
	public PutPayChannelResponse updatePayChannel(PutPayChannelRequest payChannelRequest) {
		Integer orgId = RequestHeaderUtil.getOrgId();
		Integer shopId = RequestHeaderUtil.getStoreIdByRole(payChannelRequest.getShopId());
		if(null == orgId || null == shopId){
			throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
		}
		payChannelRequest.setShopId(shopId);

		val tradeSettlementInfo = settlementRepository.queryTradeSettlementInfo(orgId,shopId);
		if(null == tradeSettlementInfo){
			throw new BusinessCommonException(TradeErrorCode.TRADE_SETTLEMENT_INFO_EMPTY);
		}
		//校验
		validateOperationRole(tradeSettlementInfo.getSettlementType(), RequestHeaderUtil.getRole());

		//1.根据支付类型校验参数信息
		TradeMchPayChannel tradeMchPayChannel = new TradeMchPayChannel();
		BeanUtils.copyProperties(payChannelRequest,tradeMchPayChannel);
		tradeMchPayChannel.setOrgId(orgId);
		validateParamsByPayType(tradeMchPayChannel);

		//2.判断该支付渠道是否已经添加
		val tradeMchPayChannelTemp = payChannelRepository.queryShopPayChannel(orgId,payChannelRequest.getShopId(),payChannelRequest.getPayType());
		if(null == tradeMchPayChannelTemp){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_IS_NOT_EXISTS);
		}
		PayTypeDict tradePayTypeDict = payTypeRepository.queryEnabledPayType(payChannelRequest.getPayType());

		//3.根据支付方式判断是否需要配置第三方支付平台支付信息
		if(!StringUtils.isEmpty(tradePayTypeDict.getPayPlatformType())){
			//3.1 根据业务系统支付方式查询聚合支付平台支付信息
			//3.3 更新聚合支付平台渠道信息
			Boolean bool = payOrderRepository.updatePayChannel(tradeMchPayChannel,tradeSettlementInfo,tradePayTypeDict);
			if(bool){
				//3.4 更新本地支付渠道信息
				return new PutPayChannelResponse(payChannelRepository.updatePayChannel(tradeMchPayChannel));
			}else{
				throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_SAVE_ERROR);
			}
		}else{
			//更新本地支付渠道信息
			return new PutPayChannelResponse(payChannelRepository.updatePayChannel(tradeMchPayChannel));
		}
	}

	/**
	 * 根据支付方式校验参数信息
	 * @param tradeMchPayChannel
	 */
	private void validateParamsByPayType(TradeMchPayChannel tradeMchPayChannel){
		DictionaryConstant dict = DictionaryConstant.getEnumByCode(DictionaryConstant.PAY_TYPE.getCode(),tradeMchPayChannel.getPayType());
		if (null == dict) {
			return;
		}
		switch(dict){
			//@1:微信支付
			case PAY_TYPE_WXEWM:
				validateWeixinParams(tradeMchPayChannel);
				break;
			//@2:支付宝支付
			case PAY_TYPE_ZFBEWM:
				validateZFBParams(tradeMchPayChannel);
				break;
			//@3:银行卡支付
			case PAY_TYPE_YHK:
				break;
			//@4:微信转账
			case PAY_TYPE_WXZZ:
				validateTransferAccountsParams(tradeMchPayChannel);
				break;
			//@5:支付宝转账
			case PAY_TYPE_ZFBZZ:
				validateTransferAccountsParams(tradeMchPayChannel);
				break;
			//@6:现金支付
			case PAY_TYPE_XJ:
				break;
			//@7:会员卡支付
			case PAY_TYPE_HYK:
				break;
			//@8:当面资金授权支付
			case PAY_TYPE_ZFBZZSQ:
				break;
			//@9:银联二维码支付
			case PAY_TYPE_YLEWM:
				break;
		}
	}

	/**
	 * 校验微信及支付宝转账参数
	 */
	private void validateTransferAccountsParams(TradeMchPayChannel tradeMchPayChannel){
		if(StringUtils.isEmpty(tradeMchPayChannel.getImageUrl())){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_IMAGE_URL_EMPTY);
		}
	}

	/**
	 * 校验微信参数
	 */
	private void validateWeixinParams(TradeMchPayChannel tradeMchPayChannel){
		if(StringUtils.isEmpty(tradeMchPayChannel.getAppid())){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_APPID_EMPTY);
		}
		if(StringUtils.isEmpty(tradeMchPayChannel.getMchId())){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_MCH_ID_EMPTY);
		}
		if(StringUtils.isEmpty(tradeMchPayChannel.getSecretKey())){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_SECRET_KEY_EMPTY);
		}
		if(StringUtils.isEmpty(tradeMchPayChannel.getScanType())){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_SCAN_TYPE_EMPTY);
		}
	}

	/**
	 * 校验支付宝参数
	 */
	private void validateZFBParams(TradeMchPayChannel tradeMchPayChannel){
		if(StringUtils.isEmpty(tradeMchPayChannel.getAppid())){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_APPID_EMPTY);
		}
		if(StringUtils.isEmpty(tradeMchPayChannel.getPrivateKey())){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_PRIVATE_KEY_EMPTY);
		}
		if(StringUtils.isEmpty(tradeMchPayChannel.getPublicKey())){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_PUBLIC_KEY_EMPTY);
		}
		if(StringUtils.isEmpty(tradeMchPayChannel.getScanType())){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_SCAN_TYPE_EMPTY);
		}
	}

	/**
	 * 商户支付渠道删除方法
	 * @return
	 */
	public DeletePayChannelResponse deletePayChannel(DeletePayChannelRequest payChannelRequest) {
		Integer orgId = RequestHeaderUtil.getOrgId();
		Integer shopId = RequestHeaderUtil.getStoreIdByRole(payChannelRequest.getShopId());
		if(null == orgId || null == shopId){
			throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
		}
		payChannelRequest.setShopId(shopId);

		val tradeSettlementInfo = settlementRepository.queryTradeSettlementInfo(orgId,shopId);
		if(null == tradeSettlementInfo){
			throw new BusinessCommonException(TradeErrorCode.TRADE_SETTLEMENT_INFO_EMPTY);
		}
		//校验
		validateOperationRole(tradeSettlementInfo.getSettlementType(), RequestHeaderUtil.getRole());

		//1.判断该支付渠道是否存在
		val tradeMchPayChannelTemp = payChannelRepository.queryShopPayChannel(orgId,payChannelRequest.getShopId(),payChannelRequest.getPayType());
		if(null == tradeMchPayChannelTemp){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_IS_NOT_EXISTS);
		}

		//2.判断该支付渠道是否已产生订单信息
		//TODO: 缺少检查
//		Boolean existOrder = orderMasterRepository.queryOrderByPayType(orgId,payChannelRequest.getShopId(),payChannelRequest.getPayType());
//		if(existOrder){
//			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_HAVE_ORDER);
//		}

		PayTypeDict tradePayTypeDict = payTypeRepository.queryEnabledPayType(payChannelRequest.getPayType());

		//3.根据支付方式判断是否需要配置第三方支付平台支付信息
		if(!StringUtils.isEmpty(tradePayTypeDict.getPayPlatformType())){
			//3.1 根据业务系统支付方式查询聚合支付平台支付信息
			//3.2 更新聚合支付平台渠道信息
			Boolean bool = payOrderRepository.deletePayChannel(tradeSettlementInfo,tradePayTypeDict);
			if(bool){
				//3.4 更新本地支付渠道信息
				return new DeletePayChannelResponse(payChannelRepository.deletePayChannel(orgId,payChannelRequest.getShopId(),payChannelRequest.getPayType()));
			}else{
				throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_DELETE_ERROR);
			}
		}else{
			//更新本地支付渠道信息
			return new DeletePayChannelResponse(payChannelRepository.deletePayChannel(orgId,payChannelRequest.getShopId(),payChannelRequest.getPayType()));
		}
	}

	/**
	 * 商户支付渠道详情查询方法
	 * @return
	 */
	public GetPayChannelResponse queryPayChannel(GetPayChannelRequest payChannelsRequest) {
		Integer orgId = RequestHeaderUtil.getOrgId();
		Integer shopIdTemp = RequestHeaderUtil.getStoreIdByRole(payChannelsRequest.getShopId());
		if(null == orgId || null == shopIdTemp){
			throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
		}
		payChannelsRequest.setShopId(shopIdTemp);

		//1.查询商户结算类型
		val tradeSettlementInfo = settlementRepository.queryTradeSettlementInfo(orgId,payChannelsRequest.getShopId());
		if(null == tradeSettlementInfo){
			throw new BusinessCommonException(TradeErrorCode.TRADE_SETTLEMENT_INFO_EMPTY);
		}

		//2.根据结算类型查询当前门店支付渠道信息
		Integer shopId = 0;
		if(DictionaryConstant.SETTLEMENT_TYPE_MD.getCode().equals(tradeSettlementInfo.getSettlementType())){
			shopId = payChannelsRequest.getShopId();
		}
		val payChannel = payChannelRepository.queryShopPayChannel(orgId,shopId,payChannelsRequest.getPayType());
		val payChannelResponse = new GetPayChannelResponse();
		BeanUtils.copyProperties(payChannel,payChannelResponse);
		return payChannelResponse;
	}
	
	/**
	 * 商户支付渠道详情查询方法
	 * @return
	 */
	public GetPayChannelResponse queryPayChannelForPos(GetPayChannelRequest payChannelsRequest) {
		val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(payChannelsRequest.getSerialNo());
		if(null == deviceInfo){
			throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
		}
		val orgId = deviceInfo.getOrganizationId();
		val shopId = deviceInfo.getStoreId();
		if(null == orgId || null == shopId){
			throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
		}
		
		//1.查询商户结算类型
		val tradeSettlementInfo = settlementRepository.queryTradeSettlementInfo(orgId,shopId);
		if(null == tradeSettlementInfo){
			throw new BusinessCommonException(TradeErrorCode.TRADE_SETTLEMENT_INFO_EMPTY);
		}
		
		//2.根据结算类型查询当前门店支付渠道信息
		Integer shopIdTemp = 0;
		if(DictionaryConstant.SETTLEMENT_TYPE_MD.getCode().equals(tradeSettlementInfo.getSettlementType())){
			shopIdTemp = shopId;
		}
		val payChannel = payChannelRepository.queryShopPayChannel(orgId,shopIdTemp,payChannelsRequest.getPayType());
		if(null == payChannel){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_IS_NOT_OPEN);
		}
		val payChannelResponse = new GetPayChannelResponse();
		BeanUtils.copyProperties(payChannel,payChannelResponse);
		if(payChannelsRequest.getPayType().equals(DictionaryConstant.PAY_TYPE_ZFB_FACE_PAY.getCode())){
			TradeMchPayDetail tradeMchPayDetail = tradeMchPayDetailRepository.queryOrderByOrderNumber(orgId,shopId,payChannelsRequest.getPayType());
			if(null != tradeMchPayDetail){
				payChannelResponse.setShopNo(tradeMchPayDetail.getStoreId());
			}
		}
		return payChannelResponse;
	}
	
	/**
	 * 商户支付渠道停启用更新方法
	 * @return
	 */
	public Boolean updatePayChannelEnable(Integer shopId,String payType,String isEnable) {
		Integer orgId = RequestHeaderUtil.getOrgId();
		shopId = RequestHeaderUtil.getStoreIdByRole(shopId);
		if(null == orgId || null == shopId){
			throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
		}

		val tradeSettlementInfo = settlementRepository.queryTradeSettlementInfo(orgId,shopId);
		if(null == tradeSettlementInfo){
			throw new BusinessCommonException(TradeErrorCode.TRADE_SETTLEMENT_INFO_EMPTY);
		}
		//校验
		validateOperationRole(tradeSettlementInfo.getSettlementType(), RequestHeaderUtil.getRole());

		//1.判断该支付渠道是否已经添加
		val tradeMchPayChannelTemp = payChannelRepository.queryShopPayChannel(orgId,shopId,payType);
		if(null == tradeMchPayChannelTemp){
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_IS_NOT_EXISTS);
		}

		TradeMchPayChannel tradeMchPayChannel = new TradeMchPayChannel();
		tradeMchPayChannel.setOrgId(orgId);
		tradeMchPayChannel.setShopId(shopId);
		tradeMchPayChannel.setPayType(payType);
		tradeMchPayChannel.setIsEnable(isEnable);
		//2.更新本地支付渠道信息
		return payChannelRepository.updatePayChannel(tradeMchPayChannel);
	}

	/**
	 * 根据结算类型及角色判断是否有支付渠道操作权限
	 * @param settlementType
	 * @param role
	 */
	private void validateOperationRole(String settlementType,String role){
		if(RoleConstant.ROLE_SA.equals(role)){
			//超管没有操作权限
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_USER_UNAUTHORIZED);
		}else if(RoleConstant.ROLE_ADMIN.equals(role)){
			//集团角色且门店结算方式不能保存
			if(settlementType.equals(DictionaryConstant.SETTLEMENT_TYPE_MD)){
				throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_USER_UNAUTHORIZED);
			}
		}if(RoleConstant.ROLE_USER.equals(role)){
			//门店角色且集团结算方式不能保存
			if(settlementType.equals(DictionaryConstant.SETTLEMENT_TYPE_JT)){
				throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_USER_UNAUTHORIZED);
			}
		}
	}

	/**
	 * 商户支付渠道详情查询方法
	 * @return
	 */
	public String queryAppIdByShopId(Integer shopIdTemp) {

		//1.查询商户结算类型
		val tradeSettlementInfo = settlementRepository.queryTradeSettlementInfo(shopIdTemp);
		if(null == tradeSettlementInfo){
			throw new BusinessCommonException(TradeErrorCode.TRADE_SETTLEMENT_INFO_EMPTY);
		}

		//2.根据结算类型查询当前门店支付渠道信息
		Integer shopId = 0;
		if(DictionaryConstant.SETTLEMENT_TYPE_MD.getCode().equals(tradeSettlementInfo.getSettlementType())){
			shopId = shopIdTemp;
		}
		val payChannel = payChannelRepository.queryShopPayChannel(tradeSettlementInfo.getOrgId(),shopId, DictionaryConstant.PAY_TYPE_WXZF_SERVICE.getCode());
		if(null == payChannel){
			throw new BusinessCommonException(TradeErrorCode.WX_PAY_CHANNEL_IS_NOT_OPEN);
		}
		return payChannel.getAppid();
	}
	/**
	 * POS支付渠道列表查询方法, 仅显示is_show
	 * @return
	 */
	public GetPayChannelsResponse queryPayChannelsOnShowPos(String serialNo) {
		val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(serialNo);
		if(null == deviceInfo){
			throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
		}
		val orgId = deviceInfo.getOrganizationId();
		val shopId = deviceInfo.getStoreId();
		if(null == orgId || null == shopId){
			throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
		}
		GetPayChannelsResponse payChannelsResponse = queryPayChannels(orgId,shopId,
				DictionaryConstant.IS_ENABLE_QY.getCode(), DictionaryConstant.TRADE_MCH_PAY_CHANNEL_ACTIVE_STATUS_SHOWN_ON_POS.getCode());
		for(int i=0;i<payChannelsResponse.getPayChannels().size();i++){
			if(payChannelsResponse.getPayChannels().get(i).getPayType().equals(DictionaryConstant.PAY_TYPE_WXZF_SERVICE.getCode())){
				payChannelsResponse.getPayChannels().remove(i);
				break;
			}
		}
		//只保留IS_ENABLE_QY和IS_SHOW_S的type
		List<PayTypeDict> payTypeDicts = payTypeRepository.queryPayTypesShowOnPos(DictionaryConstant.IS_ENABLE_QY.getCode());
		List<String> payTypeStrings = payTypeDicts.stream().map(o->o.getPayType()).collect(Collectors.toList());
		payChannelsResponse.getPayChannels().removeIf(o->!payTypeStrings.contains(o.payType));

		return payChannelsResponse;
	}
}
