package com.ruoyi.web.controller.app;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.product.domain.Product;
import com.ruoyi.ss.product.domain.ProductQuery;
import com.ruoyi.ss.product.domain.ProductVO;
import com.ruoyi.ss.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * 商品列表Controller
 *
 * @author qzz
 * @date 2024-12-16
 */
@RestController
@RequestMapping("/app/product")
public class AppProductController extends BaseController
{
    @Autowired
    private IProductService productService;

    /**
     * 查询商品列表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProductQuery query)
    {
        startPage();
        startOrderBy();
        List<ProductVO> list = productService.selectProductList(query);
        return getDataTable(list);
    }

    /**
     * 获取商品列表详细信息
     */
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        log.info("获取商品列表详细信息：{}", productId);
        return success(productService.selectProductByProductId(productId));
    }

    /**
     * 新增商品列表
     */
    @Log(title = "商品列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Product product)
    {
        log.info("新增商品列表：{}", product);
        product.setMerchantId(getUserId());
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改商品列表
     */
    @Log(title = "商品列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) Product product)
    {
        log.info("修改商品列表：{}", product);
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除商品列表
     */
    @Log(title = "商品列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        log.info("删除商品列表：{}", JSON.toJSON(productIds));
        return toAjax(productService.deleteProductByProductIds(productIds));
    }
}
