package com.tofly.feesapi.sfgl.service.impl;
import com.tofly.feesapi.bmgl.service.CbbConfigService;
import com.tofly.feesapi.common.Excel.ExcleFileUtil;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.security.util.SecurityUtils;
import com.tofly.feesapi.sfgl.entity.dto.YySspjxxMxFilter;
import com.tofly.feesapi.sfgl.entity.dto.YySspjxxReportFilter;
import com.tofly.feesapi.sfgl.entity.vo.YySspjxxLsMxReport;
import com.tofly.feesapi.sfgl.entity.vo.YySspjxxMxReport;
import com.tofly.feesapi.sfgl.entity.vo.YySspjxxReport;
import com.tofly.feesapi.sfgl.mapper.YySspjxxReportMapper;
import com.tofly.feesapi.sfgl.service.YySspjxxReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * 收费明细
 */
@Slf4j
@Service("yySspjxxReportService")
public class YySspjxxReportServiceImpl  implements YySspjxxReportService {

    @Autowired
    private YySspjxxReportMapper  yySspjxxReportMapper;
    @Autowired
    private CbbConfigService cbbConfigService;

    private List<String> getCbbConfig(){
        if(SecurityUtils.getUserId()!=null){
            return  cbbConfigService.getConfigUserId(SecurityUtils.getUserId());

        }
        return null;
    }


    private  YySspjxxReport addHj(List<YySspjxxReport> yySspjxxReportList,Integer type){
        YySspjxxReport yySspjxxReport=new YySspjxxReport();
        yySspjxxReport.setYslx("P_100");
        yySspjxxReport.setYslxName("小计");
        if(type==2){
            yySspjxxReport.setYslx("P_200");
        }else if(type==3){
            yySspjxxReport.setYslx("P_300");
            yySspjxxReport.setYslxName("总计");
        }
        //小计数据汇总
        BigDecimal sf_1=yySspjxxReportList.stream().filter(item->item.getSl1()!=null).map(YySspjxxReport::getSf1).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sl_1=yySspjxxReportList.stream().filter(item->item.getSl1()!=null).map(YySspjxxReport::getSl1).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sf_2=yySspjxxReportList.stream().filter(item->item.getSf2()!=null).map(YySspjxxReport::getSf2).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sl_2=yySspjxxReportList.stream().filter(item->item.getSl2()!=null).map(YySspjxxReport::getSl2).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sf_3=yySspjxxReportList.stream().filter(item->item.getSf3()!=null).map(YySspjxxReport::getSf3).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sl_3=yySspjxxReportList.stream().filter(item->item.getSl3()!=null).map(YySspjxxReport::getSl3).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sf_4=yySspjxxReportList.stream().filter(item->item.getSf4()!=null).map(YySspjxxReport::getSf4).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sl_4=yySspjxxReportList.stream().filter(item->item.getSl4()!=null).map(YySspjxxReport::getSl4).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sf_5=yySspjxxReportList.stream().filter(item->item.getSf5()!=null).map(YySspjxxReport::getSf5).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sl_5=yySspjxxReportList.stream().filter(item->item.getSl5()!=null).map(YySspjxxReport::getSl5).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sf_6=yySspjxxReportList.stream().filter(item->item.getSf6()!=null).map(YySspjxxReport::getSf6).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sl_6=yySspjxxReportList.stream().filter(item->item.getSl6()!=null).map(YySspjxxReport::getSl6).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sf_7=yySspjxxReportList.stream().filter(item->item.getSf7()!=null).map(YySspjxxReport::getSf7).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal sl_7=yySspjxxReportList.stream().filter(item->item.getSl7()!=null).map(YySspjxxReport::getSl7).reduce(BigDecimal.ZERO,BigDecimal::add);
        yySspjxxReport.setSf1(sf_1);
        yySspjxxReport.setSl1(sl_1);
        yySspjxxReport.setSf2(sf_2);
        yySspjxxReport.setSl2(sl_2);
        yySspjxxReport.setSf3(sf_3);
        yySspjxxReport.setSl3(sl_3);
        yySspjxxReport.setSf4(sf_4);
        yySspjxxReport.setSl4(sl_4);
        yySspjxxReport.setSf5(sf_5);
        yySspjxxReport.setSl5(sl_5);
        yySspjxxReport.setSf6(sf_6);
        yySspjxxReport.setSl6(sl_6);
        yySspjxxReport.setSf7(sf_7);
        yySspjxxReport.setSl7(sl_7);
        return yySspjxxReport;
    }

    @Override
    public List<YySspjxxReport> getYySspjxxReport(HttpServletResponse response, YySspjxxReportFilter filter){
    if(filter.getShowStatus()==1){

        filter.setCbbConfigList(getCbbConfig());
    }
        filter.setExport(filter.getExport()==null?0:filter.getExport());
        List<YySspjxxReport> list=new ArrayList<>();
        List<YySspjxxReport> yySspjxxReportList= yySspjxxReportMapper.getYySspjxxReport(filter);
        YySspjxxReport yySspjxxReportP1=new YySspjxxReport();
        yySspjxxReportP1.setType(1);
        yySspjxxReportP1.setYslx("P_001");
        yySspjxxReportP1.setYslxName("水费收入");
        list.add(yySspjxxReportP1);
        //水费收入数据
        List<YySspjxxReport> yySspjxxReportList1=yySspjxxReportList.stream().filter(item->item.getType().equals(1)).collect(Collectors.toList());
        list.addAll(yySspjxxReportList1);
        //添加合计
        YySspjxxReport yySspjxxReport1=addHj(yySspjxxReportList1,1);
        list.add(yySspjxxReport1);

        //其他费用
        YySspjxxReport yySspjxxReportP2=new YySspjxxReport();
        yySspjxxReportP2.setYslx("P_002");
        yySspjxxReportP1.setType(2);
        yySspjxxReportP2.setYslxName("附加及代收费");
        list.add(yySspjxxReportP2);

        List<YySspjxxReport> yySspjxxReportList2=yySspjxxReportList.stream().filter(item->item.getType().equals(2)).collect(Collectors.toList());
        list.addAll(yySspjxxReportList2);

        //添加总计
        YySspjxxReport yySspjxxReportP3=addHj(yySspjxxReportList,3);
        list.add(yySspjxxReportP3);

        Properties properties = new Properties();
        properties.put("title1","收费日结报表");
        properties.put("title2", "收费时间:"+filter.getStartTime()+"至"+filter.getEndTime());

        if(filter.getExport()==1){
            try {
                ExcleFileUtil.exportExcel(response, list, YySspjxxReport.class, "收费日结报表.xlsx", "",properties);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
     return  list;
   }

    @Override
    public PageResult<YySspjxxMxReport> getYySspjxxMxReport(HttpServletResponse response, PageDto pageDto, YySspjxxMxFilter filter) {
        if(filter.getShowStatus()==1){

            filter.setCbbConfigList(getCbbConfig());
        }
        filter.setExport(filter.getExport()==null?0:filter.getExport());
        if(filter.getExport()==1){
            try {
                Properties properties = new Properties();
                properties.put("title1","收费明细报表");
                properties.put("title2", "收费时间:"+filter.getStartTime()+"至"+filter.getEndTime());
               List<YySspjxxMxReport> list=yySspjxxReportMapper.getYySspjxxMxReport(filter);
                ExcleFileUtil.exportExcel(response, list, YySspjxxMxReport.class, "收费明细报表.xlsx", "",properties);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
                PageDto<YySspjxxMxReport> dto=yySspjxxReportMapper.getYySspjxxMxReport(pageDto,filter);
                return  dto.toVo();
    }

    @Override
    public PageResult<YySspjxxLsMxReport> getYySspjxxLsMxReport(HttpServletResponse response, PageDto pageDto, YySspjxxMxFilter filter) {
        if(filter.getShowStatus()==1){

            filter.setCbbConfigList(getCbbConfig());
        }
        filter.setExport(filter.getExport()==null?0:filter.getExport());
        if(filter.getExport()==1){
            try {
                Properties properties = new Properties();
                properties.put("title1", "收费流水明细报表");
                properties.put("title2", "收费时间:"+filter.getStartTime()+"至"+filter.getEndTime());
                List<YySspjxxLsMxReport> list=yySspjxxReportMapper.getYySspjxxLsMxReport(filter);
                ExcleFileUtil.exportExcel(response, list, YySspjxxMxReport.class, "收费流水明细报表.xlsx", "",properties);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
        PageDto<YySspjxxLsMxReport> dto=yySspjxxReportMapper.getYySspjxxLsMxReport(pageDto,filter);
        return  dto.toVo();
    }
}
