package com.ruoyi.asset.controller;

import java.util.List;
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.asset.domain.AmsAssetCategory;
import com.ruoyi.asset.service.IAmsAssetCategoryService;
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 Yangk
 * @date 2026-06-04
 */
@Controller
@RequestMapping("/asset/category")
public class AmsAssetCategoryController extends BaseController
{
    private String prefix = "asset/category";

    @Autowired
    private IAmsAssetCategoryService amsAssetCategoryService;

    @RequiresPermissions("asset:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询资产类别列表
     */
    @RequiresPermissions("asset:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmsAssetCategory amsAssetCategory)
    {
        startPage();
        List<AmsAssetCategory> list = amsAssetCategoryService.selectAmsAssetCategoryList(amsAssetCategory);
        return getDataTable(list);
    }

    /**
     * 导出资产类别列表
     */
    @RequiresPermissions("asset:category:export")
    @Log(title = "资产类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmsAssetCategory amsAssetCategory)
    {
        List<AmsAssetCategory> list = amsAssetCategoryService.selectAmsAssetCategoryList(amsAssetCategory);
        ExcelUtil<AmsAssetCategory> util = new ExcelUtil<AmsAssetCategory>(AmsAssetCategory.class);
        return util.exportExcel(list, "资产类别数据");
    }

    /**
     * 查看资产类别详情
     */
    @RequiresPermissions("asset:category:view")
    @GetMapping("/view/{categoryId}")
    public String view(@PathVariable("categoryId") Long categoryId, ModelMap mmap)
    {
        AmsAssetCategory amsAssetCategory = amsAssetCategoryService.selectAmsAssetCategoryByCategoryId(categoryId);
        mmap.put("amsAssetCategory", amsAssetCategory);
        return prefix + "/view";
    }

    /**
     * 新增资产类别
     */
    @RequiresPermissions("asset:category:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存资产类别
     */
    @RequiresPermissions("asset:category:add")
    @Log(title = "资产类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmsAssetCategory amsAssetCategory)
    {
        if (!amsAssetCategoryService.checkCategoryCodeUnique(amsAssetCategory))
        {
            return error("新增资产类别'" + amsAssetCategory.getCategoryName() + "'失败，类别编码已存在");
        }
        amsAssetCategory.setCreateBy(getLoginName());
        return toAjax(amsAssetCategoryService.insertAmsAssetCategory(amsAssetCategory));
    }

    /**
     * 修改资产类别
     */
    @RequiresPermissions("asset:category:edit")
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap)
    {
        AmsAssetCategory amsAssetCategory = amsAssetCategoryService.selectAmsAssetCategoryByCategoryId(categoryId);
        mmap.put("amsAssetCategory", amsAssetCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存资产类别
     */
    @RequiresPermissions("asset:category:edit")
    @Log(title = "资产类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmsAssetCategory amsAssetCategory)
    {
        if (!amsAssetCategoryService.checkCategoryCodeUnique(amsAssetCategory))
        {
            return error("修改资产类别'" + amsAssetCategory.getCategoryName() + "'失败，类别编码已存在");
        }
        amsAssetCategory.setUpdateBy(getLoginName());
        return toAjax(amsAssetCategoryService.updateAmsAssetCategory(amsAssetCategory));
    }

    /**
     * 删除资产类别
     */
    @RequiresPermissions("asset:category:remove")
    @Log(title = "资产类别", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amsAssetCategoryService.deleteAmsAssetCategoryByCategoryIds(ids));
    }
}
