package com.YuyeTech.TPlat.service.impl;

import com.YuyeTech.TPlat.dataobject.Pictures;
import com.YuyeTech.TPlat.repository.PicturesRepository;
import com.YuyeTech.TPlat.service.PicturesService;
import com.YuyeTech.TPlat.utils.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

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

/**
 * @author:slambb
 * @date:2020/6/24
 */
@Service
public class PicturesServiceImpl implements PicturesService {
    @Autowired
    private PicturesRepository picturesRepository;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Override
    public Page<Pictures> findAllByPageable(Pageable pageable) {
        return picturesRepository.findAll(pageable);
    }

    @Override
    public Pictures addPicture(Pictures pictures) {
        return picturesRepository.save(pictures);
    }

    @Override
    public Pictures addPictureAndFlush(Pictures pictures) {
        return picturesRepository.saveAndFlush(pictures);
    }

    @Override
    public Pictures findById(Integer id) {
        return picturesRepository.findById(id).orElse(null);
    }

    @Override
    public void deletePictureById(Integer id) {
        picturesRepository.deleteById(id);
    }

    @Override
    public List<Pictures> findAllByPictureIdList(List<Integer> ids) {

        List<Pictures> picturesList = picturesRepository.findByPictureIdIn(ids).stream().map(e -> {
            e.setPictureUrl(aliyunOSSUtil.addDomainName(e.getPictureUrl()));
            return e;
        }).collect(Collectors.toList());

        return picturesList;
    }

}
