package com.uas.finance.service.Impl;

import com.uas.finance.constants.Status;
import com.uas.finance.dao.AccountApplyDao;
import com.uas.finance.dao.SellerQuotaDao;
import com.uas.finance.model.AccountApply;
import com.uas.finance.model.FsSale;
import com.uas.finance.model.Receipt;
import com.uas.finance.model.SellerQuota;
import com.uas.finance.service.AccountApplyService;
import com.uas.finance.service.FsSaleService;
import com.uas.finance.service.ReceiptService;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;

/**
 * 应收账款转让接口实现层
 * created by shicr on 2018/1/27
 **/
@Service
public class AccountApplyServiceImpl implements AccountApplyService {

    @Autowired
    private AccountApplyDao dao;

    @Autowired
    private SellerQuotaDao sellerQuotaDao;

    @Autowired
    private FsSaleService saleService;

    @Autowired
    private ReceiptService receiptService;

    /**
     * 保存出账申请
     *
     * @param accountApply
     * @return
     */
    @Override
    public AccountApply save(AccountApply accountApply) {
        //该出账单下的所有合同
        Set<FsSale> fsSales = saleService.findByAa_id(accountApply.getId());
        List<Long> list = new ArrayList<>();
        for(FsSale sale : fsSales){
            list.add(sale.getId());
        }
        //新保存的出账单合同
        List<FsSale> fsSales1 = accountApply.getFsSaleList();
        List<Long> list1 = new ArrayList<Long>();
        //合同总金额
        Double aa_saamount = 0.00;
        for(FsSale fsSale : fsSales1){
            list1.add(fsSale.getId());
            if(fsSale.getSa_contractamount() != null){
                aa_saamount += fsSale.getSa_contractamount();
            }
        }
        //删除不再保存的合同
        for(Long id : list){
            if(!list1.contains(id)){
                saleService.delete(id);
            }
        }

        //获取该出账单下所有发票
        List<Receipt> receipts = receiptService.findByAaid(accountApply.getId());
        List<Long> list2 = new ArrayList<>();

        for(Receipt receipt : receipts){
            list2.add(receipt.getId());
        }
        //新保存的出账单发票
        List<Receipt> receipts1 = accountApply.getReceipts();
        List<Long> list3 = new ArrayList<>();
        //发票总金额
        Double aa_billamount = 0.00;
        for(Receipt receipt : receipts1){
            list3.add(receipt.getId());
            if(receipt.getAai_amount() != null){
                aa_billamount += receipt.getAai_amount();
            }
        }
        //删除不再保存的发票
        for(Long id : list2){
            if(!list3.contains(id)){
                receiptService.delete(id);
            }
        }
        //设置合同总金额
        accountApply.setAa_saamount(aa_saamount);
        //设置发票总金额
        accountApply.setAa_billamount(aa_billamount);
        accountApply.setStatus(Status.SAVED.code());
        return dao.save(accountApply);
    }

    /**
     * 正式提交出账申请
     *
     * @param accountApply
     * @return
     */
    @Override
    public AccountApply submit(AccountApply accountApply) {

        //该出账单下的所有合同
        Set<FsSale> fsSales = saleService.findByAa_id(accountApply.getId());
        List<Long> list = new ArrayList<>();
        for(FsSale sale : fsSales){
            list.add(sale.getId());
        }
        //新保存的出账单合同
        List<FsSale> fsSales1 = accountApply.getFsSaleList();
        List<Long> list1 = new ArrayList<Long>();
        //合同总金额
        Double aa_saamount = 0.00;
        for(FsSale fsSale : fsSales1){
            list1.add(fsSale.getId());
            if(fsSale.getSa_contractamount() != null){
                aa_saamount += fsSale.getSa_contractamount();
            }
        }
        //删除不再保存的合同
        for(Long id : list){
            if(!list1.contains(id)){
                saleService.delete(id);
            }
        }

        //获取该出账单下所有发票
        List<Receipt> receipts = receiptService.findByAaid(accountApply.getId());
        List<Long> list2 = new ArrayList<>();

        for(Receipt receipt : receipts){
            list2.add(receipt.getId());
        }
        //新保存的出账单发票
        List<Receipt> receipts1 = accountApply.getReceipts();
        List<Long> list3 = new ArrayList<>();
        //发票总金额
        Double aa_billamount = 0.00;
        for(Receipt receipt : receipts1){
            list3.add(receipt.getId());
            if(receipt.getAai_amount() != null){
                aa_billamount += receipt.getAai_amount();
            }
        }
        //删除不再保存的发票
        for(Long id : list2){
            if(!list3.contains(id)){
                receiptService.delete(id);
            }
        }
        //设置合同总金额
        accountApply.setAa_saamount(aa_saamount);
        //设置发票总金额
        accountApply.setAa_billamount(aa_billamount);

        SellerQuota sellerQuota = sellerQuotaDao.findOne(accountApply.getCqId());

        if (sellerQuota != null) {
            accountApply.setFauu(sellerQuota.getFaenuu());
        }
        //设置提交状态
        accountApply.setStatus(Status.SUBMIT.code());
        //设置提交时间
        accountApply.setIndate(new Date());
        //设置数据传输待下载状态
        accountApply.setErpstatus(Status.NOT_UPLOAD.code());

        return dao.save(accountApply);
    }

    /**
     * 找到某条出账单
     *
     * @param id
     * @return
     */
    @Override
    public AccountApply findOne(Long id) {
        return dao.findOne(id);
    }

    /**
     * 找到某买方下面的所有出账申请,计算所有出账申请的合同发票总金额
     *
     * @param id
     * @return
     */
    public List<AccountApply> findByCq_id(Long id) {

        //找到某买方下面的所有已提交的出账申请
        List<AccountApply> applies1 = dao.findByCqIdAndErpstatusIsNotNull(id);

        for (AccountApply accountApply : applies1) {
            Double saamount = 0.00;
            Double billamount = 0.00;

            //计算出账申请合同总金额
            List<FsSale> fsSales = accountApply.getFsSaleList();
            for (FsSale sale : fsSales) {
                if (sale.getSa_contractamount() != null) {

                    saamount += sale.getSa_contractamount();
                }
            }
            accountApply.setAa_saamount(saamount);

            //计算出账发票总金额
            List<Receipt> receipts = accountApply.getReceipts();
            for (Receipt receipt : receipts) {
                if (receipt.getAai_amount() != null) {

                    billamount += receipt.getAai_amount();
                }
            }
            //设置出账发票总金额
            accountApply.setAa_billamount(billamount);

            dao.save(accountApply);
        }

        List<AccountApply> applies = dao.findByCqIdOrderByIndateDesc(id);

        return applies;
    }

    /**
     * 根据保理申请单号和下载状态找到单据
     *
     * @param fauu
     * @param erpstatus
     * @return
     */
    @Override
    public List<AccountApply> findByFauuAndErpstatus(Long fauu, Integer erpstatus) {

        return dao.findByFauuAndErpstatus(fauu, erpstatus);
    }


}
