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

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.hospital.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.hospital.HospitalDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.hospital.HospitalConvert;
import com.jojubanking.boot.module.system.dal.mysql.hospital.HospitalMapper;

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 HospitalServiceImpl implements HospitalService {

    @Resource
    private HospitalMapper hospitalMapper;

    @Override
    public Integer createHospital(HospitalCreateReqVO createReqVO) {
        // 插入
        HospitalDO hospital = HospitalConvert.INSTANCE.convert(createReqVO);
        hospitalMapper.insert(hospital);
        // 返回
        return hospital.getId();
    }

    @Override
    public void updateHospital(HospitalUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateHospitalExists(updateReqVO.getId());
        // 更新
        HospitalDO updateObj = HospitalConvert.INSTANCE.convert(updateReqVO);
        hospitalMapper.updateById(updateObj);
    }

    @Override
    public void deleteHospital(Integer id) {
        // 校验存在
        this.validateHospitalExists(id);
        // 删除
        hospitalMapper.deleteById(id);
    }

    private void validateHospitalExists(Integer id) {
        if (hospitalMapper.selectById(id) == null) {
            throw exception(HOSPITAL_NOT_EXISTS);
        }
    }

    @Override
    public HospitalDO getHospital(Integer id) {
        return hospitalMapper.selectById(id);
    }

    @Override
    public List<HospitalDO> getHospitalList(Collection<Integer> ids) {
        return hospitalMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<HospitalDO> getHospitalPage(HospitalPageReqVO pageReqVO) {
        return hospitalMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HospitalDO> getHospitalList(HospitalExportReqVO exportReqVO) {
        return hospitalMapper.selectList(exportReqVO);
    }

}
