package com.srs.routine.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
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.*;
import com.srs.routine.domain.dto.RtFureservationDto;
import com.srs.routine.domain.vo.*;
import com.srs.routine.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.routine.service.IRtFuReservationService;

/**
 * 功能房预约Service业务层处理
 *
 * @author srs
 * @date 2024-07-12
 */
@Service
public class RtFuReservationServiceImpl extends ServiceImpl<RtFuReservationMapper,RtFuReservation> implements IRtFuReservationService {
    @Autowired
    private RtFuReservationMapper rtFuReservationMapper;

    @Autowired
    private RtFuResourceMapper rtFuResourceMapper;

    @Autowired
    private RtFuSoltMapper rtFuSoltMapper;
    @Autowired
    private RtFuRoomMapper rtFuRoomMapper;

    @Autowired
    private  RtFuAuditMapper rtFuAuditMapper;
    /**
     * 查询功能房预约
     *
     * @param id 功能房预约主键
     * @return 功能房预约
     */
    @Override
    public RtFuResIdVo selectRtFuReservationById(Long id) {
        RtFuResIdVo list= rtFuReservationMapper.selectRtFuReservationById(id);
        //审核和图片
        RtFuAuditVo audits= rtFuAuditMapper.selectRtFuAuditByre(list.getReNo());
        list.setAudit(audits);
        RtFuResSoltVo room=rtFuRoomMapper.selectRtFuRoomByRoNo(list.getRoNo());
        list.setRoomCapacity(room.getRoomCapacity());
        list.setRoomMatter(room.getRoomMatter());
        list.setStatus(room.getStatus());
        RtFuResource sar=new RtFuResource();
        sar.setResMainNo(list.getRoNo());
        sar.setResType("gnf01");
        List<RtFuResourceVo> list1=rtFuResourceMapper.selectRtFuResourceLists(sar);
        list.setRtFuResourceList(list1);
        //获取时间段 传星期和规则进去
        List<Map<String, String>> resultList = new ArrayList<>();
        RtFuReservation reservation=new RtFuReservation();
        reservation.setRtTime(list.getRtTime());
        reservation.setRoNo(list.getRoNo());
        List<RtFuReservation> rtres=rtFuReservationMapper.selectRtFuReservationList(reservation);
        RtFuSolt solt=new RtFuSolt();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(list.getRtTime(), formatter);
        String dayOfWeekName = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.SIMPLIFIED_CHINESE);
        solt.setRoWeekday(dayOfWeekName);
        solt.setSoNo(room.getRoomRule());
        List<RtFuSolt> fuSolts=rtFuSoltMapper.selectRtFuSoltList(solt);
        for (int i=0;i<fuSolts.size();i++){
            Map<String,String> soltMap=new HashMap<>();

            soltMap.put("start",fuSolts.get(i).getRoStartTime());
            soltMap.put("end",fuSolts.get(i).getRoEndTime());
            soltMap.put("roType",fuSolts.get(i).getRoType());

            for (int j=0;j<rtres.size();j++){
                if(list.getRtYuyue().equals(fuSolts.get(i).getRoType())&&list.getRtYuyue().equals(rtres.get(j).getRtYuyue())){
                    soltMap.put("reStatus","2");
                    break;
                }
                else if (rtres.get(j).getRtYuyue().equals(fuSolts.get(i).getRoType())){
                    soltMap.put("reStatus","1");
                    break;
                }
                else {
                    soltMap.put("reStatus","0");
                }
            }
            resultList.add(soltMap);
        }
        list.setSoTime(resultList);
        return list;
    }

    @Override
    public  List<RtfuRoTimeVo> getroTimeinfo(String roNo){
        List<RtfuRoTimeVo> list=rtFuReservationMapper.getroTimeinfo(roNo);
        for (int i =0 ;i<list.size();i++){
            //获取星期几
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate date = LocalDate.parse(list.get(i).getRtTime(), formatter);
            String dayOfWeekName = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.SIMPLIFIED_CHINESE);
            list.get(i).setRtweekday(dayOfWeekName);
            RtFuSolt solt=new RtFuSolt();
            solt.setRoWeekday(dayOfWeekName);
            solt.setRoType(list.get(i).getRtYuyue());
            RtFuSolt fuSolt=rtFuSoltMapper.selectRtFSoles(solt);
            list.get(i).setRoStartTime(fuSolt.getRoStartTime());
            list.get(i).setRoEndTime(fuSolt.getRoEndTime());
        }
        return list;
    }

    @Override
    public  List<RtFuReservationVo> selectRtFuReservationByuser(RtFuReservation rtFuReservation){
        List<RtFuReservationVo>  List=rtFuReservationMapper.selectRtFuReservationByuser(rtFuReservation);
        for(RtFuReservationVo rc:List){
            RtFuAuditVo audits= rtFuAuditMapper.selectRtFuAuditByre(rc.getReNo());
            rc.setAudit(audits);
            RtFuResource sar=new RtFuResource();
            sar.setResMainNo(rc.getRoNo());
            sar.setResType("gnf01");
            RtFuResourceVo list1=rtFuResourceMapper.getRtFuresLIst(sar);
            rc.setRtFuResourceList(list1);
            RtFuSolt solt=new RtFuSolt();
            solt.setRoType(rc.getRtYuyue());
            RtFuSolt rtFuSolt=rtFuSoltMapper.selectRtFSoles(solt);
            if (rtFuSolt!=null){
                rc.setRoStartTime(rtFuSolt.getRoStartTime());
                rc.setRoEndTime(rtFuSolt.getRoEndTime());
            }
        }
        return List;
    }
    /**
     * 查询功能房时间段和预约时间段
     *
     * @param id 功能房预约主键
     * @return 功能房预约
     */
    @Override
    public RtFuResSoltVo selectRtFuReservationTime(String ro_no, String rt_Time) {
        HashMap<Object, List<String>> objectObjectHashMap = new HashMap<>();
        List<String> Slots = new ArrayList<>();
        //获取星期几
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(rt_Time, formatter);
        String dayOfWeekName = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.SIMPLIFIED_CHINESE);
        //拿功能房名和预约时间段
        RtFuResSoltVo room=rtFuRoomMapper.selectRtFuRoomByRoNo(ro_no);
        RtFuSolt solt=new RtFuSolt();
        solt.setRoType(room.getRoomRule());
        solt.setRoWeekday(dayOfWeekName);
        List<RtfuRuleVo> fuSolt=rtFuSoltMapper.selectRtFuSoltLists(solt);
//            for (int i=0;i<fuSolt.size();i++) {
//                if(fuSolt.get(i).getRoWeektime()!=null) {
//                    String[] strArray1 = fuSolt.get(i).getRoWeektime().split(",");
//                    for (String slot : strArray1) {
//                        Slots.add(slot.trim()); // 去除可能的空格
//                    }
//                }
//        }

        objectObjectHashMap.put("Slots",Slots);
        //功能房已预约时间
        List<RtFuReservation> list=rtFuReservationMapper.seleectRtFuTime(ro_no,rt_Time);
        List<String> availableSlots  = new ArrayList<>();
        if(list.size()==0){
            objectObjectHashMap.put("availableSlots",availableSlots);
        }
        else {
            for(int i=0;i<list.size();i++){
                    availableSlots.add(list.get(i).getRtYuyue());
            }
            objectObjectHashMap.put("availableSlots",availableSlots);
        }
        RtFuResSoltVo list1=rtFuRoomMapper.selectRtFuRoomByRoNo(ro_no);
        list1.setWek(objectObjectHashMap);
        return list1;
    }

    @Override
    public RtfuyuyueVo getyuyue(RtFuReservation rtFuReservation){
        RtfuyuyueVo list=new RtfuyuyueVo();
        List<Map<String, Object>> timeSlots = new ArrayList<>();

        List<RtFuReservation> res=rtFuReservationMapper.selectRtFuReservationList(rtFuReservation);
        RtFuRoom room=rtFuRoomMapper.getFuRoomByRoNo(rtFuReservation.getRoNo());
        RtFuSolt solt=new RtFuSolt();
        solt.setSoNo(room.getRoomRule());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(rtFuReservation.getRtTime(), formatter);
        String dayOfWeekName = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.SIMPLIFIED_CHINESE);
        list.setRoWeekday(dayOfWeekName);
        solt.setRoWeekday(dayOfWeekName);
        List<RtFuSolt> solts=rtFuSoltMapper.selectRtFuSoltList(solt);
            for(int j =0;j<solts.size();j++) {
                Map<String, Object> timeSlot1 = new HashMap<>();
                timeSlot1.put("roType",solts.get(j).getRoType());
                timeSlot1.put("soNo",solts.get(j).getSoNo());
                timeSlot1.put("soRule",solts.get(j).getSoRule());
                timeSlot1.put("roStartTime", solts.get(j).getRoStartTime());
                timeSlot1.put("roEndTime", solts.get(j).getRoEndTime());
                if (res.size() != 0) {
                    for (int i = 0; i < res.size(); i++) {
                        if (res.get(i).getRtYuyue().equals(solts.get(j).getRoType())) {
                            timeSlot1.put("status", 1);
                            break;
                        }
                        else {
                            timeSlot1.put("status", 0);
                        }
                    }
                    timeSlots.add(timeSlot1);
                }
                else {
                        timeSlot1.put("status", 0);
                        timeSlots.add(timeSlot1);
                }
                list.getYuyueTimeMap().put("yuyueTime", timeSlots);
            }
        list.setRoomName(room.getRoomName());
        list.setRoomCapacity(room.getRoomCapacity());
        list.setRoNo(room.getRoNo());
        list.setRoType(room.getRoomRule());
        list.setRoomMatter(room.getRoomMatter());
        return  list;
    }

    @Override
    public RtFuResSoltVo seletRtfuCase(String ro_no ,String rt_Time) {
        HashMap<Object, List<String>> objectObjectHashMap = new HashMap<>();
        List<String> day = new ArrayList<>();
        List<String> yuyue = new ArrayList<>();
        List<String> user = new ArrayList<>();
        List<String> depart = new ArrayList<>();
        RtFuResSoltVo list=rtFuRoomMapper.selectRtFuRoomByRoNo(ro_no);
        List<RtFuReservation> res = rtFuReservationMapper.seletRtfuCase(ro_no,rt_Time);
        if(res.size()==0){
            objectObjectHashMap.put("day",null);
            objectObjectHashMap.put("yuyue",null);
            objectObjectHashMap.put("user",null);
            objectObjectHashMap.put("depart",null);
            list.setWek(objectObjectHashMap);
        }
        else {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate date = LocalDate.parse(rt_Time, formatter);
            String dayOfWeekName = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.SIMPLIFIED_CHINESE);
            day.add(dayOfWeekName);
            for (RtFuReservation rc:res){
                yuyue.add(rc.getRtYuyue());
                user.add(rc.getRtCreatRole());
                depart.add(rc.getRtDepar());
            }
            objectObjectHashMap.put("day",day);
            objectObjectHashMap.put("yuyue",yuyue);
            objectObjectHashMap.put("user",user);
            objectObjectHashMap.put("depart",depart);
            list.setWek(objectObjectHashMap);
        }
        return  list;
    }
    /**
     * 查询功能房预约列表
     *
     * @param rtFuReservation 功能房预约
     * @return 功能房预约
     */
    @Override
    public List<RtFuReservationVo> selectRtFuReservationList(RtFuReservation rtFuReservation) {
        List<RtFuReservationVo> list=rtFuReservationMapper.selectRtFuReservationLists(rtFuReservation);
        for(RtFuReservationVo rc:list){
            RtFuAuditVo audits= rtFuAuditMapper.selectRtFuAuditByre(rc.getReNo());
            rc.setAudit(audits);
            RtFuResource sar=new RtFuResource();
            sar.setResMainNo(rc.getRoNo());
            sar.setResType("gnf01");
            RtFuResourceVo list1= rtFuResourceMapper.getRtFuresLIst(sar);
            rc.setRtFuResourceList(list1);
            RtFuSolt solt=new RtFuSolt();
            solt.setRoType(rc.getRtYuyue());
            RtFuSolt rtFuSolt=rtFuSoltMapper.selectRtFSoles(solt);
            if (rtFuSolt!=null){
                rc.setRoStartTime(rtFuSolt.getRoStartTime());
                rc.setRoEndTime(rtFuSolt.getRoEndTime());
            }
        }
        return list;
    }


    @Override
    public List<RtFureservationDto> getexportrtfu(){return rtFuReservationMapper.getexportrtfu();}
    /**
     * 新增功能房预约
     *
     * @param rtFuReservation 功能房预约
     * @return 结果
     */
    @Override
    public AjaxResult insertRtFuReservation(RtFuReservation rtFuReservation) {
    try {
        RtFuReservation list=new RtFuReservation();
        list.setRoNo(rtFuReservation.getRoNo());
        list.setRtYuyue(rtFuReservation.getRtYuyue());
        list.setRtTime(rtFuReservation.getRtTime());
        List<RtFuReservation> list1=rtFuReservationMapper.selectRtFuReservationList(list);
        String Tipsw = null;
        //RtFuReservation list=rtFuReservationMapper.seleectRtFuYuyue(rtFuReservation.getRoNo(),rtFuReservation.getRtTime(),rtFuReservation.getRtYuyue());
        if (list1.size() != 0){
            Tipsw = "已预约";
            return  AjaxResult.success(Tipsw);
        }
        else {
            rtFuReservation.setReNo(com.srs.common.utils.uuid.UUID.randomUUID().toString());
            rtFuReservation.setStatus("0");
            rtFuReservation.setRtCreattime(DateUtils.getNowDate());
            RtFuAudit audit=new RtFuAudit();
            audit.setAuStatus("0");
            audit.setReNo(rtFuReservation.getReNo());
            audit.setReason(" ");
            audit.setCreateTime(DateUtils.getNowDate());
            audit.setAuditor(rtFuReservation.getRtCreatRole());
            audit.setDepart(rtFuReservation.getDepart());
            rtFuAuditMapper.insertRtFuAudit(audit);
            int res= rtFuReservationMapper.insertRtFuReservation(rtFuReservation);
            if(res !=0){
                Tipsw = "预约成功";
            }
            if(res ==0){
                Tipsw = "预约失败";
            }
            return  AjaxResult.success(Tipsw);

        }
    }
    catch(Exception ex) {
        return AjaxResult.error(ex.getMessage());
    }

    }

    /**
     * 修改功能房预约
     *
     * @param rtFuReservation 功能房预约
     * @return 结果
     */
    @Override
    public AjaxResult updateRtFuReservation(RtFuReservation rtFuReservation) {
        try {
            rtFuReservation.setRtUptime(DateUtils.getNowDate());
            rtFuResourceMapper.deleteRtFuResourceByResManNo(rtFuReservation.getReNo());
            if (rtFuReservation.getRtFuResourceList()!=null){
                for(RtFuResourceVo atta:rtFuReservation.getRtFuResourceList()){
                    RtFuResource res=new RtFuResource();
                    res.setDelFlag("0");
                    res.setResNo(UUID.randomUUID().toString());
                    res.setResMainNo(rtFuReservation.getReNo());
                    res.setResType("gnfyuyue02");
                    res.setCreateTime(DateUtils.getNowDate());
                    res.setResName(atta.getResName());
                    res.setResPath(atta.getResPath());
                    res.setStatus("0");
                    rtFuResourceMapper.insertRtFuResource(res);
                }
            }
                if(rtFuReservation.getAuStatus()!=null){
                    RtFuAudit audit=new RtFuAudit();
                    audit.setAuStatus(rtFuReservation.getAuStatus());
                    audit.setReNo(rtFuReservation.getReNo());
                    rtFuResourceMapper.updateAudit(audit);
                }
            int upres= rtFuReservationMapper.updateRtFuReservation(rtFuReservation);
            if (upres==0){
                throw new Exception("功能房预约修改失败");
            }
            else {
                return AjaxResult.success("功能房预约修改成功");
            }
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }

    }

    /**
     * 批量删除功能房预约
     *
     * @param ids 需要删除的功能房预约主键
     * @return 结果
     */
    @Override
    public int deleteRtFuReservationByIds(Long[] ids) {
        for (int i=0;i<ids.length;i++){
            RtFuResIdVo reservation=rtFuReservationMapper.selectRtFuReservationById(ids[i]);
            RtFuResource resource=rtFuResourceMapper.selectRtFuResourceByMainNo(reservation.getReNo());
            if (resource!=null){
                int list1=rtFuResourceMapper.deleteRtFuResourceByResManNo(reservation.getReNo());

            }
            RtFuAuditVo audit=rtFuAuditMapper.selectRtFuAuditByre(reservation.getReNo());
            if (audit!=null){
                int list2=rtFuAuditMapper.deleteRtFuAuditByaure(reservation.getReNo());
            }
        }
        return rtFuReservationMapper.deleteRtFuReservationByIds(ids);
    }

    /**
     * 删除功能房预约信息
     *
     * @param reNo 功能房预约编号
     * @return 结果
     */
    @Override
    public int deleteRtFuReservationByreNo(String reNo) {
        RtFuReservation list=rtFuReservationMapper.selectRtFuReservationinfos(reNo);
        RtFuResource resource=rtFuResourceMapper.selectRtFuResourceByMainNo(list.getReNo());
        if (resource!=null){
            int list1=rtFuResourceMapper.deleteRtFuResourceByResManNo(list.getReNo());

        }
        RtFuAuditVo audit=rtFuAuditMapper.selectRtFuAuditByre(list.getReNo());
        if (audit!=null){
            int list2=rtFuAuditMapper.deleteRtFuAuditByaure(list.getReNo());
        }

        return rtFuReservationMapper.deleteRtFuReservationByreNo(reNo);
    }
}
