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

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
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.BuildingDetail;
import com.ics.admin.domain.Room;
import com.ics.admin.domain.meeting.Reservation;
import com.ics.admin.domain.meeting.RoomContent;
import com.ics.admin.mapper.meeting.ReservationMapper;
import com.ics.admin.mapper.meeting.ReservationPersonMapper;
import com.ics.admin.mapper.meeting.RoomContentMapper;
import com.ics.admin.service.IBuildingDetailService;
import com.ics.admin.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ics.admin.domain.meeting.ReservationPerson;
import com.ics.admin.service.meeting.IReservationPersonService;

/**
 * 预约参观人员Service业务层处理
 * 
 * @author chen
 * @date 2024-03-06
 */
@Service
public class ReservationPersonServiceImpl extends ServiceImpl<ReservationPersonMapper, ReservationPerson> implements IReservationPersonService {
    @Autowired
    private ReservationPersonMapper reservationPersonMapper;

    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private RoomContentMapper roomContentMapper;


    @Autowired
    private IRoomService roomService;

    @Autowired
    private IBuildingDetailService buildingDetailService;

    @Autowired
    private IReservationPersonService reservationPersonService;

    /**
     * 查询预约参观人员
     * 
     * @param id 预约参观人员ID
     * @return 预约参观人员
     */
    @Override
    public ReservationPerson selectReservationPersonById(Long id) {
        return reservationPersonMapper.selectById(id);
    }

    /**
     * 查询预约参观人员列表
     * 
     * @param reservationPerson 预约参观人员
     * @return 预约参观人员
     */
    @Override
    public List<ReservationPerson> selectReservationPersonList(ReservationPerson reservationPerson) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(null != reservationPerson.getReservationId(),"reservation_id",reservationPerson.getReservationId());
        return reservationPersonMapper.selectList(queryWrapper);
    }

    /**
     * 新增预约参观人员
     * 
     * @param reservationPerson 预约参观人员
     * @return 结果
     */
    @Override
    public int insertReservationPerson(ReservationPerson reservationPerson) {
        return reservationPersonMapper.insert(reservationPerson);
    }

    /**
     * 修改预约参观人员
     * 
     * @param reservationPerson 预约参观人员
     * @return 结果
     */
    @Override
    public int updateReservationPerson(ReservationPerson reservationPerson) {
        return reservationPersonMapper.updateById(reservationPerson);
    }

    /**
     * 删除预约参观人员对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteReservationPersonByIds(String ids) {
        String[] idsArray = StrUtil.split(ids,",");
        return reservationPersonMapper.deleteBatchIds(CollUtil.toList(idsArray));
    }

    /**
     * 删除预约参观人员信息
     * 
     * @param id 预约参观人员ID
     * @return 结果
     */
    @Override
    public int deleteReservationPersonById(Long id) {
        return reservationPersonMapper.deleteReservationPersonById(id);
    }

    @Override
    public ReservationPerson isVisitor(ReservationPerson reservationPerson) {

        QueryWrapper<ReservationPerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reservation_id",reservationPerson.getReservationId());
        queryWrapper.eq("user_id",reservationPerson.getUserId());
        queryWrapper.eq("participant_id",reservationPerson.getParticipantId());
        ReservationPerson reservationPerson1 = reservationPersonMapper.selectOne(queryWrapper);
        return reservationPerson1;
    }

    @Override
    public IPage<Reservation> selectListByParticipantId(Long userId,Long parkId,Integer page,Integer limit) {

//        QueryWrapper<ReservationPerson> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("participant_id",userId);
//        queryWrapper.eq("park_id",parkId);
//        List<ReservationPerson> list = reservationPersonMapper.selectList(queryWrapper);
//        List<Long> collect = list.stream().map(item -> {
//            return item.getReservationId();
//        }).collect(Collectors.toList());
//        if (CollUtil.isEmpty(collect)){
//            IPage<Reservation> iPage = new Page<>(page,limit);
//            return iPage ;
//        }
        IPage<Reservation> iPage = new Page<>(page,limit);
        QueryWrapper<Reservation> wrapper = new QueryWrapper<>();
//        wrapper.in("id",collect);
        wrapper.eq("user_id",userId);
        wrapper.eq("park_id",parkId);
        wrapper.orderByDesc("start_time");
        IPage<Reservation> iPage1 = reservationMapper.selectPage(iPage, wrapper);
        List<Reservation> records = iPage1.getRecords();
        for (Reservation reservation1 : records) {
            reservation1.setStatusValue(reservation1.getStauts().getValue());
            reservation1.setStatusName(reservation1.getStauts().getName());
            Long roomContentId = reservation1.getRoomContentId();
            RoomContent roomContent = roomContentMapper.selectById(roomContentId);
            Room room = roomService.selectRoomById(roomContent.getRoomId());
            if (room != null) {
                roomContent.setRoomName(room.getName());
                roomContent.setArea(room.getArea());
                roomContent.setRenArea(room.getRentArea());
                //查询楼层号
                BuildingDetail buildingDetail = buildingDetailService.selectBuildingDetailById(room.getBuildingDetailId());
                if (buildingDetail != null) {
                    roomContent.setBuildingName(buildingDetail.getFloorName() +"F");
                }
            }
            reservation1.setRoomContent(roomContent);
        }

        return iPage1;

    }

    @Override
    public List<ReservationPerson> selectListByReservationId(Long collect) {
        QueryWrapper<ReservationPerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reservation_id",collect);
        return reservationPersonMapper.selectList(queryWrapper);

    }
}
