package com.fs.hospital.mapper;

import java.util.List;
import com.fs.hospital.domain.HospitalOutpatientOrder;
import com.fs.hospital.param.HospitalOutpatientOrderParam;
import com.fs.hospital.vo.HospitalOutpatientOrderCountHVO;
import com.fs.hospital.vo.HospitalOutpatientOrderHVO;
import com.fs.hospital.vo.HospitalPharmacyOrderHVO;
import com.fs.hospital.vo.HospitalPharmacyOrderVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

/**
 * 门诊单Mapper接口
 *
 * @author fs
 * @date 2024-03-22
 */
@Repository
public interface HospitalOutpatientOrderMapper
{
    /**
     * 查询门诊单
     *
     * @param orderId 门诊单主键
     * @return 门诊单
     */
    public HospitalOutpatientOrder selectHospitalOutpatientOrderByOrderId(Long orderId);

    /**
     * 查询门诊单列表
     *
     * @param hospitalOutpatientOrder 门诊单
     * @return 门诊单集合
     */
    public List<HospitalOutpatientOrder> selectHospitalOutpatientOrderList(HospitalOutpatientOrder hospitalOutpatientOrder);

    /**
     * 新增门诊单
     *
     * @param hospitalOutpatientOrder 门诊单
     * @return 结果
     */
    public int insertHospitalOutpatientOrder(HospitalOutpatientOrder hospitalOutpatientOrder);

    /**
     * 修改门诊单
     *
     * @param hospitalOutpatientOrder 门诊单
     * @return 结果
     */
    public int updateHospitalOutpatientOrder(HospitalOutpatientOrder hospitalOutpatientOrder);

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

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

    @Select({"<script> " +
            "select h.*,hu.nick_name as doctor_name,hd.dept_name,hp.patient_name,hp.mobile,hp.sex  from hospital_outpatient_order h    " +
            "left join hospital_patient hp on hp.patient_id = h.patient_id " +
            "left join hospital_user hu on hu.user_id = h.user_id " +
            "left join hospital_dept hd on hd.dept_id = h.dept_id " +
            "where 1=1 " +
            "<if test = ' maps.hospitalId!=null  '> " +
            "and h.hospital_id = #{maps.hospitalId} " +
            "</if>" +
            "<if test = ' maps.userId!=null   '> " +
            "and h.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = ' maps.status==null  '> " +
            "and h.status != 0  " +
            "</if>" +
            "<if test = ' maps.status!=null  '> " +
            "and h.status = #{maps.status} " +
            "</if>" +
            "<if test = ' maps.yuyueTime!=null  '> " +
            "and h.yuyue_Time = #{maps.yuyueTime} " +
            "</if>" +
            "<if test = ' maps.patientName!=null and maps.patientName != \"\"  '> " +
            "and hp.patient_name like concat('%', #{maps.patientName}, '%') " +
            "</if>" +
            " order by h.create_time desc "+
            "</script>"})
    List<HospitalOutpatientOrderHVO> selectHospitalOutpatientOrderListHVO(@Param("maps") HospitalOutpatientOrderParam param);

    @Select({"<script> " +
            "select h.*,hu.nick_name as doctor_name,hd.dept_name,hp.patient_name,hp.mobile,hp.sex  from hospital_outpatient_order h    " +
            "left join hospital_patient hp on hp.patient_id = h.patient_id " +
            "left join hospital_user hu on hu.user_id = h.user_id " +
            "left join hospital_dept hd on hd.dept_id = h.dept_id " +
            "where 1=1 " +
            "<if test = ' maps.hospitalId!=null  '> " +
            "and h.hospital_id = #{maps.hospitalId} " +
            "</if>" +
            "<if test = ' maps.userId!=null   '> " +
            "and h.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = ' maps.status!=null  '> " +
            "and h.status = #{maps.status} " +
            "</if>" +
            "<if test = ' maps.yuyueTime!=null  '> " +
            "and h.yuyue_Time = #{maps.yuyueTime} " +
            "</if>" +
            "<if test = ' maps.patientName!=null and maps.patientName != \"\"  '> " +
            "and hp.patient_name like concat('%', #{maps.patientName}, '%') " +
            "</if>" +
            " order by h.create_time desc "+
            "</script>"})
    List<HospitalOutpatientOrderHVO> selectAllOutpatientOrderListHVO(@Param("maps") HospitalOutpatientOrderParam param);


    @Select({"<script> " +
            "select  h.user_id,\n" +
            "    COUNT(*) AS total_count,\n" +
            "    SUM(CASE WHEN h.status = 1 THEN 1 ELSE 0 END) AS wait_count , hu.nick_name as doctor_name  from hospital_outpatient_order h    " +
            "left join hospital_user hu on hu.user_id = h.user_id " +
            "where 1=1  and h.status != 0" +
            "<if test = ' maps.yuyueTime!=null  '> " +
            "and h.yuyue_Time = #{maps.yuyueTime} " +
            "</if>" +
            " GROUP BY h.user_id  "+
            "</script>"})
    List<HospitalOutpatientOrderCountHVO> selectHospitalOutpatientOrderCountListHVO(@Param("maps") HospitalOutpatientOrderParam param);


    @Select("select COUNT(*) AS total_count,SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS wait_count from hospital_outpatient_order where status != 0 and  yuyue_Time = #{yuyueTime}")
    HospitalOutpatientOrderCountHVO selectHospitalOutpatientOrderAllCountByTime(HospitalOutpatientOrderParam param);


    @Select("select h.*,hu.nick_name as doctor_name from hospital_outpatient_order h " +
            "left join hospital_user hu on hu.user_id = h.user_id " +
            "where h.patient_id=#{patientId} order by h.create_time desc")
    List<HospitalPharmacyOrderVO> selectOutpatientOrderHistoryList(@Param("patientId") Long patientId);

    @Select("select h.*,hu.nick_name as doctor_name from hospital_outpatient_order h " +
            "left join hospital_user hu on hu.user_id = h.user_id " +
            "where h.order_id=#{orderId} order by h.create_time desc")
    HospitalPharmacyOrderVO selectOutpatientOrderHistory(@Param("orderId") Long orderId);

    @Select("select h.*,hu.nick_name as doctorName,hn.hospital_name from hospital_outpatient_order h " +
            "left join hospital_user hu on hu.user_id = h.user_id " +
            "left join hospital hn on h.hospital_id=hn.hospital_id "+
            "where h.patient_id=#{patientId} order by h.create_time desc")
    List<HospitalPharmacyOrderVO> getCaseRecords(@Param("patientId") Long patientId);


}
