package com.ruoyi.project.project.item.controller;

import java.util.List;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.project.cases.domain.Case;
import com.ruoyi.project.project.item.domain.Item;
import com.ruoyi.project.project.item.service.IItemService;
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.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;

/**
 * 物品 信息操作处理
 *
 * @author luoluo
 */
@Controller
@RequestMapping("/system/item")
public class ItemController extends BaseController
{
    private String prefix = "project/item";

    @Autowired
    private IItemService itemService;

    @RequiresPermissions("system:case:view")
    @GetMapping()
    public String item()
    {
        return prefix + "/item";
    }

    /**
     * 查询物品列表
     */
    @RequiresPermissions("system:case:list")
    @PostMapping("/list/{caseNo}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("caseNo")String caseNo)
    {
        startPage();
        List<Item> list = itemService.selectItemByCaseNo(caseNo);
        return getDataTable(list);
    }

    /**
     * 新增物品
     */
    @GetMapping("/add/{caseNo}")
    public String add(@PathVariable("caseNo") String caseNo, ModelMap mmap)
    {
        mmap.put("caseNo", caseNo);
        return prefix + "/add";
    }

    /**
     * 新增保存物品
     */
    @RequiresPermissions("system:case:add")
    @Log(title = "物品新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Item item)
    {
        int i = itemService.selectItemByItemNo(item);
        if (i != 0){
            return error("物品编号重复，请检查！！！");
        }
        return toAjax(itemService.insertItem(item));
    }

    /**
     * 导出物品列表
     */
    @RequiresPermissions("system:case:export")
    @Log(title = "物品列表导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Item item)
    {
        List<Item> list = itemService.selectItemList(item);
        ExcelUtil<Item> util = new ExcelUtil<Item>(Item.class);
        return util.exportExcel(list, "物品信息数据");
    }

    /**
     * 修改物品信息
     */
    @RequiresPermissions("system:case:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        mmap.put("item", itemService.selectItemById(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存物品
     */
    @RequiresPermissions("system:case:edit")
    @Log(title = "物品修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Item item)
    {
        int i = itemService.selectItemByItemNo(item);
        if (i != 0){
            return error("物品编号重复，请检查！！！");
        }
        return toAjax(itemService.updateItem(item));
    }

    /**
     * 删除物品
     */
    @RequiresPermissions("system:case:remove")
    @Log(title = "删除物品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(itemService.deleteItemByIds(ids));
    }
}

