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.EncounterLocation;
import com.openhis.administration.mapper.EncounterLocationMapper;
import com.openhis.administration.service.IEncounterLocationService;
import com.openhis.common.enums.DelFlag;
import com.openhis.common.enums.EncounterActivityStatus;
import com.openhis.common.enums.LocationForm;

/**
 * 就诊位置管理Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Service
public class EncounterLocationServiceImpl extends ServiceImpl<EncounterLocationMapper, EncounterLocation>
    implements IEncounterLocationService {

    /**
     * 保存住院位置信息
     *
     * @param encounterLocation 住院位置信息
     */
    @Override
    public void saveEncounterLocation(EncounterLocation encounterLocation) {
        baseMapper.insert(encounterLocation);
    }

    /**
     * 自定义插入或更新方法,根据就诊id，病区,判断是更新还是插入
     * 
     * @param encounterLocation 实体对象
     * @return 是否成功
     */
    public boolean saveOrUpdateEncounterLocation(EncounterLocation encounterLocation) {
        // 创建 LambdaQueryWrapper
        LambdaQueryWrapper<EncounterLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EncounterLocation::getEncounterId, encounterLocation.getEncounterId())
            .eq(EncounterLocation::getFormEnum, encounterLocation.getFormEnum())
            // 状态为使用中
            .eq(EncounterLocation::getStatusEnum, EncounterActivityStatus.ACTIVE.getValue());

        // 查询是否存在记录
        EncounterLocation existingRecord = baseMapper.selectOne(queryWrapper);
        if (existingRecord != null) {
            // 如果记录存在，更新记录
            encounterLocation.setId(existingRecord.getId());
            return baseMapper.updateById(encounterLocation) > 0;
        } else {
            // 如果记录不存在，插入新记录
            return baseMapper.insert(encounterLocation) > 0;
        }
    }

    /**
     * 创建住院位置
     *
     * @param encounterId 住院id
     * @param startTime 开始时间
     * @param locationId 位置id
     * @param locationForm 位置类型
     */
    public void creatEncounterLocation(Long encounterId, Date startTime, Long locationId, Integer locationForm) {
        EncounterLocation encounterLocation = new EncounterLocation();
        encounterLocation.setEncounterId(encounterId).setStartTime(startTime).setLocationId(locationId)
            .setFormEnum(locationForm);
        baseMapper.insert(encounterLocation);
    }

    /**
     * 查询住院患者床位相关信息
     *
     * @param encounterId 住院id
     * @return 床位相关信息
     */
    @Override
    public List<EncounterLocation> getEncounterLocationInfo(Long encounterId, Long locationId) {
        LambdaQueryWrapper<EncounterLocation> queryWrapper = new LambdaQueryWrapper<EncounterLocation>()
            .eq(EncounterLocation::getStatusEnum, EncounterActivityStatus.ACTIVE.getValue())
            .eq(EncounterLocation::getDeleteFlag, DelFlag.NO.getCode());
        if (locationId != null) {
            queryWrapper.eq(EncounterLocation::getLocationId, locationId).eq(EncounterLocation::getFormEnum,
                LocationForm.BED.getValue());
        }
        if (encounterId != null) {
            queryWrapper.eq(EncounterLocation::getEncounterId, encounterId).in(EncounterLocation::getFormEnum,
                LocationForm.BED.getValue(), LocationForm.HOUSE.getValue());
        }
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 更新位置使用状态
     *
     * @param encounterId 住院id
     * @param isTransfer 是否专科
     * @return 更新结果
     */
    @Override
    public Integer updateEncounterLocationStatus(Long encounterId, boolean isTransfer) {
        // 动态拼接病区条件
        List<Integer> locationForms = new ArrayList<>();
        locationForms.add(LocationForm.BED.getValue());
        locationForms.add(LocationForm.HOUSE.getValue());
        if (isTransfer) {
            locationForms.add(LocationForm.WARD.getValue());
        }
        // 更新状态为已完成
        return baseMapper.update(null,
            new LambdaUpdateWrapper<EncounterLocation>()
                .set(EncounterLocation::getStatusEnum, EncounterActivityStatus.COMPLETED.getValue())
                .eq(EncounterLocation::getEncounterId, encounterId).in(EncounterLocation::getFormEnum, locationForms));
    }

    /*
     * 根据就诊id查询就诊位置信息
     * 
     * @param encounterId 就诊id
     * @return 就诊位置集合
     */
    @Override
    public List<EncounterLocation> getEncounterLocationList(Long encounterId) {
        return baseMapper
            .selectList(new LambdaQueryWrapper<EncounterLocation>().eq(EncounterLocation::getEncounterId, encounterId));
    }

    /**
     * 根据就诊id查询就诊位置信息(某个类型)(某个状态)
     * 
     * @param locationForm 类型
     * @param encounterActivityStatus 状态
     * @param encounterId 就诊id
     * @return 就诊位置集合
     */
    @Override
    public List<EncounterLocation> getEncounterLocationList(Long encounterId, LocationForm locationForm,
        EncounterActivityStatus encounterActivityStatus) {
        LambdaQueryWrapper<EncounterLocation> queryWrapper =
            new LambdaQueryWrapper<EncounterLocation>().eq(EncounterLocation::getEncounterId, encounterId)
                .eq(EncounterLocation::getDeleteFlag, DelFlag.NO.getCode());

        if (locationForm != null) {
            queryWrapper.eq(EncounterLocation::getFormEnum, locationForm.getCode());
        }

        if (encounterActivityStatus != null) {
            queryWrapper.eq(EncounterLocation::getStatusEnum, encounterActivityStatus.getCode());
        }
        return baseMapper.selectList(queryWrapper);
    }
}