package com.ruoyi.base.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.base.domain.RobotOpsPlayPlanItem;
import com.ruoyi.base.mapper.RobotOpsPlayPlanMapper;
import com.ruoyi.base.domain.RobotOpsPlayPlan;
import com.ruoyi.base.service.IRobotOpsPlayPlanService;

/**
 * 播放方案Service业务层处理
 *
 * @author ruoyi
 * @date 2026-05-11
 */
@Service
public class RobotOpsPlayPlanServiceImpl implements IRobotOpsPlayPlanService
{
    @Autowired
    private RobotOpsPlayPlanMapper robotOpsPlayPlanMapper;

    /**
     * 查询播放方案
     *
     * @param id 播放方案主键
     * @return 播放方案
     */
    @Override
    public RobotOpsPlayPlan selectRobotOpsPlayPlanById(Long id)
    {
        return robotOpsPlayPlanMapper.selectRobotOpsPlayPlanById(id);
    }

    /**
     * 查询播放方案列表
     *
     * @param robotOpsPlayPlan 播放方案
     * @return 播放方案
     */
    @Override
    public List<RobotOpsPlayPlan> selectRobotOpsPlayPlanList(RobotOpsPlayPlan robotOpsPlayPlan)
    {
        return robotOpsPlayPlanMapper.selectRobotOpsPlayPlanList(robotOpsPlayPlan);
    }

    /**
     * 新增播放方案
     *
     * @param robotOpsPlayPlan 播放方案
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertRobotOpsPlayPlan(RobotOpsPlayPlan robotOpsPlayPlan)
    {
        robotOpsPlayPlan.setCreateTime(DateUtils.getNowDate());
        if ("1".equals(robotOpsPlayPlan.getStatus()))
        {
            RobotOpsPlayPlan currentPlayingPlan = robotOpsPlayPlanMapper.selectCurrentPlayingPlan();
            if (currentPlayingPlan != null)
            {
                robotOpsPlayPlanMapper.updatePlayPlanStatus(currentPlayingPlan.getId(), "0");
            }
        }
        int rows = robotOpsPlayPlanMapper.insertRobotOpsPlayPlan(robotOpsPlayPlan);
        insertRobotOpsPlayPlanItem(robotOpsPlayPlan);
        return rows;
    }

    /**
     * 修改播放方案
     *
     * @param robotOpsPlayPlan 播放方案
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateRobotOpsPlayPlan(RobotOpsPlayPlan robotOpsPlayPlan)
    {
        robotOpsPlayPlan.setUpdateTime(DateUtils.getNowDate());

        if ("1".equals(robotOpsPlayPlan.getStatus()))
        {
            RobotOpsPlayPlan currentPlayingPlan = robotOpsPlayPlanMapper.selectCurrentPlayingPlan();
            if (currentPlayingPlan != null && !currentPlayingPlan.getId().equals(robotOpsPlayPlan.getId()))
            {
                robotOpsPlayPlanMapper.updatePlayPlanStatus(currentPlayingPlan.getId(), "0");
            }
        }

        robotOpsPlayPlanMapper.deleteRobotOpsPlayPlanItemByPlanId(robotOpsPlayPlan.getId());
        insertRobotOpsPlayPlanItem(robotOpsPlayPlan);
        return robotOpsPlayPlanMapper.updateRobotOpsPlayPlan(robotOpsPlayPlan);
    }


    /**
     * 批量删除播放方案
     *
     * @param ids 需要删除的播放方案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRobotOpsPlayPlanByIds(Long[] ids)
    {
        robotOpsPlayPlanMapper.deleteRobotOpsPlayPlanItemByPlanIds(ids);
        return robotOpsPlayPlanMapper.deleteRobotOpsPlayPlanByIds(ids);
    }

    /**
     * 删除播放方案信息
     *
     * @param id 播放方案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRobotOpsPlayPlanById(Long id)
    {
        robotOpsPlayPlanMapper.deleteRobotOpsPlayPlanItemByPlanId(id);
        return robotOpsPlayPlanMapper.deleteRobotOpsPlayPlanById(id);
    }

    /**
     * 查询当前正在播放的方案（包含素材项）
     *
     * @return 播放方案
     */
    @Override
    public RobotOpsPlayPlan selectCurrentPlayingPlanWithItems()
    {
        RobotOpsPlayPlan plan = robotOpsPlayPlanMapper.selectCurrentPlayingPlan();
        if (plan != null) {
            List<RobotOpsPlayPlanItem> items = robotOpsPlayPlanMapper.selectRobotOpsPlayPlanItemListByPlanId(plan.getId());
            plan.setRobotOpsPlayPlanItemList(items);
        }
        return plan;
    }

    /**
     * 新增播放方案素材明细信息
     *
     * @param robotOpsPlayPlan 播放方案对象
     */
    public void insertRobotOpsPlayPlanItem(RobotOpsPlayPlan robotOpsPlayPlan)
    {
        List<RobotOpsPlayPlanItem> robotOpsPlayPlanItemList = robotOpsPlayPlan.getRobotOpsPlayPlanItemList();
        Long id = robotOpsPlayPlan.getId();
        if (StringUtils.isNotNull(robotOpsPlayPlanItemList))
        {
            List<RobotOpsPlayPlanItem> list = new ArrayList<RobotOpsPlayPlanItem>();
            for (RobotOpsPlayPlanItem robotOpsPlayPlanItem : robotOpsPlayPlanItemList)
            {
                robotOpsPlayPlanItem.setPlanId(id);
                list.add(robotOpsPlayPlanItem);
            }
            if (list.size() > 0)
            {
                robotOpsPlayPlanMapper.batchRobotOpsPlayPlanItem(list);
            }
        }
    }
}
