package com.njcn.product.device.ledger.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.njcn.product.carrycapacity.util.PubUtils;
import com.njcn.product.device.ledger.mapper.*;
import com.njcn.product.device.ledger.pojo.po.Device;
import com.njcn.product.device.ledger.pojo.po.Line;
import com.njcn.product.device.ledger.pojo.po.LineDetail;
import com.njcn.product.device.ledger.pojo.vo.LineDetailDataVO;
import com.njcn.product.device.ledger.pojo.vo.LineOverLimitVO;
import com.njcn.product.device.ledger.service.LineService;
import com.njcn.product.device.overlimit.pojo.Overlimit;
import com.njcn.product.system.dict.service.IDictDataService;
import com.njcn.web.pojo.vo.LineDataVO;
import com.njcn.web.utils.GeneralUtil;
import com.njcn.web.utils.RequestUtil;
import com.njcn.web.utils.WebUtil;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 监测点类
 *
 * @author denghuajun
 * @date 2022/2/23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LineServiceImpl extends ServiceImpl<LineMapper, Line> implements LineService {


    private final IDictDataService  iDictDataService;

    private final VoltageMapper voltageMapper;

    private final LineDetailMapper lineDetailMapper;

    private final DeviceMapper deviceMapper;

    private final OverlimitMapper overlimitMapper;

    @Override
    public Overlimit getOverLimitData(String id) {
        return overlimitMapper.selectById(id);
    }


    @Override
    public LineDetailDataVO getLineDetailData(String id) {
        if (StringUtils.isEmpty(id)) {
            return new LineDetailDataVO();
        } else {
            //根据id查询当前信息的pids
            List<String> pids = Arrays.asList(this.baseMapper.selectById(id).getPids().split(","));
            List<String> list = new ArrayList(pids);
            list.add(id);
            List<LineDataVO> lineDataVOList = this.baseMapper.getLineDetail(list);
            LineDetailDataVO lineDetailDataVO = new LineDetailDataVO();
            String areaId = "", devId = "", voId = "";
            for (LineDataVO lineDataVO : lineDataVOList) {
                switch (lineDataVO.getLevel()) {
                    case 1:
                        areaId = lineDataVO.getName();
                        break;
                    case 2:
                        lineDetailDataVO.setGdName(lineDataVO.getName());
                        break;
                    case 3:
                        lineDetailDataVO.setBdName(lineDataVO.getName());
                        break;
                    case 4:
                        devId = lineDataVO.getId();
                        lineDetailDataVO.setDevName(lineDataVO.getName());
                        break;
                    case 5:
                        voId = lineDataVO.getId();
                        break;
                    case 6:
                        lineDetailDataVO.setLineName(lineDataVO.getName());
                        break;
                    default:
                        break;
                }
            }
            lineDetailDataVO.setScale(iDictDataService.getDicDataById(voltageMapper.selectById(voId).getScale()).getName());
            LineDetail lineDetail = lineDetailMapper.selectById(id);
            Device device = deviceMapper.selectById(devId);
            lineDetailDataVO.setManufacturer(iDictDataService.getDicDataById(device.getManufacturer()).getName());
            lineDetailDataVO.setComFlag(PubUtils.comFlag(device.getComFlag()));
            lineDetailDataVO.setRunFlag(PubUtils.lineRunFlag(lineDetail.getRunFlag()));
            lineDetailDataVO.setIp(device.getIp());
            lineDetailDataVO.setLoginTime(device.getLoginTime());
            lineDetailDataVO.setDevId(device.getId());
            lineDetailDataVO.setBusinessType(iDictDataService.getDicDataById(lineDetail.getBusinessType()).getName());
            lineDetailDataVO.setLoadType(iDictDataService.getDicDataById(lineDetail.getLoadType()).getName());
            lineDetailDataVO.setObjName(lineDetail.getObjName());
            lineDetailDataVO.setId(lineDetail.getNum());
            lineDetailDataVO.setPtType(PubUtils.ptType(lineDetail.getPtType()));
            lineDetailDataVO.setPt(lineDetail.getPt1() + "/" + lineDetail.getPt2());
            lineDetailDataVO.setCt(lineDetail.getCt1() + "/" + lineDetail.getCt2());
            lineDetailDataVO.setDealCapacity(lineDetail.getDealCapacity());
            lineDetailDataVO.setDevCapacity(lineDetail.getDevCapacity());
            lineDetailDataVO.setShortCapacity(lineDetail.getShortCapacity());
            lineDetailDataVO.setStandardCapacity(lineDetail.getStandardCapacity());
            lineDetailDataVO.setTimeInterval(lineDetail.getTimeInterval());
            lineDetailDataVO.setOwner(lineDetail.getOwner());
            lineDetailDataVO.setOwnerDuty(lineDetail.getOwnerDuty());
            lineDetailDataVO.setOwnerTel(lineDetail.getOwnerTel());
            lineDetailDataVO.setWiringDiagram(lineDetail.getWiringDiagram());
            lineDetailDataVO.setPtPhaseType(lineDetail.getPtPhaseType());
            lineDetailDataVO.setUpdateTime(device.getUpdateTime());
            return lineDetailDataVO;
        }

    }


}
