package com.crm.pay.service.impl.base;

import com.alibaba.fastjson.JSON;
import com.crm.pay.entity.FinanceDepositCallbackUpdateEntity;
import com.crm.pay.service.SysConfigService;
import com.crm.pay.service.SysRemittanceChannelService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.StatusConstants;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.model.pojo.table.SysRemittanceChannelTable;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 通道基础类 提供基础的方法
 *
 * @author houn
 */
public class BasePayServiceImpl extends BaseUploadServiceImpl {
    @Autowired
    private SysRemittanceChannelService sysRemittanceChannelService;
    @Autowired
    private SysConfigService sysConfigService;


    /**
     * 获取配置数据
     *
     * @return
     * @throws ServiceException
     */
    protected <T> T getPayProperty(String code, Class<T> tClass) throws Exception {

        return getPayProperty(code, true, tClass);
    }

    protected <T> T getPayProperty(String code, boolean valid, Class<T> tClass) throws Exception {

        SysRemittanceChannelTable payChannel = getPayChannel(code, valid);

        T object = getPayProperty(payChannel, tClass);

        return object;
    }

    protected SysRemittanceChannelTable getPayChannel(String code) throws Exception {

        return getPayChannel(code, true);
    }

    /**
     * 获取通道
     *
     * @param code  通道code
     * @param valid 通道是否一定需要启用 true必须启用才能获取到  false 不需要启用就能获取到
     * @return
     * @throws Exception
     */
    protected SysRemittanceChannelTable getPayChannel(String code, boolean valid) throws Exception {

        SysRemittanceChannelTable channel = sysRemittanceChannelService.getSysRemittanceChannelByCode(code, valid);
        if (channel == null || channel.getProperty() == null || channel.getProperty().trim().length() <= 0) {
            throw new ServiceException(PayConstants.FINANCE_DEPOSIT_CHANNEL_NOT_EMPTY);
        }

        return channel;
    }

    protected <T> T getPayProperty(SysRemittanceChannelTable payChannel, Class<T> tClass) throws Exception {

        String property = payChannel.getProperty();
        T object = JSON.parseObject(property, tClass);
        if (object == null) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

        return object;
    }

    protected FinanceDepositCallbackUpdateEntity getFinanceDepositCallbackUpdateEntity(String success, String orderNo, String transactionId, String status) throws ServiceException {

        return getFinanceDepositCallbackUpdateEntity(success, orderNo, null, transactionId, status);

    }

    protected FinanceDepositCallbackUpdateEntity getFinanceDepositCallbackUpdateEntity(String success, String orderNo, String middle, String transactionId, String status) throws ServiceException {

        return getFinanceDepositCallbackUpdateEntity(success, orderNo, middle, transactionId, status, null);
    }

    protected FinanceDepositCallbackUpdateEntity getFinanceDepositCallbackUpdateEntity(String success, String orderNo
            , String middle, String transactionId, String status, Object callback) throws ServiceException {

        int statusInt;
        if (success.equals(status)) {
            statusInt = StatusConstants.PAY_CALLBACK_COMPLETED_STATUS;
        } else {
            statusInt = StatusConstants.PAY_CALLBACK_FAIL_STATUS;
        }
        return getFinanceDepositCallbackUpdateEntity(orderNo, middle, transactionId, statusInt, callback);
    }

    protected FinanceDepositCallbackUpdateEntity getFinanceDepositCallbackUpdateEntity(String orderNo, String middle, String transactionId, int status, Object callback) throws ServiceException {
        if (Strings.isNullOrEmpty(middle)) {
            middle = "_";
        }

        String[] strings = orderNo.split(middle);
        if (strings == null || strings.length < 2) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        FinanceDepositCallbackUpdateEntity balanceCallbackUpdateEntity = new FinanceDepositCallbackUpdateEntity();
        if (Strings.isNullOrEmpty(transactionId)) {
            transactionId = null;
        }
        balanceCallbackUpdateEntity.setChannelSerial(transactionId);

        balanceCallbackUpdateEntity.setSerial(strings[0]);

        balanceCallbackUpdateEntity.setCallbackStatus(status);

        balanceCallbackUpdateEntity.setCallbackTime(new Date());
        if (callback != null) {
            balanceCallbackUpdateEntity.setCallbackData(JSON.toJSONString(callback));
        }
        return balanceCallbackUpdateEntity;
    }
}
