/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workcontractinfo.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.workcontractinfo.dao.WorkContractDataDao;
import com.jeeplus.modules.workcontractinfo.dao.WorkContractDataInfoDao;
import com.jeeplus.modules.workcontractinfo.dao.WorkContractDataOfficeDao;
import com.jeeplus.modules.workcontractinfo.entity.WorkContractData;
import com.jeeplus.modules.workcontractinfo.entity.WorkContractDataInfo;
import com.jeeplus.modules.workcontractinfo.entity.WorkContractDataOffice;
import com.jeeplus.modules.workreimbursementcount.entity.WorkInvoiceCompany;
import com.jeeplus.modules.workreimbursementcount.entity.WorkInvoiceOffice;
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;

/**
 * 合同评价表Service
 * @author 杨帆
 * @version 2018-03-23
 */
@Service
@Transactional(readOnly = true)
public class WorkContractDataService extends CrudService<WorkContractDataDao, WorkContractData> {
    @Autowired
    private WorkContractDataInfoDao workContractDataInfoDao;
    @Autowired
    private WorkContractDataOfficeDao workContractDataOfficeDao;
    @Autowired
    private OfficeService officeService;
    public List<WorkContractData> findCompanyList(WorkContractData workContractData) {
        String companyId=workContractData.getCompanyId();
        if (StringUtils.isBlank(companyId)){
            companyId=UserUtils.getSelectCompany().getId();
            workContractData.setCompanyId(companyId);
        }
        List<WorkContractData> list = findList(workContractData,companyId);
        return list;
    }
    public List<WorkContractData> findBranchList(WorkContractData workContractData) {
        String companyId=workContractData.getCompanyId();
        if (StringUtils.isBlank(companyId)){
            companyId= UserUtils.getSelectBranchOffice();
            workContractData.setCompanyId(companyId);
        }
        List<WorkContractData> list = findList(workContractData,companyId);
        return list;
    }
    public List<WorkContractData> findList(WorkContractData workContractData,String companyId) {
        List<WorkContractData> listSign=new ArrayList<>();
        List<WorkContractData> list = super.findList(workContractData);
        if (list!=null && list.size()!=0) {
            int contractCountTotal = 0;
            int contractCount = 0;
            int projectCountTotal = 0;
            int projectCount = 0;
            int reportCountTotal = 0;
            int reportCount = 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");
            BigDecimal reimbursementMoneyTotal = new BigDecimal("0.00");
            BigDecimal reimbursementMoney = new BigDecimal("0.00");
            BigDecimal newValue = new BigDecimal("0.00");
            WorkContractData dataTatol = new WorkContractData();
            WorkContractData data = new WorkContractData();
            String name = "";
            for (int i=0 ; i<list.size();i++) {
                WorkContractData 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++) {
                WorkContractData contractData = listSign.get(i);
                if (companyId.equals(contractData.getId())){
                    name = contractData.getName();
                    data = contractData;
                    count = i;
                    contractCountTotal = contractData.getContractCount();
                    projectCountTotal = contractData.getProjectCount();
                    reportCountTotal = contractData.getReportCount();
                    invoiceMoneyTotal= invoiceMoneyTotal.add(contractData.getInvoiceMoney());
                    incomeMoneyTotal = incomeMoneyTotal.add(contractData.getIncomeMoney());
                    reimbursementMoneyTotal = reimbursementMoneyTotal.add(contractData.getReimbursementMoney());
                    contractData.setName("合计");
                }else {
                    contractCount += contractData.getContractCount();
                    projectCount += contractData.getProjectCount();
                    reportCount += contractData.getReportCount();
                    invoiceMoney = invoiceMoney.add(contractData.getInvoiceMoney());
                    incomeMoney = incomeMoney.add(contractData.getIncomeMoney());
                    reimbursementMoney = reimbursementMoney.add(contractData.getReimbursementMoney());
                }
            }
            dataTatol.setId(data.getId());
            dataTatol.setSign("1");
            dataTatol.setContractCount(contractCountTotal-contractCount);
            dataTatol.setProjectCount(projectCountTotal-projectCount);
            dataTatol.setReportCount(reportCountTotal-reportCount);
            dataTatol.setIncomeMoney(incomeMoneyTotal.subtract(incomeMoney));
            dataTatol.setInvoiceMoney(invoiceMoneyTotal.subtract(invoiceMoney));
            dataTatol.setReimbursementMoney(reimbursementMoneyTotal.subtract(reimbursementMoney));
            dataTatol.setName(name);
            listSign.set(count,dataTatol);
            listSign.add(data);
        }
        return listSign;
    }
    public List<WorkContractDataOffice> findListByOffice(WorkContractDataOffice workContractDataOffice) {
        if (StringUtils.isBlank(workContractDataOffice.getCompanyId())){
            workContractDataOffice.setCompanyId(UserUtils.getSelectBranchOffice());
        }
        List<WorkContractDataOffice> list = workContractDataOfficeDao.findList(workContractDataOffice);
        List<WorkContractDataOffice> listSum=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(WorkContractDataOffice workContractDataOffice1:list){
                WorkContractDataOffice workContractDataOffice2=new WorkContractDataOffice();
                workContractDataOffice2.setCompanyId(workContractDataOffice1.getId());
                workContractDataOffice2.setBeginDate(workContractDataOffice.getBeginDate());
                workContractDataOffice2.setEndDate(workContractDataOffice.getEndDate());
                WorkContractDataOffice w= workContractDataOfficeDao.findOfficeListSum(workContractDataOffice2);
                if(w.getId()!=null){
                    workContractDataOffice1.setContractCount(w.getContractCount());
                    workContractDataOffice1.setProjectCount(w.getProjectCount());
                    workContractDataOffice1.setReportCount(w.getReportCount());
                    workContractDataOffice1.setIncomeMoney(w.getIncomeMoney());
                    workContractDataOffice1.setInvoiceMoney(w.getInvoiceMoney());
                    workContractDataOffice1.setReimbursementMoney(w.getReimbursementMoney());
                }
                listSum.add(workContractDataOffice1);
            }
        }
        if (listSum!=null && listSum.size()!=0) {
            int contractCountTotal = 0;
            int projectCountTotal = 0;
            int reportCountTotal = 0;
            BigDecimal invoiceMoneyTotal = new BigDecimal("0.00");
            BigDecimal incomeMoneyTotal = new BigDecimal("0.00");
            BigDecimal reimbursementMoneyTotal = new BigDecimal("0.00");
            WorkContractDataOffice dataTatol = new WorkContractDataOffice();
            BigDecimal newValue = new BigDecimal("0.00");
            for (WorkContractDataOffice contractDataOffice:listSum){
                contractCountTotal += contractDataOffice.getContractCount();
                projectCountTotal += contractDataOffice.getProjectCount();
                reportCountTotal += contractDataOffice.getReportCount();
                invoiceMoneyTotal = invoiceMoneyTotal.add(contractDataOffice.getInvoiceMoney());
                incomeMoneyTotal = incomeMoneyTotal.add(contractDataOffice.getIncomeMoney());
                reimbursementMoneyTotal = reimbursementMoneyTotal.add(contractDataOffice.getReimbursementMoney());
            }
            dataTatol.setContractCount(contractCountTotal);
            dataTatol.setProjectCount(projectCountTotal);
            dataTatol.setReportCount(reportCountTotal);
            dataTatol.setIncomeMoney(incomeMoneyTotal);
            dataTatol.setInvoiceMoney(invoiceMoneyTotal);
            dataTatol.setReimbursementMoney(reimbursementMoneyTotal);
            dataTatol.setName("合计");
            listSum.add(dataTatol);
        }
        return listSum;
    }
    public Page<WorkContractDataInfo> findPageByInfo(Page<WorkContractDataInfo> page,WorkContractDataInfo workContractDataInfo) {
        if (StringUtils.isBlank(workContractDataInfo.getOfficeId())){
            workContractDataInfo.setOfficeId(UserUtils.getSelectOffice().getId());
        }
        workContractDataInfo.setPage(page);
        page.setList(workContractDataInfoDao.findList(workContractDataInfo));
        List<WorkContractDataInfo> workContractDataInfos = page.getList();
        if (workContractDataInfos!=null && workContractDataInfos.size()!=0) {
            int projectCountTotal = 0;
            int reportCountTotal = 0;
            BigDecimal invoiceMoneyTotal = new BigDecimal("0.00");
            BigDecimal incomeMoneyTotal = new BigDecimal("0.00");
            BigDecimal reimbursementMoneyTotal = new BigDecimal("0.00");
            WorkContractDataInfo dataTatol = new WorkContractDataInfo();
            for (WorkContractDataInfo contractDataInfo:workContractDataInfos){
                projectCountTotal += contractDataInfo.getProjectCount();
                reportCountTotal += contractDataInfo.getReportCount();
                invoiceMoneyTotal = invoiceMoneyTotal.add(contractDataInfo.getInvoiceMoney());
                incomeMoneyTotal = incomeMoneyTotal.add(contractDataInfo.getIncomeMoney());
                incomeMoneyTotal = reimbursementMoneyTotal.add(contractDataInfo.getReimbursementMoney());
            }
            dataTatol.setProjectCount(projectCountTotal);
            dataTatol.setReportCount(reportCountTotal);
            dataTatol.setIncomeMoney(incomeMoneyTotal);
            dataTatol.setInvoiceMoney(invoiceMoneyTotal);
            dataTatol.setReimbursementMoney(reimbursementMoneyTotal);
            dataTatol.setName(workContractDataInfos.size()+"");
            dataTatol.setNumber("合计");
            workContractDataInfos.add(dataTatol);
        }
        return page;
    }

}