package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hezhi.domain.TbExamCourse;
import com.ruoyi.hezhi.domain.TbLiveBroadcast;
import com.ruoyi.hezhi.mapper.TbExamCourseMapper;
import com.ruoyi.hezhi.mapper.TbLiveBroadcastMapper;
import com.ruoyi.hezhi.service.ITbLiveBroadcastService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 直播Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-22
 */
@Service
public class TbLiveBroadcastServiceImpl implements ITbLiveBroadcastService
{
    @Autowired
    private TbLiveBroadcastMapper tbLiveBroadcastMapper;
    @Autowired
    private TbExamCourseMapper tbExamCourseMapper;

    /**
     * 查询直播
     *
     * @param liveBroadcastId 直播主键
     * @return 直播
     */
    @Override
    public TbLiveBroadcast selectTbLiveBroadcastByLiveBroadcastId(Long liveBroadcastId)
    {
        TbLiveBroadcast tbLiveBroadcast = tbLiveBroadcastMapper.selectTbLiveBroadcastByLiveBroadcastId(liveBroadcastId);
        if (ObjectUtils.isNotEmpty(tbLiveBroadcast)){
            List<TbExamCourse> examCourseList = tbExamCourseMapper.getExamCourseList(null, null, null, tbLiveBroadcast.getLiveBroadcastId());
            tbLiveBroadcast.setExamCourseList(examCourseList);
        }
        return tbLiveBroadcast;
    }

    /**
     * 查询直播列表
     *
     * @param tbLiveBroadcast 直播
     * @return 直播
     */
    @Override
    public List<TbLiveBroadcast> selectTbLiveBroadcastList(TbLiveBroadcast tbLiveBroadcast)
    {
        return tbLiveBroadcastMapper.selectTbLiveBroadcastList(tbLiveBroadcast);
    }

    /**
     * 新增直播
     *
     * @param tbLiveBroadcast 直播
     * @return 结果
     */
    @Override
    public int insertTbLiveBroadcast(TbLiveBroadcast tbLiveBroadcast)
    {
        // 新增考试课程
        List<TbExamCourse> examCourseList = tbLiveBroadcast.getExamCourseList();
        tbLiveBroadcast.setCreateTime(DateUtils.getNowDate());
        int i = tbLiveBroadcastMapper.insertTbLiveBroadcast(tbLiveBroadcast);
        //先插入得到ID再插入关联表
        if (!examCourseList.isEmpty()){
            for (TbExamCourse tbExamCourse : examCourseList) {
                tbExamCourse.setLiveBroadcastId((tbLiveBroadcast.getLiveBroadcastId()));
                tbExamCourseMapper.insertTbExamCourse(tbExamCourse);
            }
        }
        return i;
    }

    /**
     * 修改直播
     *
     * @param tbLiveBroadcast 直播
     * @return 结果
     */
    @Override
    public int updateTbLiveBroadcast(TbLiveBroadcast tbLiveBroadcast)
    {
        // 新增考试课程
        List<TbExamCourse> examCourseList = tbLiveBroadcast.getExamCourseList();
        if (!examCourseList.isEmpty()){
            tbExamCourseMapper.deleteTbExamCourse(null, null, null, tbLiveBroadcast.getLiveBroadcastId());
            for (TbExamCourse tbExamCourse : examCourseList) {
                tbExamCourse.setLiveBroadcastId(tbLiveBroadcast.getLiveBroadcastId());
                tbExamCourseMapper.insertTbExamCourse(tbExamCourse);
            }
        }
        tbLiveBroadcast.setUpdateTime(DateUtils.getNowDate());
        return tbLiveBroadcastMapper.updateTbLiveBroadcast(tbLiveBroadcast);
    }

    /**
     * 批量删除直播
     *
     * @param liveBroadcastIds 需要删除的直播主键
     * @return 结果
     */
    @Override
    public int deleteTbLiveBroadcastByLiveBroadcastIds(Long[] liveBroadcastIds)
    {
        return tbLiveBroadcastMapper.deleteTbLiveBroadcastByLiveBroadcastIds(liveBroadcastIds);
    }

    /**
     * 删除直播信息
     *
     * @param liveBroadcastId 直播主键
     * @return 结果
     */
    @Override
    public int deleteTbLiveBroadcastByLiveBroadcastId(Long liveBroadcastId)
    {
        return tbLiveBroadcastMapper.deleteTbLiveBroadcastByLiveBroadcastId(liveBroadcastId);
    }

    /**
     * 直播预告列表
     *
     * @param params 参数
     * @return 直播预告列表
     */
    @Override
    public List<Map<String, Object>> getLiveBroadcastList(Map<String, Object> params) {
        List<Map<String, Object>> liveBroadcastList = tbLiveBroadcastMapper.getLiveBroadcastList(params);
        if (!liveBroadcastList.isEmpty()){
            for (Map<String, Object> stringObjectMap : liveBroadcastList) {
                String startTime = MapUtils.getString(stringObjectMap, "startTime");
                String endTime = MapUtils.getString(stringObjectMap, "endTime");
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

                try {
                    Date startDate = dateFormat.parse(startTime);
                    Date endDate = null;
                    if (StringUtils.isNotEmpty(endTime)) {
                        endDate = dateFormat.parse(endTime);
                    }
                    // 直播状态（0-未开始，1-进行中，2-已结束）
                    int liveBroadStatus = 0;
                    Date nowDate = DateUtils.getNowDate();
                    if (nowDate.before(startDate)){
                        stringObjectMap.put("liveBroadcastUrl", null);
                    }else if (null != endDate && nowDate.after(endDate)){
                        liveBroadStatus = 2;
                        stringObjectMap.put("liveBroadcastUrl", null);
                    }else {
                        liveBroadStatus = 1;
                    }
                    stringObjectMap.put("liveBroadStatus", liveBroadStatus);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return liveBroadcastList;
    }

    @Override
    public List<Map<String, Object>> getLiveBroadcastEvaluateAgencyList(){
        return tbLiveBroadcastMapper.getLiveBroadcastEvaluateAgencyList();
    }

    /**
     * 直播预告详情
     *
     * @param params 参数
     * @return 直播预告详情
     */
    @Override
    public Map<String, Object> getLiveBroadcastInfo(Map<String, Object> params) {
        Map<String, Object> liveBroadcastInfo = tbLiveBroadcastMapper.getLiveBroadcastInfo(params);
        if (!liveBroadcastInfo.isEmpty()){
            String startTime = MapUtils.getString(liveBroadcastInfo, "startTime");
            String endTime = MapUtils.getString(liveBroadcastInfo, "endTime");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                Date startDate = dateFormat.parse(startTime);
                Date endDate = dateFormat.parse(endTime);
                // 直播状态（0-未开始，1-进行中，2-已结束）
                int liveBroadStatus = 0;
                Date nowDate = DateUtils.getNowDate();
                if (nowDate.before(startDate)){
                    liveBroadcastInfo.put("liveBroadcastUrl", null);
                }else if (nowDate.after(endDate)){
                    liveBroadStatus = 2;
                }else {
                    liveBroadStatus = 1;
                }
                liveBroadcastInfo.put("liveBroadStatus", liveBroadStatus);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        return liveBroadcastInfo;
    }
}
