package com.ann.demo.repository;

import com.ann.demo.entity.filing.ArchiveDetail;
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;

/**
 * @Author: LeiJiaXin
 * @Date: 2019/7/12 9:10
 */
@Repository
public interface ArchiveDetailRepository extends JpaRepository<ArchiveDetail, String> {

    @Transactional
    @Modifying
    @Query(value = "UPDATE ArchiveDetail o set o.flag = 1 where o.id = ?1 and o.flag = 0")
    public Integer updateFlag(String id);

    @Query("select o.id from ArchiveDetail o where o.applyId = ?1  and o.flag = 0")
    public String findIdByApplyId(String applyId);

    @Query("select count (o) from ArchiveDetail  o where o.masterID = ?1 and o.assortID = ?2  and o.source=?3 and (o.applyId = ?4 and o.applyId is not null) and  o.flag = 0")
    Integer countByMasterIDAndAssortIDAndSourceAndApplyId(String masterId, String AssortID, String source, String applyId);

    public ArchiveDetail findByMasterIDAndAssortIDAndSourceAndApplyIdAndFlag(String masterId, String AssortID, String source, String applyId,String flag);

    @Transactional
    @Modifying
    @Query(value = "UPDATE ArchiveDetail o set o.flag = 3 where o.id = ?1 and o.flag = 0 ")
    Integer updateFlagById(String id);
}

