/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workreimbursementcount.service;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workreimbursementcount.dao.*;
import com.jeeplus.modules.workreimbursementcount.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 报销统计
 * @author 陈玉平
 * @version 2018-07-03
 */
@Service
@Transactional(readOnly = true)
public class WorkInvoiceCountService extends CrudService<WorkInvoiceCompanyDao, WorkInvoiceCompany> {
    @Autowired
    private WorkInvoiceInfoDao workInvoiceInfoDao;
    @Autowired
    private WorkInvoiceCompanyDao workInvoiceCompanyDao;
    @Autowired
    private WorkInvoiceOfficeDao workInvoiceOfficeDao;
    @Autowired
    private WorkInvoiceMonthDao workInvoiceMonthDao;
    @Autowired
    private OfficeService officeService;
//公司级统计
    public List<WorkInvoiceCompany> findCompanyList(WorkInvoiceCompany workInvoiceCompany) {
        String companyId=workInvoiceCompany.getCompanyId();
        if (StringUtils.isBlank(companyId)){
            companyId=UserUtils.getSelectCompany().getId();
            workInvoiceCompany.setCompanyId(companyId);
        }
        List<WorkInvoiceCompany> list = findCompany(workInvoiceCompany,companyId);
        return list;
    }
    //分公司统计
    public List<WorkInvoiceCompany> findBranchList(WorkInvoiceCompany workInvoiceCompany) {
        String companyId=workInvoiceCompany.getCompanyId();
        if (StringUtils.isBlank(companyId)){
            companyId= UserUtils.getSelectBranchOffice();
            workInvoiceCompany.setCompanyId(companyId);
        }
        List<WorkInvoiceCompany> list = findCompany(workInvoiceCompany,companyId);
        return list;
    }
    //统计计算合计
    public List<WorkInvoiceCompany> findCompany(WorkInvoiceCompany workInvoiceCompany,String companyId) {
        List<WorkInvoiceCompany> list = workInvoiceCompanyDao.findCompanyList(workInvoiceCompany);
        List<WorkInvoiceCompany> listSign=new ArrayList<>();
        if (list!=null && list.size()!=0) {
            int invoiceCountTotal=0;
            int invoiceCount = 0;
            int count = 0;
            BigDecimal invoiceMoneyTotal = new BigDecimal("0.00");
            BigDecimal invoiceMoney = new BigDecimal("0.00");
            BigDecimal incomeMoneyTotal = new BigDecimal("0.00");
            BigDecimal incomeMoney = new BigDecimal("0.00");

            WorkInvoiceCompany dataTatol = new WorkInvoiceCompany();
            WorkInvoiceCompany data = new WorkInvoiceCompany();
            String name = "";
            for (int i=0 ; i<list.size();i++) {
                WorkInvoiceCompany company = list.get(i);
                if(company.getId().equals(companyId)){
                    company.setSign("1");
                }else{
                    company.setSign("2");
                }
                listSign.add(company);
            }
            for (int i=0 ; i<listSign.size();i++) {
                WorkInvoiceCompany company = listSign.get(i);
                if (companyId.equals(company.getId())){
                    name = company.getOfficeName();
                    data = company;
                    count = i;
                    invoiceCountTotal = Integer.parseInt(company.getInvoiceCount());
                    invoiceMoneyTotal= company.getInvoiceMoneys();
                    incomeMoneyTotal= company.getIncomeMoneys();
                    company.setOfficeName("合计");
                }else {
                    invoiceCount +=  Integer.parseInt(company.getInvoiceCount());
                    invoiceMoney= invoiceMoney.add(company.getInvoiceMoneys());
                    incomeMoney= incomeMoney.add(company.getIncomeMoneys());
                }
            }
            if (StringUtils.isNotBlank(name)){
                dataTatol.setId(data.getId());
                dataTatol.setSign("1");
                dataTatol.setInvoiceCount(String.valueOf(invoiceCountTotal-invoiceCount));
                dataTatol.setInvoiceMoneys(invoiceMoneyTotal.subtract(invoiceMoney));
                dataTatol.setIncomeMoneys(incomeMoneyTotal.subtract(incomeMoney));
                dataTatol.setOfficeName(name);
                listSign.set(count,dataTatol);
                listSign.add(data);
            }else {
                dataTatol.setOfficeName("合计");
                dataTatol.setIncomeMoneys(incomeMoney);
                dataTatol.setInvoiceMoneys(invoiceMoney);
                dataTatol.setInvoiceCount(String.valueOf(invoiceCount));
                listSign.add(dataTatol);
            }
        }
        return listSign;
    }
//部门级统计
    public List<WorkInvoiceOffice> findOfficeList(WorkInvoiceOffice workInvoiceOffice) {
        if (StringUtils.isBlank(workInvoiceOffice.getCompanyId())){
            workInvoiceOffice.setCompanyId(UserUtils.getSelectBranchOffice());
        }
        List<WorkInvoiceOffice> list = workInvoiceOfficeDao.findOfficeList(workInvoiceOffice);
        List<WorkInvoiceOffice> listSum=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(WorkInvoiceOffice workInvoiceOffice1:list){
                WorkInvoiceOffice workInvoiceOffice2=new WorkInvoiceOffice();
                workInvoiceOffice2.setCompanyId(workInvoiceOffice1.getId());
                workInvoiceOffice2.setDate(workInvoiceOffice.getDate());
                WorkInvoiceOffice w= workInvoiceOfficeDao.findOfficeListSum(workInvoiceOffice2);
                if(w.getId()!=null){
                    workInvoiceOffice1.setInvoiceCount(w.getInvoiceCount());
                    workInvoiceOffice1.setInvoiceMoneys(w.getInvoiceMoneys());
                    workInvoiceOffice1.setIncomeMoneys(w.getIncomeMoneys());
                }
                listSum.add(workInvoiceOffice1);
            }
        }
        WorkInvoiceOffice wi=new WorkInvoiceOffice();
        BigDecimal sum = new BigDecimal(0);
        BigDecimal sum1 = new BigDecimal(0);
        int sum2=0;
        if(listSum!=null){
            for(WorkInvoiceOffice w:listSum){
                sum=sum.add(w.getIncomeMoneys()) ;
                sum1=sum1.add(w.getInvoiceMoneys()) ;
                sum2+=Integer.parseInt(w.getInvoiceCount());
            }
            wi.setOfficeName("合计");
            wi.setIncomeMoneys(sum);
            wi.setInvoiceMoneys(sum1);
            wi.setInvoiceCount(String.valueOf(sum2));
            listSum.add(wi);
        }
        return listSum;
    }
//明细查询
    public List<WorkInvoiceInfo> findInfoList(WorkInvoiceInfo workInvoiceInfo) {
    if (StringUtils.isBlank(workInvoiceInfo.getOfficeId())){
        workInvoiceInfo.setOfficeId(UserUtils.getSelectOffice().getId());
    }
    List<WorkInvoiceInfo> list = workInvoiceInfoDao.findInfoList(workInvoiceInfo);
    return list;
}
    public Page<WorkInvoiceInfo> findInfoPage(Page<WorkInvoiceInfo> page, WorkInvoiceInfo workInvoiceInfo) {
        workInvoiceInfo.setPage(page);
        page.setList(findInfoList(workInvoiceInfo));
        return page;
    }

//公司级按月统计
    public List<WorkInvoiceMonth> findMonthCompanyList(WorkInvoiceMonth workInvoiceMonth) {
        if (StringUtils.isBlank(workInvoiceMonth.getCompanyId())){
            workInvoiceMonth.setCompanyId(UserUtils.getSelectCompany().getId());
        }
        List<WorkInvoiceMonth> list = new ArrayList<>();
        List<WorkInvoiceMonth> listSign=new ArrayList<>();
        list = workInvoiceMonthDao.findList(workInvoiceMonth);
        for (int i=0 ; i<list.size();i++) {
            WorkInvoiceMonth w = list.get(i);
            w.setSign("1");
            listSign.add(w);
        }
        WorkInvoiceMonth data = new WorkInvoiceMonth();
        for (int i=0 ; i<listSign.size();i++) {
            WorkInvoiceMonth w = listSign.get(i);
            if (StringUtils.isBlank(w.getOfficeName())){
                data = w;
            }
        }
        if (data != null){
            listSign.remove(data);
        }
        return listSign;
    }
    //部门级按月统计
    public List<WorkInvoiceMonth> findMonthOfficeList(WorkInvoiceMonth workInvoiceMonth) {
        if (StringUtils.isBlank(workInvoiceMonth.getCompanyId())){
            workInvoiceMonth.setCompanyId(UserUtils.getSelectCompany().getId());
        }
        List<WorkInvoiceMonth> list = workInvoiceMonthDao.findOfficeList(workInvoiceMonth);
        return list;
    }

}