package com.phm.manage.controller;

import com.phm.common.annotation.Log;
import com.phm.common.core.controller.BaseController;
import com.phm.common.core.domain.AjaxResult;
import com.phm.common.enums.BusinessType;
import com.phm.common.utils.poi.ExcelUtil;
import com.phm.manage.domain.Product;
import com.phm.manage.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品树信息Controller
 *
 * @author phm
 * @date 2023-08-31
 */
@RestController
@RequestMapping("/manage/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    /**
     * 查询产品树信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:product:list')")
    @GetMapping("/list")
    public AjaxResult list(Product product) {
        List<Product> list = productService.selectProductList(product);
        return success(list);
    }

    /**
     * 导出产品树信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:product:export')")
    @Log(title = "产品树信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Product product) {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "产品树信息数据");
    }

    /**
     * 获取产品树信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productService.selectProductById(id));
    }

    /**
     * 新增产品树信息
     */
    @PreAuthorize("@ss.hasPermi('manage:product:add')")
    @Log(title = "产品树信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product) {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改产品树信息
     */
    @PreAuthorize("@ss.hasPermi('manage:product:edit')")
    @Log(title = "产品树信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除产品树信息
     */
    @PreAuthorize("@ss.hasPermi('manage:product:remove')")
    @Log(title = "产品树信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }
}
