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

import com.xinelu.common.exception.ServiceException;
import com.xinelu.manage.domain.patientinfo.PatientInfo;
import com.xinelu.manage.domain.signpatientmanageroute.SignPatientManageRoute;
import com.xinelu.manage.domain.signpatientmanageroutenode.SignPatientManageRouteNode;
import com.xinelu.manage.dto.signpatientmanageroutenode.PatientTaskDto;
import com.xinelu.manage.dto.signpatientmanageroutenode.RouteNodeCheckDto;
import com.xinelu.manage.mapper.patientinfo.PatientInfoMapper;
import com.xinelu.manage.mapper.signpatientmanageroute.SignPatientManageRouteMapper;
import com.xinelu.manage.mapper.signpatientmanageroutenode.SignPatientManageRouteNodeMapper;
import com.xinelu.manage.service.signpatientmanageroutenode.ISignPatientManageRouteNodeService;
import com.xinelu.manage.vo.signpatientmanageroutenode.PatientTaskVo;
import com.xinelu.manage.vo.signpatientmanageroutenode.SignPatientManageRouteNodeVo;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 签约患者管理任务路径节点Service业务层处理
 *
 * @author haown
 * @date 2024-03-18
 */
@Service
public class SignPatientManageRouteNodeServiceImpl implements ISignPatientManageRouteNodeService {
    @Resource
    private SignPatientManageRouteNodeMapper signPatientManageRouteNodeMapper;
	@Resource
	private PatientInfoMapper patientInfoMapper;
	@Resource
	private SignPatientManageRouteMapper signRouteMapper;

    /**
     * 查询签约患者管理任务路径节点
     *
     * @param id 签约患者管理任务路径节点主键
     * @return 签约患者管理任务路径节点
     */
    @Override
    public SignPatientManageRouteNode selectSignPatientManageRouteNodeById(Long id) {
        return signPatientManageRouteNodeMapper.selectSignPatientManageRouteNodeById(id);
    }

	@Override public List<SignPatientManageRouteNode> getNodeList(PatientTaskDto patientTaskDto) {
		if (patientTaskDto.getPatientId() == null) {
			throw new ServiceException("患者信息有误");
		}
		return signPatientManageRouteNodeMapper.getNodeList(patientTaskDto);
	}

	@Override public List<SignPatientManageRouteNodeVo> getRouteNodeList(PatientTaskDto patientTaskDto) {
		List<SignPatientManageRouteNodeVo> retList = new ArrayList<>();
		PatientInfo patientInfo = patientInfoMapper.selectPatientInfoById(patientTaskDto.getPatientId());
		if (patientTaskDto.getPatientId() == null || ObjectUtils.isEmpty(patientInfo)) {
			throw new ServiceException("请选择患者!");
		}
		// 查询任务列表
		SignPatientManageRoute signPatientManageRoute = new SignPatientManageRoute();
		signPatientManageRoute.setSignPatientRecordId(patientTaskDto.getSignPatientRecordId());
		signPatientManageRoute.setPatientId(patientTaskDto.getPatientId());
		signPatientManageRoute.setTaskCreateType(patientTaskDto.getTaskCreateType());
		List<SignPatientManageRoute> signRoutes = signRouteMapper.selectSignPatientManageRouteList(signPatientManageRoute);
		for (SignPatientManageRoute route : signRoutes) {
			SignPatientManageRouteNode nodeQuery = new SignPatientManageRouteNode();
			nodeQuery.setManageRouteId(route.getId());
			List<SignPatientManageRouteNode> nodeList = signPatientManageRouteNodeMapper.selectSignPatientManageRouteNodeList(nodeQuery);
			retList.add(SignPatientManageRouteNodeVo.builder().manageRouteId(route.getId())
				.routeName(route.getRouteName())
				.taskCreateType(route.getTaskCreateType())
				.suitRange(route.getSuitRange())
				.nodeList(nodeList).build());
		}

		return retList;
	}

	/**
     * 查询签约患者管理任务路径节点列表
     *
     * @param signPatientManageRouteNode 签约患者管理任务路径节点
     * @return 签约患者管理任务路径节点
     */
    @Override
    public List<SignPatientManageRouteNode> selectSignPatientManageRouteNodeList(SignPatientManageRouteNode signPatientManageRouteNode) {
        return signPatientManageRouteNodeMapper.selectSignPatientManageRouteNodeList(signPatientManageRouteNode);
    }

    /**
     * 新增签约患者管理任务路径节点
     *
     * @param signPatientManageRouteNode 签约患者管理任务路径节点
     * @return 结果
     */
    @Override
    public int insertSignPatientManageRouteNode(SignPatientManageRouteNode signPatientManageRouteNode) {
        signPatientManageRouteNode.setCreateTime(LocalDateTime.now());
        return signPatientManageRouteNodeMapper.insertSignPatientManageRouteNode(signPatientManageRouteNode);
    }

    /**
     * 修改签约患者管理任务路径节点
     *
     * @param signPatientManageRouteNode 签约患者管理任务路径节点
     * @return 结果
     */
    @Override
    public int updateSignPatientManageRouteNode(SignPatientManageRouteNode signPatientManageRouteNode) {
        signPatientManageRouteNode.setUpdateTime(LocalDateTime.now());
        return signPatientManageRouteNodeMapper.updateSignPatientManageRouteNode(signPatientManageRouteNode);
    }

    /**
     * 批量删除签约患者管理任务路径节点
     *
     * @param ids 需要删除的签约患者管理任务路径节点主键
     * @return 结果
     */
    @Override
    public int deleteSignPatientManageRouteNodeByIds(Long[] ids) {
        return signPatientManageRouteNodeMapper.deleteSignPatientManageRouteNodeByIds(ids);
    }

    /**
     * 删除签约患者管理任务路径节点信息
     *
     * @param id 签约患者管理任务路径节点主键
     * @return 结果
     */
    @Override
    public int deleteSignPatientManageRouteNodeById(Long id) {
        return signPatientManageRouteNodeMapper.deleteSignPatientManageRouteNodeById(id);
    }

	@Override public List<PatientTaskVo> selectPatientTaskList(PatientTaskDto patientTaskDto) {
		return signPatientManageRouteNodeMapper.selectPatientTaskList(patientTaskDto);
	}

	/**
	 * 审核任务
	 * @param routeNodeCheckDto 任务审核传输对象
	 * @return 结果
	 */
	@Override
	@Transactional
	public int audit(RouteNodeCheckDto routeNodeCheckDto) {
		int flag = 0;
		// 根据manageRouteId查询所有节点
		SignPatientManageRouteNode nodeQuery = new SignPatientManageRouteNode();
		nodeQuery.setManageRouteId(routeNodeCheckDto.getManageRouteId());
		List<SignPatientManageRouteNode> nodeList = signPatientManageRouteNodeMapper.selectSignPatientManageRouteNodeList(nodeQuery);
		for (SignPatientManageRouteNode node : nodeList) {
			node.setRouteCheckStatus(routeNodeCheckDto.getRouteCheckStatus());
			node.setRouteCheckPerson(routeNodeCheckDto.getRouteCheckPerson());
			node.setRouteCheckDate(LocalDateTime.now());
			node.setRouteCheckRemark(routeNodeCheckDto.getRouteCheckRemark());
			flag += signPatientManageRouteNodeMapper.updateSignPatientManageRouteNode(node);
		}
		return flag;
	}
}
