package com.openhis.document.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openhis.document.domain.VitalSigns;
import com.openhis.document.mapper.VitalSignsMapper;
import com.openhis.document.service.IVitalSignsService;

/**
 * 就诊位置管理Service业务层处理
 *
 * @author system
 * @date 2025-06-03
 */
@Service
public class VitalSignsServiceImpl extends ServiceImpl<VitalSignsMapper, VitalSigns> implements IVitalSignsService {

    /**
     * 查询同个时间点是否存在数据
     *
     * @param vitalSigns 实体对象
     * @return vitalSigns
     */
    public VitalSigns selectVitalSigns(VitalSigns vitalSigns) {
        // 查询是否存在记录
        return baseMapper.selectOne(this.queryWrapperSet(vitalSigns));
    }

    /**
     * 查询当前list中的数据是否在表中存在
     *
     * @param vitalSignsList 实体对象
     * @return 是否存在
     */
    public boolean selectBatchVitalSigns(List<VitalSigns> vitalSignsList) {
        boolean exitFlag = false;
        for (VitalSigns vitalSigns : vitalSignsList) {
            // 查询是否存在记录
            long count = baseMapper.selectCount(this.queryWrapperSet(vitalSigns));
            // 存在的场合
            if (count > 0) {
                exitFlag = true;
            }
        }
        return exitFlag;
    }

    /**
     * 自定义插入或更新方法,根据就诊ID，记录时间，生命体征值,判断是更新还是插入
     *
     * @param vitalSigns 实体对象
     * @return 是否成功
     */
    public boolean saveOrUpdateVitalSigns(VitalSigns vitalSigns) {

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

    /**
     * 根据就诊ID，记录时间，生命体征值，判断批量保存或者更新
     *
     * @param vitalSignsList 实体对象
     * @return 是否成功
     */
    public boolean saveOrUpdateBatchVitalSigns(List<VitalSigns> vitalSignsList) {
        boolean saveOrUpdateFlag = false;
        for (VitalSigns vitalSigns : vitalSignsList) {
            // 判断是更新还是插入
            saveOrUpdateFlag = this.saveOrUpdateVitalSigns(vitalSigns);
        }
        return saveOrUpdateFlag;
    }

    /**
     * 删除方法
     *
     * @param vitalSigns 实体对象
     * @return 是否成功
     */
    public boolean deleteVitalSigns(VitalSigns vitalSigns) {

        // 查询是否存在记录
        VitalSigns existingRecord = baseMapper.selectOne(this.queryWrapperSet(vitalSigns));
        if (existingRecord != null) {
            // 删除记录
            return baseMapper.deleteById(existingRecord) > 0;
        } else {
            // 如果记录不存在，返回false
            return false;
        }
    }

    /**
     * 检索条件设定
     *
     * @param vitalSigns 实体对象
     * @return 是否成功
     */
    private LambdaQueryWrapper queryWrapperSet(VitalSigns vitalSigns) {
        // 创建 LambdaQueryWrapper
        LambdaQueryWrapper<VitalSigns> queryWrapper = new LambdaQueryWrapper<>();
        // 有效性检查
        // 患者ID
        if (vitalSigns.getPatientId() != null) {
            queryWrapper.eq(VitalSigns::getPatientId, vitalSigns.getPatientId());
        }
        // 就诊ID
        if (vitalSigns.getEncounterId() != null) {
            queryWrapper.eq(VitalSigns::getEncounterId, vitalSigns.getEncounterId());
        }
        // 记录日期
        if (vitalSigns.getRecordingDate() != null) {
            queryWrapper.eq(VitalSigns::getRecordingDate, vitalSigns.getRecordingDate());
        }
        // 记录时间
        if (vitalSigns.getTimePoint() != null) {
            queryWrapper.eq(VitalSigns::getTimePoint, vitalSigns.getTimePoint());
        }
        // 生命体征编码
        if (vitalSigns.getVitalSignsCode() != null) {
            queryWrapper.eq(VitalSigns::getVitalSignsCode, vitalSigns.getVitalSignsCode());
        }
        return queryWrapper;
    }
}