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.PresetInfoDto;
import com.sptg.video.cloud.api.entity.*;
import com.sptg.video.cloud.api.utils.VideoCloudHttpUtil;
import com.sptg.video.cloud.biz.mapper.PresetInfoMapper;
import com.sptg.video.cloud.biz.service.VCApiLogServiceInterface;
import com.sptg.video.cloud.biz.service.PresetInfoServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zwx
 * @create 2020/10/18 16:14
 */
@Service
@RequiredArgsConstructor
public class PresetInfoServiceImpl extends ServiceImpl<PresetInfoMapper, PresetInfo> implements PresetInfoServiceInterface {

    private final VCApiLogServiceInterface apiLogServiceInterface;

    @Override
    public List<PresetInfo> showPresetInfoList(PresetInfoDto presetInfoDto) {
        // 构建查询条件
        Wrapper<PresetInfo> wrapper = new EntityWrapper<>();

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

    @Override
    public PresetInfo findPresetInfoById(Long id) {
        Wrapper<PresetInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("l_id",id);
        return this.selectOne(wrapper);
    }

    @Override
    public PresetInfo findPresetInfoByPresetId(String presetId) {
        Wrapper<PresetInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("l_preset_id", presetId);
        return this.selectOne(wrapper);
    }

    @Override
    public List<PresetInfo> syncPresetInfoList(SysParams sysParams, PresetInfoDto presetInfoDto) {
        VideoCloudHttpUtil httpUtil = new VideoCloudHttpUtil(sysParams);
        Map<String, Object> bizParamsMap = new HashMap<>();
        bizParamsMap.put("deviceNum", presetInfoDto.getDeviceNum());
        if(presetInfoDto.getPresetId() != null){
            bizParamsMap.put("presetId", presetInfoDto.getPresetId());
        }
        ApiResultBean<PresetInfo> presetInfoApiResultBean = httpUtil.deviceGetPreset(bizParamsMap);
        List<PresetInfo> presetInfoList = null;
        if(presetInfoApiResultBean.getReturnCode().equals(OperationConstant.ONE + "")){
            presetInfoList = presetInfoApiResultBean.getListInfo();
            VCApiLog apiLog = BeanUtil.copyProperties(presetInfoApiResultBean, VCApiLog.class);
            String presetInfoApiResultBeanStr = JSONUtil.toJsonStr(presetInfoApiResultBean);
            apiLog.setRespInfo(presetInfoApiResultBeanStr);
            apiLog.setCreateDate(new Date());
            apiLog.setDeviceNum(presetInfoDto.getDeviceNum());
            apiLog.setApiMethodName("获取设备预置位列表");
            this.apiLogServiceInterface.insert(apiLog);
        }
        return presetInfoList;
    }
}
