package com.openhis.administration.service.impl;

import java.util.List;

import com.openhis.common.enums.DelFlag;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openhis.administration.domain.Practitioner;
import com.openhis.administration.mapper.PractitionerMapper;
import com.openhis.administration.service.IPractitionerService;

/**
 * 医疗参与者管理Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Service
public class PractitionerServiceImpl extends ServiceImpl<PractitionerMapper, Practitioner>
    implements IPractitionerService {

    /**
     * 根据执行人ID查询
     *
     * @param userId 系统用户id
     * @return 医疗参与者管理实体
     */
    @Override
    public Practitioner getPractitionerByUserId(long userId) {

        QueryWrapper<Practitioner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("create_time"); // 按创建时间倒序，取最新的一条
        queryWrapper.last("LIMIT 1"); // 限制只返回一条
        List<Practitioner> list = baseMapper.selectList(queryWrapper);
        return list != null && !list.isEmpty() ? list.get(0) : null;
    }

    /**
     * 查询医疗参与者列表
     *
     * @return 医疗参与者列表
     */
    @Override
    public List<Practitioner> getList() {
        return baseMapper.selectList(new LambdaQueryWrapper<>());
    }

    /**
     * 根据id查询参与者集合
     * 
     * @param collect id集合
     * @return 参与者集合
     */
    @Override
    public List<Practitioner> getPractitionerListById(List<Long> collect) {
        return baseMapper.selectList(new LambdaQueryWrapper<Practitioner>().in(Practitioner::getId, collect)
            .eq(Practitioner::getDeleteFlag, DelFlag.NO.getCode()));
    }
}