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

import java.util.Date;
import java.util.List;
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.meeting.Reservation;
import com.ics.admin.domain.meeting.RoomRecord;
import com.ics.admin.mapper.RoomMapper;
import com.ics.admin.mapper.meeting.RoomRecordMapper;
import com.ics.admin.service.meeting.IVisitorPersonService;
import com.ics.system.domain.User;
import com.ics.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ics.admin.mapper.meeting.VisitorPersonMapper;
import com.ics.admin.domain.meeting.VisitorPerson;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约参观人员Service业务层处理
 * 
 * @author chen
 * @date 2024-02-25
 */
@Service
public class IVisitorPersonServiceImpl extends ServiceImpl<VisitorPersonMapper, VisitorPerson> implements IVisitorPersonService {
    @Autowired
    private VisitorPersonMapper visitorPersonMapper;

    @Autowired
    private RoomRecordMapper roomRecordMapper;

    @Autowired
    private IUserService userService;

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

    /**
     * 查询预约参观人员列表
     * 
     * @param visitorPerson 预约参观人员
     * @return 预约参观人员
     */
    @Override
    public List<VisitorPerson> selectReservationPersonList(VisitorPerson visitorPerson) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(visitorPerson.getParkId() !=null,"park_id", visitorPerson.getParkId());
        queryWrapper.eq(visitorPerson.getTenantId() !=null,"tenant_id", visitorPerson.getTenantId());
        queryWrapper.eq(CollUtil.isNotEmpty(visitorPerson.getStaffIds()),"interviewee_id", visitorPerson.getStaffIds());
        queryWrapper.orderByDesc("create_time");
        return visitorPersonMapper.selectList(queryWrapper);
    }

    /**
     * 新增预约参观人员
     * 
     * @param visitorPerson 预约参观人员
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReservationPerson(VisitorPerson visitorPerson) {
        return visitorPersonMapper.insert(visitorPerson);
    }

    /**
     * 修改预约参观人员
     * 
     * @param visitorPerson 预约参观人员
     * @return 结果
     */
    @Override
    public int updateReservationPerson(VisitorPerson visitorPerson) {
        System.out.println(visitorPerson);
        return visitorPersonMapper.updateById(visitorPerson);
    }

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

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

    @Override
    public IPage<VisitorPerson> selectVisitorRecord(Long userId, Integer pageNum, Integer pageSize) {
        //根据当前的用户id 去查询 系统用户的 用户id
        User user = userService.selectUserByStaffId(userId);
        if (null != user){
            Long customerId = user.getCustomerId();
            QueryWrapper<VisitorPerson> wrapper = new QueryWrapper<VisitorPerson>().eq("customer_id", customerId);
            wrapper.orderByDesc("create_time");

            IPage<VisitorPerson> pages = new Page<>(pageNum,pageSize);
            IPage<VisitorPerson> userIPage = visitorPersonMapper.selectPage(pages,wrapper);
            return userIPage;

        }else {
            QueryWrapper<VisitorPerson> wrapper = new QueryWrapper<VisitorPerson>().eq("user_id", userId);
            wrapper.orderByDesc("create_time");

            IPage<VisitorPerson> pages = new Page<>(pageNum,pageSize);
            IPage<VisitorPerson> userIPage = visitorPersonMapper.selectPage(pages,wrapper);
            return userIPage;
        }

    }

    @Override
    public IPage<VisitorPerson> selectVisitorRecordByIntervieweeId(Long intervieweeId,Long parkId, Integer pageNum, Integer pageSize) {
        QueryWrapper<VisitorPerson> wrapper = new QueryWrapper<VisitorPerson>().eq("interviewee_id", intervieweeId).eq("park_id",parkId);
        wrapper.orderByDesc("create_time");
        IPage<VisitorPerson> pages = new Page<>(pageNum,pageSize);
        IPage<VisitorPerson> userIPage = visitorPersonMapper.selectPage(pages,wrapper);

        return userIPage;
    }

    @Override
    public int updateVisitorPersonStatus(VisitorPerson person) {
        return visitorPersonMapper.updateById(person);
    }

    @Override
    public List<RoomRecord> getRecordByReservationId(Long id) {
        VisitorPerson visitorPerson = visitorPersonMapper.selectReservationPersonById(id);
        //获取当前预约记录，根据开始时间和结束时间去查找对应的

//        //开始时间
//        Date visitTime = visitorPerson.getVisitTime();
//        //结束时间
//        Date leaveTime = visitorPerson.getLeaveTime();


        QueryWrapper<RoomRecord> wrapper = new QueryWrapper<RoomRecord>();
//        wrapper.between("create_time",visitTime,leaveTime);
        wrapper.eq("user_id",visitorPerson.getIntervieweeId());

        return roomRecordMapper.selectList(wrapper);
    }

    @Override
    public VisitorPerson selectByUserId(String personId) {

        QueryWrapper<VisitorPerson> wrapper = new QueryWrapper<VisitorPerson>();
        wrapper.eq("user_id",personId);
        return visitorPersonMapper.selectOne(wrapper);

    }
}
