package com.fs.hospital.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.fs.common.utils.DateUtils;
import com.fs.hospital.domain.HospitalOrderItem;
import com.fs.hospital.mapper.HospitalOrderItemMapper;
import com.fs.hospital.mapper.HospitalOutpatientOrderMapper;
import com.fs.hospital.vo.HospitalOrderItemListHVO;
import com.fs.hospital.vo.HospitalPatientHVO;
import com.fs.hospital.vo.HospitalPharmacyOrderHVO;
import com.fs.hospital.vo.HospitalPharmacyOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.mapper.HospitalPatientMapper;
import com.fs.hospital.domain.HospitalPatient;
import com.fs.hospital.service.IHospitalPatientService;

/**
 * 患者Service业务层处理
 *
 * @author fs
 * @date 2024-03-04
 */
@Service
public class HospitalPatientServiceImpl implements IHospitalPatientService
{
    @Autowired
    private HospitalPatientMapper hospitalPatientMapper;

    @Autowired
    private HospitalOutpatientOrderMapper hospitalOutpatientOrderMapper;

    @Autowired
    private HospitalOrderItemMapper hospitalOrderItemMapper;

    @Autowired
    private HospitalOutpatientOrderServiceImpl hospitalOutpatientOrderService;

    /**
     * 查询患者
     *
     * @param patientId 患者主键
     * @return 患者
     */
    @Override
    public HospitalPatient selectHospitalPatientByPatientId(Long patientId)
    {
        return hospitalPatientMapper.selectHospitalPatientByPatientId(patientId);
    }

    /**
     * 查询患者列表
     *
     * @param hospitalPatient 患者
     * @return 患者
     */
    @Override
    public List<HospitalPatient> selectHospitalPatientList(HospitalPatient hospitalPatient)
    {
        return hospitalPatientMapper.selectHospitalPatientList(hospitalPatient);
    }

    /**
     * 查询患者就诊大概历史
     *
     * @param patientId 患者id
     * @return 患者就诊历史集合
     */
    @Override
    public List<HospitalPharmacyOrderVO> getCaseRecords(Long patientId) {

        //获取到当前患者的历史诊断
        return hospitalOutpatientOrderMapper.getCaseRecords(patientId);
    }

    /**
     *  查询患者的详细就诊记录（门诊单）
     */
    @Override
    public List<HospitalOrderItemListHVO> getDetailedCaseRecords(Long orderId,int type) {
        List<HospitalOrderItem> orderItemsHistory=new ArrayList<>();
        //查看根据诊断历史 查看的（门诊单类型）
        if (type==1){
            orderItemsHistory  = hospitalOrderItemMapper.selectHospitalOrderItemByOutpatientOrder(orderId);
        }else if (type==2){
            //这种为收费单类型
            orderItemsHistory  = hospitalOrderItemMapper.selectHospitalOrderItemByOrderAndType(orderId);
        }

        //获取患者的历史诊疗项目单
        //将诊疗项目单存入各次记录中(复用)
        List<HospitalOrderItemListHVO> orderItemList = hospitalOutpatientOrderService.getOrderItemList(orderItemsHistory);

        return orderItemList;
    }


    /**
     * 查询患者列表（模糊）
     *
     * @param hospitalPatient 患者
     * @return 患者
     */
    @Override
    public List<HospitalPatient> selectHospitalPatientScreenList(HospitalPatient hospitalPatient)
    {
        return hospitalPatientMapper.selectHospitalPatientScreenList(hospitalPatient);
    }

    /**
     * 新增患者
     *
     * @param hospitalPatient 患者
     * @return 结果
     */
    @Override
    public int insertHospitalPatient(HospitalPatient hospitalPatient)
    {
        hospitalPatient.setCreateTime(DateUtils.getNowDate());
        return hospitalPatientMapper.insertHospitalPatient(hospitalPatient);
    }

    /**
     * 修改患者
     *
     * @param hospitalPatient 患者
     * @return 结果
     */
    @Override
    public int updateHospitalPatient(HospitalPatient hospitalPatient)
    {
        hospitalPatient.setUpdateTime(DateUtils.getNowDate());
        return hospitalPatientMapper.updateHospitalPatient(hospitalPatient);
    }

    /**
     * 批量删除患者
     *
     * @param patientIds 需要删除的患者主键
     * @return 结果
     */
    @Override
    public int deleteHospitalPatientByPatientIds(Long[] patientIds)
    {
        return hospitalPatientMapper.deleteHospitalPatientByPatientIds(patientIds);
    }

    /**
     * 删除患者信息
     *
     * @param patientId 患者主键
     * @return 结果
     */
    @Override
    public int deleteHospitalPatientByPatientId(Long patientId)
    {
        return hospitalPatientMapper.deleteHospitalPatientByPatientId(patientId);
    }

    @Override
    public HospitalPatientHVO selectHospitalPatientPVOByPatientId(Long patientId) {
        return hospitalPatientMapper.selectHospitalPatientPVOByPatientId(patientId);
    }

    @Override
    public List<HospitalPatient> selectHospitalPatientListPVO(HospitalPatient hospitalPatient) {
        return hospitalPatientMapper.selectHospitalPatientListPVO(hospitalPatient);
    }
}
