package com.ruoyi.web.modules.finance.support;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.finance.constant.FinanceConstants;
import com.ruoyi.web.modules.finance.mapper.BizShopWithdrawMapper;
import com.ruoyi.web.modules.store.constant.ShopConstants;

/**
 * 财务模块工具
 */
@Component
public class FinanceSupport
{
    @Autowired
    private BizShopWithdrawMapper withdrawMapper;

    public String nextWithdrawNo()
    {
        String prefix = "WD" + new SimpleDateFormat("yyyyMMdd").format(new Date());
        String max = withdrawMapper.selectMaxWithdrawNoByPrefix(prefix);
        int seq = 1;
        if (StringUtils.isNotEmpty(max) && max.length() > prefix.length())
        {
            try
            {
                seq = Integer.parseInt(max.substring(prefix.length())) + 1;
            }
            catch (NumberFormatException ignored)
            {
                seq = 1;
            }
        }
        return prefix + String.format("%06d", seq);
    }

    public String maskAccountNo(String accountType, String accountNo)
    {
        if (StringUtils.isEmpty(accountNo))
        {
            return "";
        }
        if (FinanceConstants.ACCOUNT_TYPE_BANK.equals(accountType))
        {
            if (accountNo.length() <= 4)
            {
                return accountNo;
            }
            return "**** **** **** " + accountNo.substring(accountNo.length() - 4);
        }
        if (accountNo.length() <= 3)
        {
            return accountNo;
        }
        if (accountNo.length() <= 6)
        {
            return accountNo.charAt(0) + "***" + accountNo.charAt(accountNo.length() - 1);
        }
        return accountNo.substring(0, 2) + "****" + accountNo.substring(accountNo.length() - 2);
    }

    public String toAccountTypeText(String accountType)
    {
        if (FinanceConstants.ACCOUNT_TYPE_BANK.equals(accountType))
        {
            return "银行卡";
        }
        if (FinanceConstants.ACCOUNT_TYPE_ALIPAY.equals(accountType))
        {
            return "支付宝";
        }
        if (FinanceConstants.ACCOUNT_TYPE_WECHAT.equals(accountType))
        {
            return "微信";
        }
        return "—";
    }

    public String toWithdrawStatusText(String status)
    {
        if (FinanceConstants.WITHDRAW_STATUS_PENDING.equals(status))
        {
            return "待审核";
        }
        if (FinanceConstants.WITHDRAW_STATUS_REJECTED.equals(status))
        {
            return "审核不通过";
        }
        if (FinanceConstants.WITHDRAW_STATUS_COMPLETED.equals(status))
        {
            return "提现完成";
        }
        return "—";
    }

    public String toChangeReasonText(String reason)
    {
        if (FinanceConstants.REASON_ORDER_SETTLE.equals(reason))
        {
            return "订单收支";
        }
        if (FinanceConstants.REASON_WITHDRAW_APPLY.equals(reason))
        {
            return "余额提现";
        }
        if (FinanceConstants.REASON_WITHDRAW_REJECT.equals(reason))
        {
            return "提现驳回";
        }
        if (FinanceConstants.REASON_WITHDRAW_COMPLETE.equals(reason))
        {
            return "提现完成";
        }
        return "—";
    }

    public String toShopStatusText(String shopStatus, String delFlag)
    {
        if (ShopConstants.DEL_DELETED.equals(delFlag))
        {
            return "已删除";
        }
        if (ShopConstants.STATUS_OPEN.equals(shopStatus))
        {
            return "开业";
        }
        if (ShopConstants.STATUS_CLOSED.equals(shopStatus))
        {
            return "停业";
        }
        return "—";
    }

    public String buildAccountSummary(String accountType, String realName, String accountNoMask)
    {
        return toAccountTypeText(accountType) + " " + accountNoMask + " " + realName;
    }
}
