package com.njcn.device.overlimit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.ServerEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.utils.EnumUtils;
import com.njcn.device.biz.enums.DeviceResponseEnum;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.common.service.GeneralDeviceService;
import com.njcn.device.biz.mapper.OverlimitMapper;
import com.njcn.device.overlimit.service.IOverLimitService;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.vo.LineOverLimitVO;
import com.njcn.web.utils.GeneralUtil;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * pqs
 *
 * @author cdf
 * @date 2023/6/7
 */
@Service
@RequiredArgsConstructor
public class OverLimitServiceImpl extends ServiceImpl<OverlimitMapper, Overlimit> implements IOverLimitService {

    private final GeneralDeviceService generalDeviceService;

    @Override
    public Overlimit getOverLimitDataById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public LineOverLimitVO getLineOverLimitDataById(String id) {
        //根据监测点id查询所在监测点的限值表数据
        Overlimit overlimit = this.baseMapper.selectById(id);
        LineOverLimitVO lineOverLimitVO = new LineOverLimitVO();
        BeanUtil.copyProperties(overlimit, lineOverLimitVO);
        lineOverLimitVO.setOddHarm(overlimit.getUharm3());
        lineOverLimitVO.setEvenHarm(overlimit.getUharm2());
        lineOverLimitVO.setInUharm(overlimit.getInuharm1());
        lineOverLimitVO.setInUharm16(overlimit.getInuharm16());
        return lineOverLimitVO;
    }

    @Override
    public List<Overlimit> getAllLineOverLimit(String serverType, String deptId) {
        List<Overlimit> overlimitList = new ArrayList<>();
        ServerEnum serverEnum = EnumUtils.getServerEnumByName(serverType);
        List<Integer> dataType = new ArrayList<>();
        dataType.add(2);
        switch (serverEnum) {
            case EVENT:
                dataType.add(0);
                break;
            case HARMONIC:
                dataType.add(1);
                break;
            default:
                dataType.add(0);
                dataType.add(1);
                break;
        }
        if (StrUtil.isBlank(deptId)) {
            //不传部门id情况,默认查询所有的投运在线的监测点
            overlimitList = this.baseMapper.getAllLineOverLimit(dataType);
        } else {
            DeviceInfoParam deviceInfoParam = new DeviceInfoParam(RequestUtil.getDeptIndex(), serverType);
            List<GeneralDeviceDTO> generalDeviceDTOList = generalDeviceService.getDeviceInfoAsDept(deviceInfoParam, Stream.of(0).collect(Collectors.toList()), Stream.of(1).collect(Collectors.toList()));
            if (CollectionUtil.isNotEmpty(generalDeviceDTOList)) {
                List<String> lineIndexes = new ArrayList<>();
                generalDeviceDTOList.forEach((item) -> lineIndexes.addAll(item.getLineIndexes()));

                if (lineIndexes.size() > 1000) {
                    List<List<String>> index = GeneralUtil.splitList(lineIndexes, 1000);
                    for (List<String> item : index) {
                        List<Overlimit> tem = this.baseMapper.selectBatchIds(item);
                        if (CollectionUtil.isNotEmpty(tem)) {
                            overlimitList.addAll(tem);
                        }
                    }
                } else {
                    overlimitList = this.baseMapper.selectBatchIds(lineIndexes);
                }
            } else {
                throw new BusinessException(DeviceResponseEnum.LINE_EMPTY);
            }
        }
        return overlimitList;
    }
}

