package com.cirle.scientific.dao;

import java.util.List;
import java.util.Map;

import com.cirle.scientific.dto.UserOderDto;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import com.cirle.scientific.po.SalesOrdersMain;


@Repository
public interface OrderDao extends PagingAndSortingRepository<SalesOrdersMain, Long>, JpaSpecificationExecutor<SalesOrdersMain> {
    @Query(value = "SELECT * FROM sales_orders_main WHERE mem_card = ?1 AND order_id = ?2",nativeQuery = true)
    SalesOrdersMain getOrderInfo(String memCard, String orderId);

    @Query(value = "SELECT order_id FROM sales_orders_main WHERE mem_card = ?1 AND obj_id = ?2 and order_type = ?3 ",nativeQuery = true)
    List<String> getOrderByMemCardAndObjIdAndOrderType(String memCard, String objId ,  Integer orderType);

//    查询积分或金钱购买的订单
    @Query(value = "SELECT * FROM sales_orders_main WHERE mem_card = ?1 AND status = 1 AND pay_type = ?4 ORDER BY createtime DESC LIMIT ?2,?3",nativeQuery = true)
    List<SalesOrdersMain> findMyOrderList(String memCard, Integer payType);

    @Query(value = "SELECT * FROM sales_orders_main WHERE mem_card = ?1 AND status = 1 AND order_type = ?4 ORDER BY createtime DESC LIMIT ?2,?3",nativeQuery = true)
    List<SalesOrdersMain> findMyOrderListAll(String memCard,Integer start, Integer size,Integer orderType);

    @Query(value = "SELECT * FROM sales_orders_main WHERE mem_card = ?1 AND status = 1 ORDER BY createtime DESC ",nativeQuery = true)
    List<SalesOrdersMain> findUserAllOrderList(String memCard);

    @Query(value = "SELECT * FROM sales_orders_main WHERE mem_card = ?1 AND status = 1 ORDER BY createtime DESC ",nativeQuery = true)
    List<SalesOrdersMain> findMyOrderListAll(String memCard, Integer start, Integer size);

    @Query(value = "SELECT * FROM sales_orders_main WHERE mem_card = ?1 AND status = ?2 AND is_shipping = ?3 AND order_type = 15 ORDER BY createtime DESC  LIMIT ?4,?5",nativeQuery = true)
    List<SalesOrdersMain> findMyOrderListConditions(String memCard, Integer status, Integer isShipping, Integer start, Integer size);
    
    @Query(value = "SELECT * FROM sales_orders_main WHERE order_id = ?1",nativeQuery = true)
    SalesOrdersMain getOrderInfo(String orderId);

    @Query(value = "SELECT m.mem_nickname, m.mem_headimg, count(s.id) as num from sales_orders_main s "
            +"	INNER JOIN qkj_member m on s.from_mem_card = m.mem_card "
            +" where s.order_type=?1 and s.obj_id=?2 and s.ispackage=1 and s.`status`=1 and s.from_mem_card is not null GROUP BY s.from_mem_card ORDER BY num desc",nativeQuery = true)
    List<Object[]> inviteRank(Integer objType, Integer objId);

    @Query(value = "SELECT m.mem_nickname, m.mem_headimg, count(s.id) from sales_orders_main s "
            +"	INNER JOIN qkj_member m on s.from_mem_card = m.mem_card "
            +" where s.order_type=?1 and s.obj_id=?2 and s.`status`=1 and s.from_mem_card=?3",nativeQuery = true)
    Object[] inviteRankByMember(Integer objType, Integer objId, String memCard);

    List<SalesOrdersMain> findByWxpayOrderId(String orderId);


    @Query(value = "select  * from sales_orders_main where mem_card = ?1 and obj_id = ?3 and order_type = ?2 and status = 1",nativeQuery = true)
    List<SalesOrdersMain> findTqcourseOrderList(String memCard,Integer orderType,Integer productId);
}
