package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeUserLearningVideo;
import shop.alien.mapper.LifeUserLearningVideoMapper;
import shop.alien.store.service.LifeUserLearningVideoService;
import shop.alien.util.common.VideoDurationFFmpeg;

import java.io.IOException;

@Service
@RequiredArgsConstructor
@Slf4j
public class LifeUserLearningVideoServiceImpl extends ServiceImpl<LifeUserLearningVideoMapper, LifeUserLearningVideo> implements LifeUserLearningVideoService {

    @Override
    public R<String> add(LifeUserLearningVideo lifeUserLearningVideo) throws IOException, InterruptedException {
        log.info("LifeUserLearningVideoServiceImpl.add, param={}", lifeUserLearningVideo);
//        long videoDuration = VideoDurationFFmpeg.getVideoDuration(lifeUserLearningVideo.getVideoUrl());
        lifeUserLearningVideo.setVideoDuration(String.valueOf(lifeUserLearningVideo.getVideoDuration()));
        boolean result = this.save(lifeUserLearningVideo);
        if (result) {
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }

    @Override
    public R<String> deleteById(Integer id) {
        log.info("LifeUserLearningVideoServiceImpl.deleteById, id={}", id);
        boolean result = this.removeById(id);
        if (result) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @Override
    public R<String> update(LifeUserLearningVideo lifeUserLearningVideo) {
        log.info("LifeUserLearningVideoServiceImpl.update, param={}", lifeUserLearningVideo);
        boolean result = this.updateById(lifeUserLearningVideo);
        if (result) {
            return R.success("更新成功");
        }
        return R.fail("更新失败");
    }

    @Override
    public R<LifeUserLearningVideo> getInfoById(Integer id) {
        log.info("LifeUserLearningVideoServiceImpl.getInfoById, id={}", id);
        LifeUserLearningVideo result = this.getById(id);
        return R.data(result);
    }

    @Override
    public R<IPage<LifeUserLearningVideo>> list(Integer pageNum, Integer pageSize, String name) {
        log.info("LifeUserLearningVideoServiceImpl.list, pageNum={}, pageSize={}, name={}", pageNum, pageSize, name);
        Page<LifeUserLearningVideo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LifeUserLearningVideo> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(LifeUserLearningVideo::getName, name);
        }
        queryWrapper.orderByDesc(LifeUserLearningVideo::getCreatedTime);
        IPage<LifeUserLearningVideo> result = this.page(page, queryWrapper);
        return R.data(result);
    }
}

