package com.rf.psychological.wxpay.repository;

import com.rf.psychological.base.repository.BaseRepository;
import com.rf.psychological.wxpay.model.OrderInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @Description:订单信息接口
 * @Author: zsf
 * @Date: 2022/6/21
 */
public interface OrderInfoRepository extends BaseRepository<OrderInfo, String> {


    OrderInfo getNoPayOrderInfoByProductIdAndUserId(String productId,String userId);

    /**
     * 查询该商品该用户下未支付的订单
     * @param productId
     * @param userId
     * @param orderStatus
     * @return
     */
    OrderInfo findByProductIdAndUserIdAndOrderStatus(String productId,String userId,String orderStatus);

    /**
     * 获取到该量表下该用户的未支付状态订单
     * @param productId 量表id
     * @param userId 用户id
     * @param resultId 结果id
     * @param orderStatus 状态
     * @return
     */
    OrderInfo findByProductIdAndUserIdAndResultIdAndOrderStatus(String productId,String userId,String resultId,String orderStatus);

    /**
     * 根据状态查询
     * @param orderStatus
     * @return
     */
    List<OrderInfo> findByOrderStatus(String orderStatus);


    /**
     * 根据订单编号获取订单
     * @param orderNo 订单编号
     * @return
     */
    OrderInfo findByOrderNo(String orderNo);

    OrderInfo findByOrderNoAndOrderStatus(String orderNo,String status);


    /**
     * 查询订单
     * @param productId
     * @param userId
     * @param resultId
     * @return
     */
    OrderInfo findByProductIdAndUserIdAndResultId(String productId,String userId,String resultId);



    OrderInfo findByResultIdAndOrderStatus(String resultId,String status);

    /**
     * 通过手机号查询订单列表
     * @param modelPhone 用户手机号
     * @param orderStatus 订单状态
     * @return
     */
    @Query(value = "SELECT * FROM t_order_info WHERE order_status = :orderStatus AND user_id in(" +
            "SELECT id FROM t_user_info WHERE model_phone = :modelPhone)", nativeQuery = true)
    Page<OrderInfo> getOrderByUserId(@Param("modelPhone") String modelPhone, @Param("orderStatus") String orderStatus, Pageable pageable);

    OrderInfo findOrderInfoByUserIdAndResultId(String userId,String resultId);

    @Query(value = "SELECT count(1) FROM t_order_info t WHERE t.product_id = :flag and create_time >= :startTime and order_status in ('支付成功','未支付')", nativeQuery = true)
    int countNum(@Param("flag") String flag,@Param("startTime") String startTime);
}
