package com.jojubanking.boot.module.system.service.doctor;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.jojubanking.boot.module.system.controller.admin.doctor.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.doctor.DoctorDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.doctor.DoctorConvert;
import com.jojubanking.boot.module.system.dal.mysql.doctor.DoctorMapper;

import static com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jojubanking.boot.module.system.enums.ErrorCodeConstants.*;

/**
 * 医生信息 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class DoctorServiceImpl implements DoctorService {

    @Resource
    private DoctorMapper doctorMapper;

    @Override
    public Integer createDoctor(DoctorCreateReqVO createReqVO) {
        // 插入
        DoctorDO doctor = DoctorConvert.INSTANCE.convert(createReqVO);
        doctorMapper.insert(doctor);
        // 返回
        return doctor.getId();
    }

    @Override
    public void updateDoctor(DoctorUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateDoctorExists(updateReqVO.getId());
        // 更新
        DoctorDO updateObj = DoctorConvert.INSTANCE.convert(updateReqVO);
        doctorMapper.updateById(updateObj);
    }

    @Override
    public void deleteDoctor(Integer id) {
        // 校验存在
        this.validateDoctorExists(id);
        // 删除
        doctorMapper.deleteById(id);
    }

    private void validateDoctorExists(Integer id) {
        if (doctorMapper.selectById(id) == null) {
            throw exception(DOCTOR_NOT_EXISTS);
        }
    }

    @Override
    public DoctorDO getDoctor(Integer id) {
        return doctorMapper.selectById(id);
    }

    @Override
    public List<DoctorDO> getDoctorList(Collection<Integer> ids) {
        return doctorMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DoctorDO> getDoctorPage(DoctorPageReqVO pageReqVO) {
        return doctorMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DoctorDO> getDoctorList(DoctorExportReqVO exportReqVO) {
        return doctorMapper.selectList(exportReqVO);
    }

}
