package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.domain.BMainPollDisGasWzzTout;
import cn.cecep.talroad.mapper.BMainPollDisGasWzzToutMapper;
import cn.cecep.talroad.service.BMainPollDisGasWzzToutService;
import cn.cecep.talroad.vo.query.BMainPollDisGasToutQuery;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BMainPollDisGasWzzToutServiceImpl implements BMainPollDisGasWzzToutService {

    @Resource
    private BMainPollDisGasWzzToutMapper bMainPollDisGasWzzToutMapper;

    @Override
    public IPage<BMainPollDisGasWzzTout> pullPage(BMainPollDisGasToutQuery bMainPollDisGasTout) {
        Page<BMainPollDisGasWzzTout> list = bMainPollDisGasWzzToutMapper.selectPage(new Page<>(bMainPollDisGasTout.getPageNum(), bMainPollDisGasTout.getPageSize()), new QueryWrapper<BMainPollDisGasWzzTout>().eq("factory_id", bMainPollDisGasTout.getFactoryId()).eq("poll_dis_id", bMainPollDisGasTout.getPollDisId()));
        List<BMainPollDisGasWzzTout> records = list.getRecords();
        if(CollectionUtils.isNotEmpty(records)){
            List<BMainPollDisGasWzzTout> mainDrain = records.stream().filter(item -> item.getOutletUseTotal().equals("1")).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(mainDrain)){
                mainDrain.forEach(item->{
                    item.setPollutantsMgDay(item.getPollutantsType());
                    item.setPollutantsType("");
                    item.setVent("全厂无组织排放总计");
                });
            }
            List<BMainPollDisGasWzzTout> drain = records.stream().filter(item -> item.getOutletUseTotal().equals("0")).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(drain)){
                drain.sort(Comparator.comparing(BMainPollDisGasWzzTout::getOutletCode, Comparator.naturalOrder()));
            }
            ArrayList<BMainPollDisGasWzzTout> res = Lists.newArrayList();
            res.addAll(drain);
            res.addAll(mainDrain);
            list.setRecords(res);
        }
        return list;
    }
}
