package com.openhis.web.regdoctorstation.appservice.impl;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import com.core.common.utils.AgeCalculatorUtil;
import com.core.common.utils.StringUtils;
import com.core.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;

import com.core.common.core.domain.R;
import com.core.common.utils.SecurityUtils;
import com.openhis.administration.domain.Patient;
import com.openhis.administration.dto.EncounterDataDto;
import com.openhis.administration.dto.LocationDataDto;
import com.openhis.administration.dto.PatientBedInfoDto;
import com.openhis.administration.mapper.EncounterLocationMapper;
import com.openhis.administration.mapper.LocationMapper;
import com.openhis.administration.service.IEncounterService;
import com.openhis.administration.service.ILocationService;
import com.openhis.administration.service.IPatientService;
import com.openhis.administration.service.IPractitionerRoleService;
import com.openhis.common.enums.*;
import com.openhis.common.utils.EnumUtils;
import com.openhis.web.regdoctorstation.appservice.INurseManageService;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName NurseManageServiceImpl
 * @Description 护士站 业务实现类
 * @Author raymond
 * @Date 2025/10/27 15:26
 * @Version 1.0
 **/
@Slf4j
@Service
public class NurseManageServiceImpl implements INurseManageService {

    @Resource
    private IPractitionerRoleService practitionerRoleService;
    @Resource
    private ILocationService locationService;
    @Resource
    private LocationMapper locationMapper;
    @Resource
    private EncounterLocationMapper encounterLocationMapper;
    @Resource
    private IEncounterService encounterService;
    @Resource
    private IPatientService patientService;

    @Override
    public R<?> pricingBusiness(String searchKeyWord) {
        // TODO 一、基础数据 1、获取 当前护士负责的 病区 和 病区下的患者数据；2、护士点击 患者姓名时，获取患者基础数据（床号，姓名等）；
        // 此处应该提供两个接口，策略模式， 如果从 门诊过来，new 门诊的用户来源，如果从 住院来，那么 new 住院的用户来源；
        // 获取当前人 SecurityUtils.getLoginUser().getPractitionerId();
        Long practitionerId = 2L;
        // 获取当前角色的负责的区域id数组
        List<Long> roleLocationIds = practitionerRoleService.searchPractitionerRoleLocationIds(practitionerId,
            PractitionerRoles.NURSE.getCode());
        // 根据 roleLocationIds 获取病区集合
        List<LocationDataDto> locationAreaDataList = this.locationService.searchLocationDataByIds(roleLocationIds);
        for (LocationDataDto data : locationAreaDataList) {
            List<PatientBedInfoDto> locationBedDataList = this.exeEncapsulation(data);
            if (!StringUtils.isEmpty(searchKeyWord) && !locationBedDataList.isEmpty()) {
                List<PatientBedInfoDto> searchResultDataList = new LinkedList<>();
                for (PatientBedInfoDto bedInfoDto : locationBedDataList) {
                    // 根据 换着姓名和 患者床位号查询
                    if (bedInfoDto.getBedName().equals(searchKeyWord)
                        || bedInfoDto.getPatientName().matches(searchKeyWord)) {
                        PatientBedInfoDto searchResultData = new PatientBedInfoDto();
                        BeanUtils.copyProperties(bedInfoDto, searchResultData);
                        searchResultDataList.add(searchResultData);
                    }
                }
                data.setPatientBedInfoDtoList(searchResultDataList);
            } else {
                data.setPatientBedInfoDtoList(locationBedDataList);
            }
        }

        return R.ok(locationAreaDataList);
    }

    public List<PatientBedInfoDto> exeEncapsulation(LocationDataDto data) {
        // 根据病区 找到 有效床位的信息
        List<PatientBedInfoDto> locationBedDataList = this.locationMapper.searchLocationDataByBusNo(data.getBusNo(),
            LocationForm.BED.getValue(), LocationStatus.OCCUPY.getValue());
        List<PatientBedInfoDto> finalDataList = new LinkedList<>();
        if (!locationBedDataList.isEmpty()) {
            int size = locationBedDataList.size();
            for (int i = 0; i < size; i++) {
                PatientBedInfoDto bedInfoDto = locationBedDataList.get(i);
                PatientBedInfoDto patientBedInfoDto =
                    this.encounterLocationMapper.searchEncounterDataByLocationId(bedInfoDto.getBedLocationId(),
                        EncounterActivityStatus.ACTIVE.getValue(), LocationForm.BED.getValue());
                EncounterDataDto encounterDataDto = this.encounterService
                    .getPatientIdByParam(EncounterClass.IMP.getValue(), patientBedInfoDto.getEncounterId());
                Patient patient = this.patientService.getById(encounterDataDto.getPatientId());
                bedInfoDto.setPatientName(patient.getName());
                bedInfoDto.setPatientNumber(patient.getIdCard());
                bedInfoDto.setPatientAgeText(
                        patient.getBirthDate() != null ? AgeCalculatorUtil.getAge(patient.getBirthDate()) : null);
                bedInfoDto.setPatientGenderText(
                    EnumUtils.getInfoByValue(AdministrativeGender.class, patient.getGenderEnum()));
                finalDataList.add(bedInfoDto);
            }
        }
        return finalDataList;
    }

    @Override
    public R<?> medicalDeviceBusiness() {
        // 获取当前人
        Long practitionerId = SecurityUtils.getLoginUser().getPractitionerId();
        return null;
    }
}
