package com.crm.pay.controller.base;

import com.crm.pay.service.SysConfigService;
import com.crm.rely.backend.configuration.LocaleMessage;
import com.crm.rely.backend.core.constant.GlobalConfigConstants;
import com.crm.rely.backend.core.pojo.table.SysConfigTable;
import com.crm.rely.backend.core.exception.PayValidatedException;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.finance.deposit.FinanceDepositAddEntity;
import com.crm.rely.backend.util.GetIpAndMac;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;

public abstract class BasePayController {

    @Autowired
    private LocaleMessage localeMessage;

    @Autowired
    private SysConfigService sysConfigService;

    protected FinanceDepositAddEntity getFinanceDepositSaveEntity(Long login, BigDecimal amount, String payType, CustomInfoEntity infoEntity) {

        return getFinanceDepositSaveEntity(login, amount, payType, null, infoEntity);
    }

    protected FinanceDepositAddEntity getFinanceDepositSaveEntity(Long login, BigDecimal amount, String payType,
                                                                  String voucherUrl, CustomInfoEntity infoEntity) {
        FinanceDepositAddEntity financeDepositAddEntity = new FinanceDepositAddEntity();
        financeDepositAddEntity.setAmount(amount);
        financeDepositAddEntity.setLogin(login);
        financeDepositAddEntity.setAddTime(new Date());
        financeDepositAddEntity.setPayType(payType);
        financeDepositAddEntity.setVoucherUrl(voucherUrl);
        financeDepositAddEntity.setCustomId(infoEntity.getId());

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            String ip = GetIpAndMac.getIp(request);
            financeDepositAddEntity.setIp(ip);
        }

        return financeDepositAddEntity;
    }

    /**
     * 验证login和balanceNumber是否为null和0
     *
     * @param login  入金的账号
     * @param amount 入金金额($)
     * @return 返回错误信息
     */
    protected void validated(Long login, BigDecimal amount) throws PayValidatedException {
        String error = null;
        if (login == null || login <= 0) {
            error = localeMessage.getMessage(PayConstants.PAY_LOGIN_NOT_EMPTY, PayConstants.PAY_LOGIN_NOT_EMPTY);
        } else if (amount == null) {
            error = localeMessage.getMessage(PayConstants.PAY_REMITTANCE_AMOUNT_NOT_EMPTY, PayConstants.PAY_REMITTANCE_AMOUNT_NOT_EMPTY);
        }
        if (!Strings.isNullOrEmpty(error)) {
            //逻辑最好是重定向到特定页面 然后显示对应错误 现在直接返回错误信息 在页面上打印出来
            throw new PayValidatedException(error);
        }
    }

    protected void sendRedirect(HttpServletResponse response) throws Exception {
        String url = "";
        SysConfigTable sysConfigTable = sysConfigService.getByCode(GlobalConfigConstants.WEB_MY_URE_CONFIG);
        if (sysConfigTable != null) {
            url = sysConfigTable.getValue();
        }
        response.sendRedirect(url + "/payment.html");
    }

}
