package com.njcn.csdevice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.access.api.CsLineLatestDataFeignClient;
import com.njcn.access.pojo.po.CsLineLatestData;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.utils.PubUtils;
import com.njcn.csdevice.api.CsCommTerminalFeignClient;
import com.njcn.csdevice.enums.LineBaseEnum;
import com.njcn.csdevice.mapper.*;
import com.njcn.csdevice.pojo.dto.CsLineDTO;
import com.njcn.csdevice.pojo.dto.DevDetailDTO;
import com.njcn.csdevice.pojo.param.CsLineParam;
import com.njcn.csdevice.pojo.po.*;
import com.njcn.csdevice.pojo.vo.LineDetailDataVO;
import com.njcn.csdevice.pojo.vo.PqSensitiveUserLineVO;
import com.njcn.csdevice.service.CsDevModelService;
import com.njcn.csdevice.service.CsLinePOService;
import com.njcn.csdevice.service.ICsDataSetService;
import com.njcn.csdevice.service.ICsDeviceRegistryService;
import com.njcn.csharmonic.api.CsHarmonicPlanFeignClient;
import com.njcn.csharmonic.api.CsHarmonicPlanLineFeignClient;
import com.njcn.csharmonic.api.PqGovernPlanFeignClient;
import com.njcn.csharmonic.api.PqSensitiveUserFeignClient;
import com.njcn.csharmonic.param.CsHarmonicPlanLineParam;
import com.njcn.csharmonic.pojo.po.CsHarmonicPlan;
import com.njcn.csharmonic.pojo.po.PqGovernPlan;
import com.njcn.device.biz.pojo.po.PqSensitiveUser;
import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.api.DictTreeFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.constant.UserType;
import com.njcn.user.pojo.vo.UserVO;
import com.njcn.web.pojo.param.BaseParam;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * Description:
 * Date: 2023/5/18  14:01【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CsLinePOServiceImpl extends ServiceImpl<CsLinePOMapper, CsLinePO> implements CsLinePOService{

    private final CsLedgerMapper csLedgerMapper;
    private final CsDevModelService csDevModelService;
    private final ICsDataSetService csDataSetService;
    private final DicDataFeignClient dicDataFeignClient;
    private final CsTerminalLogsMapper csTerminalLogsMapper;
    private final PqSensitiveUserFeignClient pqSensitiveUserFeignClient;
    private final PqGovernPlanFeignClient pqGovernPlanFeignClient;
    private final FileStorageUtil fileStorageUtil;
    private final UserFeignClient userFeignClient;
    private final CsEquipmentDeliveryMapper csEquipmentDeliveryMapper;
    private final CsDeviceUserPOMapper csDeviceUserPOMapper;
    private final DictTreeFeignClient dictTreeFeignClient;
    private final CsHarmonicPlanFeignClient csHarmonicPlanFeignClient;
    private final CsHarmonicPlanLineFeignClient csHarmonicPlanLineFeignClient;
    private final CsLineLatestDataFeignClient csLineLatestDataFeignClient;
    private final CsCommTerminalFeignClient csCommTerminal;
    private final ICsDeviceRegistryService csDeviceRegistryService;

    @Override
    public List<CsLinePO> getLineByDev(List<String> list) {
        List<CsLinePO> result = new ArrayList<>();
        list.forEach(temp->{
            List<CsLinePO> csLinePOList = queryByDevId(temp);
            result.addAll(csLinePOList);
        });
    return result;
    }

    @Override
    public List<CsLinePO> queryByDevId(String devId) {
        QueryWrapper<CsLedger> queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",1).eq("pid",devId);
        List<CsLedger> csLedgerList = csLedgerMapper.selectList(queryWrapper);
        List<String> csPairList = csLedgerList.stream().map(CsLedger::getId).collect(Collectors.toList());
        if(CollectionUtil.isEmpty(csPairList)){
            return new ArrayList<>();
        }
        List<CsLinePO> csLinePOS = this.lambdaQuery().in(CsLinePO::getLineId,csPairList).eq(CsLinePO::getStatus,1).list();
        csLinePOS.forEach(temp->temp.setDevId(devId));
        return csLinePOS;
    }

    @Override
    public List<CsLinePO> findByNdid(String id) {
        return this.baseMapper.findByNdid(id);
    }

    public Map<String,String> getCustomDetailByLineId(String lineId){
        return this.baseMapper.getCustomDetailByLineId(lineId);
    }

    @Override
    public void updateLine(CsLineParam csLineParam) {
        LambdaUpdateWrapper<CsLinePO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CsLinePO::getLineId,csLineParam.getLineId())
                            .eq(CsLinePO::getStatus,1)
                            .set(CsLinePO::getVolGrade,csLineParam.getVolGrade())
                            .set(CsLinePO::getPtRatio,csLineParam.getPtRatio())
                            .set(CsLinePO::getCtRatio,csLineParam.getCtRatio())
                            .set(CsLinePO::getConType,csLineParam.getConType())
                            .set(CsLinePO::getLineInterval,csLineParam.getLineInterval());
        this.update(lambdaUpdateWrapper);
    }

    @Override
    public void updateIds(CsLineParam csLineParam) {
        LambdaUpdateWrapper<CsLinePO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CsLinePO::getLineId,csLineParam.getLineId())
                .eq(CsLinePO::getStatus,1)
                .set(CsLinePO::getDataSetId,csLineParam.getDataSetId())
                .set(CsLinePO::getDataModelId,csLineParam.getModelId());
        this.update(lambdaUpdateWrapper);
    }

    @Override
    public List<CsLinePO> getLinesByDevList(List<String> list) {
        return this.lambdaQuery().in(CsLinePO::getDeviceId,list).eq(CsLinePO::getStatus,1).list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CsLinePO addCldLine(CsLineParam param) {
        String lineId = IdUtil.fastSimpleUUID();
        String nDid = param.getDevMac().replace(":", "");
        List<CsDeviceRegistry> data = csDeviceRegistryService.queryByCurrentNdid(nDid);
        Map<Integer, String> clDidToIdMap;
        if (CollUtil.isNotEmpty(data)) {
            clDidToIdMap = data.stream().collect(Collectors.toMap(CsDeviceRegistry::getClDid, CsDeviceRegistry::getId, (a, b) -> a));
        } else {
            clDidToIdMap = new HashMap<>();
        }
        if (!Objects.isNull(clDidToIdMap.get(param.getLineNo()))) {
            lineId = clDidToIdMap.get(param.getLineNo());
        }
        CsLinePO po = new CsLinePO();
        //1.新增监测点信息
        BeanUtils.copyProperties(param,po);
        po.setStatus(1);
        po.setRunStatus(param.getRunStatus());
        po.setLineId(lineId);
        //模板id
        CsDevModelPO po1 = csDevModelService.getCldModel();
        po.setDataModelId(po1.getId());
        //设备id
        po.setDeviceId(param.getDevId());
        //数据集id
        List<CsDataSet> list = csDataSetService.findDataSetByModelId(po1.getId());
        if (CollectionUtil.isNotEmpty(list)) {
            po.setDataSetId(list.get(0).getId());
        }
        po.setClDid(param.getLineNo());
        //监测位置
        po.setPosition(param.getPosition().isEmpty()?null:param.getPosition());
        this.save(po);
        //2.新增台账树信息
        CsLedger csLedger = new CsLedger();
        csLedger.setId(lineId);
        csLedger.setPid(param.getDevId());
        csLedger.setPids("0," + param.getEngineeringId() + "," + param.getProjectId() + "," + param.getDevId());
        csLedger.setName(param.getName());
        csLedger.setLevel(3);
        csLedger.setState(1);
        csLedger.setSort(0);
        csLedgerMapper.insert(csLedger);
        //3.新增稳态事件指标配置
        csHarmonicPlanLineFeignClient.deleteByLineIds(Collections.singletonList(lineId));
        List<CsHarmonicPlan> planList = csHarmonicPlanFeignClient.getByName("通用方案").getData();
        if (CollectionUtil.isNotEmpty(planList)) {
            CsHarmonicPlan plan = planList.get(0);
            CsHarmonicPlanLineParam param1 = new CsHarmonicPlanLineParam();
            param1.setId(plan.getId());
            param1.setLineIds(Collections.singletonList(lineId));
            csHarmonicPlanLineFeignClient.savePlanLines(param1);
        }
        //4.新增装置注册表数据
        if (Objects.isNull(clDidToIdMap.get(param.getLineNo()))) {
            CsDeviceRegistry registry = new CsDeviceRegistry();
            registry.setId(lineId);
            registry.setCurrentNdid(nDid);
            registry.setOldNdid(nDid);
            registry.setClDid(param.getLineNo());
            registry.setFirstSeenTime(LocalDateTime.now());
            csDeviceRegistryService.add(Collections.singletonList(registry));
        }
        return po;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCldLine(CsLineParam param) {
        LambdaQueryWrapper<CsLinePO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CsLinePO::getDeviceId,param.getDevId())
                .ne(CsLinePO::getLineId,param.getLineId())
                .eq(CsLinePO::getLineNo,param.getLineNo())
                .eq(CsLinePO::getStatus,1);
        List<CsLinePO> lineList = this.list(wrapper);
        if (CollectionUtil.isNotEmpty(lineList)) {
            throw new BusinessException("监测点线路号重复,请调整!");
        }
        LambdaUpdateWrapper<CsLinePO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CsLinePO::getLineId,param.getLineId())
                .eq(CsLinePO::getStatus,1)
                .set(CsLinePO::getName,param.getName())
                .set(CsLinePO::getLineNo,param.getLineNo())
                .set(CsLinePO::getConType,param.getConType())
                .set(CsLinePO::getLineInterval,param.getLineInterval())
                .set(CsLinePO::getPtRatio,param.getPtRatio())
                .set(CsLinePO::getPt2Ratio,param.getPt2Ratio())
                .set(CsLinePO::getCtRatio,param.getCtRatio())
                .set(CsLinePO::getCt2Ratio,param.getCt2Ratio())
                .set(CsLinePO::getVolGrade,param.getVolGrade())
                .set(CsLinePO::getRunStatus,param.getRunStatus())
                .set(CsLinePO::getBasicCapacity,param.getBasicCapacity())
                .set(CsLinePO::getDevCapacity,param.getDevCapacity())
                .set(CsLinePO::getProtocolCapacity,param.getProtocolCapacity())
                .set(CsLinePO::getShortCircuitCapacity,param.getShortCircuitCapacity())
                .set(CsLinePO::getMonitorObj,param.getMonitorObj())
                .set(CsLinePO::getMonitorObj,param.getMonitorObj())
                .set(CsLinePO::getPosition,param.getPosition())
                .set(CsLinePO::getLineLogLevel,param.getLineLogLevel())
                .set(CsLinePO::getIsImportant,param.getIsImportant());
        this.update(lambdaUpdateWrapper);

        //修改台账树中监测点的名称
        QueryWrapper<CsLedger> queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",1).eq("id",param.getLineId());
        CsLedger csLedger = csLedgerMapper.selectOne(queryWrapper);
        csLedger.setName(param.getName());
        csLedgerMapper.updateById(csLedger);

        //新增台账日志 1056协议记录，其他协议不记录
        String devId = param.getDevId();
        CsEquipmentDeliveryPO po = csEquipmentDeliveryMapper.selectById(devId);
        if (Objects.equals(po.getDevAccessMethod(), "CLD")) {
            CsTerminalLogs csTerminalLogs = new CsTerminalLogs();
            csTerminalLogs.setDeviceId(devId);
            csTerminalLogs.setOperateType(1);
            csTerminalLogs.setIsPush(0);
            csTerminalLogsMapper.insert(csTerminalLogs);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCldLine(String id) {
        CsLinePO po = this.getById(id);
        this.removeById(id);
        csLedgerMapper.deleteById(id);
        //删除稳态事件指标配置
        csHarmonicPlanLineFeignClient.deleteByLineIds(Collections.singletonList(id));
        //删除装置注册表数据
        csDeviceRegistryService.delete(id);
        //新增台账日志 1056协议记录，其他协议不记录
        String devId = po.getDeviceId();
        CsEquipmentDeliveryPO devPo = csEquipmentDeliveryMapper.selectById(devId);
        if (Objects.equals(devPo.getDevAccessMethod(), "CLD")) {
            CsTerminalLogs csTerminalLogs = new CsTerminalLogs();
            csTerminalLogs.setDeviceId(devId);
            csTerminalLogs.setOperateType(1);
            csTerminalLogs.setIsPush(0);
            csTerminalLogsMapper.insert(csTerminalLogs);
        }
    }

    @Override
    public void updateCldLineStatus(List<CsLinePO> lineList, Integer status) {
        lineList.forEach(item->{item.setRunStatus(status);});
        this.updateBatchById(lineList);
    }

    @Override
    public void updateLineDataByList(List<String> list, String id, String setId) {
        LambdaUpdateWrapper<CsLinePO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(CsLinePO::getDeviceId,list).set(CsLinePO::getDataModelId,id).set(CsLinePO::getDataSetId,setId);
        this.update(updateWrapper);
    }

    @Override
    public  List<CsLinePO> getLineByName(String lineName) {
        return this.lambdaQuery().eq(CsLinePO::getStatus, 1).like(CsLinePO::getName,lineName).list();
    }

    @Override
    public List<String> getAllLine() {
        return this.lambdaQuery()
                .eq(CsLinePO::getStatus, 1)
                .list()
                .stream()
                .map(CsLinePO::getLineId)
                .collect(Collectors.toList());
    }

    @Override
    public List<CsLineDTO> getAllLineDetail() {
        return this.baseMapper.findLineList();
    }

    @Override
    public List<CsLinePO> getSimpleLine() {
        List<String> devIds = commGetDevIds(RequestUtil.getUserIndex());
        if(CollUtil.isNotEmpty(devIds)){
            List<CsLinePO> poList = this.lambdaQuery().in(CsLinePO::getDeviceId,devIds)
                    .eq(CsLinePO::getStatus, DataStateEnum.ENABLE.getCode()).list();
            return poList;
        }else {
            return Collections.emptyList();
        }
    }

    private List<String> commGetDevIds(String userId){
        UserVO userVO = userFeignClient.getUserById(userId).getData();
        List<String> devIds;
        if(userVO.getType().equals(UserType.SUPER_ADMINISTRATOR ) || userVO.getType().equals(UserType.ADMINISTRATOR )){
            devIds = csEquipmentDeliveryMapper.selectList(new LambdaQueryWrapper<CsEquipmentDeliveryPO>().ne(CsEquipmentDeliveryPO::getRunStatus,DataStateEnum.DELETED.getCode())).stream().map(CsEquipmentDeliveryPO::getId).collect(Collectors.toList());
        } else {
            LambdaQueryWrapper<CsDeviceUserPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.select(CsDeviceUserPO::getDeviceId)
                    .and(w->w.eq(CsDeviceUserPO::getPrimaryUserId,userId).or().eq(CsDeviceUserPO::getSubUserId,userId))
                    .eq(CsDeviceUserPO::getStatus, DataStateEnum.ENABLE.getCode());
            List<CsDeviceUserPO> devList = this.csDeviceUserPOMapper.selectList(lambdaQueryWrapper);
            devIds = devList.stream().map(CsDeviceUserPO::getDeviceId).distinct().collect(Collectors.toList());
        }
        return devIds;
    }

    @Override
    public List<CsEquipmentDeliveryPO> getDevBySensitiveUser(List<String> list) {
        LambdaQueryWrapper<CsEquipmentDeliveryPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
//                .in(CsEquipmentDeliveryPO::getMonitorUser,list)
                .ne(CsEquipmentDeliveryPO::getRunStatus, DataStateEnum.DELETED.getCode());
        return csEquipmentDeliveryMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public Page<PqSensitiveUserLineVO> getSensitiveUserLineList(BaseParam param) {
        Page<PqSensitiveUserLineVO> result = new Page<>();
        result.setCurrent(param.getPageNum());
        result.setSize(param.getPageSize());

        // 查询登录用户拥有的监测点台账
        List<CsLinePO> poList = getSimpleLine();
        // 构建基础查询条件
        LambdaQueryWrapper<CsLinePO> lambdaQueryWrapper = new LambdaQueryWrapper<CsLinePO>()
                .eq(CsLinePO::getStatus, 1);
        // 只有当lineList不为空时才添加in条件
        if (CollUtil.isNotEmpty(poList)) {
            List<String> lineList = poList.stream().map(CsLinePO::getLineId).collect(Collectors.toList());
            lambdaQueryWrapper.in(CsLinePO::getLineId, lineList);
        }
        if (ObjectUtil.isNotNull(param.getSearchValue())) {
            lambdaQueryWrapper.and(par -> par.like(CsLinePO::getName, param.getSearchValue()));
        }
        Page<CsLinePO> linePage = this.page(new Page<>(param.getPageNum(), param.getPageSize()), lambdaQueryWrapper);
        BeanUtil.copyProperties(linePage, result);
        List<CsLinePO> records = linePage.getRecords();
        if (CollUtil.isEmpty(records)) {
            return result;
        }
        //获取用户id
        List<String> ids = records.stream().map(CsLinePO::getLineId).distinct().collect(Collectors.toList());
        List<PqGovernPlan> governPlans = pqGovernPlanFeignClient.getListByMonitorPoint(ids).getData();
        if (CollUtil.isEmpty(governPlans)) {
            return result;
        }
        Map<String, PqGovernPlan> lineMap1 = governPlans.stream().collect(Collectors.toMap(PqGovernPlan::getGovernBefore, item -> item));
        Map<String, PqGovernPlan> lineMap2 = governPlans.stream().collect(Collectors.toMap(PqGovernPlan::getGovernAfter, item -> item));

        List<String> sensitiveUserIds = governPlans.stream().map(PqGovernPlan::getPid).distinct().collect(Collectors.toList());
        Map<String, String> sensitiveUserNameMap = new HashMap<>();
        List<PqSensitiveUser> pqSensitiveUserList = pqSensitiveUserFeignClient.getListByIds(sensitiveUserIds).getData();
        if (CollUtil.isNotEmpty(pqSensitiveUserList)) {
            sensitiveUserNameMap = pqSensitiveUserList.stream().collect(Collectors.toMap(PqSensitiveUser::getId, PqSensitiveUser::getName));
        }
        // 最新数据时间
        List<CsLineLatestData> lineLatestDataList = csLineLatestDataFeignClient.listData().getData();
        Map<String,CsLineLatestData> lineLatestDataMap = new HashMap<>();
        if (CollUtil.isNotEmpty(lineLatestDataList)) {
            lineLatestDataMap = lineLatestDataList.stream().collect(Collectors.toMap(CsLineLatestData::getLineId, item -> item));
        }

        PqSensitiveUserLineVO sensitiveUserLineVO;

        List<String> lineIds = records.stream().map(CsLinePO::getLineId).distinct().collect(Collectors.toList());
        List<DevDetailDTO> devDetailDTOList = csCommTerminal.getLedgerByLineId(lineIds).getData();
        Map<String, DevDetailDTO> devDetailDTOMap = devDetailDTOList.stream().collect(Collectors.toMap(DevDetailDTO::getLineId, item -> item));

        List<PqSensitiveUserLineVO> list = new ArrayList<>();
        for (CsLinePO record : records) {
            sensitiveUserLineVO = new PqSensitiveUserLineVO();
            sensitiveUserLineVO.setEngineeringName(devDetailDTOMap.get(record.getLineId()).getEngineeringName());
            sensitiveUserLineVO.setProjectName(devDetailDTOMap.get(record.getLineId()).getProjectName());
            sensitiveUserLineVO.setDevName(devDetailDTOMap.get(record.getLineId()).getEquipmentName());
            // 治理对象 && 是否治理
            PqGovernPlan plan1 = lineMap1.get(record.getLineId());
            if (!Objects.isNull(plan1)) {
                sensitiveUserLineVO.setSensitiveUser(sensitiveUserNameMap.getOrDefault(plan1.getPid(), null));
                sensitiveUserLineVO.setGovern(Objects.isNull(plan1.getGovernMethod()) ? "未治理" : plan1.getGovernMethod());
            }
            PqGovernPlan plan2 = lineMap2.get(record.getLineId());
            if (!Objects.isNull(plan2)) {
                sensitiveUserLineVO.setSensitiveUser(sensitiveUserNameMap.getOrDefault(plan2.getPid(), null));
                sensitiveUserLineVO.setGovern(Objects.isNull(plan2.getGovernMethod()) ? "未治理" : plan2.getGovernMethod());
            }
            // 测点名称
            sensitiveUserLineVO.setLineId(record.getLineId());
            sensitiveUserLineVO.setLineName(record.getName());
            // 监测类型
            if (ObjectUtil.isNotNull(record.getPosition())) {
                sensitiveUserLineVO.setPosition(record.getPosition());
                DictData positionDictData = dicDataFeignClient.getDicDataById(record.getPosition()).getData();
                if (positionDictData != null) {
                    sensitiveUserLineVO.setPosition(positionDictData.getName());
                }
            }
            // 电压等级
            if (ObjectUtil.isNotNull(record.getVolGrade())) {
                sensitiveUserLineVO.setVolGrade(record.getVolGrade());
            }
            // 通讯状态
            sensitiveUserLineVO.setRunStatus(String.valueOf(devDetailDTOMap.get(record.getLineId()).getRunStatus()));
            // 报告文件
            sensitiveUserLineVO.setReportFilePath(record.getReportFilePath());
            //最新数据时间
            sensitiveUserLineVO.setLatestTime(Objects.isNull(lineLatestDataMap.get(record.getLineId()))? null : lineLatestDataMap.get(record.getLineId()).getTimeId());
            list.add(sensitiveUserLineVO);
        }
        result.setRecords(list);
        return result;
    }

    @Override
    public boolean uploadReport(MultipartFile file, String lineId) {
       String filePath =  fileStorageUtil.uploadMultipart(file, OssPath.GOVERN_HARMONIC_FILE, true);
       return this.update(new LambdaUpdateWrapper<CsLinePO>().eq(CsLinePO::getLineId, lineId).set(CsLinePO::getReportFilePath, filePath));
    }

    @Override
    public String getReportUrl(String lineId) {
        CsLinePO linePO = this.getById(lineId);
        if (linePO != null) {
            String reportFilePath = linePO.getReportFilePath();
            if (StrUtil.isNotBlank(reportFilePath)) {
               return fileStorageUtil.getFileUrl(reportFilePath);
            }
        }
        return "";
    }


    @Override
    public LineDetailDataVO getLineDetailData(String id) {
        if (StringUtils.isEmpty(id)) {
            return new LineDetailDataVO();
        } else {
            CsLinePO csLinePO = this.baseMapper.selectOne(new LambdaQueryWrapper<CsLinePO>().eq(CsLinePO::getLineId,id));
            //获取设备信息
            CsEquipmentDeliveryPO csEquipmentDelivery = csEquipmentDeliveryMapper.selectById(csLinePO.getDeviceId());
            String code = dictTreeFeignClient.queryById(csEquipmentDelivery.getDevType()).getData().getCode();

            CsLedger csLedger = csLedgerMapper.selectById(csLinePO.getLineId());
            LineDetailDataVO lineDetailDataVO = new LineDetailDataVO();

            lineDetailDataVO.setScale(csLinePO.getVolGrade()+"kV");
            if (Objects.equals(DicDataEnum.PORTABLE.getCode(),code)) {
                lineDetailDataVO.setAreaName("便携式工程");
                lineDetailDataVO.setBdName("便携式项目");
            } else {
                lineDetailDataVO.setAreaName(csLedgerMapper.selectById(csLedger.getPids().split(StrUtil.COMMA)[LineBaseEnum.ENGINEERING_LEVEL.getCode()+1]).getName());
                lineDetailDataVO.setBdName(csLedgerMapper.selectById(csLedger.getPids().split(StrUtil.COMMA)[LineBaseEnum.PROJECT_LEVEL.getCode()+1]).getName());
            }
            lineDetailDataVO.setLineName(csLinePO.getName());
            lineDetailDataVO.setLineId(csLinePO.getLineId());
            lineDetailDataVO.setPtType(PubUtils.ptType(csLinePO.getConType()));
            lineDetailDataVO.setPt(csLinePO.getPtRatio() + "/" + (Objects.isNull(csLinePO.getPt2Ratio())?1.0:csLinePO.getPt2Ratio()));
            lineDetailDataVO.setCt(csLinePO.getCtRatio() + "/" + (Objects.isNull(csLinePO.getCt2Ratio())?1.0:csLinePO.getCt2Ratio()));
            lineDetailDataVO.setDealCapacity(Objects.isNull(csLinePO.getProtocolCapacity())?0.0f:csLinePO.getProtocolCapacity().floatValue());
            lineDetailDataVO.setDevCapacity(Objects.isNull(csLinePO.getDevCapacity())?0.0f:csLinePO.getDevCapacity().floatValue());
            lineDetailDataVO.setShortCapacity(Objects.isNull(csLinePO.getShortCircuitCapacity())?0.0f:csLinePO.getShortCircuitCapacity().floatValue());
            lineDetailDataVO.setStandardCapacity(Objects.isNull(csLinePO.getBasicCapacity())?0.0f:csLinePO.getBasicCapacity().floatValue());
            lineDetailDataVO.setTimeInterval(csLinePO.getLineInterval());
            //获取监测点数据类型
            CsDataSet dataSet = csDataSetService.getById(csLinePO.getDataSetId());
            if (ObjectUtil.isNotNull(dataSet)) {
                lineDetailDataVO.setDataLevel(dataSet.getDataLevel());
            }
            return lineDetailDataVO;
        }
    }

    @Override
    public List<CsLinePO> getLineList(CsLinePO param) {
        List<String> keywordsLineIds = new ArrayList<>();
        List<CsLinePO> poList = getSimpleLine();
        if (CollUtil.isNotEmpty(poList) && !poList.isEmpty()) {
            keywordsLineIds = poList.stream().map(CsLinePO::getLineId).collect(Collectors.toList());
        } else {
            return poList;
        }
        List<CsLinePO> result = this.list(new LambdaQueryWrapper<CsLinePO>()
                .eq(CsLinePO::getStatus, 1)
                .eq(StrUtil.isNotBlank(param.getPosition()), CsLinePO::getPosition, param.getPosition())
                .in(CsLinePO::getLineId, keywordsLineIds)
                .orderByAsc(CsLinePO::getName));
        //根据监测点id查询设备信息
        if (CollectionUtil.isNotEmpty(result)) {
            //获取设备信息集合
            List<String> devList = result.stream().map(CsLinePO::getDeviceId).collect(Collectors.toList());
            List<CsEquipmentDeliveryPO> equipmentDeliveryList = csEquipmentDeliveryMapper.selectBatchIds(devList);
            Map<String,Integer> runStatusMap = equipmentDeliveryList.stream().collect(Collectors.toMap(CsEquipmentDeliveryPO::getId,CsEquipmentDeliveryPO::getRunStatus));
            result.forEach(item->{
                item.setRunStatus(runStatusMap.get(item.getDeviceId()));
            });
        }
        return result;
    }


    private String getRunStatusDescription(Integer runStatus) {
        switch (runStatus) {
            case 1:
                return "离线";
            case 2:
                return "在线";
            default:
                return "未知";
        }
    }

//    /**
//     * 1.平台端默认配置拓扑图模板，包含拓扑图信息(cs_topology_diagram_template)和拓扑图上监测点的点位信息(cs_line_topology_template)
//     *
//     * 2.移动端新增项目时可以上传自己的拓扑图也可以选择拓扑图模板
//     * 维护项目和拓扑图关系(cs_topology_diagram,这个表的id可以使用拓扑图模板id)；
//     * 用户使用拓扑图模板，则根据模板id查(cs_line_topology_template)库中有几个监测点，绑定到监测点模板关系表中(cs_line_topology_diagram);
//     * 用户自己添加拓扑图，则新增拓扑图信息(cs_topology_diagram),同时记录用户在此拓扑图上的点位信息(cs_line_topology_diagram)
//     *
//     * 3.用户注册直连设备，根据模板信息返回监测点数量，页面选择拓扑图，根据(cs_topology_diagram)拓扑图id查询(cs_line_topology_diagram)拓扑图监测点关系表，获取监测点信息。根据位置来获取打点信息。
//     * 注:cs_line_topology_diagram这个表还需要加个位置字段，不然无法根据位置来匹配对应的监测点
//     *
//     * 4.保存之后要在这些表中同步数据(cs_line、cs_ledger)
//     *
//     * @param list
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void addLines(List<CsLineParm> list) {
//        List<CsLedger> csLedgerLis = new ArrayList<>();
//        list.stream().forEach(temp->{
//            CsLinePO csLinePO = new CsLinePO();
//            BeanUtils.copyProperties(temp,csLinePO);
//            this.save(csLinePO);
//            String lineId = csLinePO.getLineId();
//            CsLedger csLedger = new CsLedger();
//
//            csLedger.setId(lineId);
//            csLedger.setPid(temp.getDevId());
//            csLedger.setLevel(3);
//            csLedger.setSort(1);
//            csLedger.setState(1);
//
//
//            AppLineTopologyDiagramPO appLineTopologyDiagramPO = new AppLineTopologyDiagramPO();
//            appLineTopologyDiagramPO.setId(temp.getTopoId());
//            appLineTopologyDiagramPO.setStatus("1");
//            appLineTopologyDiagramPO.setLineId(lineId);
//            appLineTopologyDiagramPO.setLat(temp.getLat());
//            appLineTopologyDiagramPO.setLng(temp.getLng());
//
//        });
//
//    }
}
