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

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

import java.util.List;

public interface CrucibleCheckDataDAO extends BaseDao<CrucibleCheckData, Long> {

    List<CrucibleCheckData> findByCode(String code);

    @Modifying
    @Transactional
    @Query(value = "UPDATE zhxx_operate_crucible_check_data SET lot_no=?2 WHERE code=?1 ", nativeQuery = true)
    void updateLotNo(String code, String lotNo);

    @Query(value = "SELECT COUNT(1) FROM zhxx_operate_crucible_check_data WHERE lot_no=?1 ", nativeQuery = true)
    Long countByLotNo(String lotNo);

    @Query(value = "SELECT COUNT(1) FROM zhxx_operate_crucible_check_data WHERE code=?1 ", nativeQuery = true)
    Long countByCode(String code);

    @Modifying
    @Query(value = "UPDATE zhxx_operate_crucible_check_data SET lot_no=?2 WHERE lot_no=?1 ", nativeQuery = true)
    void changeLotNo(String oldLotNo, String newLotNo);

    @Query(value = "SELECT id FROM zhxx_operate_crucible_check_data WHERE lot_no=?1 ", nativeQuery = true)
    Long getIdByLotNo(String lotNo);

    @Query(value = "SELECT id FROM zhxx_operate_crucible_check_data WHERE code=?1 ", nativeQuery = true)
    Long getIdByCode(String code);

    @Query(value = "UPDATE zhxx_operate_crucible_check_data SET code=?2 WHERE code=?1 ", nativeQuery = true)
    void changeCode(String oldCode, String newCode);

    @Modifying
    @Query(value = "UPDATE zhxx_operate_crucible_check_data SET code=?2 WHERE id=?1 ", nativeQuery = true)
    void changeCodeById(String id, String newCode);

    @Modifying
    @Query(value = "UPDATE zhxx_operate_crucible_check_data SET lot_no=?2 WHERE id=?1 ", nativeQuery = true)
    void changeLotNoById(String id, String newLotNo);

}
