package com.uas.finance.service.Impl;

import com.uas.finance.constants.Status;
import com.uas.finance.dao.AccountApplyDao;
import com.uas.finance.dao.FinancingApplyDao;
import com.uas.finance.dao.SellerQuotaDao;
import com.uas.finance.model.AccountApply;
import com.uas.finance.model.FinancingApply;
import com.uas.finance.model.SellerQuota;
import com.uas.finance.service.FinancingApplyService;
import com.uas.finance.service.SellerQuotaService;
import com.uas.finance.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.StubNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 买方用户额度控制层
 * created by shicr on 2018/1/16
 **/
@Service
public class SellerQuotaServiceImpl implements SellerQuotaService {

    @Autowired
    private SellerQuotaDao dao;

    @Autowired
    private FinancingApplyDao financingApplyDao;

    @Autowired
    private AccountApplyDao accountApplyDao;


    /**
     * 接收从UAS传来的买方用户额度
     *
     * @param list
     * @return
     */
    public List<SellerQuota> getSeller(List<SellerQuota> list, Long faid, Integer status, Long access_id) {

        ArrayList<SellerQuota> arrayList = new ArrayList<SellerQuota>();

        if (list != null) {
            //找到申请单下面的所有买方额度
            List<SellerQuota> sellerQuotas = dao.findByFaid(faid);
            //之前的买方客户额度单据失效
            for (SellerQuota s : sellerQuotas) {
                s.setCq_status("UNVALID");
                dao.save(s);
            }
        }

        //保存新的买方客户额度
        if (list != null) {
            for (SellerQuota sellerQuota : list) {

                sellerQuota.setFaenuu(access_id);

                sellerQuota.setFaid(faid);
                //设置额度
                sellerQuota.setCq_quota(sellerQuota.getCq_quota());
                //设置已用额度
                sellerQuota.setCq_dueamount(0.00);
                //设置剩余额度
                sellerQuota.setCq_spamount(sellerQuota.getCq_quota());

                SellerQuota s = dao.save(sellerQuota);

                arrayList.add(s);
            }
        }

        FinancingApply apply = financingApplyDao.findOne(faid);
        apply.setApproveTime(new Date());
        if (status == 204) {
            apply.setStatus(Status.SUCCESS.code());
        } else if (status == 205) {
            apply.setStatus(Status.FAILED.code());
        }
        financingApplyDao.save(apply);

        return arrayList;
    }

    /**
     * 更新买方客户已用额度和还款状态
     *
     * @param sellerQuota 买方用户
     * @param id          应收账款转让单id
     * @param iscloseoff  应收账款是否还清
     * @return
     */
    public SellerQuota updateQuota(SellerQuota sellerQuota, Long id, String iscloseoff) {

        //更新出账单是否还款完毕状态
        AccountApply accountApply = accountApplyDao.findOne(id);
        accountApply.setAa_iscloseoff(iscloseoff);
        //设置出账申请已批复状态
        accountApply.setStatus(Status.SUCCESS.code());

        accountApplyDao.save(accountApply);

        if (sellerQuota != null) {
            //通过买方客户名和申请单
            SellerQuota s = dao.findByFaidAndCq_custname(sellerQuota.getFaid(), sellerQuota.getCq_custname());
            //获取买方已用额度
            Double money = s.getCq_dueamount();
            //重新设置买方已用额度
            if (sellerQuota.getCq_dueamount() != null) {

                money += sellerQuota.getCq_dueamount();
            }
            s.setCq_dueamount(money);
            //设置可用额度额度
            Double leftamount = s.getCq_quota() - money;
            s.setCq_spamount(leftamount);

            SellerQuota sellerQuota1 = dao.save(s);
            return sellerQuota1;
        }

        return sellerQuota;
    }

    /**
     * 找到某客户的所有买方
     *
     * @return
     */
    public List<SellerQuota> findAll(Long uu) {
        return dao.findByCq_uu(uu);
    }
}
