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


import com.neusoft.smart.pos.dto.trade.request.PostGrantOrderRequest;
import com.neusoft.smart.pos.dto.trade.request.PostGrantToPayRequest;
import com.neusoft.smart.pos.dto.trade.request.PostReverseGrantRequest;
import com.neusoft.smart.pos.dto.trade.request.PostUnFreezeGrantRequest;
import com.neusoft.smart.pos.dto.trade.response.*;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.constants.ApplicationConstants;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteGrantOrderService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("grantOrderService")
public class GrantOrderService {
    
    @Autowired
    private RemoteGrantOrderService remoteGrantOrderService;
    
    /**
     * 资金预授权订单信息创建方法
     *
     * @return
     */
    public PostGrantOrderResponse saveGrantOrder(PostGrantOrderRequest grantOrderRequest) {
        val orderResponseData = remoteGrantOrderService.saveGrantOrder(grantOrderRequest);
        if (null == orderResponseData || !ApplicationConstants.SUCCESS_CODE.equals(orderResponseData.getCode())) {
            throw new BusinessCommonException(orderResponseData);
        }
        return orderResponseData.getResult();
    }
    
    /**
     * 资金预授权资金解冻方法
     *
     * @return
     */
    public PostUnFreezeGrantResponse unFreezeGrant(PostUnFreezeGrantRequest unFreezeGrantRequest) {
        val orderResponseData = remoteGrantOrderService.unFreezeGrant(unFreezeGrantRequest);
        if (null == orderResponseData || !ApplicationConstants.SUCCESS_CODE.equals(orderResponseData.getCode())) {
            throw new BusinessCommonException(orderResponseData);
        }
        return orderResponseData.getResult();
    }
    
    /**
     * 资金预授权资金解冻方法
     *
     * @return
     */
    public PostReverseGrantResponse reverseGrant(PostReverseGrantRequest reverseGrantRequest) {
        val orderResponseData = remoteGrantOrderService.reverseGrant(reverseGrantRequest);
        if (null == orderResponseData || !ApplicationConstants.SUCCESS_CODE.equals(orderResponseData.getCode())) {
            throw new BusinessCommonException(orderResponseData);
        }
        return orderResponseData.getResult();
    }
    
    /**
     * 资金预授权资金解冻方法
     *
     * @return
     */
    public PaginationExtendBean<GetGrantOrderResponse> queryGrantOrders(String serialNo,
                                                                        String grantOrderNumber,
                                                                        String grantStatus,
                                                                        String startTime,
                                                                        String endTime,
                                                                        Integer offset,
                                                                        Integer limit,
                                                                        Boolean isPage) {
        val orderResponseData = remoteGrantOrderService.queryGrantOrders(serialNo,grantOrderNumber,grantStatus,startTime,endTime,offset,limit,isPage);
        if (null == orderResponseData || !ApplicationConstants.SUCCESS_CODE.equals(orderResponseData.getCode())) {
            throw new BusinessCommonException(orderResponseData);
        }
        return orderResponseData.getResult();
    }
    
    /**
     * 资金预授权资金解冻方法
     *
     * @return
     */
    public GetGrantOrderDetailResponse queryGrantOrderDetails(String serialNo, String grantOrderNumber) {
        val orderResponseData = remoteGrantOrderService.queryGrantOrderDetails(serialNo,grantOrderNumber);
        if (null == orderResponseData || !ApplicationConstants.SUCCESS_CODE.equals(orderResponseData.getCode())) {
            throw new BusinessCommonException(orderResponseData);
        }
        return orderResponseData.getResult();
    }
    
    /**
     * 资金预授权资金解冻方法
     *
     * @return
     */
    public PostGrantToPayResponse grantToPay(PostGrantToPayRequest grantToPayRequest) {
        val orderResponseData = remoteGrantOrderService.grantToPay(grantToPayRequest);
        if (null == orderResponseData || !ApplicationConstants.SUCCESS_CODE.equals(orderResponseData.getCode())) {
            throw new BusinessCommonException(orderResponseData);
        }
        return orderResponseData.getResult();
    }
}
