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

import com.ym.mec.biz.dal.dao.StudentPaymentOrderDao;
import com.ym.mec.biz.dal.dao.SysAccountDao;
import com.ym.mec.biz.dal.entity.SysAccount;
import com.ym.mec.biz.dal.entity.SysArea;
import com.ym.mec.biz.service.SysAccountService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.thirdparty.adapay.ConfigInit;
import com.ym.mec.util.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysAccountServiceImpl extends BaseServiceImpl<Integer, SysAccount> implements SysAccountService {

    static private SysAccount originalAccount;

    @Autowired
    private SysAccountDao sysAccountDao;
    @Autowired
    private StudentPaymentOrderDao studentPaymentOrderDao;

    @Override
    public BaseDAO<Integer, SysAccount> getDAO() {
        return sysAccountDao;
    }

    @Override
    public List<SysAccount> getAccount(String company, String channel, String type, BigDecimal amount) {
        return sysAccountDao.getAccount(company, channel, type, amount);
    }

    @Override
    public SysAccount getOneAccount(String company, String channel, String type, BigDecimal amount) {
        return sysAccountDao.getOneAccount(company, channel, type, amount);
    }

    @Override
    public SysAccount getPerAccount(String channel, SysAccount account) {
        BigDecimal maxReceive = new BigDecimal("75000");
        Date nowDate = new Date();
        Date firstDayOfMonth = DateUtil.getFirstDayOfMonth(nowDate);

        BigDecimal amount = studentPaymentOrderDao.sumPerAccountAmount(account.getRoutingMerNo(), firstDayOfMonth);

        if (amount == null || amount.compareTo(maxReceive) < 0) {
            return account;
        }

        SysAccount newAccount = null;
        List<SysAccount> perAccounts = sysAccountDao.getPerAccount(channel);
        for (SysAccount perAccount : perAccounts) {
            amount = studentPaymentOrderDao.sumPerAccountAmount(perAccount.getRoutingMerNo(), firstDayOfMonth);
            if (amount == null || amount.compareTo(maxReceive) < 0) {
                newAccount = perAccount;
                break;
            }
        }
        return newAccount;
    }

    @Override
    public List<SysAccount> getAdaAccount(Integer organId, String channel, BigDecimal amount) {
        List<SysAccount> accounts = new ArrayList<>();
        SysAccount account = sysAccountDao.getAdaAccount(organId, channel);

        if (account == null && originalAccount == null) {
            account = sysAccountDao.getAdaAccount(ConfigInit.organId, channel);
            accounts.add(account);
            return accounts;
        } else if (account == null) {
            accounts.add(originalAccount);
            return accounts;
        }

        if (originalAccount != null && originalAccount.getRoutingOrganId().equals(account.getRoutingOrganId())) {
            accounts.add(originalAccount);
            return accounts;
        }
        originalAccount = account;

        if (account.getMaxReceipt().compareTo(BigDecimal.ZERO) <= 0) {
            accounts.add(account);
            return accounts;
        }

        Date nowDate = new Date();
        Date firstDayOfMonth = DateUtil.getFirstDayOfMonth(nowDate);
        BigDecimal hasReceipt = studentPaymentOrderDao.sumAccountAmount(account.getMerNo(), firstDayOfMonth);
        hasReceipt = hasReceipt == null ? BigDecimal.ZERO : hasReceipt;
        if (hasReceipt.add(amount).compareTo(account.getMaxReceipt()) < 0) {
            accounts.add(account);
        } else {
            if(StringUtils.isBlank(account.getRoutingMerNo())){
                accounts.add(originalAccount);
            }else {
                String routingMerNo = account.getRoutingMerNo();
                return getAdaAccount(Integer.parseInt(routingMerNo), channel, amount);
            }
        }
        return accounts;
    }

}