package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.BStatTableQueryInfo;
import cn.cecep.talroad.mapper.*;
import cn.cecep.talroad.service.BStatTablePwxkWaterAndOtherService;
import cn.cecep.talroad.vo.*;
import cn.cecep.talroad.vo.query.BStatTablepPwxkWaterQuery;
import cn.cecep.talroad.vo.query.PcFactoryQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service
public class BStatTablePwxkWaterAndOtherServiceImpl implements BStatTablePwxkWaterAndOtherService {

    @Autowired
    private PcFactoryServiceImpl pcFactoryService;

    @Autowired
    private BStatTablePwxkWaterTreatmentLineMapper lineMapper;
    @Autowired
    private BStatTablePwxkWaterLifeInMapper lifeInMapper;
    @Autowired
    private BStatTablePwxkWaterTreatmentInAskMapper askMapper;
    @Autowired
    private BStatTablePwxkWaterIndustryInMapper industryInMapper;
    @Autowired
    private BStatTablePwxkWaterIndustryInTotalMapper industryInTotalMapper;
    @Autowired
    private BMainHazardousSolidWasteMapper solidWasteMapper;
    @Autowired
    private BMainHazardousSolidWasteFaMapper solidWasteFaMapper;
    @Autowired
    private BMainPollDisEnforcementMapper enforcementMapper;
    @Autowired
    private BMainPollDisInformationMapper informationMapper;
    @Autowired
    private BMainPollDisEmlRecordMapper emlRecordMapper;
    @Autowired
    private BMainPollDisNoiseRegulationMapper disNoiseRegulationMapper;
    @Autowired
    private BStatTablePwxkProductsMapper pwxkProductsMapper;
    @Autowired
    private BStatTableQueryInfoMapper queryInfoMapper;
    @Autowired
    private BStatTablePwxkProductionFacilitiesMapper productionFacilitiesMapper;
    @Autowired
    private BStatTablePwxkBoilerFuelGasMapper boilerFuelGasMapper;
    @Autowired
    private BStatTablePwxkBoilerFuelSolidLiquidMapper boilerFuelSolidLiquidMapper;
    @Autowired
    private BStatTablePwxkFuelMapper fuelMapper;
    @Autowired
    private BStatTablePwxkRawMaterialMapper materialMapper;
    @Override
    public AjaxResults<IPage<BStatTablePwxkWaterTreatmentLineVo>> selectProductLine(BStatTablepPwxkWaterQuery query) {

        return selectDataByQuery(lineMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BStatTablePwxkWaterLifeInVo>> selectLifeIn(BStatTablepPwxkWaterQuery query) {

        return selectDataByQuery(lifeInMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BStatTablePwxkWaterIndustryInVo>> selectIndustryIn(BStatTablepPwxkWaterQuery query) {

        return selectDataByQuery(industryInMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BStatTablePwxkWaterTreatmentInAskVo>> selectTreatmentInAsk(BStatTablepPwxkWaterQuery query) {

        return selectDataByQuery(askMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BStatTablePwxkWaterIndustryInTotalVo>> selectIndustryInTotal(BStatTablepPwxkWaterQuery query) {

        return selectDataByQuery(industryInTotalMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BMainHazardousSolidWasteVo>> selectHazardousSolidWaste(BStatTablepPwxkWaterQuery query) {

        return selectData(solidWasteMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BMainHazardousSolidWasteFaVo>> selectHazardousSolidWasteFa(BStatTablepPwxkWaterQuery query) {
        return selectDataByQuery(solidWasteFaMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BMainPollDisEnforcementVo>> selectEnforceComplianceReportingRequirements(BStatTablepPwxkWaterQuery query) {
        return selectDataByQuery(enforcementMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BMainPollDisInformationVo>> selectPollDisInformation(BStatTablepPwxkWaterQuery query) {
        return selectData(informationMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BMainPollDisEmlRecordVo>> selectBMainPollDisEmlRecord(BStatTablepPwxkWaterQuery query) {
        return selectData(emlRecordMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BMainPollDisNoiseRegulationVo>> selectBMainPollDisNoiseRegulation(BStatTablepPwxkWaterQuery query) {
        return selectData(disNoiseRegulationMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BStatTablePwxkProductsVo>> selectProducts(BStatTablepPwxkWaterQuery query) {
        return selectDataByQuery(pwxkProductsMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<Integer> saveQueryContent(BStatTableQueryInfo query) {
        query.setQueryUserId(SecurityUtils.getLoginUser().getSysUser().getUserId().toString());
        query.setTableStatus("0");
        return AjaxResults.success(queryInfoMapper.insert(query));
    }

    @Override
    public AjaxResults<IPage<BStatTablePwxkProductionFacilitiesVo>> selectProductionFacilities(BStatTablepPwxkWaterQuery query) {
        return selectDataByQuery(productionFacilitiesMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BStatTablePwxkBoilerFuelGasVo>> selectBoilerFuelGas(BStatTablepPwxkWaterQuery query) {

        return selectDataByQuery(boilerFuelGasMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BStatTablePwxkBoilerFuelSolidLiquidVo>> selectBoilerFuelSolidLiquid(BStatTablepPwxkWaterQuery query) {
        return selectDataByQuery(boilerFuelSolidLiquidMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BStatTablePwxkFuelVo>> selectFuel(BStatTablepPwxkWaterQuery query) {
        return selectDataByQuery(fuelMapper::selectListAndFactory,query);
    }

    @Override
    public AjaxResults<IPage<BStatTablePwxkRawMaterialVo>> selectMaterial(BStatTablepPwxkWaterQuery query) {
        return selectDataByQuery(materialMapper::selectListAndFactory,query);
    }

    /**
     * 获取企业id
     * @param query 高级查询查询条件
     * @return 企业id
     */
    public IPage<String> getFactoryIdList(BStatTablepPwxkWaterQuery query){
       /* Map<String,List<String>> map = new HashMap<>();
        for (Map.Entry<String, Object> entry : query.getLabelMap().entrySet()) {
            Object value = entry.getValue();
            if (value instanceof String){
                List<String> stringList =new ArrayList<>();
                stringList.add((String)value);
                map.put(entry.getKey(),stringList);
            }else {
                map.put(entry.getKey(),(List<String>) value);
            }

        }
        long l = System.currentTimeMillis();
        Page<String> page = new Page<>(query.getPageNum(),query.getPageSize());
        List<String> stringList = mapper.selectId(page, map, query.getFactoryName());
        long l1 = System.currentTimeMillis();
        System.out.println((l1-l)+"毫秒");
        page.setRecords(stringList);
        return page  ;*/
        Page<PcFactoryVo> page;
        Page<String> result=new Page<>();
        if(query.getPageNum()!=null){
            page = new Page<>(query.getPageNum(),query.getPageSize());
        }else {
            page = new Page<>(1,Integer.MAX_VALUE);
        }
        PcFactoryQuery.SearchVo searchVo=new PcFactoryQuery.SearchVo();
        BeanUtils.copyProperties(query,searchVo);

        List<PcFactoryVo> search = pcFactoryService.search(page, searchVo, null, SecurityUtils.getLoginUser().getSysUser().getRegionCode());

        BeanUtils.copyProperties(page, result);

        result.setRecords(search.stream().map(PcFactoryVo::getId).collect(Collectors.toList()));
        return result;
    }


    public <T> AjaxResults<IPage<T>> selectData(Function<List<String>,List<T>> selectListFunction,
                                                BStatTablepPwxkWaterQuery query) {
        Page<T> page = new Page<>();
        if(StringUtils.isBlank(query.getFactoryName()) && Collections.isEmpty(query.getLabelMap())&&query.getExportOperate()){

        }else {
            IPage<String> factoryIdList = getFactoryIdList(query);
            BeanUtils.copyProperties(factoryIdList, page);
            if (factoryIdList.getRecords().size() < 1&&!query.getExportOperate()) {
                return AjaxResults.success(page);
            }
            query.setFactoryIdList(factoryIdList.getRecords());
        }
        List<T> result = selectListFunction.apply(query.getFactoryIdList());
        page.setRecords(result);
        return AjaxResults.success(page);
    }
    public <T> AjaxResults<IPage<T>> selectDataByQuery(Function<BStatTablepPwxkWaterQuery,List<T>> selectListFunction,
                                                       BStatTablepPwxkWaterQuery query) {
        Page<T> page = new Page<>();
        //如果这样通过，说明是导出且是全部企业，之所以不放过查询，是因为mapper方法没有加page参数
        if(StringUtils.isBlank(query.getFactoryName()) && Collections.isEmpty(query.getLabelMap())&&query.getExportOperate()){

        }else {
            IPage<String> factoryIdList = getFactoryIdList(query);
            BeanUtils.copyProperties(factoryIdList, page);
            if (factoryIdList.getRecords().size() < 1) {
                return AjaxResults.success(page);
            }
            query.setFactoryIdList(factoryIdList.getRecords());
        }
        List<T> result = selectListFunction.apply(query);
        page.setRecords(result);
        return AjaxResults.success(page);
    }


}
