/**
 * 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.projectcontentinfo.entity.ProjectReportDataCompany;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectReportDataMonth;
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.workreimbursement.entity.WorkReimbursement;
import com.jeeplus.modules.workreimbursementcount.dao.WorkReimbursementCompanyDao;
import com.jeeplus.modules.workreimbursementcount.dao.WorkReimbursementMonthDao;
import com.jeeplus.modules.workreimbursementcount.dao.WorkReimbursementOfficeDao;
import com.jeeplus.modules.workreimbursementcount.dao.WorkReimbursementInfoDao;
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 WorkReimbursementCountService extends CrudService<WorkReimbursementCompanyDao, WorkReimbursementCompany> {
    @Autowired
    private WorkReimbursementInfoDao workReimbursementInfoDao;
    @Autowired
    private WorkReimbursementCompanyDao workReimbursementCompanyDao;
    @Autowired
    private WorkReimbursementOfficeDao workReimbursementOfficeDao;
    @Autowired
    private WorkReimbursementMonthDao workReimbursementMonthDao;
    @Autowired
    private OfficeService officeService;

    //公司级统计
    public List<WorkReimbursementCompany> findCompanyList(WorkReimbursementCompany workReimbursementCompany) {
        String companyId=workReimbursementCompany.getCompanyId();
        if (StringUtils.isBlank(companyId)){
            companyId=UserUtils.getSelectCompany().getId();
            workReimbursementCompany.setCompanyId(companyId);
        }
        List<WorkReimbursementCompany> list = findCompany(workReimbursementCompany,companyId);
        return list;
    }
    //分公司级统计
    public List<WorkReimbursementCompany> findBranchList(WorkReimbursementCompany workReimbursementCompany) {
        String companyId=workReimbursementCompany.getCompanyId();
        if (StringUtils.isBlank(companyId)){
            companyId= UserUtils.getSelectBranchOffice();
            workReimbursementCompany.setCompanyId(companyId);
        }
        List<WorkReimbursementCompany> list = findCompany(workReimbursementCompany,companyId);
        return list;
    }
    public List<WorkReimbursementCompany> findCompany(WorkReimbursementCompany workReimbursementCompany,String companyId) {
        List<WorkReimbursementCompany> list = workReimbursementCompanyDao.findCompanyList(workReimbursementCompany);
        List<WorkReimbursementCompany> listSign=new ArrayList<>();
        if (list!=null && list.size()!=0) {
            BigDecimal reportCountTotal =  new BigDecimal(0);
            BigDecimal reportCount =  new BigDecimal(0);
            int count = 0;
            WorkReimbursementCompany dataTatol = new WorkReimbursementCompany();
            WorkReimbursementCompany data = new WorkReimbursementCompany();
            String name = "";
            for (int i=0 ; i<list.size();i++) {
                WorkReimbursementCompany 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++) {
                WorkReimbursementCompany reportDataCompany = listSign.get(i);
                if (companyId.equals(reportDataCompany.getId())){
                    count = i;
                    name = reportDataCompany.getOfficeName();
                    data = reportDataCompany;
                    reportCountTotal = reportDataCompany.getMoney();
                    reportDataCompany.setOfficeName("合计");
                }else {
                    reportCount=reportCount.add(reportDataCompany.getMoney()) ;
                }
            }
            if (StringUtils.isNotBlank(name)){
                dataTatol.setId(data.getId());
                dataTatol.setSign("1");
                dataTatol.setMoney(reportCountTotal.subtract(reportCount));
                dataTatol.setOfficeName(name);
                listSign.set(count,dataTatol);
                listSign.add(data);
            }else {
                dataTatol.setOfficeName("合计");
                dataTatol.setMoney(reportCount);
                listSign.add(dataTatol);
            }
        }
        return listSign;
    }

   //部门级统计
    public List<WorkReimbursementOffice> findOfficeList(WorkReimbursementOffice workReimbursementOffice) {
        if (StringUtils.isBlank(workReimbursementOffice.getCompanyId())){
            workReimbursementOffice.setCompanyId(UserUtils.getSelectCompany().getId());
        }
        List<WorkReimbursementOffice> list = workReimbursementOfficeDao.findOfficeList(workReimbursementOffice);
        List<WorkReimbursementOffice> listSum=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(WorkReimbursementOffice workReimbursementOffice1:list){
                WorkReimbursementOffice workReimbursementOffice2=new WorkReimbursementOffice();
                workReimbursementOffice2.setCompanyId(workReimbursementOffice1.getId());
                workReimbursementOffice2.setDate(workReimbursementOffice1.getDate());
                WorkReimbursementOffice w= workReimbursementOfficeDao.findOfficeListSum(workReimbursementOffice2);
                if(w.getId()!=null){
                    workReimbursementOffice1.setMoney(w.getMoney());
                }
                listSum.add(workReimbursementOffice1);
            }
        }
        WorkReimbursementOffice wr=new WorkReimbursementOffice();
        //合计
        BigDecimal sum = new BigDecimal(0);
        if(list!=null){
           for( WorkReimbursementOffice w:listSum){
               sum=sum.add(w.getMoney()) ;
           }
            wr.setOfficeName("合计");
            wr.setMoney(sum);
        }
        listSum.add(wr);
        return listSum;
    }
    //明细查询
    public List<WorkReimbursementInfo> findInfoList(WorkReimbursementInfo workReimbursementInfo) {
    if (StringUtils.isBlank(workReimbursementInfo.getOfficeId())){
        workReimbursementInfo.setOfficeId(UserUtils.getSelectOffice().getId());
    }
    List<WorkReimbursementInfo> list = workReimbursementInfoDao.findInfoList(workReimbursementInfo);
    return list;
}
    public Page<WorkReimbursementInfo> findInfoPage(Page<WorkReimbursementInfo> page, WorkReimbursementInfo workReimbursementInfo) {
        workReimbursementInfo.setPage(page);
        page.setList(findInfoList(workReimbursementInfo));
        return page;
    }


    //按月统计查询列表
    public List<WorkReimbursementMonth> findMonthCompanyList(WorkReimbursementMonth workReimbursementMonth) {
        if (StringUtils.isBlank(workReimbursementMonth.getCompanyId())){
            workReimbursementMonth.setCompanyId(UserUtils.getSelectCompany().getId());
        }
        List<WorkReimbursementMonth> list = new ArrayList<>();
        List<WorkReimbursementMonth> listSign=new ArrayList<>();
        list = workReimbursementMonthDao.findList(workReimbursementMonth);

        for (int i=0 ; i<list.size();i++) {
            WorkReimbursementMonth w = list.get(i);
            w.setSign("1");
            listSign.add(w);
        }
        return listSign;
    }

    public List<WorkReimbursementMonth> findMonthOfficeList(WorkReimbursementMonth workReimbursementMonth) {
        if (StringUtils.isBlank(workReimbursementMonth.getCompanyId())){
            workReimbursementMonth.setCompanyId(UserUtils.getSelectCompany().getId());
        }
        List<WorkReimbursementMonth> list = workReimbursementMonthDao.findOfficeList(workReimbursementMonth);
        WorkReimbursementMonth data = new WorkReimbursementMonth();
        return list;
    }
}