package com.openhis.web.jlau.appservice.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.core.common.core.domain.R;
import com.core.common.exception.ServiceException;
import com.core.common.utils.MessageUtils;
import com.core.common.utils.SecurityUtils;
import com.core.common.utils.StringUtils;
import com.openhis.administration.domain.ChargeItem;
import com.openhis.administration.service.IChargeItemService;
import com.openhis.common.constant.PromptMsgConstant;
import com.openhis.common.enums.ChargeItemStatus;
import com.openhis.common.enums.Whether;
import com.openhis.jlau.domain.ReviewPrescriptionRecords;
import com.openhis.jlau.service.IReviewPrescriptionRecordsService;
import com.openhis.web.jlau.appservice.IReviewPrescriptionRecordsAppService;
import com.openhis.web.jlau.dto.ReviewPrescriptionRecordsDto;
import com.openhis.web.jlau.mapper.ReviewPrescriptionRecordsAppMapper;

import lombok.extern.slf4j.Slf4j;

/**
 * 农大审方记录 应用实现类
 */
@Slf4j
@Service
public class ReviewPrescriptionRecordsAppServiceImpl implements IReviewPrescriptionRecordsAppService {

    @Resource
    ReviewPrescriptionRecordsAppMapper reviewPrescriptionRecordsAppMapper;

    @Resource
    IChargeItemService chargeItemService;

    @Resource
    IReviewPrescriptionRecordsService reviewPrescriptionRecordsService;

    /**
     * 审核处方
     *
     * @param reviewPrescriptionRecordsDto 审方记录dto
     * @return 结果
     */
    @Override
    public R<?> reviewPrescription(ReviewPrescriptionRecordsDto reviewPrescriptionRecordsDto) {
        // 参与者id
        Long practitionerId = SecurityUtils.getLoginUser().getPractitionerId();
        // 就诊id
        Long encounterId = reviewPrescriptionRecordsDto.getEncounterId();
        // 处方号
        String prescriptionNo = reviewPrescriptionRecordsDto.getPrescriptionNo();
        // 药品请求ids
        String medRequestIds = reviewPrescriptionRecordsDto.getMedRequestIds();
        if (StringUtils.isNotEmpty(medRequestIds)) {
            List<Long> medRequestIdList = Arrays.stream(medRequestIds.split(",")).map(String::trim) // 去除空格
                .map(Long::valueOf) // 转换为Long
                .collect(Collectors.toList());
            if (!medRequestIdList.isEmpty()) {
                List<ChargeItem> list = chargeItemService.list(new LambdaQueryWrapper<ChargeItem>()
                    .eq(ChargeItem::getStatusEnum, ChargeItemStatus.BILLED.getValue())
                    .in(ChargeItem::getServiceId, medRequestIdList));
                if (!list.isEmpty()) {
                    throw new ServiceException("已收费药品,无需审核");
                }
            }
        }
        // 审方id
        Long reviewId = reviewPrescriptionRecordsDto.getReviewId();
        // 通过标识
        Integer passFlag = reviewPrescriptionRecordsDto.getPassFlag();
        // 原因
        String reasonText = reviewPrescriptionRecordsDto.getReasonText();
        // 保存审核记录
        ReviewPrescriptionRecords reviewPrescriptionRecords;
        reviewPrescriptionRecords = new ReviewPrescriptionRecords();
        reviewPrescriptionRecords.setId(reviewId); // id
        reviewPrescriptionRecords.setEncounterId(encounterId); // 就诊id
        reviewPrescriptionRecords.setPrescriptionNo(prescriptionNo); // 处方号
        reviewPrescriptionRecords.setMedRequestIds(medRequestIds); // 药品请求ids
        reviewPrescriptionRecords.setPassFlag(passFlag); // 通过标识
        reviewPrescriptionRecords.setReasonText(reasonText); // 原因
        reviewPrescriptionRecords.setPractitionerId(practitionerId); // 审方人id
        reviewPrescriptionRecordsService.saveOrUpdate(reviewPrescriptionRecords);

        return R.ok(null, MessageUtils.createMessage(PromptMsgConstant.Common.M00004, new Object[] {"审核处方"}));
    }

    /**
     * 通过处方号查询审方记录
     *
     * @param prescriptionNo 处方号
     * @return 审方记录
     */
    @Override
    public R<?> getReviewByPrescriptionNo(String prescriptionNo) {
        List<ReviewPrescriptionRecordsDto> reviewPrescriptionRecords =
            reviewPrescriptionRecordsAppMapper.getReviewPrescriptionRecords(prescriptionNo, null, null);
        return R.ok(reviewPrescriptionRecords);
    }

    /**
     * 通过就诊id查询审方记录
     *
     * @param encounterId 就诊id
     * @return 审方记录
     */
    @Override
    public R<?> getReviewByEncounterId(Long encounterId) {
        List<ReviewPrescriptionRecordsDto> reviewPrescriptionRecords =
            reviewPrescriptionRecordsAppMapper.getReviewPrescriptionRecords(null, encounterId, Whether.YES.getValue());
        return R.ok(reviewPrescriptionRecords);
    }

}
