package com.ruoyi.web.controller.baseinfo;

import java.util.List;

import com.ruoyi.baseinfo.service.IBaseMaterialtypeInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.baseinfo.domain.BaseMaterialInfo;
import com.ruoyi.baseinfo.service.IBaseMaterialInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料基础信息Controller
 * 
 * @author CaesarBao
 * @date 2020-10-22
 */
@Controller
@RequestMapping("/baseinfo/materialinfo")
public class BaseMaterialInfoController extends BaseController
{
    private String prefix = "baseinfo/materialinfo";

    @Autowired
    private IBaseMaterialInfoService baseMaterialInfoService;
    @Autowired
    private IBaseMaterialtypeInfoService baseMaterialtypeInfoService;

    @RequiresPermissions("baseinfo:materialinfo:view")
    @GetMapping()
    public String materialinfo(ModelMap mmap)
    {
        mmap.put("cbMaterialTypeInfo",baseMaterialtypeInfoService.selectAddBaseMaterialtypeInfoList());
        return prefix + "/materialinfo";
    }

    /**
     * 查询物料基础信息列表
     */
    @RequiresPermissions("baseinfo:materialinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BaseMaterialInfo baseMaterialInfo)
    {
        startPage();
        List<BaseMaterialInfo> list = baseMaterialInfoService.selectBaseMaterialInfoList(baseMaterialInfo);
        return getDataTable(list);
    }

    /**
     * 导出物料基础信息列表
     */
    @RequiresPermissions("baseinfo:materialinfo:export")
    @Log(title = "物料基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BaseMaterialInfo baseMaterialInfo)
    {
        List<BaseMaterialInfo> list = baseMaterialInfoService.selectBaseMaterialInfoList(baseMaterialInfo);
        ExcelUtil<BaseMaterialInfo> util = new ExcelUtil<BaseMaterialInfo>(BaseMaterialInfo.class);
        return util.exportExcel(list, "materialinfo");
    }

    /**
     * 新增物料基础信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("cbMaterialTypeInfo",baseMaterialtypeInfoService.selectAddBaseMaterialtypeInfoList());
        return prefix + "/add";
    }

    /**
     * 新增保存物料基础信息
     */
    @RequiresPermissions("baseinfo:materialinfo:add")
    @Log(title = "物料基础信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BaseMaterialInfo baseMaterialInfo)
    {
        return toAjax(baseMaterialInfoService.insertBaseMaterialInfo(baseMaterialInfo));
    }

    /**
     * 修改物料基础信息
     */
    @GetMapping("/edit/{objid}")
    public String edit(@PathVariable("objid") Long objid, ModelMap mmap)
    {
        BaseMaterialInfo baseMaterialInfo = baseMaterialInfoService.selectBaseMaterialInfoById(objid);
        mmap.put("cbEditMaterialTypeInfo",baseMaterialtypeInfoService.selectEditBaseMaterialtypeInfoList(baseMaterialInfo.getMaterialTypeId()));
        mmap.put("baseMaterialInfo", baseMaterialInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存物料基础信息
     */
    @RequiresPermissions("baseinfo:materialinfo:edit")
    @Log(title = "物料基础信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BaseMaterialInfo baseMaterialInfo)
    {
        return toAjax(baseMaterialInfoService.updateBaseMaterialInfo(baseMaterialInfo));
    }

    /**
     * 删除物料基础信息
     */
    @RequiresPermissions("baseinfo:materialinfo:remove")
    @Log(title = "物料基础信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(baseMaterialInfoService.deleteBaseMaterialInfoByIds(ids));
    }
}
