package com.xinelu.manage.service.screeningproject.impl;

import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.manage.domain.hospitalinfo.HospitalInfo;
import com.xinelu.manage.domain.hospitalpersoninfo.HospitalPersonInfo;
import com.xinelu.manage.domain.screeningproject.ScreeningProject;
import com.xinelu.manage.mapper.hospitalinfo.HospitalInfoMapper;
import com.xinelu.manage.mapper.hospitalpersoninfo.HospitalPersonInfoMapper;
import com.xinelu.manage.mapper.screeningproject.ScreeningProjectMapper;
import com.xinelu.manage.service.screeningproject.IScreeningProjectService;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @description: 项目管理实现类
 * @author: haown
 * @create: 2023-01-16 15:30
 **/
@Service
public class ScreeningProjectServiceImpl implements IScreeningProjectService {

	@Resource
	private ScreeningProjectMapper projectMapper;
	@Resource
	private HospitalPersonInfoMapper hospitalPersonInfoMapper;
	@Resource
	private HospitalInfoMapper hospitalInfoMapper;

	@Override public List<ScreeningProject> findList(ScreeningProject project) {
		return projectMapper.findList(project);
	}

	@Override public int insert(ScreeningProject project) {
		if (project.getHospitalId() == null) {
			HospitalPersonInfo hospitalPersonInfo = hospitalPersonInfoMapper.selectHospitalPersonInfoById(SecurityUtils.getLoginUser().getUser().getHospitalPersonId());
			if (hospitalPersonInfo != null) {
				project.setHospitalId(hospitalPersonInfo.getHospitalId());
				HospitalInfo hospital = hospitalInfoMapper.selectHospitalInfoById(hospitalPersonInfo.getHospitalId());
				if (hospital != null) {
					project.setHospitalName(hospital.getHospitalName());
				}
			}
		}
		return projectMapper.insertSelective(project);
	}

	@Override
	public int update(ScreeningProject project) {
		if (project.getHospitalId() == null) {
			HospitalPersonInfo hospitalPersonInfo = hospitalPersonInfoMapper.selectHospitalPersonInfoById(SecurityUtils.getLoginUser().getUser().getHospitalPersonId());
			if (hospitalPersonInfo != null) {
				project.setHospitalId(hospitalPersonInfo.getHospitalId());
				HospitalInfo hospital = hospitalInfoMapper.selectHospitalInfoById(hospitalPersonInfo.getHospitalId());
				if (hospital != null) {
					project.setHospitalName(hospital.getHospitalName());
				}
			}
		}
		return projectMapper.updateByPrimaryKey(project);
	}

	@Override public void delete(ScreeningProject project) {
		projectMapper.updateByPrimaryKeySelective(project);
	}

	@Override public boolean checkSameProjectName(String projectName, Long hospitalId, String projectId) {
		return projectMapper.checkSameName(projectName, hospitalId, projectId) != null;
	}
}
