package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.SysPaymentConfigDao;
import com.ym.mec.biz.dal.entity.SysPaymentConfig;
import com.ym.mec.biz.dal.enums.AccountType;
import com.ym.mec.biz.dal.enums.PaymentChannelEnum;
import com.ym.mec.biz.service.SysPaymentConfigService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysPaymentConfigServiceImpl extends BaseServiceImpl<Integer, SysPaymentConfig> implements SysPaymentConfigService {
    @Autowired
    private SysPaymentConfigDao sysPaymentConfigDao;

    @Override
    public BaseDAO<Integer, SysPaymentConfig> getDAO() {
        return sysPaymentConfigDao;
    }

    @Override
    public SysPaymentConfig findPaymentConfigByOrganId(Integer organId) {
        return sysPaymentConfigDao.findPaymentConfigByOrganId(organId);
    }

    @Override
    public List<SysPaymentConfig> findPaymentConfigByOrganIds(List<Integer> organIds) {
        return sysPaymentConfigDao.findPaymentConfigByOrganIds(organIds);
    }

    @Override
    public List<SysPaymentConfig> getPaymentConfigs(PaymentChannelEnum payType) {
        return sysPaymentConfigDao.getPaymentConfigs(payType);
    }

    @Override
    public AccountType checkAccountType(PaymentChannelEnum payType, String merNos) {
        if (payType == null || (!payType.equals(PaymentChannelEnum.ADAPAY) && !payType.equals(PaymentChannelEnum.YQPAY)) || StringUtils.isBlank(merNos)) {
            return AccountType.INTERNAL;
        }
        List<SysPaymentConfig> accounts = sysPaymentConfigDao.getOutAccounts(payType);
        String[] merNoArr = merNos.split(",");
        AccountType accountType = AccountType.INTERNAL;
        NODE1:
        for (String merNo : merNoArr) {
            for (SysPaymentConfig account : accounts) {
                String accountNo = "";
                if (payType.equals(PaymentChannelEnum.ADAPAY)) {
                    accountNo = account.getHfMerNo();
                } else {
                    accountNo = account.getYqMerNo();
                }
                if (accountNo.equals(merNo)) {
                    accountType = AccountType.EXTERNAL;
                    break NODE1;
                }
            }
        }
        return accountType;
    }

    @Override
    public SysPaymentConfig findPaymentConfigByMerNo(String merNo) {
        return sysPaymentConfigDao.findPaymentConfigByMerNo(merNo);
    }
}
