package com.ruoyi.project.system.controller;

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.ProductMaterial;
import com.ruoyi.project.system.domain.vo.ProductCraftCateVo;
import com.ruoyi.project.system.domain.vo.ProductMaterialVo;
import com.ruoyi.project.system.service.IProductCraftCateService;
import com.ruoyi.project.system.service.IProductMaterialService;
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 java.util.List;

/**
 * 产品材料Controller
 *
 * @author zhu
 * @date 2023-10-28
 */
@RestController
@RequestMapping("/system/material")
public class ProductMaterialController extends BaseController {
    @Autowired
    private IProductMaterialService productMaterialService;
    @Autowired
    private IProductCraftCateService productCraftCateService;

    /**
     * 查询产品材料列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductMaterial productMaterial) {
        List<ProductMaterial> list = productMaterialService.selectProductMaterialList(productMaterial);
        return getDataTable(list);
    }


    /**
     * 获取产品材料详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ProductMaterial productMaterial = productMaterialService.selectProductMaterialById(id);
        ProductMaterialVo productMaterialVo = null;
        if (productMaterial != null) {
            productMaterialVo = new ProductMaterialVo();
            BeanUtils.copyProperties(productMaterial, productMaterialVo);

            List<ProductCraftCateVo> productCraftCateVo = productCraftCateService.selectProductDetail(productMaterialVo.getProductId(), productMaterialVo.getId());
            productMaterialVo.setProductCates(productCraftCateVo);
        }
        return success(productMaterialVo);
    }

    /**
     * 新增产品材料
     */
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "产品材料", businessType = BusinessType.INSERT)
    @PostMapping("/addMate")
    public AjaxResult add(@RequestBody ProductMaterialVo productMaterialVo) {
        ProductMaterial nProduct = new ProductMaterial();
        //数据copy更新商品
        BeanUtils.copyProperties(productMaterialVo, nProduct);
        nProduct.setUpdateBy(getUsername());

        nProduct.setCreateBy(getUsername());
        productMaterialService.insertProductMaterial(nProduct);

        return toAjax(productCraftCateService.updatePrice(productMaterialVo.getProductCates(), nProduct.getProductId(), nProduct.getId()));
    }

    /**
     * 修改产品材料
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "产品材料", businessType = BusinessType.UPDATE)
    @PutMapping("/editMate")
    public AjaxResult edit(@RequestBody ProductMaterialVo productMaterialVo) {
        ProductMaterial nProduct = new ProductMaterial();
        //数据copy更新商品
        BeanUtils.copyProperties(productMaterialVo, nProduct);
        nProduct.setUpdateBy(getUsername());

        productMaterialService.updateProductMaterial(nProduct);
        return toAjax(productCraftCateService.updatePrice(productMaterialVo.getProductCates(), nProduct.getProductId(), nProduct.getId()));
    }

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