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

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.platform.common.exception.RRException;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.Query;
import com.platform.modules.video.dao.VideoDao;
import com.platform.modules.video.entity.VideoEntity;
import com.platform.modules.video.service.VideoService;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Map;


@Service("videoService")
public class VideoServiceImpl extends ServiceImpl<VideoDao, VideoEntity> implements VideoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	String materialType = (String)params.get("materialType");
    	String title = (String)params.get("title");
        Page<VideoEntity> page = this.selectPage(
                new Query<VideoEntity>(params).getPage(),
                new EntityWrapper<VideoEntity>()
                .eq(StringUtils.isNotBlank(materialType),"video_type",materialType)
                .like(StringUtils.isNotBlank(title),"title",title));
        return new PageUtils(page);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(VideoEntity video) {
        this.insert(video);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(VideoEntity video) {
        this.updateById(video);
    }

    @Override
    public PageUtils queryPageByConditions(Map<String, Object> params) {
        Page<VideoEntity> page = this.selectPage(
                new Query<VideoEntity>(params).getPage(),
                new EntityWrapper<VideoEntity>()
                .between("create_date",params.get("dateStart"),params.get("dateEnd"))
                .eq("status",params.get("status"))
                .eq("create_method",params.get("create_method"))
                .like("title",(String) params.get("title"), SqlLike.DEFAULT)
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOKState(Integer id,String opinion) {
        VideoEntity video = this.selectById(id);
        if (video == null || video.equals("")){
            throw new RRException("视频对象不能为空！");
        }
        video.setOpinion(opinion);
        video.setStatus(2);
        this.updateById(video);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNoState(Integer id, String opinion) {
        VideoEntity video = this.selectById(id);
        if (video == null || video.equals("")){
            throw new RRException("视频对象不能为空！");
        }
        video.setOpinion(opinion);
        video.setStatus(0);
        this.updateById(video);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] videoIds) {
        this.deleteBatchIds(Arrays.asList(videoIds));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        this.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendState(Integer id) {
        VideoEntity video = this.selectById(id);
        if (video == null || video.equals("")){
            throw new RRException("视频对象不能为空！");
        }
        video.setStatus(1);
        this.updateById(video);
    }
}
