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

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.trade.request.PostInitFacePayRequest;
import com.neusoft.smart.pos.dto.trade.response.PostInitFacePayResponse;
import com.neusoft.smart.pos.dto.trade.response.PostPaymentInitFacePayResponse;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.order.repository.OrderMasterRepository;
import com.neusoft.smart.pos.payment.constants.PaymentErrorCode;
import com.neusoft.smart.pos.payorder.persistent.domain.PayTypeDict;
import com.neusoft.smart.pos.payorder.persistent.domain.TradeSettlementInfo;
import com.neusoft.smart.pos.payorder.repository.PayChannelRepository;
import com.neusoft.smart.pos.payorder.repository.PayTypeDictRepository;
import com.neusoft.smart.pos.payorder.repository.TradeSettlementRepository;
import com.neusoft.smart.pos.utils.PosDeviceRedisUtils;
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;

@Service("facePayService")
public class FacePayService {
	
	@Autowired
	private OrderMasterRepository orderMasterRepository;
	
	@Autowired
	private TradeSettlementRepository tradeSettlementRepository;

	@Autowired
	private PayChannelRepository payChannelRepository;

	@Autowired
	private PayTypeDictRepository payTypeDictRepository;

	@Autowired
    private PaymentService paymentService;

	/**
	 * 人脸支付初始化方法
	 * @return
	 */
	public PostInitFacePayResponse initFacePay(PostInitFacePayRequest initFacePayRequest,String faceType) {
		//1.查询订单信息，并进行校验
        val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(initFacePayRequest.getSerialNo());
        if(null == deviceInfo){
            throw new BusinessCommonException(PaymentErrorCode.DEVICE_ORG_EMPTY);
        }
        val orgId = deviceInfo.getOrganizationId();
        val shopId = deviceInfo.getStoreId();
		
		//1.查询商户结算及聚合支付平台商户信息
		TradeSettlementInfo tradeSettlementInfo = tradeSettlementRepository.queryTradeSettlementInfo(orgId,shopId);
		if(null == tradeSettlementInfo){
			throw new BusinessCommonException(PaymentErrorCode.TRADE_SETTLEMENT_INFO_EMPTY);
		}
		
		//2.查询商户是否开通此支付方式渠道
		Integer shopIdTemp = 0;
		if(DictionaryConstant.SETTLEMENT_TYPE_MD.getCode().equals(tradeSettlementInfo.getSettlementType())){
			shopIdTemp = shopId;
		}else{
			tradeSettlementInfo = tradeSettlementRepository.queryTradeSettlementInfo(orgId,shopIdTemp);
		}
		val tradeMchPayChannel = payChannelRepository.queryShopPayChannelByPayType(orgId,shopIdTemp,initFacePayRequest.getPayType());
		if(null == tradeMchPayChannel){
			throw new BusinessCommonException(PaymentErrorCode.PAY_CHANNEL_IS_NOT_OPEN);
		}
		
		//3.根据支付方式查询对应聚合支付平台支付信息
		val tradePayTypeDict = payTypeDictRepository.queryEnablePayTypeDict(initFacePayRequest.getPayType());
		if(null == tradePayTypeDict){
			throw new BusinessCommonException(PaymentErrorCode.DICT_PAY_TYPE_NOT_EXIST);
		}
		if(StringUtils.isEmpty(tradePayTypeDict.getPayPlatformType())){
			throw new BusinessCommonException(PaymentErrorCode.PAY_TYPE_ERROR);
		}
		
		//4.调用聚合支付平台生成预付单信息
		PostPaymentInitFacePayResponse paymentInitFacePayResponse = initFacePay(initFacePayRequest,faceType,tradeSettlementInfo,tradePayTypeDict);
		if(null == paymentInitFacePayResponse){
			throw new BusinessCommonException(PaymentErrorCode.PAYMENT_INIT_FACE_PAY_FAIL);
		}
		
		PostInitFacePayResponse initFacePayResponse = new PostInitFacePayResponse();
		initFacePayResponse.setInitStatus(paymentInitFacePayResponse.getInitStatus());
		initFacePayResponse.setZimId(paymentInitFacePayResponse.getZimId());
		initFacePayResponse.setZimInitClientData(paymentInitFacePayResponse.getZimInitClientData());
		initFacePayResponse.setAuthinfo(paymentInitFacePayResponse.getAuthinfo());
		initFacePayResponse.setExpiresIn(paymentInitFacePayResponse.getExpiresIn());
		initFacePayResponse.setOrderNumber(orderMasterRepository.generateOrderNumber());
		return initFacePayResponse;
	}

	private PostPaymentInitFacePayResponse initFacePay(PostInitFacePayRequest facePayRequest,String faceType,TradeSettlementInfo tradeSettlementInfo, PayTypeDict payTypeDict){
		val initFacePayRequest = new com.neusoft.smart.pos.dto.payment.request.PostInitFacePayRequest();
		initFacePayRequest.setMcode(tradeSettlementInfo.getMcode());
		initFacePayRequest.setPayType(payTypeDict.getPayPlatformType());
		initFacePayRequest.setZimmetainfo(facePayRequest.getZimmetainfo());
		initFacePayRequest.setDeviceId(facePayRequest.getDeviceId());
		initFacePayRequest.setStoreId(facePayRequest.getStoreId());
		initFacePayRequest.setStoreName(facePayRequest.getStoreName());
		initFacePayRequest.setFaceType(faceType);
		if(facePayRequest.getCardNo()!= null &&!("").equals(facePayRequest.getCardNo())){
			initFacePayRequest.setUserName(facePayRequest.getUserName());
			initFacePayRequest.setCardNo(facePayRequest.getCardNo());
			if("1".equals(faceType)){
				initFacePayRequest.setBizType("8");
			}else{
				initFacePayRequest.setBizType("1");
			}
		}else{
			if("1".equals(faceType)){
				//刷脸支付
				initFacePayRequest.setBizType("8");
			}
		}
		com.neusoft.smart.pos.dto.payment.response.PostInitFacePayResponse initFacePayResponse = paymentService.initFacePay(initFacePayRequest);
        PostPaymentInitFacePayResponse postInitFacePayResponse = new PostPaymentInitFacePayResponse();
        BeanUtils.copyProperties(initFacePayResponse, postInitFacePayResponse);
		return postInitFacePayResponse;
	}
	
}
