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

import com.github.pagehelper.PageInfo;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.constant.HttpStatus;
import com.xinelu.common.constant.KnowledgeBaseConstants;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.PageUtils;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.common.utils.uuid.IdUtils;
import com.xinelu.manage.domain.agency.Agency;
import com.xinelu.manage.domain.department.Department;
import com.xinelu.manage.domain.departmentdiseasetype.DepartmentDiseaseType;
import com.xinelu.manage.domain.operationInfo.OperationInfo;
import com.xinelu.manage.domain.propagandainfo.PropagandaInfo;
import com.xinelu.manage.domain.propagandamaterials.PropagandaMaterials;
import com.xinelu.manage.domain.questioninfo.QuestionInfo;
import com.xinelu.manage.domain.scriptInfo.ScriptInfo;
import com.xinelu.manage.domain.specialdiseasenode.SpecialDiseaseNode;
import com.xinelu.manage.domain.specialdiseasetriggercondition.SpecialDiseaseTriggerCondition;
import com.xinelu.manage.domain.textmessage.TextMessage;
import com.xinelu.manage.domain.textmessagesuittask.TextMessageSuitTask;
import com.xinelu.manage.domain.wechattemplate.WechatTemplate;
import com.xinelu.manage.domain.wechattemplatesuittask.WechatTemplateSuitTask;
import com.xinelu.manage.dto.departmentdiseasetype.DepartmentDiseaseTypeDto;
import com.xinelu.manage.dto.knowledgebase.KnowledgeBaseSaveDto;
import com.xinelu.manage.dto.operationinfo.BaseInfoQueryDto;
import com.xinelu.manage.dto.operationinfo.OperationInfoDto;
import com.xinelu.manage.dto.propagandainfo.PropagandaInfoDto;
import com.xinelu.manage.dto.questioninfo.QuestionInfoDto;
import com.xinelu.manage.dto.script.ScriptInfoDto;
import com.xinelu.manage.dto.servicepackage.ServicePackageAddDTO;
import com.xinelu.manage.dto.servicepackage.ServicePackageDto;
import com.xinelu.manage.dto.specialdiseaseroute.SpecialDiseaseRouteDTO;
import com.xinelu.manage.dto.textmessage.TextMessageDTO;
import com.xinelu.manage.dto.textmessage.TextMessageTaskDTO;
import com.xinelu.manage.dto.wechattemplate.WechatTemplateDTO;
import com.xinelu.manage.dto.wechattemplate.WechatTemplateTaskDTO;
import com.xinelu.manage.mapper.agency.AgencyMapper;
import com.xinelu.manage.mapper.department.DepartmentMapper;
import com.xinelu.manage.mapper.operationInfo.OperationInfoMapper;
import com.xinelu.manage.mapper.propagandainfo.PropagandaInfoMapper;
import com.xinelu.manage.mapper.propagandamaterials.PropagandaMaterialsMapper;
import com.xinelu.manage.mapper.questionsubjectoption.QuestionSubjectOptionMapper;
import com.xinelu.manage.mapper.scriptInfo.ScriptInfoMapper;
import com.xinelu.manage.mapper.servicepackage.ServicePackageMapper;
import com.xinelu.manage.mapper.servicepackagecontent.ServicePackageContentMapper;
import com.xinelu.manage.mapper.specialdiseasetriggercondition.SpecialDiseaseTriggerConditionMapper;
import com.xinelu.manage.mapper.textmessage.TextMessageMapper;
import com.xinelu.manage.mapper.wechattemplate.WechatTemplateMapper;
import com.xinelu.manage.mapper.wechattemplatesuittask.WechatTemplateSuitTaskMapper;
import com.xinelu.manage.service.departmentdiseasetype.IDepartmentDiseaseTypeService;
import com.xinelu.manage.service.knowledgebase.IKnowledgeBaseService;
import com.xinelu.manage.service.operationInfo.IOperationInfoService;
import com.xinelu.manage.service.propagandainfo.IPropagandaInfoService;
import com.xinelu.manage.service.questioninfo.IQuestionInfoService;
import com.xinelu.manage.service.scriptInfo.IScriptInfoService;
import com.xinelu.manage.service.servicepackage.IServicePackageService;
import com.xinelu.manage.service.specialdiseasenode.ISpecialDiseaseNodeService;
import com.xinelu.manage.service.specialdiseaseroute.ISpecialDiseaseRouteService;
import com.xinelu.manage.service.specialdiseasetriggercondition.ISpecialDiseaseTriggerConditionService;
import com.xinelu.manage.service.textmessage.ITextMessageService;
import com.xinelu.manage.service.wechattemplate.IWechatTemplateService;
import com.xinelu.manage.vo.questionInfo.QuestionVO;
import com.xinelu.manage.vo.questionsubject.QuestionSubjectVO;
import com.xinelu.manage.vo.questionsubjectoption.QuestionSubjectOptionVO;
import com.xinelu.manage.vo.servicepackage.ServicePackageDetailVO;
import com.xinelu.manage.vo.servicepackage.ServicePackageVO;
import com.xinelu.manage.vo.servicepackagecontent.ServicePackageContentVO;
import com.xinelu.manage.vo.specialdiseasenode.SpecialDiseaseNodeVO;
import com.xinelu.manage.vo.specialdiseaseroute.SpecialDiseaseRouteVO;
import com.xinelu.manage.vo.textmessage.TextMessageVO;
import com.xinelu.manage.vo.wechattemplate.WechatTemplateTaskVO;
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.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: 知识库Service业务层处理
 * @author: haown
 * @create: 2024-07-08 16:25
 **/
@Service
public class KnowledgeBaseServiceImpl implements IKnowledgeBaseService {

	@Resource
	private OperationInfoMapper operationInfoMapper;
	@Resource
	private IOperationInfoService operationInfoService;
	@Resource
	private ScriptInfoMapper scriptInfoMapper;
	@Resource
	private IScriptInfoService scriptInfoService;
	@Resource
	private WechatTemplateMapper wechatTemplateMapper;
	@Resource
	private IWechatTemplateService wechatTemplateService;
	@Resource
	private WechatTemplateSuitTaskMapper wechatTemplateSuitTaskMapper;
	@Resource
	private TextMessageMapper textMessageMapper;
	@Resource
	private ITextMessageService textMessageService;
	@Resource
	private IDepartmentDiseaseTypeService departmentDiseaseTypeService;
	@Resource
	private IPropagandaInfoService propagandaInfoService;
	@Resource
	private PropagandaInfoMapper propagandaInfoMapper;
	@Resource
	private GenerateSystemCodeUtil generateSystemCodeUtil;
	@Resource
	private PropagandaMaterialsMapper propagandaMaterialsMapper;
	@Resource
	private DepartmentMapper departmentMapper;
	@Resource
	private AgencyMapper agencyMapper;
	@Resource
	private ServicePackageMapper servicePackageMapper;
	@Resource
	private IServicePackageService servicePackageService;
	@Resource
	private ServicePackageContentMapper servicePackageContentMapper;
	@Resource
	private IQuestionInfoService questionInfoService;
	@Resource
	private QuestionSubjectOptionMapper questionSubjectOptionMapper;
	@Resource
	private ISpecialDiseaseRouteService specialDiseaseRouteService;
	@Resource
	private ISpecialDiseaseNodeService specialDiseaseNodeService;
	@Resource
	private ISpecialDiseaseTriggerConditionService specialDiseaseTriggerConditionService;
	@Resource
	private SpecialDiseaseTriggerConditionMapper specialDiseaseTriggerConditionMapper;

	@Override
	public TableDataInfo getBaseInfoList(BaseInfoQueryDto baseInfoQueryDto) {
		if (StringUtils.isBlank(baseInfoQueryDto.getType())) {
			throw new ServiceException("知识库类型不能为空");
		}
		TableDataInfo rspData = new TableDataInfo();
		switch(baseInfoQueryDto.getType()){
			case KnowledgeBaseConstants.OPERATOR:
				OperationInfoDto operationInfo = new OperationInfoDto();
				BeanUtils.copyBeanProp(operationInfo, baseInfoQueryDto);
				PageUtils.startPage();
				List<OperationInfo> list = operationInfoMapper.selectOperationList(operationInfo);
				rspData.setCode(HttpStatus.SUCCESS);
				rspData.setMsg("查询成功");
				rspData.setRows(list);
				// 返回 页码
				rspData.setPageNum(new PageInfo(list).getPageNum());
				rspData.setTotal(new PageInfo(list).getTotal());
				break;
			case KnowledgeBaseConstants.SCRIPT:
				ScriptInfoDto scriptInfo = new ScriptInfoDto();
				BeanUtils.copyBeanProp(scriptInfo, baseInfoQueryDto);
				PageUtils.startPage();
				List<ScriptInfo> scriptList = scriptInfoMapper.selectScriptInfoList(scriptInfo);
				rspData.setCode(HttpStatus.SUCCESS);
				rspData.setMsg("查询成功");
				rspData.setRows(scriptList);
				// 返回 页码
				rspData.setPageNum(new PageInfo(scriptList).getPageNum());
				rspData.setTotal(new PageInfo(scriptList).getTotal());
				break;
			case KnowledgeBaseConstants.WECHAT:
				WechatTemplateDTO wechatTemplateDto = new WechatTemplateDTO();
				BeanUtils.copyBeanProp(wechatTemplateDto, baseInfoQueryDto);
				PageUtils.startPage();
				List<WechatTemplateTaskVO> wechatTemplateList = wechatTemplateMapper.selectWechatTemplateDtoList(wechatTemplateDto);
				rspData.setCode(HttpStatus.SUCCESS);
				rspData.setMsg("查询成功");
				rspData.setRows(wechatTemplateList);
				// 返回 页码
				rspData.setPageNum(new PageInfo(wechatTemplateList).getPageNum());
				rspData.setTotal(new PageInfo(wechatTemplateList).getTotal());
				break;
			case KnowledgeBaseConstants.MESSAGE:
				TextMessageDTO textMessageDTO = new TextMessageDTO();
				BeanUtils.copyBeanProp(textMessageDTO, baseInfoQueryDto);
				PageUtils.startPage();
				List<TextMessageVO> messageList = textMessageMapper.selectTextMessageList(textMessageDTO);
				rspData.setCode(HttpStatus.SUCCESS);
				rspData.setMsg("查询成功");
				rspData.setRows(messageList);
				// 返回 页码
				rspData.setPageNum(new PageInfo(messageList).getPageNum());
				rspData.setTotal(new PageInfo(messageList).getTotal());
				break;
			case KnowledgeBaseConstants.PROPAGANDA:
				PropagandaInfoDto propagandaInfo = new PropagandaInfoDto();
				BeanUtils.copyBeanProp(propagandaInfo, baseInfoQueryDto);
				PageUtils.startPage();
				List<PropagandaInfo> propagandaList = propagandaInfoService.selectPropagandaInfoList(propagandaInfo);
				rspData.setCode(HttpStatus.SUCCESS);
				rspData.setMsg("查询成功");
				rspData.setRows(propagandaList);
				// 返回 页码
				rspData.setPageNum(new PageInfo(propagandaList).getPageNum());
				rspData.setTotal(new PageInfo(propagandaList).getTotal());
				break;
			case KnowledgeBaseConstants.SERVICEPACKAGE:
				ServicePackageDto servicePackage = new ServicePackageDto();
				BeanUtils.copyBeanProp(servicePackage, baseInfoQueryDto);
				PageUtils.startPage();
				List<ServicePackageVO> servicePackageList = servicePackageMapper.selectServicePackageLists(servicePackage);
				rspData.setCode(HttpStatus.SUCCESS);
				rspData.setMsg("查询成功");
				rspData.setRows(servicePackageList);
				// 返回 页码
				rspData.setPageNum(new PageInfo(servicePackageList).getPageNum());
				rspData.setTotal(new PageInfo(servicePackageList).getTotal());
				break;
			case KnowledgeBaseConstants.QUESTION:
				QuestionInfoDto questionInfo = new QuestionInfoDto();
				BeanUtils.copyBeanProp(questionInfo, baseInfoQueryDto);
				PageUtils.startPage();
				List<QuestionInfo> questionInfoList = questionInfoService.selectQuestionInfoList(questionInfo);
				rspData.setCode(HttpStatus.SUCCESS);
				rspData.setMsg("查询成功");
				rspData.setRows(questionInfoList);
				// 返回 页码
				rspData.setPageNum(new PageInfo(questionInfoList).getPageNum());
				rspData.setTotal(new PageInfo(questionInfoList).getTotal());
				break;
			case KnowledgeBaseConstants.DISEASEROUTE:
				SpecialDiseaseRouteDTO specialDiseaseRoute = new SpecialDiseaseRouteDTO();
				BeanUtils.copyBeanProp(specialDiseaseRoute, baseInfoQueryDto);
				PageUtils.startPage();
				List<SpecialDiseaseRouteVO> routeList = specialDiseaseRouteService.selectSpecialDiseaseRouteList(specialDiseaseRoute);
				rspData.setCode(HttpStatus.SUCCESS);
				rspData.setMsg("查询成功");
				rspData.setRows(routeList);
				// 返回 页码
				rspData.setPageNum(new PageInfo(routeList).getPageNum());
				rspData.setTotal(new PageInfo(routeList).getTotal());
				break;
		}
		return rspData;
	}

	@Override
	public void copyBaseInfo(KnowledgeBaseSaveDto knowledgeBaseSaveDto) {
		if (StringUtils.isBlank(knowledgeBaseSaveDto.getType())) {
			throw new ServiceException("请输入知识库类型");
		}
		if (ObjectUtils.isEmpty(knowledgeBaseSaveDto.getDepartmentId())) {
			throw new ServiceException("请选择科室");
		}
		if (CollectionUtils.isEmpty(knowledgeBaseSaveDto.getSourceTemplateIds())) {
			throw new ServiceException("请选择知识库信息");
		}
		switch(knowledgeBaseSaveDto.getType()) {
			case KnowledgeBaseConstants.OPERATOR:
				saveOperator(knowledgeBaseSaveDto);
				break;
			case KnowledgeBaseConstants.SCRIPT:
				saveScript(knowledgeBaseSaveDto);
				break;
			case KnowledgeBaseConstants.WECHAT:
				saveWechat(knowledgeBaseSaveDto);
				break;
			case KnowledgeBaseConstants.MESSAGE:
				saveMessage(knowledgeBaseSaveDto);
				break;
			case KnowledgeBaseConstants.PROPAGANDA:
				savePropaganda(knowledgeBaseSaveDto);
				break;
			case KnowledgeBaseConstants.SERVICEPACKAGE:
				saveServicePackage(knowledgeBaseSaveDto);
				break;
			case KnowledgeBaseConstants.QUESTION:
				saveQuestionInfo(knowledgeBaseSaveDto);
				break;
			case KnowledgeBaseConstants.DISEASEROUTE:
				saveSpecialDiseaseRoute(knowledgeBaseSaveDto);
				break;
		}
	}

	private void saveOperator(KnowledgeBaseSaveDto knowledgeBaseSaveDto) {
		knowledgeBaseSaveDto.getSourceTemplateIds().forEach(sourceId -> {
			OperationInfo operationInfo = operationInfoMapper.selectOperationInfoById(sourceId);
			operationInfo.setId(null);
			operationInfo.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
			operationInfo.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
			operationInfo.setSourceTemplateId(sourceId);
			operationInfoService.insertOperationInfo(operationInfo);
		});
	}

	private void saveScript(KnowledgeBaseSaveDto knowledgeBaseSaveDto) {
		knowledgeBaseSaveDto.getSourceTemplateIds().forEach(sourceId -> {
			ScriptInfo scriptInfo = scriptInfoMapper.selectScriptInfoById(sourceId);
			scriptInfo.setId(null);
			scriptInfo.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
			scriptInfo.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
			scriptInfo.setSourceTemplateId(sourceId);
			// 判断病种是否存在，不存在则要先增加病种
			if (scriptInfo.getDiseaseTypeId() != null) {
				DepartmentDiseaseTypeDto departmentDiseaseType = new DepartmentDiseaseTypeDto();
				departmentDiseaseType.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
				departmentDiseaseType.setDiseaseTypeName(scriptInfo.getDiseaseTypeName());
				List<DepartmentDiseaseType> diseaseTypeList = departmentDiseaseTypeService.selectDepartmentDiseaseTypeList(departmentDiseaseType);
				if (CollectionUtils.isEmpty(diseaseTypeList)) {
					// 新增病种
					DepartmentDiseaseType diseaseTypeSaveObj = new DepartmentDiseaseType();
					diseaseTypeSaveObj.setDiseaseTypeName(scriptInfo.getDiseaseTypeName());
					diseaseTypeSaveObj.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
					diseaseTypeSaveObj.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
					departmentDiseaseTypeService.insertDepartmentDiseaseType(diseaseTypeSaveObj);
				} else {
					scriptInfo.setDiseaseTypeId(diseaseTypeList.get(0).getId());
					scriptInfo.setDiseaseTypeName(diseaseTypeList.get(0).getDiseaseTypeName());
				}
			}
			if (scriptInfoMapper.countByScriptInfo(scriptInfo) > 0) {
				throw new ServiceException("通用话术名称已存在");
			}
			// 设置创建人与创建时间
			scriptInfo.setCreateBy(SecurityUtils.getUsername());
			scriptInfo.setCreateTime(LocalDateTime.now());
			scriptInfo.setScriptId(IdUtils.fastUUID());
			scriptInfoMapper.insertScriptInfo(scriptInfo);
		});
	}

	private void saveWechat(KnowledgeBaseSaveDto knowledgeBaseSaveDto) {
		knowledgeBaseSaveDto.getSourceTemplateIds().forEach(sourceId -> {
			WechatTemplate wechatTemplate = wechatTemplateMapper.selectWechatTemplateById(sourceId);
			WechatTemplateTaskDTO wechatTemplateTaskDTO = new WechatTemplateTaskDTO();
			BeanUtils.copyBeanProp(wechatTemplateTaskDTO, wechatTemplate);
			wechatTemplateTaskDTO.setId(null);
			wechatTemplateTaskDTO.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
			wechatTemplateTaskDTO.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
			wechatTemplateTaskDTO.setSourceTemplateId(sourceId);
			// 判断病种是否存在，不存在则要先增加病种
			if (wechatTemplate.getDiseaseTypeId() != null) {
				DepartmentDiseaseTypeDto departmentDiseaseType = new DepartmentDiseaseTypeDto();
				departmentDiseaseType.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
				departmentDiseaseType.setDiseaseTypeName(wechatTemplate.getDiseaseTypeName());
				List<DepartmentDiseaseType> diseaseTypeList = departmentDiseaseTypeService.selectDepartmentDiseaseTypeList(departmentDiseaseType);
				if (CollectionUtils.isEmpty(diseaseTypeList)) {
					// 新增病种
					DepartmentDiseaseType diseaseTypeSaveObj = new DepartmentDiseaseType();
					diseaseTypeSaveObj.setDiseaseTypeName(wechatTemplate.getDiseaseTypeName());
					diseaseTypeSaveObj.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
					diseaseTypeSaveObj.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
					departmentDiseaseTypeService.insertDepartmentDiseaseType(diseaseTypeSaveObj);
					wechatTemplateTaskDTO.setDiseaseTypeId(diseaseTypeSaveObj.getId());
					wechatTemplateTaskDTO.setDiseaseTypeName(diseaseTypeSaveObj.getDiseaseTypeName());
				} else {
					wechatTemplateTaskDTO.setDiseaseTypeId(diseaseTypeList.get(0).getId());
					wechatTemplateTaskDTO.setDiseaseTypeName(diseaseTypeList.get(0).getDiseaseTypeName());
				}
			}
			// 根据微信库模板id查询适用任务类型
			WechatTemplateSuitTask wechatTemplateSuitTaskQuery = new WechatTemplateSuitTask();
			wechatTemplateSuitTaskQuery.setWechatTemplateId(wechatTemplate.getId());
			List<WechatTemplateSuitTask> wechatTemplateSuitTasks = wechatTemplateSuitTaskMapper.selectWechatTemplateSuitTaskList(wechatTemplateSuitTaskQuery);
			if (CollectionUtils.isNotEmpty(wechatTemplateSuitTasks)) {
				List<Long> suitTaskIds = wechatTemplateSuitTasks.stream().map(WechatTemplateSuitTask::getSuitTaskTypeId).collect(Collectors.toList());
				wechatTemplateTaskDTO.setSuitTaskTypeIds(suitTaskIds.toArray(new Long[0]));
			}
			wechatTemplateService.insertWechatTemplate(wechatTemplateTaskDTO);
		});
	}

	private void saveMessage(KnowledgeBaseSaveDto knowledgeBaseSaveDto) {
		knowledgeBaseSaveDto.getSourceTemplateIds().forEach(sourceId -> {
			TextMessage textMessage = textMessageMapper.selectTextMessageById(sourceId);
			TextMessageTaskDTO textMessageTaskDTO = new TextMessageTaskDTO();
			BeanUtils.copyBeanProp(textMessageTaskDTO, textMessage);
			textMessageTaskDTO.setId(null);
			textMessageTaskDTO.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
			textMessageTaskDTO.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
			textMessageTaskDTO.setSourceTemplateId(sourceId);
			// 判断病种是否存在，不存在则要先增加病种
			if (textMessage.getDiseaseTypeId() != null) {
				DepartmentDiseaseTypeDto departmentDiseaseType = new DepartmentDiseaseTypeDto();
				departmentDiseaseType.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
				departmentDiseaseType.setDiseaseTypeName(textMessage.getDiseaseTypeName());
				List<DepartmentDiseaseType> diseaseTypeList = departmentDiseaseTypeService.selectDepartmentDiseaseTypeList(departmentDiseaseType);
				if (CollectionUtils.isEmpty(diseaseTypeList)) {
					// 新增病种
					DepartmentDiseaseType diseaseTypeSaveObj = new DepartmentDiseaseType();
					diseaseTypeSaveObj.setDiseaseTypeName(textMessage.getDiseaseTypeName());
					diseaseTypeSaveObj.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
					diseaseTypeSaveObj.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
					departmentDiseaseTypeService.insertDepartmentDiseaseType(diseaseTypeSaveObj);
					textMessageTaskDTO.setDiseaseTypeId(diseaseTypeSaveObj.getId());
					textMessageTaskDTO.setDiseaseTypeName(diseaseTypeSaveObj.getDiseaseTypeName());
				} else {
					textMessageTaskDTO.setDiseaseTypeId(diseaseTypeList.get(0).getId());
					textMessageTaskDTO.setDiseaseTypeName(diseaseTypeList.get(0).getDiseaseTypeName());
				}
			}
			// 根据短信库模板id查询适用任务类型
			TextMessageSuitTask textMessageSuitTask = new TextMessageSuitTask();
			textMessageSuitTask.setMessageId(textMessage.getId());
			List<TextMessageSuitTask> textMessageSuitTasks = textMessageMapper.selectSuitTaskList(textMessageSuitTask);
			if (CollectionUtils.isNotEmpty(textMessageSuitTasks)) {
				List<Long> suitTaskIds = textMessageSuitTasks.stream().map(TextMessageSuitTask::getSuitTaskTypeId).collect(Collectors.toList());
				textMessageTaskDTO.setSuitTaskTypeIds(suitTaskIds.toArray(new Long[0]));
			}
			textMessageService.insertTextMessage(textMessageTaskDTO);
		});
	}

	@Transactional(rollbackFor = Exception.class)
	public void savePropaganda(KnowledgeBaseSaveDto knowledgeBaseSaveDto) {
		knowledgeBaseSaveDto.getSourceTemplateIds().forEach(sourceId -> {
			PropagandaInfo propagandaInfo = propagandaInfoMapper.selectPropagandaInfoById(sourceId);
			propagandaInfo.setId(null);
			propagandaInfo.setSourceTemplateId(sourceId);
			propagandaInfo.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
			propagandaInfo.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
			Department department = departmentMapper.selectDepartment(knowledgeBaseSaveDto.getDepartmentId());
			if (ObjectUtils.isEmpty(department)) {
				throw new ServiceException("未找到科室,请联系管理员");
			}
			Agency agency = agencyMapper.selectAgencyById(department.getHospitalAgencyId());
			if (ObjectUtils.isEmpty(agency)) {
				throw new ServiceException("未找到机构,请联系管理员");
			}
			propagandaInfo.setHospitalAgencyId(agency.getId());
			propagandaInfo.setHospitalAgencyName(agency.getAgencyName());
			propagandaInfo.setDelFlag(0);
			propagandaInfo.setCreateTime(LocalDateTime.now());
			propagandaInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
			propagandaInfo.setUpdateTime(null);
			propagandaInfo.setUpdateBy(null);
			propagandaInfo.setPropagandaCode(Constants.PROPAGANDA_INFO_CODE + generateSystemCodeUtil.generateSystemCode(Constants.PROPAGANDA_INFO_CODE));
			int flag = propagandaInfoMapper.insertPropagandaInfo(propagandaInfo);
			if (flag > 0) {
				// 查询宣教素材关联关系
				PropagandaMaterials query = new PropagandaMaterials();
				query.setPropagandaId(sourceId);
				List<PropagandaMaterials> propagandaMaterialsList = propagandaMaterialsMapper.selectPropagandaMaterialsList(query);
				if (CollectionUtils.isNotEmpty(propagandaMaterialsList)) {
					for (PropagandaMaterials propagandaMaterials : propagandaMaterialsList) {
						propagandaMaterials.setPropagandaId(propagandaInfo.getId());
						propagandaMaterials.setPropagandaTitle(propagandaInfo.getPropagandaTitle());
						propagandaMaterials.setDelFlag(0);
						propagandaMaterials.setCreateBy(SecurityUtils.getLoginUser().getUser().getCreateBy());
						propagandaMaterials.setCreateTime(LocalDateTime.now());
						propagandaMaterialsMapper.insertPropagandaMaterials(propagandaMaterials);
					}
				}
			}
		});
	}

	private void saveServicePackage(KnowledgeBaseSaveDto knowledgeBaseSaveDto) {
		knowledgeBaseSaveDto.getSourceTemplateIds().forEach(sourceId -> {
			ServicePackageAddDTO servicePackageAddDTO = new ServicePackageAddDTO();
			ServicePackageDetailVO servicePackageDetail = servicePackageService.selectServicePackageById(sourceId);

			BeanUtils.copyBeanProp(servicePackageAddDTO, servicePackageDetail);
			servicePackageAddDTO.setId(null);
			servicePackageAddDTO.setSourceTemplateId(sourceId);
			servicePackageAddDTO.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
			servicePackageAddDTO.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());

			if (servicePackageDetail.getDiseaseTypeId() != null) {
				DepartmentDiseaseTypeDto departmentDiseaseType = new DepartmentDiseaseTypeDto();
				departmentDiseaseType.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
				departmentDiseaseType.setDiseaseTypeName(servicePackageDetail.getDiseaseTypeName());
				List<DepartmentDiseaseType> diseaseTypeList = departmentDiseaseTypeService.selectDepartmentDiseaseTypeList(departmentDiseaseType);
				if (CollectionUtils.isEmpty(diseaseTypeList)) {
					// 新增病种
					DepartmentDiseaseType diseaseTypeSaveObj = new DepartmentDiseaseType();
					diseaseTypeSaveObj.setDiseaseTypeName(servicePackageDetail.getDiseaseTypeName());
					diseaseTypeSaveObj.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
					diseaseTypeSaveObj.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
					departmentDiseaseTypeService.insertDepartmentDiseaseType(diseaseTypeSaveObj);
					servicePackageAddDTO.setDiseaseTypeId(diseaseTypeSaveObj.getId());
					servicePackageAddDTO.setDiseaseTypeName(diseaseTypeSaveObj.getDiseaseTypeName());
				} else {
					servicePackageAddDTO.setDiseaseTypeId(diseaseTypeList.get(0).getId());
					servicePackageAddDTO.setDiseaseTypeName(diseaseTypeList.get(0).getDiseaseTypeName());
				}
			}

			List<ServicePackageContentVO> voList = servicePackageDetail.getVoList();

			servicePackageAddDTO.setVoList(voList);
			servicePackageService.insertServicePackage(servicePackageAddDTO);
		});
	}

	private void saveQuestionInfo(KnowledgeBaseSaveDto knowledgeBaseSaveDto) {
		knowledgeBaseSaveDto.getSourceTemplateIds().forEach(sourceId -> {
			QuestionVO questionVO = questionInfoService.selectQuestionInfoById(sourceId);
			QuestionVO saveObj = new QuestionVO();
			BeanUtils.copyBeanProp(saveObj, questionVO);
			saveObj.setId(null);
			saveObj.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
			saveObj.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
			saveObj.setSourceTemplateId(sourceId);
			if (questionVO.getDiseaseTypeId() != null) {
				DepartmentDiseaseTypeDto departmentDiseaseType = new DepartmentDiseaseTypeDto();
				departmentDiseaseType.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
				departmentDiseaseType.setDiseaseTypeName(questionVO.getDiseaseTypeName());
				List<DepartmentDiseaseType> diseaseTypeList = departmentDiseaseTypeService.selectDepartmentDiseaseTypeList(departmentDiseaseType);
				if (CollectionUtils.isEmpty(diseaseTypeList)) {
					// 新增病种
					DepartmentDiseaseType diseaseTypeSaveObj = new DepartmentDiseaseType();
					diseaseTypeSaveObj.setDiseaseTypeName(questionVO.getDiseaseTypeName());
					diseaseTypeSaveObj.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
					diseaseTypeSaveObj.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
					departmentDiseaseTypeService.insertDepartmentDiseaseType(diseaseTypeSaveObj);
					saveObj.setDiseaseTypeId(diseaseTypeSaveObj.getId());
					saveObj.setDiseaseTypeName(diseaseTypeSaveObj.getDiseaseTypeName());
				} else {
					saveObj.setDiseaseTypeId(diseaseTypeList.get(0).getId());
					saveObj.setDiseaseTypeName(diseaseTypeList.get(0).getDiseaseTypeName());
				}
			}
			// 题目保存
			if (CollectionUtils.isNotEmpty(questionVO.getQuestionSubjectList())) {
				List<QuestionSubjectVO> questionSubjectList = new ArrayList<>();
				questionVO.getQuestionSubjectList().forEach(subject -> {
					Long subjectId = subject.getId();
					subject.setId(null);
					subject.setQuestionInfoId(null);
					// 查询题目选项
					List<Long> questionSubjectIds = new ArrayList<>();
					questionSubjectIds.add(subjectId);
					List<QuestionSubjectOptionVO> options = questionSubjectOptionMapper.selectQuestionSubjectOptions(questionSubjectIds);
					options.forEach(option -> {
						option.setId(null);
						option.setQuestionNumber(subject.getQuestionNumber());
					});
					subject.setQuestionSubjectOptionList(options);
					questionSubjectList.add(subject);
				});
				saveObj.setQuestionSubjectList(questionSubjectList);
			}
			// 保存
			questionInfoService.insertQuestionInfo(saveObj);
		});
	}

	private void saveSpecialDiseaseRoute(KnowledgeBaseSaveDto knowledgeBaseSaveDto) {
		knowledgeBaseSaveDto.getSourceTemplateIds().forEach(sourceId -> {
			Long saveRouteId = saveRoute(sourceId, null, knowledgeBaseSaveDto);
			// 查询子路径
			SpecialDiseaseRouteDTO specialDiseaseRoute = new SpecialDiseaseRouteDTO();
			specialDiseaseRoute.setParentRouteId(sourceId);
			List<SpecialDiseaseRouteVO> childRouteList = specialDiseaseRouteService.selectSpecialDiseaseRouteList(specialDiseaseRoute);
			childRouteList.forEach(childRoute -> {
				Long childRouteId = saveRoute(childRoute.getId(), saveRouteId, knowledgeBaseSaveDto);
				// 触发条件保存
				SpecialDiseaseTriggerCondition specialDiseaseTriggerCondition = new SpecialDiseaseTriggerCondition();
				specialDiseaseTriggerCondition.setRouteId(childRoute.getId());
				List<SpecialDiseaseTriggerCondition> triggerConditionList = specialDiseaseTriggerConditionService.selectSpecialDiseaseTriggerConditionList(specialDiseaseTriggerCondition);
				List<SpecialDiseaseTriggerCondition> triggerConditionSaveList = new ArrayList<>();
				triggerConditionList.forEach(triggerCondition -> {
					triggerCondition.setId(null);
					triggerCondition.setRouteId(childRouteId);
					triggerConditionSaveList.add(triggerCondition);
				});
				specialDiseaseTriggerConditionMapper.insertTriggerConditionList(triggerConditionSaveList);
			});
		});

	}

	private Long saveRoute(Long specialDiseaseRouteId, Long parentRouteId, KnowledgeBaseSaveDto knowledgeBaseSaveDto) {
		SpecialDiseaseRouteVO specialDiseaseRouteVO = specialDiseaseRouteService.selectSpecialDiseaseRouteById(specialDiseaseRouteId);
		SpecialDiseaseRouteVO saveObj = new SpecialDiseaseRouteVO();
		BeanUtils.copyBeanProp(saveObj, specialDiseaseRouteVO);
		saveObj.setId(null);
		saveObj.setParentRouteId(parentRouteId);
		saveObj.setDepartmentId(knowledgeBaseSaveDto.getDepartmentId());
		saveObj.setDepartmentName(knowledgeBaseSaveDto.getDepartmentName());
		specialDiseaseRouteService.insertSpecialDiseaseRoute(saveObj);
		// 节点信息
		SpecialDiseaseNode specialDiseaseNode = new SpecialDiseaseNode();
		specialDiseaseNode.setRouteId(specialDiseaseRouteId);
		List<SpecialDiseaseNode> nodeList = specialDiseaseNodeService.selectSpecialDiseaseNodeList(specialDiseaseNode);
		List<SpecialDiseaseNodeVO> specialDiseaseNodeVOs = new ArrayList<>();
		nodeList.forEach(node -> {
			SpecialDiseaseNodeVO specialDiseaseNodeVO = new SpecialDiseaseNodeVO();
			BeanUtils.copyBeanProp(specialDiseaseNodeVO, node);
			specialDiseaseNodeVO.setId(null);
			specialDiseaseNodeVO.setRouteId(saveObj.getId());
			specialDiseaseNodeVOs.add(specialDiseaseNodeVO);
		});
		saveObj.setSpecialDiseaseNodeList(specialDiseaseNodeVOs);
		specialDiseaseRouteService.insertSpecialDiseaseRoute(saveObj);
		return saveObj.getId();
	}
}
