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

import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.manage.domain.patientnodeparamscurrent.PatientNodeParamsCurrent;
import com.xinelu.manage.domain.patientnodeparamslog.PatientNodeParamsLog;
import com.xinelu.manage.mapper.patientnodeparamslcurrent.PatientNodeParamsCurrentMapper;
import com.xinelu.manage.mapper.patientnodeparamslog.PatientNodeParamsLogMapper;
import com.xinelu.manage.service.patientnodeparamscurrent.IPatientNodeParamsCurrentService;
import com.xinelu.manage.service.signpatientmanageroutenode.ISignPatientManageRouteNodeService;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 患者节点参数Service业务层处理
 *
 * @author haown
 * @date 2024-05-20
 */
@Service
public class PatientNodeParamsCurrentServiceImpl implements IPatientNodeParamsCurrentService {
    @Resource
    private PatientNodeParamsCurrentMapper patientNodeParamsCurrentMapper;
	@Resource
	private PatientNodeParamsLogMapper patientNodeParamsLogMapper;
	@Resource
	private ISignPatientManageRouteNodeService manageRouteNodeService;

    /**
     * 查询患者节点参数
     *
     * @param id 患者节点参数主键
     * @return 患者节点参数
     */
    @Override
    public PatientNodeParamsCurrent selectPatientNodeParamsCurrentById(Long id) {
        return patientNodeParamsCurrentMapper.selectPatientNodeParamsCurrentById(id);
    }

    /**
     * 查询患者节点参数列表
     *
     * @param patientNodeParamsCurrent 患者节点参数
     * @return 患者节点参数
     */
    @Override
    public List<PatientNodeParamsCurrent> selectPatientNodeParamsCurrentList(PatientNodeParamsCurrent patientNodeParamsCurrent) {
        return patientNodeParamsCurrentMapper.selectPatientNodeParamsCurrentList(patientNodeParamsCurrent);
    }

    /**
     * 新增患者节点参数
     *
     * @param patientNodeParamsCurrent 患者节点参数
     * @return 结果
     */
    @Override
    public int insertPatientNodeParamsCurrent(PatientNodeParamsCurrent patientNodeParamsCurrent) {
                patientNodeParamsCurrent.setCreateTime(LocalDateTime.now());
            return patientNodeParamsCurrentMapper.insertPatientNodeParamsCurrent(patientNodeParamsCurrent);
    }

    /**
     * 修改患者节点参数
     *
     * @param patientNodeParamsCurrent 患者节点参数
     * @return 结果
     */
    @Override
    public int updatePatientNodeParamsCurrent(PatientNodeParamsCurrent patientNodeParamsCurrent) {
                patientNodeParamsCurrent.setUpdateTime(LocalDateTime.now());
        return patientNodeParamsCurrentMapper.updatePatientNodeParamsCurrent(patientNodeParamsCurrent);
    }

    /**
     * 批量删除患者节点参数
     *
     * @param ids 需要删除的患者节点参数主键
     * @return 结果
     */
    @Override
    public int deletePatientNodeParamsCurrentByIds(Long[] ids) {
        return patientNodeParamsCurrentMapper.deletePatientNodeParamsCurrentByIds(ids);
    }

    /**
     * 删除患者节点参数信息
     *
     * @param id 患者节点参数主键
     * @return 结果
     */
    @Override
    public int deletePatientNodeParamsCurrentById(Long id) {
        return patientNodeParamsCurrentMapper.deletePatientNodeParamsCurrentById(id);
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertList(List<List<PatientNodeParamsCurrent>> paramsList) {
		if (CollectionUtils.isEmpty(paramsList)) {
			throw new ServiceException("传输数据错误");
		}
		Long patientId = paramsList.get(0).get(0).getPatientId();
		if (patientId == null) {
			throw new ServiceException("患者信息传输错误");
		}
		Long routeNodeId = paramsList.get(0).get(0).getRouteNodeId();
		if (routeNodeId == null) {
			throw new ServiceException("路径节点传输错误");
		}
		// 多个list组合成一个参数list
		List<PatientNodeParamsCurrent> logList = new ArrayList<>();
		List<PatientNodeParamsCurrent> saveList = new ArrayList<>();
		List<PatientNodeParamsCurrent> updList = new ArrayList<>();
		for (List<PatientNodeParamsCurrent> paramList : paramsList) {
			paramList.forEach(param-> {
				if (param.getId() == null) {
					param.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
					param.setCreateTime(LocalDateTime.now());
					saveList.add(param);
				} else {
					param.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
					param.setUpdateTime(LocalDateTime.now());
					updList.add(param);
				}
				logList.add(param);
			});
		}

		PatientNodeParamsCurrent paramsCurrentQuery = new PatientNodeParamsCurrent();
		paramsCurrentQuery.setPatientId(patientId);
		paramsCurrentQuery.setRouteNodeId(routeNodeId);
		List<PatientNodeParamsCurrent> paramsOldList = patientNodeParamsCurrentMapper.selectPatientNodeParamsCurrentList(paramsCurrentQuery);
		List<Long> deleteIds = paramsOldList.stream().map(PatientNodeParamsCurrent::getId).collect(Collectors.toList());

		if (CollectionUtils.isNotEmpty(updList)) {
			// 查询是否有删除的参数
			updList.forEach(updParam -> {
				patientNodeParamsCurrentMapper.updatePatientNodeParamsCurrent(updParam);
				deleteIds.remove(updParam.getId());
			});
		}
		// 删除参数
		if (CollectionUtils.isNotEmpty(deleteIds)) {
			patientNodeParamsCurrentMapper.deletePatientNodeParamsCurrentByIds(deleteIds.toArray(new Long[0]));
		}
		// 新增的参数保存
		if (CollectionUtils.isNotEmpty(saveList)) {
			patientNodeParamsCurrentMapper.insertList(saveList);
		}
		// 保存参数日志表
		List<PatientNodeParamsLog> paramsLogList = new ArrayList<>();
		logList.forEach(paramCurrent -> {
			PatientNodeParamsLog paramsLog = new PatientNodeParamsLog();
			BeanUtils.copyBeanProp(paramsLog, paramCurrent);
			paramsLog.setId(null);
			paramsLog.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
			paramsLog.setCreateTime(LocalDateTime.now());
			paramsLogList.add(paramsLog);
		});
		patientNodeParamsLogMapper.insertList(paramsLogList);
		// 生成任务---判断是否为主路径，主路径直接生成任务，子路径触发条件成立则生成任务
		manageRouteNodeService.updateTaskWithParam(paramsList);
	}
}
