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

import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.projectcontentinfo.dao.ProjectReportDataCompanyDao;
import com.jeeplus.modules.projectcontentinfo.dao.ProjectReportDataInfoDao;
import com.jeeplus.modules.projectcontentinfo.dao.ProjectReportDataMonthDao;
import com.jeeplus.modules.projectcontentinfo.dao.ProjectReportDataOfficeDao;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectReportDataCompany;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectReportDataInfo;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectReportDataMonth;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectReportDataOffice;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.modules.workreimbursementcount.entity.WorkInvoiceCompany;
import com.jeeplus.modules.workreimbursementcount.entity.WorkInvoiceMonth;
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 ProjectReportStatisticsService extends CrudService<ProjectReportDataInfoDao, ProjectReportDataInfo> {
    @Autowired
    private ProjectReportDataOfficeDao projectReportDataOfficeDao;
    @Autowired
    private ProjectReportDataCompanyDao projectReportDataCompanyDao;
    @Autowired
    private ProjectReportDataMonthDao projectReportDataMonthDao;
    @Autowired
    private OfficeService officeService;
    public List<ProjectReportDataCompany> findCompanyList(ProjectReportDataCompany projectReportDataCompany) {
        String companyId=projectReportDataCompany.getCompanyId();
        if (StringUtils.isBlank(companyId)){
            companyId=UserUtils.getSelectCompany().getId();
            projectReportDataCompany.setCompanyId(companyId);
        }
        List<ProjectReportDataCompany> list = findList(projectReportDataCompany,companyId);
        return list;
    }
    public List<ProjectReportDataCompany> findBranchList(ProjectReportDataCompany projectReportDataCompany) {
        String companyId=projectReportDataCompany.getCompanyId();
        if (StringUtils.isBlank(companyId)){
            companyId= UserUtils.getSelectBranchOffice();
            projectReportDataCompany.setCompanyId(companyId);
        }
        List<ProjectReportDataCompany> list = findList(projectReportDataCompany,companyId);
        return list;
    }
    public List<ProjectReportDataCompany> findList(ProjectReportDataCompany projectReportDataCompany,String companyId) {
        List<ProjectReportDataCompany> projectReportDataCompanys = projectReportDataCompanyDao.findList(projectReportDataCompany);
        List<ProjectReportDataCompany> listSign=new ArrayList<>();
        if (projectReportDataCompanys!=null && projectReportDataCompanys.size()!=0) {
            int reportCountTotal = 0;
            int reportCount = 0;
            int count = 0;
            ProjectReportDataCompany dataTatol = new ProjectReportDataCompany();
            ProjectReportDataCompany data = new ProjectReportDataCompany();
            String name = "";
            for (int i=0 ; i<projectReportDataCompanys.size();i++) {
                ProjectReportDataCompany company = projectReportDataCompanys.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++) {
                ProjectReportDataCompany reportDataCompany = listSign.get(i);
                if (companyId.equals(reportDataCompany.getId())){
                    count = i;
                    name = reportDataCompany.getName();
                    data = reportDataCompany;
                    reportCountTotal = reportDataCompany.getReportCount();
                    reportDataCompany.setName("合计");
                }else {
                    reportCount += reportDataCompany.getReportCount();
                }
            }
            if (StringUtils.isNotBlank(name)){
                dataTatol.setId(data.getId());
                dataTatol.setSign("1");
                dataTatol.setReportCount(reportCountTotal-reportCount);
                dataTatol.setName(name);
                listSign.set(count,dataTatol);
                listSign.add(data);
            }else {
                dataTatol.setName("合计");
                dataTatol.setReportCount(reportCount);
                listSign.add(dataTatol);
            }

        }
        return listSign;
    }
    public List<ProjectReportDataOffice> findListByOffice(ProjectReportDataOffice projectReportDataOffice) {
        if (StringUtils.isBlank(projectReportDataOffice.getCompanyId())){
            projectReportDataOffice.setCompanyId(UserUtils.getSelectBranchOffice());
        }
        List<ProjectReportDataOffice> list = projectReportDataOfficeDao.findList(projectReportDataOffice);
        List<ProjectReportDataOffice> listSum=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(ProjectReportDataOffice projectReportDataOffice1:list){
                ProjectReportDataOffice projectReportDataOffice2=new ProjectReportDataOffice();
                projectReportDataOffice2.setCompanyId(projectReportDataOffice1.getId());
                projectReportDataOffice2.setDate(projectReportDataOffice.getDate());
                ProjectReportDataOffice w= projectReportDataOfficeDao.findOfficeListSum(projectReportDataOffice2);
                if(w.getId()!=null){
                    projectReportDataOffice1.setReportCount(w.getReportCount());
                }
                listSum.add(projectReportDataOffice1);
            }
        }
        if (listSum!=null && listSum.size()!=0) {
            int reportCountTotal = 0;
            ProjectReportDataOffice dataTatol = new ProjectReportDataOffice();
            for (ProjectReportDataOffice reportDataOffice:listSum){
                reportCountTotal += reportDataOffice.getReportCount();
            }
            dataTatol.setReportCount(reportCountTotal);
            dataTatol.setName("合计");
            listSum.add(dataTatol);
        }
        return listSum;
    }
    public Page<ProjectReportDataInfo> findPageByInfo(Page<ProjectReportDataInfo> page,ProjectReportDataInfo projectReportDataInfo) {
        if (StringUtils.isBlank(projectReportDataInfo.getOfficeId())){
            projectReportDataInfo.setOfficeId(UserUtils.getSelectOffice().getId());
        }
        projectReportDataInfo.setPage(page);
        page.setList(dao.findList(projectReportDataInfo));
        return page;
    }

    public List<ProjectReportDataMonth> findMonthCompanyList(ProjectReportDataMonth projectReportDataMonth) {
        Office company = null;
        if (StringUtils.isBlank(projectReportDataMonth.getCompanyId())){
            company =  UserUtils.getSelectCompany();
            projectReportDataMonth.setCompanyId(company.getId());
        }
        List<ProjectReportDataMonth> list = new ArrayList<>();
        List<ProjectReportDataMonth> listSign=new ArrayList<>();
        list = projectReportDataMonthDao.findList(projectReportDataMonth);
        for (int i=0 ; i<list.size();i++) {
            ProjectReportDataMonth w = list.get(i);
            w.setSign("1");
            listSign.add(w);
        }
        if (listSign!=null && listSign.size()!=0) {
            int reportCount = 0;
            ProjectReportDataMonth dataTatol = new ProjectReportDataMonth();
            ProjectReportDataMonth data = new ProjectReportDataMonth();
            for (int i=0 ; i<listSign.size();i++) {
                ProjectReportDataMonth reportDataMonth = listSign.get(i);
                reportCount += reportDataMonth.getReportCount();
            }
            if (listSign!=null && listSign.size()!=0){
                dataTatol.setReportCount(reportCount);
                dataTatol.setName("合计");
                listSign.add(dataTatol);
            }
        }
        return listSign;
    }

    public List<ProjectReportDataMonth> findOfficeCompanyList(ProjectReportDataMonth projectReportDataMonth) {
        if (StringUtils.isBlank(projectReportDataMonth.getCompanyId())){
            projectReportDataMonth.setCompanyId(UserUtils.getSelectCompany().getId());
        }
        List<ProjectReportDataMonth> projectReportDataMonths = projectReportDataMonthDao.findByOfficeList(projectReportDataMonth);
        if (projectReportDataMonths!=null && projectReportDataMonths.size()!=0) {
            int reportCount = 0;
            ProjectReportDataMonth dataTatol = new ProjectReportDataMonth();
            ProjectReportDataMonth data = new ProjectReportDataMonth();
            for (int i=0 ; i<projectReportDataMonths.size();i++) {
                ProjectReportDataMonth reportDataMonth = projectReportDataMonths.get(i);
                reportCount += reportDataMonth.getReportCount();
            }
            if (projectReportDataMonths!=null && projectReportDataMonths.size()!=0){
                dataTatol.setName("合计");
                dataTatol.setReportCount(reportCount);
                projectReportDataMonths.add(dataTatol);
            }
        }
        return projectReportDataMonths;
    }

}