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

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.framework.constants.ApplicationConstants;
import com.neusoft.smart.pos.framework.utils.DateUtils;
import com.neusoft.smart.pos.payment.repository.PayNotifyRepository;
import com.neusoft.smart.pos.payment.utils.LogUtils;
import com.neusoft.smart.pos.payorder.persistent.domain.PayOrder;
import com.neusoft.smart.pos.payorder.repository.MchPayChannelRepository;
import com.neusoft.smart.pos.payorder.repository.MchPayInfoRepository;
import com.neusoft.smart.pos.payorder.repository.PayOrderRepository;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service("xyhfPayNotifyService")
public class XYHFPayNotifyService {

	@Autowired
    private MchPayChannelRepository mchPayChannelRepository;

    @Autowired
    private PayOrderRepository payOrderRepository;


    @Autowired
    private MchPayInfoRepository mchPayInfoRepository;

    @Autowired
    private PayNotifyRepository payNotifyRepository;

    /**
     * 银联商务支付完成通知数据处理方法
     * @param notifyDataStr：微信回调数据
     * @return
     */
    public String xyhfNotifyDataHandle(String notifyDataStr){
        //2.根据微信商户号查询微信商户信息
        Map<String,String> notifyDataMap = (Map) JSONUtils.parse(notifyDataStr);
        String orderNumber = notifyDataMap.get("OUT_TRADE_NO").toString();
        String payType = DictionaryConstant.PAYMENT_PAY_TYPE_XYHF.getCode();
        PayOrder payOrder = payOrderRepository.queryPayOrderByNumber(orderNumber, payType);
        if(payOrder.getPayStatus().equals(DictionaryConstant.PAY_STATUS_YFK.getCode())){
            return ApplicationConstants.SUCCESS;
        }
        val mchPayChannel = mchPayChannelRepository.queryMchPayChannelByMcode(payOrder.getMcode(), payType);
        //3.校验微信回调签名信息
//        if(!ChinaumsUtils.checkSign(mchPayChannel.getSecretKey(),notifyDataMap)){
//            LogUtils.savePaymentTradeLog(mchPayChannel.getMcode(),orderNumber, payType,LogUtils.LOG_DESCRIPTION_ZFCGHD,ApplicationConstants.FAIL, JSON.toJSONString(notifyDataMap),DictionaryConstant.PAYMENT_REQUEST_TYPE_BD.getCode());
//            return ApplicationConstants.FAIL;
//        }

        //4.更新聚合支付平台订单支付状态信息
        val payOrderTemp = new PayOrder();
        if(notifyDataMap.get("TIME_END") != null){
            Date payDate = DateUtils.parse(notifyDataMap.get("TIME_END").toString(),DateUtils.FORMAT_LONG_UNMARK);
            payOrder.setPayTime(payDate);
            payOrderRepository.updateOrderPayStatus(payOrderTemp,mchPayChannel.getMcode(),orderNumber,payType);
        }

        //5.回调业务系统通知方法(由于目前聚合支付平台不支持独立回调请求，所以暂时只做请求转发)
        val mchPayInfo = mchPayInfoRepository.queryMchPayInfoByMcode(mchPayChannel.getMcode(),DictionaryConstant.PAYMENT_MCH_STATUS_SHTG.getCode());
        Boolean bool = payNotifyRepository.sendPayNotify(payOrder,mchPayInfo.getSecretKey());
        LogUtils.savePaymentTradeLog(mchPayChannel.getMcode(),orderNumber, payType,LogUtils.LOG_DESCRIPTION_ZFCGHD,ApplicationConstants.SUCCESS,JSON.toJSONString(notifyDataMap),DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());
        if(bool){
            return ApplicationConstants.SUCCESS;
        }else{
            return ApplicationConstants.FAIL;
        }
    }

}
