package com.sw.mes.yb.pc.dao;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.pc.entity.Section;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

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

public interface SectionDAO extends BaseDao<Section, Long> {

    @Query(value = "SELECT * FROM zhxx_data_section WHERE section_no IN (?1) ", nativeQuery = true)
    List<Section> findBySectionNoIn(List<String> sectionNos);

    List<Section> findBySectionNo(String sectionNo);

    @Query(value = "SELECT * FROM zhxx_data_section WHERE FIND_IN_SET(section_no,?1) AND operate_time>=?2", nativeQuery = true)
    List<Section> getInfoBatch(String sectionNos, Date date);

    @Query(value = "SELECT * FROM zhxx_data_section WHERE section_no IN (?1)", nativeQuery = true)
    List<Section> getInfoBatchNew(List<String> sectionNos);

    @Modifying
    @Transactional
    @Query(value = "DELETE FROM zhxx_fb_data_section WHERE section_no IN (?1) ", nativeQuery = true)
    void deleteBySectionNos(List<String> sectionNos);

    @Modifying
    @Transactional
    @Query(value = "UPDATE zhxx_data_section SET `state`=1,unbundling_user_id=?2,unbundling_user_name=?3,unbundling_time=?4,relieve_reason=?5 WHERE id =?1 ", nativeQuery = true)
    void UpdateState(Long id, Integer userID, String username, Date date, String relieveReason);

    @Query(value = "SELECT * FROM `zhxx_data_section` WHERE id =?1 ", nativeQuery = true)
    Section getOneBySectionId(Long sectionId);


    @Query(value = "SELECT * FROM `zhxx_data_section` WHERE id = (select section_id from zhxx_yb_data_spell_info where section_no = ?1 order by id DESC limit 1)", nativeQuery = true)
    List<Section> getBySectionNo(String sectionNo);

    @Modifying
    @Transactional
    @Query(value = "UPDATE zhxx_data_section SET `state`=1,unbundling_user_id=?2,unbundling_user_name=?3,unbundling_time=?4,relieve_reason=?5 WHERE section_no_group =?1 ", nativeQuery = true)
    void updateStateBySectionNoGroup(String sectionNoGroup, Integer userID, String username, Date date, String relieveReason);
}
