package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.BMainProductionOrganizGas;
import cn.cecep.talroad.domain.PcOutlet;
import cn.cecep.talroad.mapper.BMainProductionOrganizGasMapper;
import cn.cecep.talroad.mapper.PcOutletMapper;
import cn.cecep.talroad.service.IAmVideoDevFiService;
import cn.cecep.talroad.service.IBMainProductionOrganizGasService;
import cn.cecep.talroad.service.IPcFactoryService;
import cn.cecep.talroad.service.IProductionManageService;
import cn.cecep.talroad.vo.BMainProductionOrganizGasVo;
import cn.cecep.talroad.vo.PcFactoryVo;
import cn.cecep.talroad.vo.query.BMainProductionOrganizGasQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 废气有组织排放产治排信息Service业务层处理
 *
 * @author szhpt
 * @date 2023-02-08*/
@Service
public class BMainProductionOrganizGasServiceImpl extends ServiceImpl<BMainProductionOrganizGasMapper,BMainProductionOrganizGas> implements IBMainProductionOrganizGasService
{
    @Autowired
    private BMainProductionOrganizGasMapper bMainProductionOrganizGasMapper;

    @Autowired
    private PcOutletMapper outletMapper;

    @Autowired
    private IPcFactoryService factoryService;

    @Autowired
    private IProductionManageService productionManageService;

    @Autowired
    private IAmVideoDevFiService videoDevFiService;

    /**
     * 查询废气有组织排放产治排信息列表
     *
     * @param bMainProductionOrganizGas 废气有组织排放产治排信息
     * @return 废气有组织排放产治排信息
     */
    @Override
    public List<BMainProductionOrganizGas> selectBMainProductionOrganizGasList(BMainProductionOrganizGas bMainProductionOrganizGas)
    {
        return bMainProductionOrganizGasMapper.selectBMainProductionOrganizGasList(bMainProductionOrganizGas);
    }

    @Override
    public IProductionManageService.FrameDataVo getFrameData(String id, String startTime, String endTime, Long pageNum, Long pageSize) {
        BMainProductionOrganizGas organizGas = getById(Long.valueOf(id));

        List<PcOutlet> outlets = outletMapper.selectList(new QueryWrapper<PcOutlet>()
                .eq("outlet_code", organizGas.getOutletCode())
                .eq("factory_id", organizGas.getFactoryId())
                .in("pollutant_type", "1", "2")
        );

        String outletId = outlets.isEmpty() ? null : outlets.get(0).getId();

        // 排口关联的工况设备/分表计电设备id列表
        List<String> deviceIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(organizGas.getCepFacilityId())) {
            deviceIds.addAll(Arrays.asList(organizGas.getCepFacilityId().split(",")));
        }
        if (StringUtils.isNotEmpty(organizGas.getProFacilityId())) {
            deviceIds.addAll(Arrays.asList(organizGas.getProFacilityId().split(",")));
        }
        IProductionManageService.SearchVo searchVo = productionManageService.getSearchVo(outletId, deviceIds);
        searchVo.setStartTime(startTime);
        searchVo.setEndTime(endTime);
        searchVo.setPageNum(pageNum);
        searchVo.setPageSize(pageSize);

        IProductionManageService.FrameDataVo frameData = productionManageService.getFrameData(searchVo);
        frameData.setVideoDevFiVos(videoDevFiService.getGroupList(organizGas.getFactoryId(), outletId, deviceIds));
        return frameData;
    }

    @Override
    public List<BMainProductionOrganizGasVo.PwxkCzp> selectPwxkList(Page<BMainProductionOrganizGasVo.PwxkCzp> page, BMainProductionOrganizGasQuery.PwxkCzp 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<BMainProductionOrganizGasVo.PwxkCzp> dataList = baseMapper.selectPwxkList(factoryIds, query);
        return dataList;
    }

}
