package com.ywt.outpatient.domain.entity.center;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface OutpatientOrderRepository extends JpaRepository<OutpatientOrder, Integer> {
    @Query(value = "select count(0) from outpatient_order where his_order_no = ? and deleted = 0", nativeQuery = true)
    int count(String hisOrderNo);

    @Query(value = "select * from outpatient_order where id >= ? and id <= ? and deleted = 0 order by id;", nativeQuery = true)
    List<OutpatientOrder> getList(int minId, int maxId);

    /**
     * 获取昨日门诊缴费单
     *
     * @return
     */
    @Query(value = "select * from outpatient_order where to_days(now()) - to_days(create_time) = 1 and deleted = 0 order by id;", nativeQuery = true)
    List<OutpatientOrder> getYesterdayList();

    /**
     * 根据订单ID得到门诊缴费订单
     *
     * @param orderId
     * @return
     */
    @Query(value = "select oo.* from orders o , outpatient_order oo where o.id = ? and o.order_type = 8 and o.business_id = oo.id  and oo.deleted = 0 limit 1 ;", nativeQuery = true)
    OutpatientOrder getOneByOrderId(int orderId);

    @Modifying
    @Transactional
    @Query(value = "update outpatient_order set order_id = ?1, order_no = ?2 where id = ?3", nativeQuery = true)
    void updateOrderIdAndOrderNoById(int orderId, String orderNo, int id);
}
