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

import com.xinelu.common.constant.SignRecordServiceStatusConstants;
import com.xinelu.common.constant.TaskStatisticsTypeConstants;
import com.xinelu.manage.domain.patientinfo.PatientInfo;
import com.xinelu.manage.domain.patienttaskstatistics.PatientTaskStatistics;
import com.xinelu.manage.domain.signpatientmanageroute.SignPatientManageRoute;
import com.xinelu.manage.domain.signpatientmanageroutenode.SignPatientManageRouteNode;
import com.xinelu.manage.domain.signpatientrecord.SignPatientRecord;
import com.xinelu.manage.mapper.patientinfo.PatientInfoMapper;
import com.xinelu.manage.mapper.patienttaskstatistics.PatientTaskStatisticsMapper;
import com.xinelu.manage.mapper.signpatientmanageroute.SignPatientManageRouteMapper;
import com.xinelu.manage.mapper.signpatientmanageroutenode.SignPatientManageRouteNodeMapper;
import com.xinelu.manage.mapper.signpatientrecord.SignPatientRecordMapper;
import com.xinelu.manage.service.patienttaskstatistics.IPatientTaskStatisticsService;
import com.xinelu.manage.vo.patientinfo.PatientNextTaskVo;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 患者任务统计Service业务层处理
 *
 * @author haown
 * @date 2024-08-09
 */
@Service
public class PatientTaskStatisticsServiceImpl implements IPatientTaskStatisticsService {
    @Resource
    private PatientTaskStatisticsMapper patientTaskStatisticsMapper;
    @Resource
    private SignPatientRecordMapper signPatientRecordMapper;
    @Resource
    private SignPatientManageRouteNodeMapper signPatientManageRouteNodeMapper;
    @Resource
    private SignPatientManageRouteMapper signPatientManageRouteMapper;
    @Resource
    private PatientInfoMapper patientInfoMapper;

    /**
     * 查询患者任务统计
     *
     * @param patientId 患者任务统计主键
     * @return 患者任务统计
     */
    @Override
    public PatientTaskStatistics selectPatientTaskStatisticsByPatientId(Long patientId) {
        return patientTaskStatisticsMapper.selectPatientTaskStatisticsByPatientId(patientId);
    }

    /**
     * 查询患者任务统计列表
     *
     * @param patientTaskStatistics 患者任务统计
     * @return 患者任务统计
     */
    @Override
    public List<PatientTaskStatistics> selectPatientTaskStatisticsList(PatientTaskStatistics patientTaskStatistics) {
        return patientTaskStatisticsMapper.selectPatientTaskStatisticsList(patientTaskStatistics);
    }

    /**
     * 新增患者任务统计
     *
     * @param patientTaskStatistics 患者任务统计
     * @return 结果
     */
    @Override
    public int insertPatientTaskStatistics(PatientTaskStatistics patientTaskStatistics) {
        return patientTaskStatisticsMapper.insertPatientTaskStatistics(patientTaskStatistics);
    }

    /**
     * 修改患者任务统计
     *
     * @param patientTaskStatistics 患者任务统计
     * @return 结果
     */
    @Override
    public int updatePatientTaskStatistics(PatientTaskStatistics patientTaskStatistics) {
        return patientTaskStatisticsMapper.updatePatientTaskStatistics(patientTaskStatistics);
    }

    /**
     * 批量删除患者任务统计
     *
     * @param signPatientRecordIds 需要删除的患者任务统计主键
     * @return 结果
     */
    @Override
    public int deletePatientTaskStatisticsBySignPatientRecordIds(Long[] signPatientRecordIds) {
        return patientTaskStatisticsMapper.deletePatientTaskStatisticsByPatientIds(signPatientRecordIds);
    }

    /**
     * 删除患者任务统计信息
     *
     * @param signPatientRecordId 患者任务统计主键
     * @return 结果
     */
    @Override
    public int deletePatientTaskStatisticsBySignPatientRecordId(Long signPatientRecordId) {
        return patientTaskStatisticsMapper.deletePatientTaskStatisticsByPatientId(signPatientRecordId);
    }

    @Override
    public int insertOrUpdate(PatientTaskStatistics patientTaskStatistics) {
        int flag = 0;
        String serviceStatus = "";
        String routeCheckStatus = "";

        //如果有签约id，则获取相关状态；否则，统计表不再记录 签约相关属性  20241212 zyk
        if (Objects.nonNull(patientTaskStatistics.getSignPatientRecordId())) {
            SignPatientRecord signPatientRecord = signPatientRecordMapper.selectByPrimaryKey(patientTaskStatistics.getSignPatientRecordId());
            if (ObjectUtils.isNotEmpty(signPatientRecord)) {
                serviceStatus = signPatientRecord.getServiceStatus();
                routeCheckStatus = signPatientRecord.getRouteCheckStatus();
            }
        }

        PatientTaskStatistics patientTask = patientTaskStatisticsMapper.selectPatientTaskStatisticsByPatientId(patientTaskStatistics.getPatientId());
        if (ObjectUtils.isEmpty(patientTask)) {
            // 插入数据
            flag = patientTaskStatisticsMapper.insertPatientTaskStatistics(patientTaskStatistics);
            // 查询最新的任务
            PatientNextTaskVo patientNextTaskVo = patientTaskStatisticsMapper.getPatientNextTask(patientTaskStatistics.getPatientId());
            if (ObjectUtils.isNotEmpty(patientNextTaskVo)) {
                patientTaskStatistics.setNextTaskName(patientNextTaskVo.getTaskName());
                patientTaskStatistics.setNextTaskTime(patientNextTaskVo.getExecuteDateTime());
                patientTaskStatisticsMapper.updatePatientTaskStatistics(patientTaskStatistics);
            }
        } else {
            // 修改数据
            patientTask.setServiceStatus(serviceStatus);
            patientTask.setRouteCheckStatus(routeCheckStatus);
            patientTask.setTaskNum(patientTask.getTaskNum().add(patientTaskStatistics.getTaskNum()));
            patientTask.setTaskExecuteNum(patientTask.getTaskExecuteNum().add(patientTaskStatistics.getTaskExecuteNum()));
            patientTask.setTaskFinishNum(patientTask.getTaskFinishNum().add(patientTaskStatistics.getTaskFinishNum()));
            // 查询最新的任务
            PatientNextTaskVo patientNextTaskVo = patientTaskStatisticsMapper.getPatientNextTask(patientTaskStatistics.getPatientId());
            if (ObjectUtils.isNotEmpty(patientNextTaskVo)) {
                patientTask.setNextTaskName(patientNextTaskVo.getTaskName());
                patientTask.setNextTaskTime(patientNextTaskVo.getExecuteDateTime());
            }
            flag = patientTaskStatisticsMapper.updatePatientTaskStatistics(patientTask);
        }

        return flag;
    }

    /**
     * @param patientId 签约记录主键
     * @param type      字段种类，TASK_NUM：任务总数，TASK_EXECUTE_NUM: 任务执行数，TASK_FINISH_NUM：任务完成数
     * @param count     数量
     * @return 结果
     * @description 修改任务总数、执行数、完成数
     * @Author haown
     * @Date 2024-08-09 17:43
     */
    @Override
    public int updateNum(Long patientId, String type, Integer count) {
        PatientTaskStatistics patientTaskStatistics = patientTaskStatisticsMapper.selectPatientTaskStatisticsByPatientId(patientId);
        PatientInfo patientInfo = patientInfoMapper.selectPatientInfoById(patientId);
        // if (ObjectUtils.isNotEmpty(patientInfo) && StringUtils.equals(SignRecordServiceStatusConstants.SERVICE_CENTER, patientInfo.getServiceStatus()))
        // 针对 导入用户，未签约的情况，不再判断签约服务状态SignRecordServiceStatusConstants.SERVICE_CENTER
        if (ObjectUtils.isNotEmpty(patientInfo)) {
            PatientTaskStatistics patientTaskStatisticsView = patientTaskStatisticsMapper.selectNum(patientId);
            switch (type) {
                case TaskStatisticsTypeConstants.TASK_NUM:
                    patientTaskStatistics.setTaskNum(patientTaskStatistics.getTaskNum().add(BigDecimal.valueOf(count)));
                    break;
                case TaskStatisticsTypeConstants.TASK_EXECUTE_NUM:
                    if (ObjectUtils.isEmpty(patientTaskStatisticsView)) {
                        patientTaskStatistics.setTaskExecuteNum(patientTaskStatistics.getTaskExecuteNum().add(BigDecimal.valueOf(count)));
                    } else {
                        patientTaskStatistics.setTaskExecuteNum(Objects.isNull(patientTaskStatisticsView.getTaskExecuteNum())? BigDecimal.ZERO :patientTaskStatisticsView.getTaskExecuteNum());
                        patientTaskStatistics.setTaskFinishNum(Objects.isNull(patientTaskStatisticsView.getTaskFinishNum())? BigDecimal.ZERO :patientTaskStatisticsView.getTaskFinishNum());
                    }
                    // 下次任务
                    PatientNextTaskVo patientNextTask = patientTaskStatisticsMapper.getPatientNextTask(patientId);
                    if (ObjectUtils.isNotEmpty(patientNextTask)) {
                        patientTaskStatistics.setNextTaskName(patientNextTask.getTaskName());
                        patientTaskStatistics.setNextTaskTime(patientNextTask.getExecuteDateTime());
                        patientTaskStatisticsMapper.updatePatientTaskStatistics(patientTaskStatistics);
                    }
                    break;
                case TaskStatisticsTypeConstants.TASK_FINISH_NUM:
                    if (ObjectUtils.isEmpty(patientTaskStatisticsView)) {
                        patientTaskStatistics.setTaskFinishNum(patientTaskStatistics.getTaskFinishNum().add(BigDecimal.valueOf(count)));
                    } else {
                        patientTaskStatistics.setTaskExecuteNum(patientTaskStatisticsView.getTaskExecuteNum());
                        patientTaskStatistics.setTaskFinishNum(patientTaskStatisticsView.getTaskFinishNum());
                    }
                    break;
            }
        }
        return patientTaskStatisticsMapper.updatePatientTaskStatistics(patientTaskStatistics);
    }

    @Override
    public int updateNumByManageRouteNodeId(Long manageRouteNodeId, String type, Integer count) {
        SignPatientManageRouteNode signPatientManageRouteNode = signPatientManageRouteNodeMapper.selectSignPatientManageRouteNodeById(manageRouteNodeId);
        if (ObjectUtils.isNotEmpty(signPatientManageRouteNode)) {
            SignPatientManageRoute signPatientManageRoute = signPatientManageRouteMapper.selectSignPatientManageRouteById(signPatientManageRouteNode.getManageRouteId());
            if (ObjectUtils.isNotEmpty(signPatientManageRoute)) {
                return updateNum(signPatientManageRoute.getPatientId(), type, count);
            }
        }
        return 0;
    }

    /**
     * @param patientIdList 患者主键列表
     * @description 修改患者统计表任务数量、下次任务
     * @Author haown
     * @Date 2024-8-14 11:37
     */
    @Override
    public void updateNum(List<Long> patientIdList) {
        // 修改任务执行数量、任务完成数量
        patientIdList.forEach(patientId -> {
            PatientTaskStatistics patientTaskStatistics = patientTaskStatisticsMapper.selectNum(patientId);
            // 修改数据
            if (ObjectUtils.isNotEmpty(patientTaskStatistics)) {
                // 下次任务
                PatientNextTaskVo patientNextTask = patientTaskStatisticsMapper.getPatientNextTask(patientId);
                patientTaskStatistics.setNextTaskName(patientNextTask.getTaskName());
                patientTaskStatistics.setNextTaskTime(patientNextTask.getExecuteDateTime());
                patientTaskStatisticsMapper.updatePatientTaskStatistics(patientTaskStatistics);
            }
        });
    }
}
