package com.crm.web.service.impl;

import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.dto.web.video.WebVideoSearchDto;
import com.crm.rely.backend.model.entity.web.video.WebVideoAddEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoDeleteEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoSearchListEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoUpdateEntity;
import com.crm.rely.backend.model.pojo.table.WebVideoTable;
import com.crm.rely.backend.model.pojo.view.WebVideoView;
import com.crm.web.dao.mapper.WebVideoMapper;
import com.crm.web.dao.repository.WebVideoRepository;
import com.crm.web.service.WebVideoService;
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.ArrayList;
import java.util.List;

@Service
public class WebVideoServiceImpl implements WebVideoService {

    @Autowired
    private WebVideoRepository repository;

    @Autowired
    private WebVideoMapper mapper;

    @Override
    public WebVideoTable getById(Long id) throws ServiceException {

        WebVideoTable table = repository.getFirstById(id);
        if (table == null) {
            throw new ServiceException(Constants.INFO_NOT_FOUND);
        }
        return table;
    }

    @Override
    public boolean validateAllRead(Long customId) throws ServiceException {

        List<WebVideoView> views = mapper.getCustomAllVideo(customId);
        if (views == null || views.size() <= 0) {
            return true;
        }
        for (WebVideoView view : views) {
            if (view.getRecordId() == null) {
                return false;
            }
        }
        return true;
    }

    @Override
    public BaseResultDto<WebVideoSearchDto> searchSingle(Long id) throws ServiceException {
        WebVideoTable WebVideoTable = getById(id);

        if (WebVideoTable == null) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

        WebVideoSearchDto WebVideoSearchDto = new WebVideoSearchDto();
        BeanUtils.copyProperties(WebVideoTable, WebVideoSearchDto);

        return BaseResultDto.success(WebVideoSearchDto);
    }

    @Override
    public List<WebVideoSearchDto> searchList(WebVideoSearchListEntity entity) throws ServiceException {

        List<WebVideoView> views = mapper.pageList(entity);

        if (views == null || views.size() <= 0) {

            return null;
        }
        List<WebVideoSearchDto> dtos = new ArrayList<>(views.size());
        for (WebVideoView view : views) {
            WebVideoSearchDto dto = new WebVideoSearchDto();
            BeanUtils.copyProperties(view, dto);
            if (view.getRecordId() != null) {
                dto.setRead(1);
            } else {
                dto.setRead(0);
            }
            dtos.add(dto);
        }

        return dtos;
    }

    @Override
    public ResultWithPagerDto<WebVideoSearchDto> pageSearchList(WebVideoSearchListEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<WebVideoSearchDto> dtos = searchList(entity);
        if (dtos == null || dtos.size() <= 0) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        return ResultWithPagerDto.success(pageDto, dtos);
    }

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

        WebVideoTable WebVideoTable = new WebVideoTable();
        BeanUtils.copyProperties(entity, WebVideoTable);
        repository.save(WebVideoTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WebVideoUpdateEntity entity) throws ServiceException {
        WebVideoTable WebVideoTable = repository.findFirstById(entity.getId());
        if (WebVideoTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        BeanUtils.copyProperties(entity, WebVideoTable);

        repository.save(WebVideoTable);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(WebVideoDeleteEntity entity) throws ServiceException {
        List<WebVideoTable> WebVideoRepositoryAllByIdIn = repository.findAllByIdIn(entity.getIds());
        if (WebVideoRepositoryAllByIdIn == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        repository.delete(WebVideoRepositoryAllByIdIn);

    }

}
