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

import com.xinelu.common.constant.DeviceStatusConstants;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.manage.domain.projectdevice.ProjectDevice;
import com.xinelu.manage.mapper.projectdevice.ProjectDeviceMapper;
import com.xinelu.manage.service.projectdevice.IProjectDeviceService;
import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

/**
 * 检测项目设备Service业务层处理
 *
 * @author haown
 * @date 2024-03-11
 */
@Service
public class ProjectDeviceServiceImpl implements IProjectDeviceService {
    @Resource
    private ProjectDeviceMapper projectDeviceMapper;

    /**
     * 查询检测项目设备
     *
     * @param id 检测项目设备主键
     * @return 检测项目设备
     */
    @Override
    public ProjectDevice selectProjectDeviceById(Long id) {
        return projectDeviceMapper.selectProjectDeviceById(id);
    }

    /**
     * 查询检测项目设备列表
     *
     * @param projectDevice 检测项目设备
     * @return 检测项目设备
     */
    @Override
    public List<ProjectDevice> selectProjectDeviceList(ProjectDevice projectDevice) {
        return projectDeviceMapper.selectProjectDeviceList(projectDevice);
    }

    /**
     * 新增检测项目设备
     *
     * @param projectDevice 检测项目设备
     * @return 结果
     */
    @Override
    public int insertProjectDevice(ProjectDevice projectDevice) {
		// 查询居民是否绑定过相同类型的设备
	    if (StringUtils.isBlank(projectDevice.getDeviceType())) {
			throw new ServiceException("请选择设备类型");
	    }
	    ProjectDevice query = new ProjectDevice();
		query.setPatientId(projectDevice.getPatientId());
		query.setDeviceType(projectDevice.getDeviceType());
	    query.setDeviceStatus(DeviceStatusConstants.BIND);
	    List<ProjectDevice> deviceList = projectDeviceMapper.selectProjectDeviceList(query);
		if (CollectionUtils.isNotEmpty(deviceList)) {
			throw new ServiceException("该居民已绑定过相同设备类型的设备，请解绑后重新绑定");
		}
	    // 根据设备编码查询是否已被绑定
	    query.setPatientId(null);
	    query.setDeviceCode(projectDevice.getDeviceCode());
	    deviceList = projectDeviceMapper.selectProjectDeviceList(query);
	    if (CollectionUtils.isNotEmpty(deviceList)) {
		    throw new ServiceException("该设备已被绑定，请解绑后重新绑定");
	    }
	    projectDevice.setDeviceStatus(DeviceStatusConstants.BIND);
	    projectDevice.setDeviceBindTime(LocalDateTime.now());
	    projectDevice.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        projectDevice.setCreateTime(LocalDateTime.now());
        return projectDeviceMapper.insertProjectDevice(projectDevice);
    }

    /**
     * 修改检测项目设备
     *
     * @param projectDevice 检测项目设备
     * @return 结果
     */
    @Override
    public int updateProjectDevice(ProjectDevice projectDevice) {
	    projectDevice.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        projectDevice.setUpdateTime(LocalDateTime.now());
        return projectDeviceMapper.updateProjectDevice(projectDevice);
    }

    /**
     * 批量删除检测项目设备
     *
     * @param ids 需要删除的检测项目设备主键
     * @return 结果
     */
    @Override
    public int deleteProjectDeviceByIds(Long[] ids) {
        return projectDeviceMapper.deleteProjectDeviceByIds(ids);
    }

    /**
     * 删除检测项目设备信息
     *
     * @param id 检测项目设备主键
     * @return 结果
     */
    @Override
    public int deleteProjectDeviceById(Long id) {
        return projectDeviceMapper.deleteProjectDeviceById(id);
    }
}
