package com.xinelu.mobile.service.homepage.Impl;

import com.alibaba.fastjson2.JSONObject;
import com.xinelu.common.constant.TaskStatisticsTypeConstants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.enums.*;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.PageServiceUtil;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.manage.domain.patientquestionoptionresult.PatientQuestionOptionResult;
import com.xinelu.manage.domain.patientquestionsubjectresult.PatientQuestionSubjectResult;
import com.xinelu.manage.domain.patientquestionsubmitresult.PatientQuestionSubmitResult;
import com.xinelu.manage.domain.patienttaskexecuterecord.PatientTaskExecuteRecord;
import com.xinelu.manage.domain.questioninfo.QuestionInfo;
import com.xinelu.manage.domain.questionsubject.QuestionSubject;
import com.xinelu.manage.domain.signpatientmanageroutenode.SignPatientManageRouteNode;
import com.xinelu.manage.dto.patientquestionoptionresult.PatientQuestionOptionResultDTO;
import com.xinelu.manage.dto.patientquestionsubjectresult.PatientQuestionSubjectResultDTO;
import com.xinelu.manage.dto.patientquestionsubmitresult.PatientQuestionSubmitResultDTO;
import com.xinelu.manage.mapper.patientquestionoptionresult.PatientQuestionOptionResultMapper;
import com.xinelu.manage.mapper.patientquestionsubjectresult.PatientQuestionSubjectResultMapper;
import com.xinelu.manage.mapper.patientquestionsubmitresult.PatientQuestionSubmitResultMapper;
import com.xinelu.manage.mapper.patienttaskexecuterecord.PatientTaskExecuteRecordMapper;
import com.xinelu.manage.mapper.questioninfo.QuestionInfoMapper;
import com.xinelu.manage.mapper.questionsubject.QuestionSubjectMapper;
import com.xinelu.manage.mapper.questionsubjectoption.QuestionSubjectOptionMapper;
import com.xinelu.manage.mapper.signpatientmanageroutenode.SignPatientManageRouteNodeMapper;
import com.xinelu.manage.service.patienttaskstatistics.IPatientTaskStatisticsService;
import com.xinelu.manage.service.propagandainfo.IPropagandaInfoService;
import com.xinelu.manage.vo.propagandainfo.PropagandaMaterialsVo;
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.signpatientmanageroute.PhonePush;
import com.xinelu.mobile.mapper.homepage.HomePageMapper;
import com.xinelu.mobile.service.homepage.HomePageService;
import com.xinelu.mobile.vo.homepage.HealthLog;
import com.xinelu.mobile.vo.homepage.MessageContentVO;
import com.xinelu.mobile.vo.homepage.MessageTabulationVO;
import com.xinelu.mobile.vo.homepage.NodeExecuteStatus;
import com.xinelu.mobile.vo.myfollowup.MyFollowUpVO;
import com.xinelu.mobile.vo.satisfactionquestionnaire.SatisfactionQuestionnaire;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 小程序首页ServiceImpl
 *
 * @author zh
 * @date 2024-04-18
 */
@Service
@Slf4j
public class HomePageServiceImpl implements HomePageService {

    @Resource
    private HomePageMapper homePageMapper;
    @Resource
    private PageServiceUtil pageServiceUtil;
    @Resource
    private QuestionInfoMapper questionInfoMapper;
    @Resource
    private QuestionSubjectMapper questionSubjectMapper;
    @Resource
    private QuestionSubjectOptionMapper questionSubjectOptionMapper;
    @Resource
    private PatientQuestionSubmitResultMapper submitResultMapper;
    @Resource
    private PatientQuestionSubmitResultMapper patientQuestionSubmitResultMapper;
    @Resource
    private PatientQuestionSubjectResultMapper patientQuestionSubjectResultMapper;
    @Resource
    private PatientQuestionOptionResultMapper patientQuestionOptionResultMapper;
    @Resource
    private SignPatientManageRouteNodeMapper signPatientManageRouteNodeMapper;
    @Resource
    private IPropagandaInfoService propagandaInfoService;
    @Resource
    private PatientTaskExecuteRecordMapper patientTaskExecuteRecordMapper;
	@Resource
	private IPatientTaskStatisticsService patientTaskStatisticsService;

    /**
     * 我的随访列表
     *
     * @param myFollowUp 用户信息
     * @return TableDataInfo
     */
    @Override
    public TableDataInfo myFollowUp(MyFollowUpVO myFollowUp) {
        //查询已执行数据
        myFollowUp.setNodeExecuteStatus(NodeExecuteStatusEnum.EXECUTED.getInfo());
        List<MyFollowUpVO> myFollowUpList = homePageMapper.selectManageRouteNode(myFollowUp);
        int total = myFollowUpList.size();
        if (CollectionUtils.isEmpty(myFollowUpList)) {
            return pageServiceUtil.getDataTable(new ArrayList<>());
        }
        //筛选未提交路径、组装数据
        List<MyFollowUpVO> myFollowUpVOS = new ArrayList<>();
        for (MyFollowUpVO myFollowUpVO : myFollowUpList) {
            //筛选未提交路径
            if (Objects.nonNull(myFollowUpVO.getSign()) && myFollowUpVO.getSign() == 0) {
                continue;
            }
            //组装数据
            if (Objects.nonNull(myFollowUpVO.getDischargeTime())) {
                myFollowUpVO.setFollowDate(myFollowUpVO.getDischargeTime().plusDays(myFollowUpVO.getRouteNodeDay()));
                myFollowUpVO.setManageRouteNodeName(RouteNodeNameEnum.getNameByInfo(myFollowUpVO.getRouteNodeName()).getName());
            }
            myFollowUpVO.setFollowName(myFollowUpVO.getManageRouteNodeName() + myFollowUpVO.getRouteNodeDay() + "天");
            myFollowUpVOS.add(myFollowUpVO);
        }
        myFollowUpVOS.sort(Comparator.comparing(MyFollowUpVO::getFollowDate).reversed());
        //分页
        if (Objects.nonNull(myFollowUp.getNum()) || Objects.nonNull(myFollowUp.getSize())) {
            Integer pageNum = myFollowUp.getNum();
            Integer pageSize = myFollowUp.getSize();
            myFollowUpVOS = myFollowUpVOS.stream().skip((long) (pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        }
        TableDataInfo dataTable = pageServiceUtil.getDataTable(myFollowUpVOS);
        dataTable.setTotal(total);
        return dataTable;
    }

    /**
     * 查询话术信息
     *
     * @param patientTaskExecuteRecordId 话术信息主键
     * @return 话术信息
     */
    @Override
    public PhonePush selectScriptInfo(Long patientTaskExecuteRecordId) {
        return homePageMapper.selectPhoneNodeContent(patientTaskExecuteRecordId);
    }

    /**
     * 问卷信息
     *
     * @param templateId 问卷表id
     * @return AjaxResult
     */
    @Override
    public AjaxResult selectQuestion(Long templateId) {
        //查询问卷表
        QuestionInfo questionInfo = questionInfoMapper.selectQuestionInfoById(templateId);
        QuestionVO questionVO = new QuestionVO();
        // 查询题目表
        if (Objects.nonNull(questionInfo)) {
            BeanUtils.copyBeanProp(questionVO, questionInfo);
            List<QuestionSubjectVO> questionSubjects = questionSubjectMapper.selectQuestionSubjectBy(templateId);
            List<Long> questionSubjectIds = questionSubjects.stream().filter(Objects::nonNull).filter(item -> Objects.nonNull(item.getId())).map(QuestionSubject::getId).collect(Collectors.toList());
            if (questionSubjectIds.size() == 0) {
                questionVO.setQuestionSubjectList(questionSubjects);
                return AjaxResult.success(questionVO);
            }
            // 查询选项表
            List<QuestionSubjectOptionVO> questionSubjectOptions = questionSubjectOptionMapper.selectQuestionSubjectOptions(questionSubjectIds);
            for (QuestionSubjectVO questionSubject : questionSubjects) {
                List<QuestionSubjectOptionVO> collect = questionSubjectOptions.stream().filter(Objects::nonNull).filter(item -> Objects.nonNull(item.getQuestionnaireSubjectId()) && questionSubject.getId().equals(item.getQuestionnaireSubjectId())).collect(Collectors.toList());
                questionSubject.setQuestionSubjectOptionList(collect);
            }
            questionVO.setQuestionSubjectList(questionSubjects);
        }
        return AjaxResult.success(questionVO);
    }

    /**
     * 问卷记录信息
     *
     * @param patientQuestionSubmitResultId 记录表id
     * @return AjaxResult
     */
    @Override
    public AjaxResult selectQuestionSubmit(Long patientQuestionSubmitResultId, Long patientTaskExecuteRecordId) {
        return AjaxResult.success(submitResultMapper.selectResultByTaskExecuteRecordId(patientTaskExecuteRecordId, patientQuestionSubmitResultId));
    }

    /**
     * 小程序问卷提交
     *
     * @param dto 患者问卷提交结果信息表DTO
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updatePatientTaskExecuteRecord(PatientQuestionSubmitResultDTO dto) {
        LocalDateTime time = LocalDateTime.now();
        //新增患者管理任务执行记录表
        PatientTaskExecuteRecord patientTaskExecuteRecord = homePageMapper.selectPatientManageRouteByManageRouteNodeId(dto.getManageRouteNodeId());
        patientTaskExecuteRecord.setExecuteTime(time);
        patientTaskExecuteRecord.setCreateTime(time);
        patientTaskExecuteRecordMapper.insertPatientTaskExecuteRecord(patientTaskExecuteRecord);
        //患者问卷提交结果信息表
        PatientQuestionSubmitResult patientQuestionSubmitResult = new PatientQuestionSubmitResult();
        BeanUtils.copyBeanProp(patientQuestionSubmitResult, dto);
        patientQuestionSubmitResult.setTaskExecuteRecordId(patientTaskExecuteRecord.getId());
        patientQuestionSubmitResult.setQuestionInfoId(dto.getQuestionInfoId());
        patientQuestionSubmitResult.setPatientId(patientTaskExecuteRecord.getPatientId());
        patientQuestionSubmitResult.setPatientName(patientTaskExecuteRecord.getPatientName());
        patientQuestionSubmitResult.setManageRouteId(patientTaskExecuteRecord.getManageRouteId());
        patientQuestionSubmitResult.setManageRouteName(patientTaskExecuteRecord.getManageRouteName());
        patientQuestionSubmitResult.setCreateTime(time);
        List<PatientQuestionOptionResultDTO> patientQuestionOptionResultsFlag = new ArrayList<>();
        patientQuestionSubmitResult.setRemarkableFlag(ContentedAndRemarkableFlagEnum.ROUTINE.getInfo());
        patientQuestionSubmitResult.setContentedFlag(ContentedAndRemarkableFlagEnum.CONTENTED.getInfo());
        dto.getSubjectResultDTOList().forEach(item -> patientQuestionOptionResultsFlag.addAll(item.getOptionResultList()));
        if (CollectionUtils.isNotEmpty(patientQuestionOptionResultsFlag)) {
            List<PatientQuestionOptionResultDTO> remarkableFlag = patientQuestionOptionResultsFlag.stream().filter(Objects::nonNull).filter(item -> StringUtils.isNotBlank(item.getRemarkableFlag()) && Objects.nonNull(item.getOptionChooseSign()) && item.getOptionChooseSign().equals(0) && ContentedAndRemarkableFlagEnum.REMARKABLE.getInfo().equals(item.getRemarkableFlag())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(remarkableFlag)) {
                patientQuestionSubmitResult.setRemarkableFlag(ContentedAndRemarkableFlagEnum.REMARKABLE.getInfo());
            }
            List<PatientQuestionOptionResultDTO> contentedFlag = patientQuestionOptionResultsFlag.stream().filter(Objects::nonNull).filter(item -> StringUtils.isNotBlank(item.getContentedFlag()) && Objects.nonNull(item.getOptionChooseSign()) && item.getOptionChooseSign().equals(0) && ContentedAndRemarkableFlagEnum.DISCONTENTED.getInfo().equals(item.getRemarkableFlag())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(contentedFlag)) {
                patientQuestionSubmitResult.setContentedFlag(ContentedAndRemarkableFlagEnum.DISCONTENTED.getInfo());
            }
        }
        // 新增患者问卷提交结果
        if (patientQuestionSubmitResultMapper.insertPatientQuestionSubmitResult(patientQuestionSubmitResult) <= 0) {
            log.info("新增任务记录表失败");
            throw new ServiceException("问卷提交失败！");
        }
        // 获取到患者问卷题目提交结果信息DTO
        List<PatientQuestionSubjectResultDTO> subjectResultList = dto.getSubjectResultDTOList();
        // 初始化患者问卷题目提交结果信息
        List<PatientQuestionSubjectResult> patientQuestionSubjectResults = new ArrayList<>();
        // 获取到患者问卷题目选项提交结果DTO
        List<PatientQuestionOptionResultDTO> patientQuestionOptionResults = new ArrayList<>();
        for (PatientQuestionSubjectResultDTO questionSubjectResult : subjectResultList) {
            PatientQuestionSubjectResult saveQuestionSubject = new PatientQuestionSubjectResult();
            BeanUtils.copyBeanProp(saveQuestionSubject, questionSubjectResult);
            saveQuestionSubject.setQuestionSubmitResultId(patientQuestionSubmitResult.getId());
            saveQuestionSubject.setCreateTime(time);
            saveQuestionSubject.setQuestionSubjectId(questionSubjectResult.getId());
            patientQuestionSubjectResults.add(saveQuestionSubject);
            if (CollectionUtils.isNotEmpty(questionSubjectResult.getOptionResultList())) {
                patientQuestionOptionResults.addAll(questionSubjectResult.getOptionResultList());
            }
        }
        // 新增患者问卷题目提交结果
        if (patientQuestionSubjectResultMapper.saveQuestionSubjectList(patientQuestionSubjectResults) < 0) {
            log.info("新增患者问卷题目提交结果失败");
            throw new ServiceException("问卷提交失败");
        }
        if (CollectionUtils.isEmpty(patientQuestionOptionResults)) {
            return AjaxResult.success();
        }
        // 初始化患者问卷题目选项提交结果
        List<PatientQuestionOptionResult> saveQuestionSubjectOptions = new ArrayList<>();
        for (PatientQuestionOptionResultDTO patientQuestionOptionResult : patientQuestionOptionResults) {
            PatientQuestionOptionResult saveQuestionOption = new PatientQuestionOptionResult();
            BeanUtils.copyBeanProp(saveQuestionOption, patientQuestionOptionResult);
            // 从已保存的患者问卷题目结果列表中查找当前选项所对应的题目结果
            PatientQuestionSubjectResult patientQuestionSubjectResult = patientQuestionSubjectResults.stream().filter(Objects::nonNull).filter(item -> Objects.nonNull(item.getQuestionNumber()) && patientQuestionOptionResult.getQuestionNumber().compareTo(item.getQuestionNumber()) == 0).findFirst().orElse(new PatientQuestionSubjectResult());
            saveQuestionOption.setQuestionSubjectResultId(patientQuestionSubjectResult.getId());
            saveQuestionOption.setCreateTime(time);
            saveQuestionOption.setQuestionSubjectOptionId(patientQuestionOptionResult.getId());
            saveQuestionSubjectOptions.add(saveQuestionOption);
        }
        // 新增患者问卷题目选项提交结果
        if (patientQuestionOptionResultMapper.saveQuestionOptionList(saveQuestionSubjectOptions) < 0) {
            log.info("新增患者问卷题目选项提交结果失败");
            throw new ServiceException("问卷提交失败");
        }
        SignPatientManageRouteNode signPatientManageRouteNode = new SignPatientManageRouteNode();
        signPatientManageRouteNode.setId(dto.getManageRouteNodeId());
        signPatientManageRouteNode.setNodeFinishDate(time);
        signPatientManageRouteNode.setRouteCheckDate(time);
        signPatientManageRouteNode.setNodeFinishStatus(NodeExecuteResultStatusEnum.SUCCESS.getInfo());
        signPatientManageRouteNode.setRouteHandleId(dto.getPatientId());
        signPatientManageRouteNode.setRouteHandlePerson(dto.getRouteHandlePerson());
        if (signPatientManageRouteNodeMapper.updateSignPatientManageRouteNode(signPatientManageRouteNode) < 0) {
            log.info("修改签约患者管理任务路径节点表失败");
            throw new ServiceException("问卷提交失败");
        }
        return AjaxResult.success();
    }

    /**
     * 健康日志
     *
     * @param residentId 用户id
     * @return AjaxResult
     */
    @Override
    public List<HealthLog> selectPatientTaskExecuteRecord(Long residentId) {
        List<HealthLog> patientTaskExecuteRecords = homePageMapper.selectTaskExecuteRecordByResidentId(residentId, null);
        if (CollectionUtils.isNotEmpty(patientTaskExecuteRecords)) {
            for (HealthLog patientTaskExecuteRecord : patientTaskExecuteRecords) {
                LocalDate localDate = null;
                if (Objects.nonNull(patientTaskExecuteRecord.getDischargeTime())) {
                    localDate = patientTaskExecuteRecord.getDischargeTime().plusDays(patientTaskExecuteRecord.getRouteNodeDay());
                }
                if (Objects.isNull(patientTaskExecuteRecord.getDischargeTime()) && Objects.nonNull(patientTaskExecuteRecord.getVisitDate())) {
                    localDate = patientTaskExecuteRecord.getVisitDate().plusDays(patientTaskExecuteRecord.getRouteNodeDay());
                }
                if (Objects.isNull(localDate)) {
                    continue;
                }
                patientTaskExecuteRecord.setExecuteTime(localDate.atTime(0, 0, 0));
            }
        }
        return patientTaskExecuteRecords;
    }

    /**
     * 满意度问卷
     *
     * @param residentId 居民信息
     * @return SatisfactionQuestionnaire
     */
    @Override
    public TableDataInfo satisfactionQuestionnaire(Long residentId) {
        pageServiceUtil.startPage();
        List<SatisfactionQuestionnaire> satisfactionQuestionnaires = homePageMapper.satisfactionQuestionnaireByResidentId(residentId, QuestionTypeEnum.SATISFACTION_QUESTIONNAIRE.getInfo(), QuestionnaireStatusEnum.PUBLISHED.getInfo());
        //判断是否完成
        for (SatisfactionQuestionnaire satisfactionQuestionnaire : satisfactionQuestionnaires) {
            if (Objects.isNull(satisfactionQuestionnaire.getPatientQuestionSubmitResultId())) {
                satisfactionQuestionnaire.setSign("incomplete");
            } else {
                satisfactionQuestionnaire.setSign("completed");
            }
        }
        return pageServiceUtil.getDataTable(satisfactionQuestionnaires);
    }

    /**
     * 消息通知列表
     *
     * @param residentId 用户id
     * @return AjaxResult
     */
    @Override
    public AjaxResult selectSignPatientManageRouteNode(Long residentId) {
        //查询已执行
        List<MessageTabulationVO> signPatientManageRouteNodes = homePageMapper.selectManageRouteByResidentId(residentId, NodeExecuteStatusEnum.EXECUTED.getInfo());
        if (CollectionUtils.isEmpty(signPatientManageRouteNodes)) {
            return AjaxResult.success(new ArrayList<>());
        }
        //过滤电话外呼数据
        List<MessageTabulationVO> messageTabulationVOS = new ArrayList<>();
        for (MessageTabulationVO manageRouteNode : signPatientManageRouteNodes) {
            if (Objects.isNull(manageRouteNode) || TaskContentEnum.PHONE_OUTBOUND.getInfo().equals(manageRouteNode.getTaskNodeType())) {
                continue;
            }
            if (StringUtils.isNotBlank(manageRouteNode.getManageRouteName()) && StringUtils.isNotBlank(manageRouteNode.getRouteNodeName()) && Objects.nonNull(manageRouteNode.getRouteNodeDay())) {
                if (StringUtils.isNotBlank(manageRouteNode.getRouteNodeName())) {
                    manageRouteNode.setRouteNodeName(RouteNodeNameEnum.getNameByInfo(manageRouteNode.getRouteNodeName()).getName());
                }
                manageRouteNode.setRouteNodeName(manageRouteNode.getManageRouteName() + manageRouteNode.getRouteNodeName() + "第" + manageRouteNode.getRouteNodeDay() + "天");
                messageTabulationVOS.add(manageRouteNode);
            }
        }
        return AjaxResult.success(messageTabulationVOS);
    }

    /**
     * 消息推送内容
     *
     * @param manageRouteNodeId 节点id
     * @return AjaxResult
     */
    @Override
    public AjaxResult selectMessageContent(Long manageRouteNodeId) {
        SignPatientManageRouteNode signPatientManageRouteNode = signPatientManageRouteNodeMapper.selectSignPatientManageRouteNodeById(manageRouteNodeId);
        if (Objects.isNull(signPatientManageRouteNode)) {
            return AjaxResult.error("该节点已删除！");
        }
        MessageContentVO messageContentVO = new MessageContentVO();
        //问卷或人工随访塞值
        if (TaskContentEnum.QUESTIONNAIRE_SCALE.getInfo().equals(signPatientManageRouteNode.getTaskNodeType()) || TaskContentEnum.ARTIFICIAL_FOLLOW_UP.getInfo().equals(signPatientManageRouteNode.getTaskNodeType())) {
            messageContentVO.setTaskNodeType(signPatientManageRouteNode.getTaskNodeType());
            //查询问卷
            QuestionInfo questionInfo = questionInfoMapper.selectQuestionInfoById(signPatientManageRouteNode.getQuestionInfoId());
            // 查询题目表
            QuestionVO questionVO = new QuestionVO();
            if (Objects.nonNull(questionInfo)) {
                BeanUtils.copyBeanProp(questionVO, questionInfo);
                List<QuestionSubjectVO> questionSubjects = questionSubjectMapper.selectQuestionSubjectBy(signPatientManageRouteNode.getQuestionInfoId());
                if (CollectionUtils.isNotEmpty(questionSubjects)) {
                    List<Long> questionSubjectIds = questionSubjects.stream().filter(Objects::nonNull).filter(item -> Objects.nonNull(item.getId())).map(QuestionSubject::getId).collect(Collectors.toList());
                    // 选项表
                    List<QuestionSubjectOptionVO> questionSubjectOptions = questionSubjectOptionMapper.selectQuestionSubjectOptions(questionSubjectIds);
                    for (QuestionSubjectVO questionSubject : questionSubjects) {
                        List<QuestionSubjectOptionVO> collect = questionSubjectOptions.stream().filter(Objects::nonNull).filter(item -> Objects.nonNull(item.getQuestionnaireSubjectId()) && questionSubject.getId().equals(item.getQuestionnaireSubjectId())).collect(Collectors.toList());
                        questionSubject.setQuestionSubjectOptionList(collect);
                    }
                    questionVO.setQuestionSubjectList(questionSubjects);
                    messageContentVO.setQuestion(questionVO);
                }
            }
        }
        //宣教塞值
        if (TaskContentEnum.PROPAGANDA_ARTICLE.getInfo().equals(signPatientManageRouteNode.getTaskNodeType())) {
            //查询宣教信息
            PropagandaMaterialsVo propagandaMaterialsVo = propagandaInfoService.selectPropagandaInfoById(signPatientManageRouteNode.getPropagandaInfoId());
            messageContentVO.setTaskNodeType(TaskContentEnum.PROPAGANDA_ARTICLE.getInfo());
            messageContentVO.setPropagandaContent(JSONObject.parseObject(JSONObject.toJSONString(propagandaMaterialsVo)));
        }
        //副文本塞值
        if (!TaskContentEnum.PROPAGANDA_ARTICLE.getInfo().equals(signPatientManageRouteNode.getTaskNodeType()) && !TaskContentEnum.QUESTIONNAIRE_SCALE.getInfo().equals(signPatientManageRouteNode.getTaskNodeType()) && !TaskContentEnum.ARTIFICIAL_FOLLOW_UP.getInfo().equals(signPatientManageRouteNode.getTaskNodeType())) {
            messageContentVO.setTaskNodeType(TaskContentEnum.TEXT_REMIND.getInfo());
            if (StringUtils.isNotBlank(signPatientManageRouteNode.getNodeContent())) {
                messageContentVO.setNodeContent(signPatientManageRouteNode.getNodeContent());
            }
            if (StringUtils.isNotBlank(signPatientManageRouteNode.getTextRemindContent())) {
                messageContentVO.setNodeContent(signPatientManageRouteNode.getTextRemindContent());
            }
        }
        return AjaxResult.success(messageContentVO);
    }

    /**
     * 修改消息状态
     *
     * @param signPatientManageRouteNode 节点信息
     * @return int
     */
    @Override
    public AjaxResult updateMessageStatus(SignPatientManageRouteNode signPatientManageRouteNode) {
        return AjaxResult.success(signPatientManageRouteNodeMapper.updateMessageStatus(signPatientManageRouteNode));
    }

    /**
     * 任务完成情况
     *
     * @param manageRouteNodeId 节点id
     * @return AjaxResult
     */
    @Override
    public AjaxResult nodeExecuteStatus(Long manageRouteNodeId) {
        //查询任务信息
        NodeExecuteStatus nodeExecuteStatus = homePageMapper.nodeExecuteStatus(manageRouteNodeId);
        //判断完成情况
        if (Objects.nonNull(nodeExecuteStatus) && Objects.isNull(nodeExecuteStatus.getPatientQuestionSubmitResultId())) {
            nodeExecuteStatus.setSign(0);
        }
        if (Objects.nonNull(nodeExecuteStatus) && Objects.nonNull(nodeExecuteStatus.getPatientQuestionSubmitResultId())) {
            nodeExecuteStatus.setSign(1);
        }
        return AjaxResult.success(nodeExecuteStatus);
    }

    /**
     * 保存宣教文章查看时间
     *
     * @param manageRouteNodeId 节点id
     * @return AjaxResult
     */
    @Override
    public AjaxResult viewPropaganda(Long manageRouteNodeId) {
        SignPatientManageRouteNode signPatientManageRouteNode = new SignPatientManageRouteNode();
        signPatientManageRouteNode.setId(manageRouteNodeId);
        signPatientManageRouteNode.setNodeFinishDate(LocalDateTime.now());
        signPatientManageRouteNodeMapper.updateSignPatientManageRouteNode(signPatientManageRouteNode);
	    // 修改任务统计表
	    patientTaskStatisticsService.updateNumByManageRouteNodeId(manageRouteNodeId, TaskStatisticsTypeConstants.TASK_FINISH_NUM, 1);
        return AjaxResult.success();
    }
}
