package com.qmth.qrzk.dao.repositories;

import com.qmth.qrzk.repository.user.model.Confidential;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface ConfidentialRepo extends JpaRepository<Confidential, Integer>, JpaSpecificationExecutor<Confidential> {

    @Modifying
    @Query("update Confidential set bagNum = ?1 where id = ?2")
    void updateById(int bagNum, int id);

    @Query("select b from Confidential b where b.paperBatch.id = ?1 order by b.province.code asc")
    List<Confidential> findByBatchId(Integer batchId);

//    @Query("select max(b.paperBatch.id) from Confidential b")
//    int findMaxBatchId();

    @Modifying
    @Query(value="INSERT INTO c_confidential_handover (`province_code`,`bag_num`,`batch_id`,`year_month`) SELECT q.CODE `province_code`,0 AS `bag_num`,?1,NULL AS year_code FROM qr_province q", nativeQuery=true)
    void copyConfidential(int batchId);
}
