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.CruiseInfoDto;
import com.sptg.video.cloud.api.entity.VCApiLog;
import com.sptg.video.cloud.api.entity.CruiseInfo;
import com.sptg.video.cloud.api.entity.SysParams;
import com.sptg.video.cloud.api.utils.VideoCloudHttpUtil;
import com.sptg.video.cloud.biz.mapper.CruiseInfoMapper;
import com.sptg.video.cloud.biz.service.VCApiLogServiceInterface;
import com.sptg.video.cloud.biz.service.CruiseInfoServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwx
 * @create 2020/10/18 17:07
 * 设备循环 服务层实现层
 */
@Service
@RequiredArgsConstructor
public class CruiseInfoServiceImpl extends ServiceImpl<CruiseInfoMapper, CruiseInfo> implements CruiseInfoServiceInterface {

    private final VCApiLogServiceInterface apiLogServiceInterface;

    @Override
    public List<CruiseInfo> showCruiseInfoList(CruiseInfoDto cruiseInfoDto) {
        // 构建查询条件
        Wrapper<CruiseInfo> wrapper = new EntityWrapper<>();

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

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

    @Override
    public CruiseInfo findCruiseInfoByCruiseGr(Long cruiseGr) {
        Wrapper<CruiseInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("l_cruise_gr", cruiseGr);
        return this.selectOne(wrapper);
    }

    @Override
    public List<CruiseInfo> syncCruiseInfoList(SysParams sysParams, CruiseInfoDto cruiseInfoDto) {
        VideoCloudHttpUtil httpUtil = new VideoCloudHttpUtil(sysParams);
        Map<String, Object> bizParamsMap = new HashMap<>();
        bizParamsMap.put("deviceNum", cruiseInfoDto.getDeviceNum());
        if(cruiseInfoDto.getCruiseGr() != null){
            bizParamsMap.put("cruiseGr", cruiseInfoDto.getCruiseGr());
        }
        ApiResultBean<CruiseInfo> cruiseInfoApiResultBean = httpUtil.deviceGetCruise(bizParamsMap);
        List<CruiseInfo> presetInfoList = null;
        if(cruiseInfoApiResultBean.getReturnCode().equals(OperationConstant.ONE + "")){
            presetInfoList = cruiseInfoApiResultBean.getListInfo();
            VCApiLog apiLog = BeanUtil.copyProperties(cruiseInfoApiResultBean, VCApiLog.class);
            String cruiseInfoApiResultBeanStr = JSONUtil.toJsonStr(cruiseInfoApiResultBean);
            apiLog.setRespInfo(cruiseInfoApiResultBeanStr);
            apiLog.setCreateDate(new Date());
            apiLog.setApiMethodName("获取设备巡航");
            this.apiLogServiceInterface.insert(apiLog);
        }
        return presetInfoList;
    }
}
