package com.dji.sample.manage.service.impl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dji.sample.manage.dao.IDeviceCameraMapper;
import com.dji.sample.manage.model.dto.CapacityCameraDTO;
import com.dji.sample.manage.model.dto.CapacityVideoDTO;
import com.dji.sample.manage.model.entity.DeviceCameraEntity;
import com.dji.sample.manage.service.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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


/**
 *
 * @author huiqing.jiang
 * @version 0.1
 * @date 2024/11/22
 */
@Service
@Slf4j
@Transactional
public class DeviceCameraServiceImpl implements IDeviceCameraService {

    @Autowired
    private IDeviceCameraMapper mapper;

    @Override
    public void saveDeviceCamera(List<CapacityCameraDTO> list, String deviceSn) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<CapacityCameraDTO> veL = selCameraListByDeviceSn(deviceSn);
        if (!CollectionUtils.isEmpty(veL)) {
            delete(deviceSn);
        }
        for (CapacityCameraDTO dto : list) {
            dto.setDeviceSn(deviceSn);
            DeviceCameraEntity entity = dtoToEntity(dto);
            save(entity);
        }

    }

    @Override
    public void save(DeviceCameraEntity entity) {
        mapper.insert(entity);
    }

    @Override
    public void delete(String deviceSn) {
        LambdaQueryWrapper<DeviceCameraEntity> wrapper = new LambdaQueryWrapper<DeviceCameraEntity>()
                .eq(DeviceCameraEntity ::getDeviceSn,deviceSn);
        mapper.delete(wrapper);
    }

    @Override
    public List<CapacityCameraDTO> selCameraListByDeviceSn(String deviceSn) {
        LambdaQueryWrapper<DeviceCameraEntity> wrapper = new LambdaQueryWrapper<DeviceCameraEntity>()
                .eq(DeviceCameraEntity::getDeviceSn,deviceSn);
        List<DeviceCameraEntity> entityList = mapper.selectList(wrapper);
        List<CapacityCameraDTO> dtoList = entityList.stream().map(this::entityToDto).collect(Collectors.toList());
        return dtoList;
    }

    private DeviceCameraEntity dtoToEntity(CapacityCameraDTO dto) {
        DeviceCameraEntity entity = new DeviceCameraEntity();
        entity.setId(dto.getId());
        entity.setType(dto.getType());
        entity.setName(dto.getName());
        entity.setDeviceSn(dto.getDeviceSn());
        entity.setCameraIndex(dto.getIndex());
        if(!CollectionUtils.isEmpty(dto.getVideosList())) {
            entity.setVideoList(JSONUtil.parse(dto.getVideosList()).toString());
        }

        return entity;
    }

    private CapacityCameraDTO entityToDto(DeviceCameraEntity entity) {
        CapacityCameraDTO dto = new CapacityCameraDTO();
        dto.setId(entity.getId());
        dto.setType(entity.getType());
        dto.setName(entity.getName());
        dto.setDeviceSn(entity.getDeviceSn());
        dto.setIndex(entity.getCameraIndex());
        if(null != entity.getVideoList()) {
            dto.setVideosList(JSONUtil.toList(entity.getVideoList(), CapacityVideoDTO.class));
        }
        return dto;
    }
}