package com.izouma.nineth.repo;

import com.izouma.nineth.domain.DivMoney;
import com.izouma.nineth.dto.DivMoneyDTO;
import com.izouma.nineth.enums.DivStatus;
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 java.time.LocalDateTime;
import java.util.List;

public interface DivMoneyRepo extends JpaRepository<DivMoney, Long>, JpaSpecificationExecutor<DivMoney> {

    /**
     * 逻辑删除未处理的分账信息
     * @param orderId
     */
    @Query("update DivMoney dm set dm.del  = true where dm.status = com.izouma.nineth.enums.DivStatus.PENDING and dm.orderId = ?1")
    @Modifying
    Integer softDeleteByOrderId(Long orderId);

    /**
     * 物理删除未处理的分账信息
     * @param orderId
     */
    @Query("delete from DivMoney dm where dm.status = com.izouma.nineth.enums.DivStatus.PENDING and dm.orderId = ?1")
    @Modifying
    Integer deleteByOrderId(Long orderId);

    /**
     * 查询分账信息
     * @param orderId
     * @return
     */
    @Query("select dm from DivMoney  dm where dm.orderId = ?1 ")
    List<DivMoney> findByOrderId(Long orderId);


    @Query("update DivMoney  set status = ?2 where id = ?1")
    @Modifying
    Integer updateDivStatus(Long divMoneyId, DivStatus status);

    @Query("update DivMoney  set transferResult = ?2 where id = ?1")
    @Modifying
    Integer updateDivResult(Long divMoneyId, String result);

}
