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

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

import java.util.List;

/**
* @author JS111801
* @description 针对表【zhxx_operate_piece_check(【圆棒生产域—检验数据】样片检验【新】)】的数据库操作Mapper
* @createDate 2023-11-01 09:48:55
* @Entity com.sw.mes.yb.check.entity.ZhxxOperatePieceCheck
*/
public interface ZhxxOperatePieceCheckDao extends BaseDao<ZhxxOperatePieceCheck,Long> {


    @Query(value = "SELECT * FROM `piece_check`.`auto_zhxx_operate_piece_check` WHERE `type` is not null and `type` = '0' ", nativeQuery = true)
    List<ZhxxOperatePieceCheck> pullAllNewRecordFromTable();


    @Query(value = "select * from `piece_check`.`auto_zhxx_operate_piece_check` where id in (select\n" +
            "\tmax(id)\n" +
            "from\n" +
            "\t`piece_check`.`auto_zhxx_operate_piece_check`\n" +
            "where\n" +
            "\t`type` is not null\n" +
            "\tand `type` = '0'\n" +
            "\tgroup by piece_no\n" +
            "\torder by id desc)", nativeQuery = true)
    List<ZhxxOperatePieceCheck> pullAllNewRecordFromTableGroup();


    @Query(value = "SELECT * FROM `piece_check`.`auto_zhxx_operate_piece_check` WHERE `type` is not null and `type` = '0' and piece_no = ?1",nativeQuery = true)
    List<ZhxxOperatePieceCheck> findByPieceNo(String pieceNo);

    @Modifying
    @Transactional
    @Query(value = "update `piece_check`.`auto_zhxx_operate_piece_check` set `type` = '1' where `id` = ?1",nativeQuery = true)
    void updateStateForPiece (Long id);

    @Modifying
    @Transactional
    @Query(value = "update `piece_check`.`auto_zhxx_operate_piece_check` set `type` = '1' where `id` in (?1)",nativeQuery = true)
    void updateStateForPieceBatch(List<Long> ids);
}




