package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.PcControlRequirements;
import cn.cecep.talroad.mapper.PcControlRequirementsMapper;
import cn.cecep.talroad.service.IPcControlRequirementsService;
import cn.cecep.talroad.service.IPcFactoryService;
import cn.cecep.talroad.vo.PcControlRequirementsVo;
import cn.cecep.talroad.vo.PcFactoryVo;
import cn.cecep.talroad.vo.query.PcControlRequirementsQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 管控要求Service业务层处理
 *
 * @author szhpt
 * @date 2023-01-10*/
@Service
public class PcControlRequirementsServiceImpl extends ServiceImpl<PcControlRequirementsMapper,PcControlRequirements> implements IPcControlRequirementsService
{
    @Autowired
    private PcControlRequirementsMapper pcControlRequirementsMapper;

    @Autowired
    private IPcFactoryService factoryService;

    /**
     * 查询管控要求列表
     *
     * @param pcControlRequirements 管控要求
     * @return 管控要求
     */
    @Override
    public List<PcControlRequirements> selectPcControlRequirementsList(PcControlRequirements pcControlRequirements)
    {
        return pcControlRequirementsMapper.selectPcControlRequirementsList(pcControlRequirements);
    }

    @Override
    public List<PcControlRequirementsVo.PwxkGasOrg> selectPwxkGasOrg(Page<PcControlRequirementsVo.PwxkGasOrg> page, PcControlRequirementsQuery.PwxkGasOrg query) {
        Page<PcFactoryVo> factoryPage = new Page<>(page.getCurrent(), page.getSize());
        List<PcFactoryVo> list = factoryService.search(factoryPage, query.getSearchVo(), "list", SecurityUtils.getLoginUser().getSysUser().getRegionCode());
        page.setTotal(factoryPage.getTotal());
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> factoryIds = list.stream().map(PcFactoryVo::getId).collect(Collectors.toList());
        List<PcControlRequirementsVo.PwxkGasOrg> dataList = baseMapper.selectPwxkGasOrg(factoryIds, query);
        return dataList;
    }

    @Override
    public List<PcControlRequirementsVo.PwxkGasNoOrg> selectPwxkGasNoOrg(Page<PcControlRequirementsVo.PwxkGasNoOrg> page, PcControlRequirementsQuery.PwxkGasNoOrg query) {
        Page<PcFactoryVo> factoryPage = new Page<>(page.getCurrent(), page.getSize());
        List<PcFactoryVo> list = factoryService.search(factoryPage, query.getSearchVo(), "list", SecurityUtils.getLoginUser().getSysUser().getRegionCode());
        page.setTotal(factoryPage.getTotal());
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> factoryIds = list.stream().map(PcFactoryVo::getId).collect(Collectors.toList());
        List<PcControlRequirementsVo.PwxkGasNoOrg> dataList = baseMapper.selectPwxkGasNoOrg(factoryIds, query);
        return dataList;
    }

    @Override
    public List<PcControlRequirementsVo.PwxkWaterDirect> selectPwxkWaterDirect(Page<PcControlRequirementsVo.PwxkWaterDirect> page, PcControlRequirementsQuery.PwxkWaterDirect query) {
        Page<PcFactoryVo> factoryPage = new Page<>(page.getCurrent(), page.getSize());
        List<PcFactoryVo> list = factoryService.search(factoryPage, query.getSearchVo(), "list", SecurityUtils.getLoginUser().getSysUser().getRegionCode());
        page.setTotal(factoryPage.getTotal());
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> factoryIds = list.stream().map(PcFactoryVo::getId).collect(Collectors.toList());
        List<PcControlRequirementsVo.PwxkWaterDirect> dataList = baseMapper.selectPwxkWaterDirect(factoryIds, query);
        return dataList;
    }

    @Override
    public List<PcControlRequirementsVo.PwxkWaterIndirect> selectPwxkWaterIndirect(Page<PcControlRequirementsVo.PwxkWaterIndirect> page, PcControlRequirementsQuery.PwxkWaterIndirect query) {
        Page<PcFactoryVo> factoryPage = new Page<>(page.getCurrent(), page.getSize());
        List<PcFactoryVo> list = factoryService.search(factoryPage, query.getSearchVo(), "list", SecurityUtils.getLoginUser().getSysUser().getRegionCode());
        page.setTotal(factoryPage.getTotal());
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> factoryIds = list.stream().map(PcFactoryVo::getId).collect(Collectors.toList());
        List<PcControlRequirementsVo.PwxkWaterIndirect> dataList = baseMapper.selectPwxkWaterIndirect(factoryIds, query);
        return dataList;
    }

}
