package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.BMainProductionInorgGas;
import cn.cecep.talroad.domain.PcOutlet;
import cn.cecep.talroad.mapper.BMainProductionInorgGasMapper;
import cn.cecep.talroad.mapper.PcOutletMapper;
import cn.cecep.talroad.service.IAmVideoDevFiService;
import cn.cecep.talroad.service.IBMainProductionInorgGasService;
import cn.cecep.talroad.service.IProductionManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;


/**
 * 废气无组织排放产治排信息Service业务层处理
 *
 * @author szhpt
 * @date 2023-02-08*/
@Service
public class BMainProductionInorgGasServiceImpl extends ServiceImpl<BMainProductionInorgGasMapper,BMainProductionInorgGas> implements IBMainProductionInorgGasService
{
    @Autowired
    private BMainProductionInorgGasMapper bMainProductionInorgGasMapper;

    @Autowired
    private PcOutletMapper outletMapper;

    @Autowired
    private IProductionManageService productionManageService;

    @Autowired
    private IAmVideoDevFiService videoDevFiService;

    /**
     * 查询废气无组织排放产治排信息列表
     *
     * @param bMainProductionInorgGas 废气无组织排放产治排信息
     * @return 废气无组织排放产治排信息
     */
    @Override
    public List<BMainProductionInorgGas> selectBMainProductionInorgGasList(BMainProductionInorgGas bMainProductionInorgGas)
    {
        return bMainProductionInorgGasMapper.selectBMainProductionInorgGasList(bMainProductionInorgGas);
    }

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

        List<PcOutlet> outlets = outletMapper.selectList(new QueryWrapper<PcOutlet>()
                .eq("outlet_code", inorgGas.getOutletCode())
                .eq("factory_id", inorgGas.getFactoryId())
                .in("pollutant_type", "1", "2")
        );
        String outletId = outlets.isEmpty() ? null : outlets.get(0).getId();

        // 排口关联的工况设备/分表计电设备id列表
        List<String> deviceIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(inorgGas.getCorrespondingProductionId())) {
            deviceIds.addAll(Arrays.asList(inorgGas.getCorrespondingProductionId().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(inorgGas.getFactoryId(), outletId, deviceIds));
        return frameData;
    }

}
