package com.openhis.clinical.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.core.common.utils.DateUtils;
import com.core.common.utils.SecurityUtils;
import com.openhis.clinical.domain.Procedure;
import com.openhis.clinical.mapper.ProcedureMapper;
import com.openhis.clinical.service.IProcedurePerformerService;
import com.openhis.clinical.service.IProcedureService;
import com.openhis.common.constant.CommonConstants;
import com.openhis.common.enums.DelFlag;
import com.openhis.common.enums.EventStatus;
import com.openhis.common.enums.ProcedureCategory;
import com.openhis.medication.domain.MedicationRequest;

/**
 * 手术与治疗管理Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Service
public class ProcedureServiceImpl extends ServiceImpl<ProcedureMapper, Procedure> implements IProcedureService {

    @Resource
    private IProcedurePerformerService procedurePerformerService;

    /**
     * 查询执行记录
     *
     * @param requestIds 医嘱ids
     * @param requestTable 医嘱所在表
     * @return 执行记录
     */
    @Override
    public List<Procedure> getProcedureRecords(List<Long> requestIds, String requestTable) {
        LambdaQueryWrapper<Procedure> queryWrapper = new LambdaQueryWrapper<Procedure>()
            .in(Procedure::getRequestId, requestIds).eq(Procedure::getDeleteFlag, DelFlag.NO.getCode());
        if (requestTable != null) {
            queryWrapper.eq(Procedure::getRequestTable, requestTable);
        }
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 添加执行记录
     *
     * @param encounterId 就诊id
     * @param patientId 患者id
     * @param requestId 医嘱id
     * @param requestTable 医嘱所在表
     * @param eventStatus 执行状态
     * @param procedureCategory 执行种类
     * @param locationId 执行位置
     * @param exeDate 执行时间
     * @param groupId 组号
     * @param refundId 取消执行id
     * @return 是否成功
     */
    @Override
    public Long addProcedureRecord(Long encounterId, Long patientId, Long requestId, String requestTable,
        EventStatus eventStatus, ProcedureCategory procedureCategory, Long locationId, Date exeDate, Long groupId,
        Long refundId) {
        Long orgId = SecurityUtils.getLoginUser().getOrgId();
        Procedure procedure = new Procedure();
        procedure
            // 医嘱id
            .setRequestId(requestId)
            // 医嘱所在表
            .setRequestTable(requestTable)
            // 就诊id
            .setEncounterId(encounterId)
            // 执行科室
            .setOrgId(orgId)
            // 患者id
            .setPatientId(patientId)
            // 执行时间
            .setOccurrenceTime(exeDate)
            // 执行状态
            .setStatusEnum(eventStatus.getValue())
            // 执行种类
            .setCategoryEnum(procedureCategory.getValue());
        if (locationId != null) {
            // 执行位置
            procedure.setLocationId(locationId);
        }
        if (groupId != null) {
            // 组号
            procedure.setGroupId(groupId);
        }
        if (refundId != null) {
            procedure.setRefundId(refundId);
        }
        int result = baseMapper.insert(procedure);
        if (result > 0) {
            // 添加执行人子表
            boolean performerResult = procedurePerformerService.addPerformRecord(procedure.getId(), exeDate);
            if (!performerResult) {
                return null;
            }
        } else {
            return null;
        }
        return procedure.getId();
    }

    /**
     * 添加药品执行记录
     *
     * @param medicationRequest 药品医嘱
     * @param procedureCategory 执行种类
     * @param locationId 执行位置
     * @return 是否成功
     */
    @Override
    public boolean addMedicationProcedureRecord(MedicationRequest medicationRequest,
        ProcedureCategory procedureCategory, Long locationId) {
        Date now = DateUtils.getNowDate();
        Long orgId = SecurityUtils.getLoginUser().getOrgId();
        Procedure procedure = new Procedure();
        procedure
            // 药品医嘱id
            .setRequestId(medicationRequest.getId())
            // 药品申请表
            .setRequestTable(CommonConstants.TableName.MED_MEDICATION_REQUEST)
            // 就诊id
            .setEncounterId(medicationRequest.getEncounterId())
            // 执行科室
            .setOrgId(orgId)
            // 患者id
            .setPatientId(medicationRequest.getPatientId())
            // 执行时间
            .setOccurrenceTime(now)
            // 执行种类
            .setCategoryEnum(procedureCategory.getValue())
            // 执行位置
            .setLocationId(locationId);
        int result = baseMapper.insert(procedure);
        if (result > 0) {
            // 添加执行人子表
            return procedurePerformerService.addPerformRecord(procedure.getId(), now);
        } else {
            return false;
        }
    }
}