package com.jeeplus.business.meetingRoom.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.business.meetingRoom.domain.MeetingRoom;
import com.jeeplus.business.meetingRoom.mapper.MeetingRoomMapper;
import com.jeeplus.business.meetingRoom.service.dto.MeetingRoomDTO;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class MeetingRoomService {

    @Autowired
    private MeetingRoomMapper meetingRoomMapper;


    /**
     * 会议室列表查询
     * @param meetingRoom
     * @param page
     * @return
     */
    public IPage<MeetingRoom> findPageList(MeetingRoom meetingRoom, Page<MeetingRoom> page) throws Exception {
        QueryWrapper<MeetingRoom> meetingRoomQueryWrapper = QueryWrapperGenerator.buildQueryCondition(meetingRoom, MeetingRoom.class);
        if (ObjectUtil.isNotEmpty(meetingRoomQueryWrapper) && StringUtils.isNotBlank(meetingRoom.getCreateById())){
            meetingRoomQueryWrapper.eq("a.create_by_id",meetingRoom.getCreateById());
        }
        meetingRoomQueryWrapper.eq("a.del_flag",0);
        //会议开始时间
        if (meetingRoom.getMeetingDates() != null && meetingRoom.getMeetingDates().length >0){
            meetingRoomQueryWrapper.between("a.start_time",meetingRoom.getMeetingDates()[0],meetingRoom.getMeetingDates()[1]);
        }
        //标题
        if (StringUtils.isNotBlank(meetingRoom.getTitle())){
            meetingRoomQueryWrapper.like("a.title",meetingRoom.getTitle());
        }
        //发起人部门
        if (StringUtils.isNotBlank(meetingRoom.getOfficeId())){
            meetingRoomQueryWrapper.eq("so.id",meetingRoom.getOfficeId());
        }
        IPage<MeetingRoom> iPage=meetingRoomMapper.findPageList(page,meetingRoomQueryWrapper);

        return iPage;
    }

    /**
     * 会议室新增或修改
     * @param meetingRoomDTO
     * @return
     */
    public String saveForm(MeetingRoomDTO meetingRoomDTO) {
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        //创建会议室对象并进行复制
        MeetingRoom meetingRoom = new MeetingRoom();
        BeanUtil.copyProperties(meetingRoomDTO,meetingRoom);
        meetingRoom.setStartTime(meetingRoomDTO.getStartTime());// 会议开始时间
        meetingRoom.setTitle(meetingRoomDTO.getTitle()); //标题
        Integer timeLong=0;
        //会议时长计算
        if (meetingRoomDTO.getDuration()==30){
            timeLong=30*60;
        }
        if (meetingRoomDTO.getDuration()==1 || meetingRoomDTO.getDuration()==2 || meetingRoomDTO.getDuration()==3){
            timeLong=meetingRoomDTO.getDuration()*60*60;
        }
         //会议时长
        meetingRoom.setDuration(meetingRoomDTO.getDuration());
        meetingRoom.setRemarks(meetingRoomDTO.getRemarks()); //会议说明
        meetingRoom.setCreateById(userDTO.getId());
        meetingRoom.setCreateTime(new Date());
        meetingRoom.setUpdateById(userDTO.getId());
        meetingRoom.setUpdateTime(new Date());
        meetingRoom.setDelFlag(0);
        //会议结束时间
        Date endDate = new Date(meetingRoomDTO.getStartTime().getTime() + (timeLong * 1000));
        meetingRoom.setEndTime(endDate);

        if (StringUtils.isNotBlank(meetingRoomDTO.getId())){
            meetingRoomMapper.updateById(meetingRoom);
        }else {
            meetingRoomMapper.insert(meetingRoom);
        }
        return meetingRoom.getId();

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public MeetingRoomDTO findById(String id) {
        MeetingRoomDTO roomDTO= meetingRoomMapper.findById(id);
        return roomDTO;
    }

    /**
     * 根据id修改状态
     * @param id
     * @param status
     */
    public void updateStatusById(String id, String status) {
        meetingRoomMapper.updateStatusById(id,status);
    }

    /**
     * 根据会议开始时间查询最近结束的会议时间
     * @param startTime
     * @return
     */
    public Boolean checkTime(String startTime) throws ParseException {
        //格式化输入的开始时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = simpleDateFormat.parse(startTime);

        MeetingRoom meetingRoom=meetingRoomMapper.checkTime(date);

        if (ObjectUtil.isNotEmpty(meetingRoom)){
            //判断上一个会议结束的时间在新的开始时间之前还是之后
            if (meetingRoom.getEndTime().after(date)){
                return false;
            }else {
                //将上一个会议往后顺延30分钟,然后在判断顺延后的时间与新的开始时间大小
                Long endTime=meetingRoom.getEndTime().getTime()+30*60*1000;
                Long time=date.getTime();
                if (endTime>=time){
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    public String deleteById(String id) {
        meetingRoomMapper.deleteById(id);
        return "删除成功";
    }
}
