package com.openhis.administration.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openhis.administration.domain.EncounterParticipant;
import com.openhis.administration.mapper.EncounterParticipantMapper;
import com.openhis.administration.service.IEncounterParticipantService;
import com.openhis.common.enums.DelFlag;
import com.openhis.common.enums.EncounterActivityStatus;
import com.openhis.common.enums.ParticipantType;

/**
 * 就诊参与者管理Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Service
public class EncounterParticipantServiceImpl extends ServiceImpl<EncounterParticipantMapper, EncounterParticipant>
    implements IEncounterParticipantService {

    /**
     * 保存就诊参与者
     *
     * @param encounterParticipant 就诊参与者信息
     */
    @Override
    public void saveEncounterParticipant(EncounterParticipant encounterParticipant) {
        baseMapper.insert(encounterParticipant);
    }

    /**
     * 创建住院参与者
     *
     * @param encounterId 住院id
     * @param startTime 入科时间
     * @param practitionerId 参与者id
     * @param typeCode 参与者类型
     */
    @Override
    public void creatEncounterParticipants(Long encounterId, Date startTime, Long practitionerId, String typeCode) {
        EncounterParticipant encounterParticipant = new EncounterParticipant();
        encounterParticipant.setEncounterId(encounterId).setStartTime(startTime).setPractitionerId(practitionerId)
            .setTypeCode(typeCode);
        baseMapper.insert(encounterParticipant);
    }

    /**
     * 更新医疗参与者状态
     *
     * @param encounterId 住院id
     * @return 更新结果
     */
    @Override
    public Integer updateEncounterParticipantsStatus(Long encounterId) {
        // 更新状态为已完成
        return baseMapper.update(null,
            new LambdaUpdateWrapper<EncounterParticipant>()
                .set(EncounterParticipant::getStatusEnum, EncounterActivityStatus.COMPLETED.getValue())
                .eq(EncounterParticipant::getEncounterId, encounterId).in(EncounterParticipant::getTypeCode,
                    ParticipantType.ATTENDING_DOCTOR.getCode(), ParticipantType.CHIEF_DOCTOR.getCode(),
                    ParticipantType.PRIMARY_NURSE.getCode(), ParticipantType.ADMITTING_DOCTOR.getCode()));
    }

    /*
     * 根据就诊id查询参与者
     * 
     * @param encounterId
     * @return
     */
    @Override
    public List<EncounterParticipant> getEncounterParticipantList(Long encounterId) {
        return baseMapper.selectList(
            new LambdaQueryWrapper<EncounterParticipant>().eq(EncounterParticipant::getEncounterId, encounterId)
                .eq(EncounterParticipant::getStatusEnum, EncounterActivityStatus.ACTIVE.getValue())
                .eq(EncounterParticipant::getDeleteFlag, DelFlag.NO.getCode()));
    }

    /**
     * 查询某一个类型的参与者
     * 
     * @param encounterId
     * @param participantType
     * @return
     */
    @Override
    public List<EncounterParticipant> getEncounterParticipantListByTypeCode(Long encounterId,
        ParticipantType participantType) {
        List<EncounterParticipant> list = new ArrayList<>();
        for (EncounterParticipant encounterParticipant : getEncounterParticipantList(encounterId)) {
            if (participantType.getCode().equals(encounterParticipant.getTypeCode())) {
                list.add(encounterParticipant);
            }
        }
        return list;
    }
}