package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.domain.BMainPollDisGasMout;
import cn.cecep.talroad.mapper.BMainPollDisGasMoutMapper;
import cn.cecep.talroad.service.IBMainPollDisGasMoutService;
import cn.cecep.talroad.vo.query.BMainPollDisGasMoutQuery;
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 BMainPollDisGasMoutServiceImpl extends ServiceImpl<BMainPollDisGasMoutMapper,BMainPollDisGasMout> implements IBMainPollDisGasMoutService
{
    @Autowired
    private BMainPollDisGasMoutMapper bMainPollDisGasMoutMapper;

    /**
     * 查询排污许可大气主排放口列表
     *
     * @param bMainPollDisGasMout 排污许可大气主排放口
     * @return 排污许可大气主排放口
     */
    @Override
    public List<BMainPollDisGasMout> selectBMainPollDisGasMoutList(BMainPollDisGasMout bMainPollDisGasMout)
    {
        return bMainPollDisGasMoutMapper.selectBMainPollDisGasMoutList(bMainPollDisGasMout);
    }

    @Override
    public IPage<BMainPollDisGasMout> pullPage(BMainPollDisGasMoutQuery bMainPollDisGasMout) {
        Page<BMainPollDisGasMout> page = new Page<>(bMainPollDisGasMout.getPageNum(), bMainPollDisGasMout.getPageSize());
        IPage<BMainPollDisGasMout> list = bMainPollDisGasMoutMapper.selectPage(page, BMainPollDisGasMoutQuery.createLambdaQueryWrapper(bMainPollDisGasMout));
        List<BMainPollDisGasMout> records = list.getRecords();
        if(CollectionUtils.isNotEmpty(records)){
            List<BMainPollDisGasMout> 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<BMainPollDisGasMout> drain = records.stream().filter(item -> item.getOutletUseTotal().equals("0")).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(drain)){
                drain.sort(Comparator.comparing(BMainPollDisGasMout::getOutletCode, Comparator.naturalOrder()));
            }
            ArrayList<BMainPollDisGasMout> res = Lists.newArrayList();
            res.addAll(drain);
            res.addAll(mainDrain);
            list.setRecords(res);
        }
        return list;
    }
}
