package com.srs.routine.service.impl;

import java.util.List;
import java.util.UUID;

import com.srs.common.core.domain.AjaxResult;
import com.srs.routine.domain.vo.RtFuInserSoltVo;
import com.srs.routine.domain.vo.RtfuRuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.routine.mapper.RtFuSoltMapper;
import com.srs.routine.domain.RtFuSolt;
import com.srs.routine.service.IRtFuSoltService;

/**
 * 功能房开放时间段Service业务层处理
 *
 * @author srs
 * @date 2024-07-19
 */
@Service
public class RtFuSoltServiceImpl extends ServiceImpl<RtFuSoltMapper,RtFuSolt> implements IRtFuSoltService {
    @Autowired
    private RtFuSoltMapper rtFuSoltMapper;

    /**
     * 查询功能房开放时间段
     *
     * @param id 功能房开放时间段主键
     * @return 功能房开放时间段
     */
    @Override
    public RtFuSolt selectRtFuSoltById(Long id) {
        return rtFuSoltMapper.selectRtFuSoltById(id);
    }

    /**
     * 查询功能房开放时间段列表
     *
     * @param rtFuSolt 功能房开放时间段
     * @return 功能房开放时间段
     */
    @Override
    public List<RtFuSolt> selectRtFuSoltList(RtFuSolt rtFuSolt) {

        return rtFuSoltMapper.selectRtFuSoltList(rtFuSolt);
    }

    @Override
    public List<RtfuRuleVo> getList(RtFuSolt rtFuSolt){
        return rtFuSoltMapper.selectRtFuSoltLists(rtFuSolt);
    }

    /**
     * 新增功能房开放时间段
     *
     * @param rtFuSolt 功能房开放时间段
     * @return 结果
     */
    @Override
    public int insertRtFuSolt(RtFuSolt rtFuSolt) {
            return rtFuSoltMapper.insertRtFuSolt(rtFuSolt);
    }

    public AjaxResult insertSoltAll(List<RtFuInserSoltVo> rtFuInserSoltVo){
        try {
            RtFuSolt solt=new RtFuSolt();
            solt.setSoNo(UUID.randomUUID().toString());
            for (int i=0 ;i<rtFuInserSoltVo.size();i++) {
                solt.setSoRule(rtFuInserSoltVo.get(i).getSoRule());
                solt.setRoWeekday(rtFuInserSoltVo.get(i).getRoWeekday());
                if (rtFuInserSoltVo.get(i).getTimeSolts().size()!=0) {
                    for (int j = 0; j < rtFuInserSoltVo.get(i).getTimeSolts().size(); j++) {
                        if (rtFuInserSoltVo.get(i).getTimeSolts().get(j)!=null){
                            solt.setRoType(UUID.randomUUID().toString());
                            solt.setRoStartTime(rtFuInserSoltVo.get(i).getTimeSolts().get(j).getRoStartTime());
                            solt.setRoEndTime(rtFuInserSoltVo.get(i).getTimeSolts().get(j).getRoEndTime());
                            int res = rtFuSoltMapper.insertRtFuSolt(solt);
                            if (res == 0) {
                                throw new Exception("规则添加失败");
                            }
                        }

                    }
                }
            }
            return AjaxResult.success("规则添加成功");
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }
    /**
     * 修改功能房开放时间段
     *
     * @param rtFuSolt 功能房开放时间段
     * @return 结果
     */
    @Override
    public int updateRtFuSolt(RtFuSolt rtFuSolt) {
        return rtFuSoltMapper.updateRtFuSolt(rtFuSolt);
    }

    /**
     * 批量删除功能房开放时间段
     *
     * @param ids 需要删除的功能房开放时间段主键
     * @return 结果
     */
    @Override
    public int deleteRtFuSoltByIds(Long[] ids) {
        return rtFuSoltMapper.deleteRtFuSoltByIds(ids);
    }

    /**
     * 删除功能房开放时间段信息
     *
     * @param id 功能房开放时间段主键
     * @return 结果
     */
    @Override
    public int deleteRtFuSoltById(Long id) {
        return rtFuSoltMapper.deleteRtFuSoltById(id);
    }
}
