package com.ann.demo.repository;

import com.ann.demo.entity.interfaceEntity.ArchiveMaster;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author: LeiJiaXin
 * @Date: 2019/7/11 20:55
 */
@Repository
public interface ArchiveMasterRepository extends JpaRepository<ArchiveMaster, String> {

    @Query("select o.id from ArchiveMaster o where o.patientId = ?1 and o.visitId = ?2 and o.inpNo =?3 and isValid = 0")
    public String findArchiveMasterId(String patientId, String visitId, String inpNo);

    @Transactional
    @Modifying
    @Query("update ArchiveMaster o set o.archiveState = '作废',o.isValid = 1 where o.id = ?1 and o.isValid = 0 ")
    public void updateArchiveMasterValid(String id);

    @Transactional
    @Modifying
    @Query("update ArchiveMaster o set o.transferDept = ?2 where o.id = ?1  and  o.isValid = 0 ")
    public void transferDepartment(String id, String transferDept);

    @Query("select o.deptAdmissionTo from ArchiveMaster o where o.patientId = ?1 and o.visitId = ?2 and o.inpNo =?3")
    public String findArchiveMasterDept(String patientId, String visitId, String inpNo);

    public ArchiveMaster findByIdAndIsValid(String id, Integer isValid);

    public ArchiveMaster findByPatientIdAndVisitIdAndIsValid(String patientId, String visitId, Integer isValid);

    public ArchiveMaster findByInpNoAndVisitIdAndIsValid(String inpNo, String visitId, int isValid);

    @Query("select o from ArchiveMaster o where o.checkedDatetime is not null and  o.isValid = 0")
    public List<ArchiveMaster> findAllArchiveMaster();

    @Transactional
    @Modifying
    @Query("update ArchiveMaster o set o.checkedDatetime = null where o.id = ?1 and o.isValid = 0 ")
    public Integer updateCheckedDatetime(String id);

    @Query("select o.transferDept from ArchiveMaster o where o.id = ?1")
    String findTransferDept(String id);

    @Transactional
    @Modifying
    @Query("update ArchiveMaster o set o.name = ?3,o.sex = ?2 where o.id = ?1 and o.isValid = 0 ")
    Integer updateArchiveMaster(String id, String sex, String name);
}
