package com.njcn.csdevice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.csdevice.mapper.AppLineTopologyDiagramMapper;
import com.njcn.csdevice.pojo.param.AppTopologyDiagramQueryParm;
import com.njcn.csdevice.pojo.param.LinePostionParam;
import com.njcn.csdevice.pojo.po.AppLineTopologyDiagramPO;
import com.njcn.csdevice.pojo.po.CsLedger;
import com.njcn.csdevice.pojo.po.CsLinePO;
import com.njcn.csdevice.pojo.vo.AppLineTopologyDiagramVO;
import com.njcn.csdevice.pojo.vo.AppTopologyDiagramVO;
import com.njcn.csdevice.service.AppLineTopologyDiagramService;
import com.njcn.csdevice.service.AppTopologyDiagramService;
import com.njcn.csdevice.service.CsLinePOService;
import com.njcn.csdevice.service.ICsLedgerService;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.api.DictTreeFeignClient;
import com.njcn.system.pojo.vo.DictTreeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2023/3/27  10:18【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class AppLineTopologyDiagramServiceImpl extends ServiceImpl<AppLineTopologyDiagramMapper, AppLineTopologyDiagramPO> implements AppLineTopologyDiagramService {
    private final CsLinePOService csLinePOService;
    private final AppTopologyDiagramService appTopologyDiagramService;
    private final ICsLedgerService iCsLedgerService;
    private final RedisUtil redisUtil;
    private final DictTreeFeignClient dictTreeFeignClient;

    @Override
    public AppTopologyDiagramVO query(String devId) {
        CsLedger one = iCsLedgerService.lambdaQuery().eq(CsLedger::getId, devId).eq(CsLedger::getState,1).one();
        List<CsLinePO> csLinePOList = csLinePOService.queryByDevId(devId);
        List<String> collect = csLinePOList.stream().map(CsLinePO::getLineId).collect(Collectors.toList());
        if(CollectionUtil.isEmpty(collect)){
            throw new BusinessException("设备下暂未绑定监测点，未找到拓扑图信息");
        }
        QueryWrapper<AppLineTopologyDiagramPO> queryWrap = new QueryWrapper<>();
        queryWrap.select("distinct (id)").eq("status","1").in("line_id",collect);
        AppLineTopologyDiagramPO appLineTopologyDiagramPO = this.getBaseMapper().selectOne(queryWrap);
        AppTopologyDiagramQueryParm appTopologyDiagramQueryParm = new AppTopologyDiagramQueryParm();
        appTopologyDiagramQueryParm.setId(appLineTopologyDiagramPO.getId());
        List<AppTopologyDiagramVO> appTopologyDiagramVOS = appTopologyDiagramService.queryAppTopologyDiagram(appTopologyDiagramQueryParm);
        if(CollectionUtil.isEmpty(appTopologyDiagramVOS)){
            return new AppTopologyDiagramVO();
//            throw new BusinessException("设备下未找到拓扑图信息");
        }
        AppTopologyDiagramVO appTopologyDiagramVO = appTopologyDiagramVOS.get(0);
        appTopologyDiagramVO.setAppsLineTopologyDiagramPO(this.queryByLineIds(collect));
        appTopologyDiagramVO.setDevName(one.getName());
        return appTopologyDiagramVO;
    }

    @Override
    public List<AppLineTopologyDiagramVO> queryByTopoId(String id) {
        List<AppLineTopologyDiagramVO> result = new ArrayList<>();
        result = this.getBaseMapper().queryByTopoId( id);
        return result;
    }
    public List<AppLineTopologyDiagramVO> queryByLineIds(List<String> lineIds) {
        List<AppLineTopologyDiagramVO> result = new ArrayList<>();
        if (lineIds != null && !lineIds.isEmpty()) {
            result = this.getBaseMapper().queryByLineIds( lineIds);
        }
        return result;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditList(LinePostionParam linePostionParam) {
        linePostionParam.getPointList().forEach(temp->{
            csLinePOService.lambdaUpdate().eq(CsLinePO::getLineId,temp.getLineId()).set(CsLinePO::getName,temp.getName()).set(CsLinePO::getPosition,temp.getLinePostion()).update();
        this.lambdaUpdate().eq(AppLineTopologyDiagramPO::getId,temp.getId()).
                eq(AppLineTopologyDiagramPO::getLineId,temp.getLineId()).set(AppLineTopologyDiagramPO::getLat,temp.getLat()).
                set(AppLineTopologyDiagramPO::getLng,temp.getLng()).set(AppLineTopologyDiagramPO::getId,linePostionParam.getId())
                .set(AppLineTopologyDiagramPO::getTarget,temp.getTarget())
                .update();
        iCsLedgerService.lambdaUpdate().eq(CsLedger::getId,temp.getLineId()).set(CsLedger::getName,temp.getName()).update();
        });
        //修改指标后，删除缓存数据
        String deviceId = csLinePOService.getById(linePostionParam.getPointList().get(0).getLineId()).getDeviceId();
        DictTreeVO dictTreeVO = dictTreeFeignClient.queryByCode("baseData").getData();
        String key = deviceId + "#";
        if (dictTreeVO != null) {
            key = deviceId + "#" + dictTreeVO.getId();
        }
        redisUtil.delete(key);
    }
}
