package com.crm.web.service.impl;

import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.entity.custom.video.record.CustomVideoRecordAddEntity;
import com.crm.rely.backend.model.entity.custom.video.record.CustomVideoRecordDeleteEntity;
import com.crm.rely.backend.model.pojo.table.CustomVideoRecordTable;
import com.crm.web.dao.mapper.WebVideoMapper;
import com.crm.web.dao.repository.CustomVideoRecordRepository;
import com.crm.web.service.CustomVideoRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomVideoRecordServiceImpl implements CustomVideoRecordService {

    @Autowired
    private CustomVideoRecordRepository repository;

    @Autowired
    private WebVideoMapper webVideoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CustomVideoRecordAddEntity entity) throws ServiceException {

        if (repository.existsByCustomIdAndVideoId(entity.getCustomId(), entity.getVideoId())) {
            return;
        }
        CustomVideoRecordTable table = new CustomVideoRecordTable();
        BeanUtils.copyProperties(entity, table);
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CustomVideoRecordDeleteEntity entity) throws ServiceException {
        List<CustomVideoRecordTable> tables = repository.findAllByCustomId(entity.getCustomId());
        if (tables == null) {
           return;
        }

        repository.delete(tables);

    }

}
