package com.example.scaffold.mapper;

import com.example.scaffold.entity.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Order MyBatis Mapper
 */
@Mapper
public interface OrderMapper {
    
    @Select("SELECT * FROM orders ORDER BY created_at DESC")
    List<Order> findAll();
    
    @Select("SELECT * FROM orders WHERE id = #{id}")
    Order findById(@Param("id") Long id);
    
    @Select("SELECT * FROM orders WHERE user_id = #{userId}")
    List<Order> findByUserId(@Param("userId") Long userId);
    
    @Insert("INSERT INTO orders(user_id, product_id, quantity, total_price, status, created_at) " +
            "VALUES(#{userId}, #{productId}, #{quantity}, #{totalPrice}, #{status}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Order order);
    
    @Update("UPDATE orders SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") String status);
    
    @Delete("DELETE FROM orders WHERE id = #{id}")
    int deleteById(@Param("id") Long id);
}