package com.platform.modules.live.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.Query;

import com.platform.modules.live.dao.LiveStreamRecordTaskDao;
import com.platform.modules.live.entity.LiveStreamRecordTaskEntity;
import com.platform.modules.live.service.LiveStreamRecordTaskService;
import org.springframework.transaction.annotation.Transactional;


@Service("liveStreamRecordTaskService")
@Transactional
public class LiveStreamRecordTaskServiceImpl extends ServiceImpl<LiveStreamRecordTaskDao, LiveStreamRecordTaskEntity> implements LiveStreamRecordTaskService {

    @Override
    @Transactional(readOnly = true)
    public PageUtils queryPage(Map<String, Object> params) {
        Page<LiveStreamRecordTaskEntity> page = this.selectPage(
                new Query<LiveStreamRecordTaskEntity>(params).getPage(),
                new EntityWrapper<LiveStreamRecordTaskEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(readOnly = true)
    public LiveStreamRecordTaskEntity selectByVideoId(String videoId) {
        Wrapper<LiveStreamRecordTaskEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("video_id", videoId);
        List<LiveStreamRecordTaskEntity> result = this.baseMapper.selectList(wrapper);
        if(result != null && result.size() == 1)
            return result.get(0);
        return null;
    }

    @Override
    @Transactional(readOnly = true)
    public List<LiveStreamRecordTaskEntity> selectUnOkTasks(String domainName, String appName, String streamName) {
        Wrapper<LiveStreamRecordTaskEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("domain_name", domainName);
        wrapper.eq("app_name", appName);
        wrapper.eq("stream_name", streamName);
        wrapper.eq("is_ok", 0);

        return this.baseMapper.selectList(wrapper);
    }

    @Override
    @Transactional(readOnly = true)
    public List<LiveStreamRecordTaskEntity> selectUnOkTasks(String domainName, String appName) {
        Wrapper<LiveStreamRecordTaskEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("domain_name", domainName);
        wrapper.eq("app_name", appName);
        wrapper.eq("is_ok", 0);

        return this.baseMapper.selectList(wrapper);
    }
}
