package com.eshore.gringotts.web.domain.repository;

import com.eshore.gringotts.web.domain.base.repository.SimpleRepository;
import com.eshore.gringotts.web.domain.entity.CheckOrder;
import com.eshore.gringotts.web.domain.entity.User;
import java.util.List;
import java.util.Optional;
import javax.transaction.Transactional;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**
 * @author lanyuanxiaoyao
 * @date 2024-11-29
 */
@SuppressWarnings("NullableProblems")
@Repository
public interface CheckOrderRepository extends SimpleRepository<CheckOrder, Long> {
    @Override
    @EntityGraph(value = "check_order.list", type = EntityGraph.EntityGraphType.FETCH)
    List<CheckOrder> findAll(Specification<CheckOrder> specification);

    @Override
    @EntityGraph(value = "check_order.list", type = EntityGraph.EntityGraphType.FETCH)
    List<CheckOrder> findAll(Specification<CheckOrder> specification, Sort sort);

    @Override
    @EntityGraph(value = "check_order.detail", type = EntityGraph.EntityGraphType.FETCH)
    Optional<CheckOrder> findOne(Specification<CheckOrder> specification);

    @Transactional
    @Modifying
    @Query("update CheckOrder check set check.state = ?2, check.modifiedUser = ?3, check.modifiedTime = current_timestamp where check.id = ?1")
    void overById(Long id, CheckOrder.State state, User modifiedUser);
}
