package com.sptg.video.cloud.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.video.cloud.api.beans.ApiResultBean;
import com.sptg.video.cloud.api.dto.DistrictNodeDto;
import com.sptg.video.cloud.api.entity.VCApiLog;
import com.sptg.video.cloud.api.entity.DistrictNode;
import com.sptg.video.cloud.api.entity.SysParams;
import com.sptg.video.cloud.api.utils.VideoCloudHttpUtil;
import com.sptg.video.cloud.biz.mapper.DistrictNodeMapper;
import com.sptg.video.cloud.biz.service.VCApiLogServiceInterface;
import com.sptg.video.cloud.biz.service.DistrictNodeServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author zwx
 * @create 2020/10/18 12:01
 * 监控点 服务层实现层
 */
@Service
@RequiredArgsConstructor
public class DistrictNodeServiceImpl extends ServiceImpl<DistrictNodeMapper, DistrictNode> implements DistrictNodeServiceInterface {

    private final VCApiLogServiceInterface apiLogServiceInterface;

    @Override
    public List<DistrictNode> showDistrictNodeList(DistrictNodeDto districtNodeDto) {
        // 构建查询条件
        Wrapper<DistrictNode> wrapper = new EntityWrapper<>();

        //根据id查询
        if (districtNodeDto.getLId() != null) {
            wrapper.eq("l_id", districtNodeDto.getId());
        }
        List<DistrictNode> districtNodes = this.selectList(wrapper);
        return districtNodes;
    }

    @Override
    public DistrictNode findDistrictNodeByLId(Long lid) {
        return this.selectById(lid);
    }

    @Override
    public DistrictNode findByDistrictId(String id) {

        Wrapper<DistrictNode> wrapper = new EntityWrapper<>();
        //根据id查询
        if (StringUtils.hasText(id)) {
            wrapper.eq("s_id", id);
        }
        DistrictNode districtNode = this.selectOne(wrapper);
        return districtNode;
    }

    @Override
    public List<DistrictNode> syncDistrictNodeList(SysParams sysParams, DistrictNodeDto districtNodeDto) {
        VideoCloudHttpUtil httpUtil = new VideoCloudHttpUtil(sysParams);
        List<DistrictNode> districtList = new ArrayList<>();
            ApiResultBean<DistrictNode> districtApiResultBean = httpUtil.deviceTreeList(null);
            if(districtApiResultBean.getReturnCode().equals(OperationConstant.ONE + "")){
                DistrictNode districtNode = BeanUtil.copyProperties(districtApiResultBean.getResult(), DistrictNode.class);
                VCApiLog apiLog = BeanUtil.copyProperties(districtApiResultBean, VCApiLog.class);
                districtList.add(districtNode);
                String districtApiResultBeanStr = JSONUtil.toJsonStr(districtApiResultBean);
                apiLog.setRespInfo(districtApiResultBeanStr);
                apiLog.setCreateDate(new Date());
                apiLog.setApiMethodName("获取设备监控点列表");
                this.apiLogServiceInterface.insert(apiLog);
            }
        return districtList;
    }
}
