package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.domain.BMainPollDisGasOout;
import cn.cecep.talroad.mapper.BMainPollDisGasOoutMapper;
import cn.cecep.talroad.service.IBMainPollDisGasOoutService;
import cn.cecep.talroad.vo.query.BMainPollDisGasOoutQuery;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;




/**
 * 排污许可大气一般排放口Service业务层处理
 *
 * @author szhpt
 * @date 2023-01-18*/
@Service
public class BMainPollDisGasOoutServiceImpl extends ServiceImpl<BMainPollDisGasOoutMapper,BMainPollDisGasOout> implements IBMainPollDisGasOoutService
{
    @Autowired
    private BMainPollDisGasOoutMapper bMainPollDisGasOoutMapper;

    /**
     * 查询排污许可大气一般排放口列表
     *
     * @param bMainPollDisGasOout 排污许可大气一般排放口
     * @return 排污许可大气一般排放口
     */
    @Override
    public List<BMainPollDisGasOout> selectBMainPollDisGasOoutList(BMainPollDisGasOout bMainPollDisGasOout)
    {
        return bMainPollDisGasOoutMapper.selectBMainPollDisGasOoutList(bMainPollDisGasOout);
    }

    @Override
    public IPage<BMainPollDisGasOout> pullPage(BMainPollDisGasOoutQuery bMainPollDisGasOout) {
        Page<BMainPollDisGasOout> page = new Page<>(bMainPollDisGasOout.getPageNum(), bMainPollDisGasOout.getPageSize());
        IPage<BMainPollDisGasOout> list = bMainPollDisGasOoutMapper.selectPage(page, BMainPollDisGasOoutQuery.createLambdaQueryWrapper(bMainPollDisGasOout));
        List<BMainPollDisGasOout> records = list.getRecords();
        if(CollectionUtils.isNotEmpty(records)){
            List<BMainPollDisGasOout> mainDrain = records.stream().filter(item -> item.getOutletUseTotal().equals("1")).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(mainDrain)){
                mainDrain.forEach(item->{
                    item.setPollutantsMgRate(item.getPollutantsType());
                    item.setPollutantsType("");
                    item.setVent("一般排放口合计");
                });
            }
            List<BMainPollDisGasOout> drain = records.stream().filter(item -> item.getOutletUseTotal().equals("0")).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(drain)){
                drain.sort(Comparator.comparing(BMainPollDisGasOout::getOutletCode, Comparator.naturalOrder()));
            }
            ArrayList<BMainPollDisGasOout> res = Lists.newArrayList();
            res.addAll(drain);
            res.addAll(mainDrain);
            list.setRecords(res);
        }
        return list;
    }
}
