package com.xinelu.quartz.task;

import com.xinelu.common.constant.SignRecordServiceStatusConstants;
import com.xinelu.common.constant.TaskCreateTypeConstant;
import com.xinelu.common.enums.RouteCheckStatusEnum;
import com.xinelu.manage.domain.signpatientmanageroute.SignPatientManageRoute;
import com.xinelu.manage.domain.specialdiseaseroute.SpecialDiseaseRoute;
import com.xinelu.manage.dto.signpatientrecord.SignPatientListDto;
import com.xinelu.manage.mapper.signpatientmanageroute.SignPatientManageRouteMapper;
import com.xinelu.manage.mapper.signpatientrecord.SignPatientRecordMapper;
import com.xinelu.manage.mapper.specialdiseaseroute.SpecialDiseaseRouteMapper;
import com.xinelu.manage.service.signpatientmanageroutenode.ISignPatientManageRouteNodeService;
import com.xinelu.manage.vo.signpatientrecord.SignPatientListVo;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

/**
 * @description: 根据触发条件生成子路径任务
 * @author: haown
 * @create: 2024-06-28 14:27
 **/
@Slf4j
@Component("GenerateChildRouteTask")
public class GenerateChildRouteTask {

	@Resource
	private SignPatientRecordMapper signPatientRecordMapper;
	@Resource
	private SignPatientManageRouteMapper signPatientManageRouteMapper;
	@Resource
	private SpecialDiseaseRouteMapper specialDiseaseRouteMapper;
	@Resource
	private ISignPatientManageRouteNodeService signPatientManageRouteNodeService;

	public void GenerateChildRouteTask() {
		log.info("开始执行生成子路径任务定时任务......");
		// 查询画像和路径都审核通过的在签患者
		SignPatientListDto signPatientRecord = new SignPatientListDto();
		signPatientRecord.setRouteCheckStatus(RouteCheckStatusEnum.AGREE.getInfo());
		signPatientRecord.setServiceStatus(SignRecordServiceStatusConstants.SERVICE_CENTER);
		List<SignPatientListVo> signPatientList = signPatientRecordMapper.selectList(signPatientRecord);
		// 查询患者签约专病路径包含的子路径id
		signPatientList.forEach(signPatient -> {
			// 查询签约路径
			SignPatientManageRoute manageRouteQuery = new SignPatientManageRoute();
			manageRouteQuery.setSignPatientRecordId(signPatient.getId());
			manageRouteQuery.setTaskCreateType(TaskCreateTypeConstant.MANUAL_MATCHE);
			List<SignPatientManageRoute> manageRouteList = signPatientManageRouteMapper.selectSignPatientManageRouteList(manageRouteQuery);
			if (CollectionUtils.isNotEmpty(manageRouteList)) {
				SignPatientManageRoute signPatientManageRoute = manageRouteList.get(0);
				SpecialDiseaseRoute specialDiseaseRouteQuery = new SpecialDiseaseRoute();
				specialDiseaseRouteQuery.setParentRouteId(signPatientManageRoute.getRouteId());
				List<SpecialDiseaseRoute> childSpecialRoutes = specialDiseaseRouteMapper.selectSpecialDiseaseRouteList(specialDiseaseRouteQuery);
				if (CollectionUtils.isNotEmpty(childSpecialRoutes)) {
					childSpecialRoutes.forEach(specialDiseaseRoute -> {
						// 查询路径是否生成过任务
						manageRouteQuery.setTaskCreateType(TaskCreateTypeConstant.MANUAL_MATCHE);
						manageRouteQuery.setParentRouteId(signPatientManageRoute.getId());
						List<SignPatientManageRoute> createManageRouteList = signPatientManageRouteMapper.selectSignPatientManageRouteList(manageRouteQuery);
						if (CollectionUtils.isEmpty(createManageRouteList)) {
							log.info("开始生成" + signPatient.getPatientName() + "子路径任务定时任务......");
							signPatientManageRouteNodeService.generateChildRouteTask(signPatient.getPatientId(), signPatient.getId(), specialDiseaseRoute.getId());
						}
					});
				}
			}
		});
		log.info("完成生成子路径任务定时任务......");
	}
}
