package com.example.scaffold.mapper;

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

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

/**
 * Product MyBatis Mapper - 演示复杂查询
 */
@Mapper
public interface ProductMapper {
    
    @Select("SELECT * FROM products ORDER BY created_at DESC")
    List<Product> findAll();
    
    @Select("SELECT * FROM products WHERE id = #{id}")
    Product findById(@Param("id") Long id);
    
    @Select("SELECT * FROM products WHERE category = #{category}")
    List<Product> findByCategory(@Param("category") String category);
    
    @Select("SELECT * FROM products WHERE price BETWEEN #{min} AND #{max} ORDER BY price")
    List<Product> findByPriceRange(@Param("min") BigDecimal min, @Param("max") BigDecimal max);
    
    @Insert("INSERT INTO products(name, description, price, stock_quantity, category, created_at) " +
            "VALUES(#{name}, #{description}, #{price}, #{stockQuantity}, #{category}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Product product);
    
    @Update("UPDATE products SET stock_quantity = stock_quantity - #{quantity} WHERE id = #{id} AND stock_quantity >= #{quantity}")
    int decreaseStock(@Param("id") Long id, @Param("quantity") Integer quantity);
    
    @Update("UPDATE products SET stock_quantity = stock_quantity + #{quantity} WHERE id = #{id}")
    int increaseStock(@Param("id") Long id, @Param("quantity") Integer quantity);
    
    @Delete("DELETE FROM products WHERE id = #{id}")
    int deleteById(@Param("id") Long id);
}