package com.fs.hospital.service.impl;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.hospital.domain.*;
import com.fs.hospital.mapper.*;
import com.fs.hospital.param.HospitalOrderItemParam;
import com.fs.hospital.vo.HospitalOrderItemListHVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.service.IHospitalOrderItemService;

/**
 * 诊疗项目Service业务层处理
 *
 * @author fs
 * @date 2024-03-22
 */
@Service
public class HospitalOrderItemServiceImpl implements IHospitalOrderItemService
{
    @Autowired
    private HospitalOrderItemMapper hospitalOrderItemMapper;
    @Autowired
    private HospitalTreatItemMapper hospitalTreatItemMapper;
    @Autowired
    private HospitalExamItemMapper hospitalExamItemMapper;
    @Autowired
    private HospitalInspectItemMapper hospitalInspectItemMapper;
    @Autowired
    private HospitalProductMapper hospitalProductMapper;
    @Autowired
    private HospitalOutpatientOrderMapper outpatientOrderMapper;

    /**
     * 查询诊疗项目
     *
     * @param itemId 诊疗项目主键
     * @return 诊疗项目
     */
    @Override
    public HospitalOrderItem selectHospitalOrderItemByItemId(Long itemId)
    {
        return hospitalOrderItemMapper.selectHospitalOrderItemByItemId(itemId);
    }

    /**
     * 查询诊疗项目列表
     *
     * @param hospitalOrderItem 诊疗项目
     * @return 诊疗项目
     */
    @Override
    public List<HospitalOrderItem> selectHospitalOrderItemList(HospitalOrderItem hospitalOrderItem)
    {
        return hospitalOrderItemMapper.selectHospitalOrderItemList(hospitalOrderItem);
    }

    /**
     * 新增诊疗项目
     *
     * @param hospitalOrderItem 诊疗项目
     * @return 结果
     */
    @Override
    public int insertHospitalOrderItem(HospitalOrderItem hospitalOrderItem)
    {
        return hospitalOrderItemMapper.insertHospitalOrderItem(hospitalOrderItem);
    }

    /**
     * 修改诊疗项目
     *
     * @param hospitalOrderItem 诊疗项目
     * @return 结果
     */
    @Override
    public int updateHospitalOrderItem(HospitalOrderItem hospitalOrderItem)
    {
        return hospitalOrderItemMapper.updateHospitalOrderItem(hospitalOrderItem);
    }

    /**
     * 批量删除诊疗项目
     *
     * @param itemIds 需要删除的诊疗项目主键
     * @return 结果
     */
    @Override
    public int deleteHospitalOrderItemByItemIds(Long[] itemIds)
    {
        return hospitalOrderItemMapper.deleteHospitalOrderItemByItemIds(itemIds);
    }

    /**
     * 删除诊疗项目信息
     *
     * @param itemId 诊疗项目主键
     * @return 结果
     */
    @Override
    public int deleteHospitalOrderItemByItemId(Long itemId)
    {
        return hospitalOrderItemMapper.deleteHospitalOrderItemByItemId(itemId);
    }

    @Override
    public R selectHospitalOrderItemListHVO(HospitalOrderItemParam param) {
        //检查
        if (param.getType()==1) {
            List<HospitalOrderItemListHVO> list = hospitalExamItemMapper.selectHospitalOrderItemListHVOList(param);
            return R.ok().put("data", list);
        }
        //检验
        if (param.getType()==2){
            List<HospitalOrderItemListHVO> list = hospitalInspectItemMapper.selectHospitalOrderItemListHVOList(param);
            return R.ok().put("data", list);
        }
        //治疗
        if (param.getType()==3){
            List<HospitalOrderItemListHVO> list = hospitalTreatItemMapper.selectHospitalOrderItemListHVOList(param);
            return R.ok().put("data", list);
        }

        //商品
        if (param.getType()==4){
            List<HospitalOrderItemListHVO> list = hospitalProductMapper.selectHospitalOrderItemListHVOList(param);
            return R.ok().put("data", list);
        }
        return R.error("暂无数据");
    }

    @Override
    public R finishRegistration(HospitalOrderItem orderItem) {
        if (hospitalOrderItemMapper.updateHospitalOrderItem(orderItem)>0){
            if (orderItem.getItemType()==0){
                HospitalOutpatientOrder outpatientOrder = outpatientOrderMapper.selectHospitalOutpatientOrderByOrderId(orderItem.getOutpatientOrderId());
                if (outpatientOrder!=null && outpatientOrder.getIsPay()==0){
                    outpatientOrder.setIsPay(1);
                    outpatientOrderMapper.updateHospitalOutpatientOrder(outpatientOrder);
                }
            }
            return R.ok();
        }
        return R.error();
    }

    @Override
    public HospitalOrderItem selectHospitalOrderItemByRegistration(Long orderId) {
        return hospitalOrderItemMapper.selectHospitalOrderItemByRegistration(orderId);
    }
}
