package com.zhyc.module.feed.controller;

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

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhyc.common.annotation.Log;
import com.zhyc.common.core.controller.BaseController;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.common.enums.BusinessType;
import com.zhyc.module.feed.domain.SgMaterial;
import com.zhyc.module.feed.service.ISgMaterialService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 原料Controller
 *
 * @author HashMap
 * @date 2026-01-16
 */
@RestController
@RequestMapping("/feed/material")
public class SgMaterialController extends BaseController {
    @Autowired
    private ISgMaterialService sgMaterialService;

    /**
     * 查询原料列表
     */
    @PreAuthorize("@ss.hasPermi('feed:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(SgMaterial sgMaterial) {
        startPage();
        List<SgMaterial> list = sgMaterialService.selectSgMaterialList(sgMaterial);
        return getDataTable(list);
    }

    /**
     * 导出原料列表
     */
    @PreAuthorize("@ss.hasPermi('feed:material:export')")
    @Log(title = "原料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SgMaterial sgMaterial) {
        List<SgMaterial> list = sgMaterialService.selectSgMaterialList(sgMaterial);
        ExcelUtil<SgMaterial> util = new ExcelUtil<SgMaterial>(SgMaterial.class);
        util.exportExcel(response, list, "原料数据");
    }

    /**
     * 获取原料详细信息
     */
    @PreAuthorize("@ss.hasPermi('feed:material:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") String materialId) {
        return success(sgMaterialService.selectSgMaterialByMaterialId(materialId));
    }

    /**
     * 新增原料
     */
    @PreAuthorize("@ss.hasPermi('feed:material:add')")
    @Log(title = "原料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SgMaterial sgMaterial) {
        sgMaterial.setUserId(getUserId());
        sgMaterial.setDeptId(getDeptId());
        return toAjax(sgMaterialService.insertSgMaterial(sgMaterial));
    }

    /**
     * 修改原料
     */
    @PreAuthorize("@ss.hasPermi('feed:material:edit')")
    @Log(title = "原料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SgMaterial sgMaterial) {
        return toAjax(sgMaterialService.updateSgMaterial(sgMaterial));
    }

    /**
     * 删除原料
     */
    @PreAuthorize("@ss.hasPermi('feed:material:remove')")
    @Log(title = "原料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable String[] materialIds) {
        return toAjax(sgMaterialService.deleteSgMaterialByMaterialIds(materialIds));
    }
}
