package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.Product;
import com.ruoyi.project.system.domain.vo.ProductCraftCateVo;
import com.ruoyi.project.system.domain.vo.ProductVo;
import com.ruoyi.project.system.service.IProductCraftCateService;
import com.ruoyi.project.system.service.IProductMaterialService;
import com.ruoyi.project.system.service.IProductService;
import org.springframework.beans.BeanUtils;
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 zhu
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/system/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    @Autowired
    private IProductCraftCateService productCraftCateService;

    @Autowired
    private IProductMaterialService productMaterialService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('system: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('system:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ProductVo productVo = productService.selectProductById(id);
        List<ProductCraftCateVo> catesVo = productCraftCateService.selectProductDetail(id, 0L);
        productVo.setProductCates(catesVo);
        return success(productVo);
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product) {
        product.setCreateBy(getUsername());
        product.setUpdateBy(getUsername());
        return toAjax(productService.insertProduct(product));
    }

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

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

    /**
     * 修改商品价格
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePrice")
    public AjaxResult updatePrice(@RequestBody ProductVo product) {
        Product nProduct = new Product();
        //数据copy更新商品
        BeanUtils.copyProperties(product, nProduct);
        nProduct.setUpdateBy(getUsername());
        productService.updateProduct(nProduct);

        return toAjax(productCraftCateService.updatePrice(product.getProductCates(), product.getId(), 0L));
    }

    /**
     * 修改备注
     */
    @PreAuthorize("@ss.hasPermi('system:product:RemarkEdit')")
    @Log(title = "商品备注", businessType = BusinessType.UPDATE)
    @PutMapping("/updateRemark")
    public AjaxResult updateRemark(@RequestBody Product product) {
        return toAjax(productService.updateProduct(product));
    }

}
