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 RegisteredOrderRepository extends JpaRepository<RegisteredOrder, Integer> {
    @Query(value = "select * from registered_order where medical_card_id = ? and deleted = 0 order by create_time desc limit 1;", nativeQuery = true)
    RegisteredOrder getRegisteredOrderByMedicalCardId(int medicalCardId);

    /**
     * 得到该用户当然所有的未就诊记录
     *
     * @param userId
     * @return
     */
    @Query(value = "select * from registered_order where user_id = ? and deleted = 0 and hospital_id = 12 and registered_date >= ? and (registered_status & 1 = 1 and registered_status & 2 != 2 and registered_status & 8 != 8 and registered_status & 16 != 16  ) order by create_time desc ", nativeQuery = true)
    List<RegisteredOrder> getTaiheQueueOrderListByUserId(int userId, String date);

    /**
     * 获取东华相关医院（太和、白云）已预约的挂号列表
     * TODO: remove the hard code!
     * @return
     */
    @Query(value = "select * from registered_order where (hospital_id = 12 or hospital_id = 41) and to_days(registered_date) = to_days(now()) and registered_status = 1 and deleted = 0 order by start_time;", nativeQuery = true)
    List<RegisteredOrder> getDHTodayRegisteredList();

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

    /**
     * 更新挂号信息
     *
     * @param hisSeqCode       挂号序号
     * @param hisRegFee        挂号费
     * @param hisAdmitRange    就诊时间段
     * @param hisAdmitAddress  就诊地点
     * @param hisOrderContent  预约单内容（内容说明：卡号^病人Rowid^病人姓名^性别^挂号序号(班别)^挂号序号^科室名称^医生姓名^预约日期^时间^候诊日期^^）
     * @param hisTransactionId his系统交易流水号
     * @param hisOrderCode     预约单号
     * @param registeredStatus 订单状态
     * @param id               主键
     */
    @Modifying
    @Transactional
    @Query(value =
            "update registered_order " +
                    "set his_seq_code = ?1" +
                    ", his_reg_fee = ?2" +
                    ", his_admit_range = ?3" +
                    ", his_admit_address = ?4" +
                    ", his_order_content = ?5" +
                    ", his_transaction_id = ?6" +
                    ", his_order_code = ?7" +
                    ", registered_status = ?8 " +
                    ", his_clinic_code = ?10 " +
                    "where id = ?9"
            , nativeQuery = true)
    void updateRegisterInfoById(
            String hisSeqCode, String hisRegFee, String hisAdmitRange, String hisAdmitAddress,
            String hisOrderContent, String hisTransactionId, String hisOrderCode, int registeredStatus, int id,
            String hisClinicCode);

    RegisteredOrder getFirstByOrderIdAndHospitalIdAndDeletedFalse(int orderId, int hospitalId);

    @Modifying
    @Transactional
    @Query(value = "update registered_order set registered_status = registered_status | ?1 where id = ?2", nativeQuery = true)
    void updateRegisteredStatus(int registeredStatus, int id);
}
