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.FileInfoDto;
import com.sptg.video.cloud.api.entity.VCApiLog;
import com.sptg.video.cloud.api.entity.FileInfo;
import com.sptg.video.cloud.api.entity.SysParams;
import com.sptg.video.cloud.api.beans.PageInfo;
import com.sptg.video.cloud.api.utils.VideoCloudHttpUtil;
import com.sptg.video.cloud.biz.mapper.FileInfoMapper;
import com.sptg.video.cloud.biz.service.VCApiLogServiceInterface;
import com.sptg.video.cloud.biz.service.FileInfoServiceInterface;
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 21:00
 * 录像信息 服务层实现层
 */
@Service
@RequiredArgsConstructor
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoServiceInterface {

    private final VCApiLogServiceInterface apiLogServiceInterface;

    @Override
    public List<FileInfo> showFileInfoList(FileInfoDto fileInfoDto) {
        // 构建查询条件
        Wrapper<FileInfo> wrapper = new EntityWrapper<>();

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

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

    @Override
    public List<FileInfo> findFileInfoByDeviceNum(String deviceNum) {
        Wrapper<FileInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("s_device_num", deviceNum);
        return this.selectList(wrapper);
    }

    @Override
    public List<FileInfo> syncFileInfoList(SysParams sysParams, FileInfoDto fileInfoDto) {
        VideoCloudHttpUtil httpUtil = new VideoCloudHttpUtil(sysParams);
        Map<String, Object> bizParamsMap = new HashMap<>();
        bizParamsMap.put("deviceNum", fileInfoDto.getDeviceNum());
        ApiResultBean<FileInfo> apiResultBean = httpUtil.deviceFileQuery(bizParamsMap);
        List<FileInfo> fileInfoList = null;
        if(apiResultBean.getReturnCode().equals(OperationConstant.ONE + "")){
            PageInfo<FileInfo> pageInfo = apiResultBean.getPageInfo();
            fileInfoList = pageInfo.getList();
            VCApiLog apiLog = BeanUtil.copyProperties(apiResultBean, VCApiLog.class);
            String apiResultBeanStr = JSONUtil.toJsonStr(apiResultBean);
            apiLog.setRespInfo(apiResultBeanStr);
            apiLog.setCreateDate(new Date());
            apiLog.setDeviceNum(fileInfoDto.getDeviceNum());
            apiLog.setApiMethodName("获取设备录像信息列表");
            this.apiLogServiceInterface.insert(apiLog);
        }
        return fileInfoList;
    }
}
