package com.nursinghome.service;

import com.nursinghome.common.ApiException;
import com.nursinghome.entity.Elder;
import com.nursinghome.mapper.ElderMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ElderService {
    private final ElderMapper elderMapper;

    public ElderService(ElderMapper elderMapper) {
        this.elderMapper = elderMapper;
    }

    public List<Elder> listAll() {
        return elderMapper.listAll();
    }

    public Elder findById(Long id) {
        return elderMapper.findById(id);
    }

    public Elder findByIdCard(String idCard) {
        return elderMapper.findByIdCard(idCard);
    }

    public Elder create(Elder elder) {
        if (elderMapper.findByIdCard(elder.getIdCard()) != null) {
            throw new ApiException("身份证号已存在");
        }
        elderMapper.insert(elder);
        return elder;
    }

    public void update(Elder elder) {
        elderMapper.update(elder);
    }

    public void delete(Long id) {
        elderMapper.delete(id);
    }
}
