package com.srs.routine.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.*;

import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.uuid.UUID;
import com.srs.routine.domain.RtFuReservation;
import com.srs.routine.domain.RtFuResource;
import com.srs.routine.domain.RtFuSolt;
import com.srs.routine.domain.vo.*;
import com.srs.routine.mapper.RtFuReservationMapper;
import com.srs.routine.mapper.RtFuResourceMapper;
import com.srs.routine.mapper.RtFuSoltMapper;
import com.srs.routine.service.IRtFuSoltService;
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.RtFuRoomMapper;
import com.srs.routine.domain.RtFuRoom;
import com.srs.routine.service.IRtFuRoomService;

/**
 * 功能房Service业务层处理
 *
 * @author srs
 * @date 2024-07-12
 */
@Service
public class RtFuRoomServiceImpl extends ServiceImpl<RtFuRoomMapper,RtFuRoom> implements IRtFuRoomService {
    @Autowired
    private RtFuRoomMapper rtFuRoomMapper;

    @Autowired
    IRtFuSoltService iRtFuSoltService;
    @Autowired
    private RtFuResourceMapper rtFuResourceMapper;

    @Autowired
    private RtFuSoltMapper rtFuSoltMapper;

    @Autowired
    private  RtFuReservationMapper rtFuReservationMapper;

    /**
     * 查询功能房
     *
     * @param id 功能房主键
     * @return 功能房
     */
    @Override
    public RtFuRoomVo selectRtFuRoomById(Long id) {
        RtFuRoomVo  list= rtFuRoomMapper.selectRtFuRoomByIds(id);
        Map<String, List<Map<String, String>>> response = new HashMap<>();
        List<Map<String, String>> timeList = new ArrayList<>();
        //判断当前日期是否已预约
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 创建一个字符串数组来存储最近17天的日期
        String[] recentDates = new String[17];
        // 定义一个日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        // 从当前日期开始，向前遍历17天
        for (int i = 0; i < 17; i++) {
            Map<String,String> dayData=new HashMap<>();
            // 将每一天的日期格式化为字符串并存储到数组中
            recentDates[i] = today.plusDays(i).format(formatter);
            dayData.put("Data", recentDates[i]);
            //
            RtFuReservation re=new RtFuReservation();
            re.setRoNo(list.getRoNo());
            re.setRtTime(recentDates[i]);
            List<RtFuReservation> relist=rtFuReservationMapper.selectRtFuReservationList(re);
            //判断
            if (relist.size()==0){
                dayData.put("Tstatus", "0");
                timeList.add(dayData);
            }
            else {
                LocalDate date=LocalDate.parse(recentDates[i],formatter);
                String dayweekName=date.getDayOfWeek().getDisplayName(TextStyle.FULL,Locale.SIMPLIFIED_CHINESE);
                RtFuResSoltVo fuRoom=rtFuRoomMapper.selectRtFuRoomByRoNo(list.getRoNo());
                RtFuSolt solt=new RtFuSolt();
                solt.setSoNo(fuRoom.getRoomRule());
                solt.setRoWeekday(dayweekName);
                List<RtFuSolt> fuSolts=rtFuSoltMapper.selectRtFuSoltList(solt);
                if (relist.size()==fuSolts.size()){
                    dayData.put("Tstatus", "1");
                    timeList.add(dayData);
                }
                else {
                    dayData.put("Tstatus", "0");
                    timeList.add(dayData);
                }
            }
        }
        response.put("roomTime", timeList);
        list.setResponse(response);
        //图片
        RtFuResource sar=new RtFuResource();
        sar.setResMainNo(list.getRoNo());
        sar.setResType("gnf01");
        List<RtFuResourceVo> list1=rtFuResourceMapper.selectRtFuResourceLists(sar);
        if (list1.size()==0){
            list1=new ArrayList<>();
        }
        list.setRtFuResourcesList(list1);
        //时间段
        RtFuSolt solt=new RtFuSolt();
        solt.setSoNo(list.getRoomRule());
        List<RtfuRuleVo> so=rtFuSoltMapper.selectRtFuSoltLists(solt);
        list.setRtFuSoltList(so);
        return list;
    }

    public RtFuResSoltVo selectRtFuRoomByRoNo(String ro_no){
        RtFuResSoltVo list=rtFuRoomMapper.selectRtFuRoomByRoNo(ro_no);
        RtFuSolt solt=new RtFuSolt();
        solt.setSoNo(list.getRoomRule());
        List<RtfuRuleVo> soltList=rtFuSoltMapper.selectRtFuSoltLists(solt);
        list.setRtFuSoltList(soltList);
        return list;
    }

    @Override
    public List<RtFuRoomVo> selectRtFuRoomByroom(String room_name){
        List<RtFuRoomVo> list=rtFuRoomMapper.selectRtFuRoomByroom(room_name);
        for(RtFuRoomVo rc:list){
            RtFuResource sar=new RtFuResource();
            sar.setResMainNo(rc.getRoNo());
            sar.setResType("gnf01");
            List<RtFuResourceVo> list1=rtFuResourceMapper.selectRtFuResourceLists(sar);
            rc.setRtFuResourcesList(list1);
        }
        return list;
    }

    /**
     * 查询功能房列表
     *
     * @param rtFuRoom 功能房
     * @return 功能房
     */
    @Override
    public List<RtFuRoom> selectRtFuRoomList(RtFuRoom rtFuRoom) {
        List<RtFuRoom> list=rtFuRoomMapper.selectRtFuRoomList(rtFuRoom);
        for(RtFuRoom rc:list){
            RtFuResource sar=new RtFuResource();
            sar.setResMainNo(rc.getRoNo());
            sar.setResType("gnf01");
            List<RtFuResourceVo> list1=rtFuResourceMapper.selectRtFuResourceLists(sar);
            rc.setRtFuResourcesList(list1);
        }
        return list;
    }

    @Override
    public List<RtFuRoomVo> selectRtFuRoomListVo(RtFuRoomVo rtFuRoomVo) {
        List<RtFuRoomVo> list=rtFuRoomMapper.selectRtFuRoomLists(rtFuRoomVo);
        for(RtFuRoomVo rc:list){
            RtFuResource sar=new RtFuResource();
            sar.setResMainNo(rc.getRoNo());
            sar.setResType("gnf01");
            List<RtFuResourceVo> list1=rtFuResourceMapper.selectRtFuResourceLists(sar);
            rc.setRtFuResourcesList(list1);
        }
        return list;
    }

    /**
     * 新增功能房
     *
     * @param rtFuRoom 功能房
     * @return 结果
     */
    @Override
    public AjaxResult insertRtFuRoom(RtFuRoom rtFuRoom) {
        try {
            rtFuRoom.setRoNo(UUID.randomUUID().toString());
            rtFuRoom.setRoomCreatetime(DateUtils.getNowDate());
            if (rtFuRoom.getRtFuResourcesList()!=null){
                for(RtFuResourceVo atta:rtFuRoom.getRtFuResourcesList()){
                    RtFuResource res=new RtFuResource();
                    res.setResPath(atta.getResPath());
                    res.setResName(atta.getResName());
                    res.setDelFlag("0");
                    res.setResNo(UUID.randomUUID().toString());
                    res.setResMainNo(rtFuRoom.getRoNo());
                    res.setResType("gnf01");
                    res.setCreateTime(DateUtils.getNowDate());
                    res.setStatus("0");
                    int ress= rtFuResourceMapper.insertRtFuResource(res);
                    if (ress==0){
                        throw new Exception("图片新增失败");
                    }
                }
            }
//            if (rtFuRoom.getRtFuSoltList()!=null){
//                for(RtfuRuleVo solts:rtFuRoom.getRtFuSoltList()){
//                    RtFuSolt solt=new RtFuSolt();
//                    solt.setRoWeekday(solts.getRoWeekday());
//                    solt.setRoType(UUID.randomUUID().toString());
//                    solt.setSoNo(rtFuRoom.getRoomRule());
//                    solt.setRoStartTime(solts.getRoStartTime());
//                    solt.setRoEndTime(solts.getRoEndTime());
//                    int res= rtFuSoltMapper.insertRtFuSolt(solt);
//                    if (res==0){
//                        throw new Exception("时间段新增失败");
//                    }
//                }
//            }
            int rt=rtFuRoomMapper.insertRtFuRoom(rtFuRoom);
            if (rt==0){
                throw new Exception("新增失败");
            }
            else {
                return AjaxResult.success("新增成功");
            }
        }catch (Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }

    /**
     * 修改功能房
     *
     * @param rtFuRoom 功能房
     * @return 结果
     */
    @Override
    public AjaxResult updateRtFuRoom(RtFuRoom rtFuRoom) {
        try {
            rtFuRoom.setRoomUptime(DateUtils.getNowDate());
            rtFuResourceMapper.deleteRtFuResourceByResManNo(rtFuRoom.getRoNo());

/*            if (rtFuRoom.getRtFuSoltList()!=null){
                for(RtfuRuleVo solts:rtFuRoom.getRtFuSoltList()){
                    RtFuSolt solt=new RtFuSolt();
                    solt.setSoNo(rtFuRoom.getRoomRule());
                    solt.setRoWeekday(solts.getRoWeekday());
                    solt.setRoType(solts.getRoType());
                    RtFuSolt soltss =rtFuSoltMapper.selectRtFSoles(solt);
                    if (soltss!=null){
                        solt.setId(soltss.getId());
                        solt.setRoStartTime(solts.getRoStartTime());
                        solt.setRoEndTime(solts.getRoEndTime());
                        int upSolt= rtFuSoltMapper.updateRtFuSolt(solt);
                        if (upSolt==0){
                            throw new Exception("功能房时间段修改失败");
                        }
                    }
                    else {
                        int res= rtFuSoltMapper.insertRtFuSolt(solt);
                        if (res==0){
                            throw new Exception("功能房时间段新增失败");
                        }
                    }
                }
            }*/
            if (rtFuRoom.getRtFuResourcesList()!=null){
                for(RtFuResourceVo atta:rtFuRoom.getRtFuResourcesList()){
                    RtFuResource res=new RtFuResource();
                    res.setResPath(atta.getResPath());
                    res.setResName(atta.getResName());
                    res.setDelFlag("0");
                    res.setResNo(UUID.randomUUID().toString());
                    res.setResMainNo(rtFuRoom.getRoNo());
                    res.setResType("gnf01");
                    res.setCreateTime(DateUtils.getNowDate());
                    res.setStatus("0");
                    rtFuResourceMapper.insertRtFuResource(res);
                }
            }
            int upro= rtFuRoomMapper.updateRtFuRoom(rtFuRoom);
            if (upro==0){
                throw new Exception("功能房修改失败");
            }
            else {
                return AjaxResult.success("功能房修改成功");
            }
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }

    }

    /**
     * 修改功能房状态
     *
     * @param rtFuRoom 功能房信息
     * @return 结果
     */
    @Override
    public AjaxResult updateRtfuSta(RtFuRoom rtFuRoom) {
        try {
            rtFuRoom.setRoomUptime(DateUtils.getNowDate());
            int upsta= rtFuRoomMapper.updateRtfuSta(rtFuRoom);
            if (upsta==0){
                throw new Exception("功能房状态修改失败");
            }
            else {
                return AjaxResult.success("功能房状态修改成功");
            }
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }
    /**
     * 批量删除功能房
     *
     * @param ids 需要删除的功能房主键
     * @return 结果
     */
    @Override
    public int deleteRtFuRoomByIds(Long[] ids) {
        for(int i = 0 ; i < ids.length ; i++){
            RtFuRoom list=rtFuRoomMapper.selectRtFuRoomById(ids[i]);
            RtFuReservation reservation=rtFuReservationMapper.selectRtFuReservationinfo(list.getRoNo());
            if (reservation!=null){
                int res=rtFuReservationMapper.deleteRtFuReservationByreNo(reservation.getRoNo());
            }
            RtFuResource resource=rtFuResourceMapper.selectRtFuResourceByMainNo(list.getRoNo());
            if (resource!=null){
                int source=rtFuResourceMapper.deleteRtFuResourceByResManNo(resource.getResMainNo());
            }
        }
        return rtFuRoomMapper.deleteRtFuRoomByIds(ids);
    }

    /**
     * 删除功能房信息
     *
     * @param id 功能房主键
     * @return 结果
     */
    @Override
    public int deleteRtFuRoomById(Long id) {
        return rtFuRoomMapper.deleteRtFuRoomById(id);
    }
}
