package com.fs.hospital.service.impl;

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

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.hospital.domain.HospitalOrderItem;
import com.fs.hospital.mapper.HospitalOrderItemMapper;
import com.fs.hospital.param.HospitalPharmacyOrderParam;
import com.fs.hospital.vo.HospitalOrderItemListHVO;
import com.fs.hospital.vo.HospitalPharmacyOrderHVO;
import com.fs.hospital.vo.HospitalPharmacyOrderListHVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.mapper.HospitalPharmacyOrderMapper;
import com.fs.hospital.domain.HospitalPharmacyOrder;
import com.fs.hospital.service.IHospitalPharmacyOrderService;

/**
 * 药房订单Service业务层处理
 *
 * @author fs
 * @date 2024-03-22
 */
@Service
public class HospitalPharmacyOrderServiceImpl implements IHospitalPharmacyOrderService
{
    @Autowired
    private HospitalPharmacyOrderMapper hospitalPharmacyOrderMapper;

    @Autowired
    private HospitalOrderItemMapper hospitalOrderItemMapper;

    @Autowired
    HospitalOutpatientOrderServiceImpl  hospitalOutpatientOrderService;


    /**
     * 查询药房订单
     *
     * @param orderId 药房订单主键
     * @return 药房订单
     */
//    @Override
//    public HospitalPharmacyOrder selectHospitalPharmacyOrderByOrderId(Long orderId)
//    {
//        return hospitalPharmacyOrderMapper.selectHospitalPharmacyOrderByOrderId(orderId);
//    }

//    @Override
//    public HospitalPharmacyOrderHVO selectHospitalPharmacyOrderPVOByOrderId(Long orderId)
//    {
//        return hospitalPharmacyOrderMapper.selectHospitalPharmacyOrderPVOByOrderId(orderId);
//    }
    @Override
    public R selectHospitalPharmacyOrderPVOByOrderId(Long orderId)
    {
        HospitalPharmacyOrderHVO hospitalPharmacyOrderHVO = hospitalPharmacyOrderMapper.selectHospitalPharmacyOrderPVOByOrderId(orderId);
        if (hospitalPharmacyOrderHVO !=null){
            //根据订单id 查询诊疗项目单
            List<HospitalOrderItem> hospitalOrderItems = hospitalOrderItemMapper.selectHospitalPharmacyOrderByOrder(orderId);
            //整合诊疗项目单详情（复用）
            List<HospitalOrderItemListHVO> list = hospitalOutpatientOrderService.getOrderItemList(hospitalOrderItems);

            return  R.ok().put("order",hospitalPharmacyOrderHVO).put("list",list);

        }
        return R.error("未找到订单信息");
    }
    /**
     * 查询药房订单列表
     *
     * @param hospitalPharmacyOrder 药房订单
     * @return 药房订单
     */
    @Override
    public List<HospitalPharmacyOrder> selectHospitalPharmacyOrderList(HospitalPharmacyOrder hospitalPharmacyOrder)
    {
        return hospitalPharmacyOrderMapper.selectHospitalPharmacyOrderList(hospitalPharmacyOrder);
    }

    /**
     * 新增药房订单
     *
     * @param hospitalPharmacyOrder 药房订单
     * @return 结果
     */
    @Override
    public int insertHospitalPharmacyOrder(HospitalPharmacyOrder hospitalPharmacyOrder)
    {
        hospitalPharmacyOrder.setCreateTime(DateUtils.getNowDate());
        return hospitalPharmacyOrderMapper.insertHospitalPharmacyOrder(hospitalPharmacyOrder);
    }

    /**
     * 修改药房订单
     *
     * @param hospitalPharmacyOrder 药房订单
     * @return 结果
     */
    @Override
    public int updateHospitalPharmacyOrder(HospitalPharmacyOrder hospitalPharmacyOrder)
    {
        hospitalPharmacyOrder.setUpdateTime(DateUtils.getNowDate());
        return hospitalPharmacyOrderMapper.updateHospitalPharmacyOrder(hospitalPharmacyOrder);
    }

    /**
     *  修改发药状态
     */
    @Override
    public int UpdateDispensingHospitalPharmacyOrder(Long orderId,int type) {
        return  hospitalPharmacyOrderMapper.UpdateDispensingHospitalPharmacyOrder(orderId,type);
    }


    /**
     * 批量删除药房订单
     *
     * @param orderIds 需要删除的药房订单主键
     * @return 结果
     */
    @Override
    public int deleteHospitalPharmacyOrderByOrderIds(Long[] orderIds)
    {
        return hospitalPharmacyOrderMapper.deleteHospitalPharmacyOrderByOrderIds(orderIds);
    }

    /**
     * 删除药房订单信息
     *
     * @param orderId 药房订单主键
     * @return 结果
     */
    @Override
    public int deleteHospitalPharmacyOrderByOrderId(Long orderId)
    {
        return hospitalPharmacyOrderMapper.deleteHospitalPharmacyOrderByOrderId(orderId);
    }


    @Override
    public List<HospitalPharmacyOrderListHVO> selectHospitalPharmacyOrderListHVO(HospitalPharmacyOrderParam param) {
        return hospitalPharmacyOrderMapper.selectHospitalPharmacyOrderListHVO(param);
    }
}


