package com.ics.admin.service.impl.meeting;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ics.admin.domain.meeting.Reservation;
import com.ics.admin.domain.meeting.ReservationDTO;
import com.ics.admin.domain.meeting.ShowroomRecordDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ics.admin.mapper.meeting.ShowroomRecordMapper;
import com.ics.admin.domain.meeting.ShowroomRecord;
import com.ics.admin.service.meeting.IShowroomRecordService;

import static com.ics.admin.service.impl.meeting.ReservationServiceImpl.judge;

/**
 * 展厅预约记录Service业务层处理
 * 
 * @author ics
 * @date 2024-03-12
 */
@Service
public class ShowroomRecordServiceImpl extends ServiceImpl<ShowroomRecordMapper, ShowroomRecord> implements IShowroomRecordService {
    @Autowired
    private ShowroomRecordMapper showroomRecordMapper;

    /**
     * 查询展厅预约记录
     * 
     * @param id 展厅预约记录ID
     * @return 展厅预约记录
     */
    @Override
    public ShowroomRecord selectShowroomRecordById(Long id) {
        return showroomRecordMapper.selectById(id);
    }

    /**
     * 查询展厅预约记录列表
     * 
     * @param showroomRecord 展厅预约记录
     * @return 展厅预约记录
     */
    @Override
    public List<ShowroomRecord> selectShowroomRecordList(ShowroomRecord showroomRecord) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(showroomRecord.getTitle() !=null,"title", showroomRecord.getTitle());
        queryWrapper.eq(showroomRecord.getParkId() !=null,"park_id", showroomRecord.getParkId());
        queryWrapper.eq(showroomRecord.getTenantId() !=null,"tenant_id", showroomRecord.getTenantId());
        queryWrapper.eq(CollUtil.isNotEmpty(showroomRecord.getStaffIds()),"user_id", showroomRecord.getStaffIds());
        return showroomRecordMapper.selectList(queryWrapper);
    }

    /**
     * 新增展厅预约记录
     * 
     * @param showroomRecord 展厅预约记录
     * @return 结果
     */
    @Override
    public int insertShowroomRecord(ShowroomRecord showroomRecord) {
        return showroomRecordMapper.insert(showroomRecord);
    }

    /**
     * 修改展厅预约记录
     * 
     * @param showroomRecord 展厅预约记录
     * @return 结果
     */
    @Override
    public int updateShowroomRecord(ShowroomRecord showroomRecord) {
        return showroomRecordMapper.updateById(showroomRecord);
    }

    /**
     * 删除展厅预约记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteShowroomRecordByIds(String ids) {
        String[] idsArray = StrUtil.split(ids,",");
        return showroomRecordMapper.deleteBatchIds(CollUtil.toList(idsArray));
    }

    /**
     * 删除展厅预约记录信息
     * 
     * @param id 展厅预约记录ID
     * @return 结果
     */
    @Override
    public int deleteShowroomRecordById(Long id) {
        return showroomRecordMapper.deleteShowroomRecordById(id);
    }

    @Override
    public List<ShowroomRecordDTO> appointmentRecord(ShowroomRecord showroomRecord) {
        ArrayList<ShowroomRecordDTO> list = new ArrayList<>();

        // 根据最近七天查询数据
        List<Date> dates = showroomRecordMapper.selectListByDate(showroomRecord.getShowroomId());
        for (Date dateTime : dates) {
            ShowroomRecordDTO showroomRecordDTO = new ShowroomRecordDTO();
            showroomRecordDTO.setNowDate(DateUtil.format(dateTime,"yyyy-MM-dd"));
            // 查询会议室记录
            QueryWrapper<ShowroomRecord> wrapper = new QueryWrapper<>();
            wrapper.eq("showroom_id",showroomRecord.getShowroomId());
            wrapper.ne("status",4);
            wrapper.gt("start_time", DateUtil.format(dateTime,"yyyy-MM-dd")+ " 00:00:00");
            wrapper.lt("end_date",DateUtil.format(dateTime,"yyyy-MM-dd") + " 23:59:59");
            List<ShowroomRecord> showroomRecords = showroomRecordMapper.selectList(wrapper);

            showroomRecordDTO.setShowroomRecords(showroomRecords);
            list.add(showroomRecordDTO);
        }
        return list;

    }

    @Override
    public boolean selectFreeMeetingRoom(ShowroomRecord showroomRecord) {

        QueryWrapper<ShowroomRecord> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("showroom_id",showroomRecord.getShowroomId());
        queryWrapper.ne("status",4);

        Date startTime = showroomRecord.getStartTime();
        Date endDate = showroomRecord.getEndDate();
        List<ShowroomRecord> reservations = showroomRecordMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(reservations)){
            for (ShowroomRecord reservation1 : reservations) {
                Boolean judge = judge(reservation1.getStartTime(), reservation1.getEndDate(), startTime, endDate);
                if (judge){
                    return true;
                }else {
                    continue;
                }
            }
        }
        return false;
    }

    @Override
    public IPage<ShowroomRecord> selectShowroomRecord(ShowroomRecord showroomRecord, Integer pageNum, Integer pageSize) {
        QueryWrapper<ShowroomRecord> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id",showroomRecord.getUserId());
        queryWrapper.eq("park_id",showroomRecord.getParkId());
        queryWrapper.orderByDesc("create_time");
        IPage<ShowroomRecord> pages = new Page<>(pageNum,pageSize);

        IPage<ShowroomRecord> showroomRecordIPage = baseMapper.selectPage(pages, queryWrapper);

        return showroomRecordIPage;

    }
}
