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

import com.xinelu.common.annotation.DataScope;
import com.xinelu.common.constant.TaskStatisticsTypeConstants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.utils.AgeUtil;
import com.xinelu.manage.domain.patienttaskexecuterecord.PatientTaskExecuteRecord;
import com.xinelu.manage.mapper.patientquestionsubmitresult.PatientQuestionSubmitResultMapper;
import com.xinelu.manage.mapper.patienttaskexecuterecord.PatientTaskExecuteRecordMapper;
import com.xinelu.manage.service.patienttaskexecuterecord.IPatientTaskExecuteRecordService;
import com.xinelu.manage.service.patienttaskstatistics.IPatientTaskStatisticsService;
import com.xinelu.manage.vo.patienttaskexecuterecord.PatientTaskExecuteRecordVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 患者管理任务执行记录Service业务层处理
 *
 * @author xinelu
 * @date 2024-03-25
 */
@Service
public class PatientTaskExecuteRecordServiceImpl implements IPatientTaskExecuteRecordService {
    @Resource
    private PatientTaskExecuteRecordMapper patientTaskExecuteRecordMapper;
    @Resource
    private PatientQuestionSubmitResultMapper submitResultMapper;
	@Resource
	private IPatientTaskStatisticsService patientTaskStatisticsService;

    /**
     * 查询患者管理任务执行记录
     *
     * @param id 患者管理任务执行记录主键
     * @return 患者管理任务执行记录
     */
    @Override
    public PatientTaskExecuteRecord selectPatientTaskExecuteRecordById(Long id) {
        return patientTaskExecuteRecordMapper.selectPatientTaskExecuteRecordById(id);
    }

    /**
     * 查询患者管理任务执行记录列表
     *
     * @param patientTaskExecuteRecord 患者管理任务执行记录
     * @return 患者管理任务执行记录
     */
    @Override
    @DataScope(agencyAlias = "pi", deptAlias = "pi")
    public List<PatientTaskExecuteRecordVO> selectPatientTaskExecuteRecordList(PatientTaskExecuteRecordVO patientTaskExecuteRecord) {
        return patientTaskExecuteRecordMapper.selectPatientAndExecuteRecordList(patientTaskExecuteRecord);
    }

    /**
     * 新增患者管理任务执行记录
     *
     * @param patientTaskExecuteRecord 患者管理任务执行记录
     * @return 结果
     */
    @Override
    public int insertPatientTaskExecuteRecord(PatientTaskExecuteRecord patientTaskExecuteRecord) {
        patientTaskExecuteRecord.setCreateTime(LocalDateTime.now());
	    int flag = patientTaskExecuteRecordMapper.insertPatientTaskExecuteRecord(patientTaskExecuteRecord);
	    // 任务统计表修改
	    patientTaskStatisticsService.updateNum(patientTaskExecuteRecord.getPatientId(), TaskStatisticsTypeConstants.TASK_EXECUTE_NUM, 1);
		return flag;
    }

    /**
     * 修改患者管理任务执行记录
     *
     * @param patientTaskExecuteRecord 患者管理任务执行记录
     * @return 结果
     */
    @Override
    public int updatePatientTaskExecuteRecord(PatientTaskExecuteRecord patientTaskExecuteRecord) {
        patientTaskExecuteRecord.setUpdateTime(LocalDateTime.now());
        return patientTaskExecuteRecordMapper.updatePatientTaskExecuteRecord(patientTaskExecuteRecord);
    }

    /**
     * 批量删除患者管理任务执行记录
     *
     * @param ids 需要删除的患者管理任务执行记录主键
     * @return 结果
     */
    @Override
    public int deletePatientTaskExecuteRecordByIds(Long[] ids) {
        return patientTaskExecuteRecordMapper.deletePatientTaskExecuteRecordByIds(ids);
    }

    /**
     * 删除患者管理任务执行记录信息
     *
     * @param id 患者管理任务执行记录主键
     * @return 结果
     */
    @Override
    public int deletePatientTaskExecuteRecordById(Long id) {
        return patientTaskExecuteRecordMapper.deletePatientTaskExecuteRecordById(id);
    }

    /**
     * 根据患者信息查询就诊记录
     *
     * @param patientId id
     * @return AjaxResult
     */
    @Override
    public AjaxResult selectVisitRecord(Long patientId) {
        if (Objects.isNull(patientId)) {
            return AjaxResult.error("请选择要查询的信息！");
        }
        PatientTaskExecuteRecordVO patientTaskExecuteRecordVO = patientTaskExecuteRecordMapper.selectVisitRecord(patientId);
        if (Objects.nonNull(patientTaskExecuteRecordVO) && Objects.nonNull(patientTaskExecuteRecordVO.getBirthDate())) {
            patientTaskExecuteRecordVO.setAge(AgeUtil.getAgeMonth(patientTaskExecuteRecordVO.getBirthDate().toString()));
        }
        return AjaxResult.success(patientTaskExecuteRecordVO);
    }

    /**
     * 根据任务执行记录查询患者问卷信息
     *
     * @param taskExecuteRecordId 患者管理任务执行记录表id
     * @return PatientQuestionSubmitResultVO
     */
    @Override
    public AjaxResult selectPatientQuestionSubmit(Long taskExecuteRecordId) {
        return AjaxResult.success(submitResultMapper.selectResultByTaskExecuteRecordId(taskExecuteRecordId, null));
    }
}
