package com.yn.bftl.thirdparty.common.repository;

import com.yn.bftl.common.modules.subaccount.entity.StatementLine;
import com.yn.bftl.common.modules.subaccount.enums.StatementType;
import com.yn.bftl.common.modules.subaccount.enums.SubaccountType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 结算单明细Repository
 *
 * @author chendehuihuo
 * @date 2021/11/23
 **/
@Repository
public interface StatementLineRepository extends JpaRepository<StatementLine, Long> {

    List<StatementLine> findAllByOrderItemId(Long orderItemId);

    List<StatementLine> findAllByShippingItemIdAndStatementStatementTypeAndOrderItemIdAndStatementSubaccountType(Long shippingItemId, StatementType statementType, Long orderItemId, SubaccountType subaccountType);

    /**
     * 结算单明细-根据订单项ID计算已结算金额
     *
     * @param orderItemId:订单项ID
     * @return
     * @description
     * @author huabiao
     * @create 2022/9/19  16:59
     */
    @Query(value = "select COALESCE(sum(statementLine.subaccountAmount),0)  from StatementLine statementLine where statementLine.orderItem.id = ?1")
    BigDecimal calculateSubaccountAmount(Long orderItemId);

    /**
     * 结算单明细-根据订单项ID计算已结算数量
     *
     * @param orderItemId 订单项ID
     * @return
     * @description
     * @author huabiao
     * @create 2023/2/15  21:30
     */
    @Query(value = "select COALESCE(sum(statementLine.quantity),0)  from StatementLine statementLine where statementLine.orderItem.id = ?1")
    BigDecimal calculateSubaccountQuantity(Long orderItemId);
}
