package com.fs.hospital.mapper;

import java.util.List;

import com.fs.common.annotation.Log;
import com.fs.hospital.domain.HospitalOrder;
import com.fs.hospital.param.HospitalOrderParam;
import com.fs.hospital.param.HospitalOutpatientOrderParam;
import com.fs.hospital.vo.HospitalOrderListHVO;
import com.fs.hospital.vo.HospitalOrderVO;
import com.fs.hospital.vo.HospitalOutpatientOrderHVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 * 诊疗订单Mapper接口
 *
 * @author fs
 * @date 2024-03-22
 */
@Repository
public interface HospitalOrderMapper
{
    /**
     * 查询诊疗订单
     *
     * @param orderId 诊疗订单主键
     * @return 诊疗订单
     */
    public HospitalOrder selectHospitalOrderByOrderId(Long orderId);

    /**
     * 查询诊疗订单列表
     *
     * @param hospitalOrder 诊疗订单
     * @return 诊疗订单集合
     */
    public List<HospitalOrder> selectHospitalOrderList(HospitalOrder hospitalOrder);

    /**
     * 新增诊疗订单
     *
     * @param hospitalOrder 诊疗订单
     * @return 结果
     */
    public int insertHospitalOrder(HospitalOrder hospitalOrder);

    /**
     * 修改诊疗订单
     *
     * @param hospitalOrder 诊疗订单
     * @return 结果
     */
    public int updateHospitalOrder(HospitalOrder hospitalOrder);

    /**
     * 删除诊疗订单
     *
     * @param orderId 诊疗订单主键
     * @return 结果
     */
    public int deleteHospitalOrderByOrderId(Long orderId);

    /**
     * 批量删除诊疗订单
     *
     * @param orderIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteHospitalOrderByOrderIds(Long[] orderIds);


    @Select({"<script> " +
            "select h.*,hp.patient_name  from hospital_order h    " +
            "left join hospital_patient hp on hp.patient_id = h.patient_id " +
            "where 1=1 " +
            "<if test = ' maps.hospitalId!=null and maps.hospitalId!=\"\" '> " +
            "and h.hospital_id = #{maps.hospitalId} " +
            "</if>" +
            "<if test = ' maps.status!=null '> " +
            "and h.status = #{maps.status} " +
            "</if>" +
            "<if test = \"maps.beginTime!=null and maps.beginTime!='' and maps.endTime!=null and maps.endTime!=''\"> " +
            "and h.create_time BETWEEN #{maps.beginTime} and #{maps.endTime} " +
            "</if>" +
            "<if test = \"maps.patientName!=null and maps.patientName != ''\"> " +
            "and hp.patient_name like concat('%', #{maps.patientName}, '%') " +
            "</if>" +
            " order by h.order_id desc "+
            "</script>"})
    List<HospitalOrderListHVO> selectHospitalOrderListHVO(@Param("maps") HospitalOrderParam param);

    @Update("update hospital_order set status= #{type} ,update_time= NOW() where order_id=#{orderId}")
    public  int handleOrderChangeExecute(@Param("orderId") Long orderId,@Param("type") int type);


    @Select("select o.*,hu.nick_name as userName, h.hospital_name,hoo.money from hospital_order o " +
            "left join  hospital h on h.hospital_id=o.hospital_id " +
            "left join hospital_user hu on hu.user_id = o.user_id " +
            "left join hospital_outpatient_order hoo on hoo.order_id = o.outpatient_order_id " +
            "where o.patient_id=#{patientId} and o.status=#{status} ")
    public List<HospitalOrderVO> selectHospitalOrderByPatientIdList(@Param("patientId") Long patientId,@Param("status") int status);

    @Select("select o.*,hu.nick_name as userName, h.hospital_name,hoo.money from hospital_order o " +
            "left join  hospital h on h.hospital_id=o.hospital_id " +
            "left join hospital_user hu on hu.user_id = o.user_id " +
            "left join hospital_outpatient_order hoo on hoo.order_id = o.outpatient_order_id " +
            "where o.order_id=#{orderId}")
    public HospitalOrderVO getFeeRecordsByOrderId(@Param("orderId") Long orderId);

}
