package com.izouma.jiashanxia.repo;

import com.izouma.jiashanxia.domain.UserPackage;
import com.izouma.jiashanxia.dto.UserPackageDTO;
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 javax.transaction.Transactional;
import java.util.List;

public interface UserPackageRepo extends JpaRepository<UserPackage, Long>, JpaSpecificationExecutor<UserPackage> {
    @Query("update UserPackage t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    List<UserPackage> findAllByUserId(Long userId);

    @Query(value = "select goods_info.*, ifnull(user_package.num, 0) as num " +
            "from goods_info " +
            "left join user_package on goods_info.id = user_package.goods_info_id and user_package.user_id = ?1 " +
            "where goods_info.del = 0", nativeQuery = true)
    List<UserPackageDTO> userPackage(Long userId);
}