package com.ruoyi.abuwx.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.abuwx.domain.VideoContent;
import com.ruoyi.abuwx.domain.VideoOperationRecord;
import com.ruoyi.abuwx.mapper.VideoContentMapper;
import com.ruoyi.abuwx.mapper.VideoOperationRecordMapper;
import com.ruoyi.abuwx.service.IVideoContentService;
import com.ruoyi.abuwx.vo.VideoContentVo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 视频信息Service业务层处理
 *
 * @author lujie
 * @date 2025-01-13
 */
@Service
public class VideoContentServiceImpl implements IVideoContentService {
    @Autowired
    private VideoContentMapper videoContentMapper;
    @Autowired
    private VideoOperationRecordMapper videoOperationRecordMapper;

    /**
     * 查询视频信息
     *
     * @param id 视频信息主键
     * @return 视频信息
     */
    @Override
    public VideoContent selectVideoContentById(Long id) {
        return videoContentMapper.selectVideoContentById(id);
    }

    /**
     * 查询视频信息列表
     *
     * @param videoContent 视频信息
     * @return 视频信息
     */
    @Override
    public List<VideoContent> selectVideoContentList(VideoContent videoContent) {
        return videoContentMapper.selectVideoContentList(videoContent);
    }

    /**
     * 新增视频信息
     *
     * @param videoContent 视频信息
     * @return 结果
     */
    @Override
    public int insertVideoContent(VideoContent videoContent) {
        Date nowDate = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        videoContent.setCreateTime(nowDate);
        videoContent.setCreateBy(username);
        int i = videoContentMapper.insertVideoContent(videoContent);
        if (i > 0) {
            VideoOperationRecord videoOperationRecord = new VideoOperationRecord();
            videoOperationRecord.setOperateType("创建");
            videoOperationRecord.setCreateBy(username);
            videoOperationRecord.setCreateTime(nowDate);
            videoOperationRecord.setVideoId(videoContent.getId());
            videoOperationRecordMapper.insertVideoOperationRecord(videoOperationRecord);
            return 1;
        }
        return 0;
    }

    /**
     * 修改视频信息
     *
     * @param videoContent 视频信息
     * @return 结果
     */
    @Override
    public int updateVideoContent(VideoContent videoContent) {
        VideoOperationRecord videoOperationRecord = new VideoOperationRecord();
        Date nowDate = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        videoContent.setUpdateTime(DateUtils.getNowDate());
        String checkStatus = videoContent.getCheckStatus();
        videoOperationRecord.setOperateType("修改");
        if (StrUtil.isNotBlank(checkStatus) && checkStatus.equals("5")) {
            videoContent.setPublishTime(nowDate);
            videoOperationRecord.setOperateType("发布");
        }
        if (StrUtil.isNotBlank(checkStatus) && checkStatus.equals("6")) {
            videoContent.setCancelTime(nowDate);
            videoOperationRecord.setOperateType("撤回发布");
        }
        if (StrUtil.isNotBlank(checkStatus) && checkStatus.equals("2")) {
            videoOperationRecord.setOperateType("发起申请");
        }
        if (StrUtil.isNotBlank(checkStatus) && checkStatus.equals("3")) {
            videoOperationRecord.setOperateType("审核");
        }
        videoOperationRecord.setCreateBy(username);
        videoOperationRecord.setCreateTime(nowDate);
        videoOperationRecord.setVideoId(videoContent.getId());
        videoOperationRecordMapper.insertVideoOperationRecord(videoOperationRecord);
        return videoContentMapper.updateVideoContent(videoContent);
    }

    /**
     * 批量删除视频信息
     *
     * @param ids 需要删除的视频信息主键
     * @return 结果
     */
    @Override
    public int deleteVideoContentByIds(Long[] ids) {
        return videoContentMapper.deleteVideoContentByIds(ids);
    }

    /**
     * 删除视频信息信息
     *
     * @param id 视频信息主键
     * @return 结果
     */
    @Override
    public int deleteVideoContentById(Long id) {
        return videoContentMapper.deleteVideoContentById(id);
    }

    @Override
    public List<VideoContentVo> selectVideoContentVoList(VideoContent videoContent) {
        return videoContentMapper.selectVideoContentVoList(videoContent);
    }
}
