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.BMainProductionWater;
import cn.cecep.talroad.domain.PcOutlet;
import cn.cecep.talroad.mapper.BMainProductionWaterMapper;
import cn.cecep.talroad.mapper.PcOutletMapper;
import cn.cecep.talroad.service.IAmVideoDevFiService;
import cn.cecep.talroad.service.IBMainProductionWaterService;
import cn.cecep.talroad.service.IPcFactoryService;
import cn.cecep.talroad.service.IProductionManageService;
import cn.cecep.talroad.vo.BMainProductionWaterVo;
import cn.cecep.talroad.vo.PcFactoryVo;
import cn.cecep.talroad.vo.query.BMainProductionWaterQuery;
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 BMainProductionWaterServiceImpl extends ServiceImpl<BMainProductionWaterMapper,BMainProductionWater> implements IBMainProductionWaterService
{
    @Autowired
    private BMainProductionWaterMapper bMainProductionWaterMapper;

    @Autowired
    private PcOutletMapper outletMapper;

    @Autowired
    private IPcFactoryService factoryService;

    @Autowired
    private IProductionManageService productionManageService;

    @Autowired
    private IAmVideoDevFiService videoDevFiService;

    /**
     * 查询废水排放产治排信息列表
     *
     * @param bMainProductionWater 废水排放产治排信息
     * @return 废水排放产治排信息
     */
    @Override
    public List<BMainProductionWater> selectBMainProductionWaterList(BMainProductionWater bMainProductionWater)
    {
        return bMainProductionWaterMapper.selectBMainProductionWaterList(bMainProductionWater);
    }

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

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

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

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

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

}
