package com.izouma.jiashanxia.repo;

import com.izouma.jiashanxia.domain.PackageGoods;
import com.izouma.jiashanxia.dto.PackageGoodsDTO;
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 PackageGoodsRepo extends JpaRepository<PackageGoods, Long>, JpaSpecificationExecutor<PackageGoods> {
    @Query("update PackageGoods t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    List<PackageGoods> findAllByPackageId(Long packageId);

    @Query(value = "select gi.name,gi.unit," +
            "gi.id AS id," +
            "ifnull(pg.num, 0) AS num ," +
            "ifnull(pg.num * gi.price, 0) AS price " +
            "from goods_info AS gi " +
            "left join package_goods AS pg on gi.id = pg.goods_info_id and pg.package_id = ?1 " +
            "where gi.del = 0 and pg.del = 0", nativeQuery = true)
    List<PackageGoodsDTO> packageGoods(Long packageId);
}