package com.ruoyi.web.work.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import cn.hutool.captcha.CaptchaUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.web.work.domain.Visit;
import com.ruoyi.web.work.domain.dto.ObrRequestDto;
import com.ruoyi.web.work.mapper.VisitMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.work.mapper.RequestMapper;
import com.ruoyi.web.work.domain.Request;
import com.ruoyi.web.work.service.IRequestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

/**
 * @author lsw
 * @date 2024-07-23
 */
@Service
public class RequestServiceImpl extends ServiceImpl<RequestMapper, Request> implements IRequestService {
    @Autowired
    private RequestMapper requestMapper;

    @Autowired
    private VisitMapper visitMapper;

    @Override
    public List<Request> selectList(Request request) {
        return requestMapper.selectList(request);
    }

    @Override
    public List<Request> getRequestByCard(String card) {
        return requestMapper.getRequestByCard(card);
    }

    @Override
    public List<Request> getAllRequestByPatId(Long patId) {
        return requestMapper.getAllRequestByPatId(patId);
    }

    @Override
    public List<ObrRequestDto> getRequestByPatId(String patId, String startTime, String endTime) {
        List<ObrRequestDto> dtoList = new ArrayList<>();
        //根据患者ID获取就诊的入院和出院时间
        String admissionTime = null;//入院时间
        String dischargeTime = null;//出院时间
        List<Visit> visitList = visitMapper.selectVisitsByPatId(patId,null,null);
        if (!CollectionUtils.isEmpty(visitList)){
            Visit visitEntity = visitList.get(visitList.size() - 1); //获取最后一次就诊记录
            admissionTime = visitEntity.getAdmissionTime();//入院时间
            dischargeTime = visitEntity.getDischargeTime();//出院时间
        }
        List<Request> requests = requestMapper.getRequestListByPatId(patId, startTime, endTime);
        for (Request request : requests) {
            ObrRequestDto dto = new ObrRequestDto();
            dto.setAdmissionTime(admissionTime);
            dto.setDischargeTime(dischargeTime);
            BeanUtils.copyProperties(request,dto);
            dtoList.add(dto);
        }
        return dtoList;
    }
}
