package com.openhis.medication.service.impl;

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

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openhis.common.enums.DelFlag;
import com.openhis.common.enums.RequestStatus;
import com.openhis.medication.domain.MedicationRequest;
import com.openhis.medication.mapper.MedicationRequestMapper;
import com.openhis.medication.service.IMedicationRequestService;

/**
 * 药品请求管理Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Service
public class MedicationRequestServiceImpl extends ServiceImpl<MedicationRequestMapper, MedicationRequest>
    implements IMedicationRequestService {

    /**
     * 更新请求状态：已完成
     *
     * @param requestIdList 药品请求id列表
     * @param practitionerId 校对人
     * @param checkDate 校对时间
     */
    @Override
    public void updateCompletedStatusBatch(List<Long> requestIdList, Long practitionerId, Date checkDate) {
        baseMapper.update(new MedicationRequest().setStatusEnum(RequestStatus.COMPLETED.getValue()),
            new LambdaUpdateWrapper<MedicationRequest>().in(MedicationRequest::getId, requestIdList));
    }

    /**
     * 更新请求状态：待发送
     *
     * @param requestIdList 药品请求id列表
     * @param practitionerId 校对人
     * @param checkDate 校对时间
     */
    @Override
    public void updateDraftStatusBatch(List<Long> requestIdList, Long practitionerId, Date checkDate) {
        LambdaUpdateWrapper<MedicationRequest> updateWrapper =
            new LambdaUpdateWrapper<MedicationRequest>().in(MedicationRequest::getId, requestIdList)
                .set(MedicationRequest::getStatusEnum, RequestStatus.DRAFT.getValue());
        if (practitionerId != null) {
            updateWrapper.set(MedicationRequest::getPerformerCheckId, practitionerId);
        }
        if (checkDate != null) {
            updateWrapper.set(MedicationRequest::getCheckTime, checkDate);
        }
        baseMapper.update(null, updateWrapper);
    }

    /**
     * 更新请求状态：取消
     *
     * @param requestIdList 药品请求id列表
     * @param practitionerId 校对人
     * @param checkDate 校对时间
     */
    @Override
    public void updateCancelledStatusBatch(List<Long> requestIdList, Long practitionerId, Date checkDate) {
        LambdaUpdateWrapper<MedicationRequest> updateWrapper =
            new LambdaUpdateWrapper<MedicationRequest>().in(MedicationRequest::getId, requestIdList)
                .set(MedicationRequest::getStatusEnum, RequestStatus.CANCELLED.getValue());
        if (practitionerId != null) {
            updateWrapper.set(MedicationRequest::getPerformerCheckId, practitionerId);
        }
        if (checkDate != null) {
            updateWrapper.set(MedicationRequest::getCheckTime, checkDate);
        }
        baseMapper.update(null, updateWrapper);
    }

    /**
     * 根据药品id获取药品请求
     *
     * @param medicationId 药品id
     * @return 药品请求列表
     */
    @Override
    public List<MedicationRequest> getMedRequestByMedicationId(Long medicationId) {
        return baseMapper
            .selectList(new LambdaQueryWrapper<MedicationRequest>().eq(MedicationRequest::getMedicationId, medicationId)
                .eq(MedicationRequest::getDeleteFlag, DelFlag.NO.getCode()));
    }
}
