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 org.springframework.web.multipart.MultipartFile;
import com.ruoyi.asset.domain.AmsAsset;
import com.ruoyi.asset.domain.AmsAssetCategory;
import com.ruoyi.asset.domain.AmsAssetLocation;
import com.ruoyi.asset.domain.AmsWarehouse;
import com.ruoyi.asset.service.IAmsAssetCategoryService;
import com.ruoyi.asset.service.IAmsAssetLocationService;
import com.ruoyi.asset.service.IAmsAssetService;
import com.ruoyi.asset.service.IAmsWarehouseService;
import com.ruoyi.asset.service.IAssetLifecycleService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 资产台账Controller
 * 
 * @author Yangk
 * @date 2026-06-04
 */
@Controller
@RequestMapping("/asset/asset")
public class AmsAssetController extends BaseController
{
    private static final String ENABLED_YES = "Y";

    private String prefix = "asset/asset";

    @Autowired
    private IAmsAssetService amsAssetService;

    @Autowired
    private IAssetLifecycleService assetLifecycleService;

    @Autowired
    private IAmsAssetCategoryService amsAssetCategoryService;

    @Autowired
    private IAmsWarehouseService amsWarehouseService;

    @Autowired
    private IAmsAssetLocationService amsAssetLocationService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService sysUserService;

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

    /**
     * 查询资产台账列表
     */
    @RequiresPermissions("asset:asset:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmsAsset amsAsset)
    {
        startPage();
        List<AmsAsset> list = amsAssetService.selectAmsAssetList(amsAsset);
        return getDataTable(list);
    }

    /**
     * 导出资产台账列表
     */
    @RequiresPermissions("asset:asset:export")
    @Log(title = "资产台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmsAsset amsAsset)
    {
        List<AmsAsset> list = amsAssetService.selectAmsAssetList(amsAsset);
        ExcelUtil<AmsAsset> util = new ExcelUtil<AmsAsset>(AmsAsset.class);
        return util.exportExcel(list, "资产台账数据");
    }

    /**
     * 导入资产台账列表
     */
    @RequiresPermissions("asset:asset:import")
    @Log(title = "资产台账", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<AmsAsset> util = new ExcelUtil<AmsAsset>(AmsAsset.class);
        List<AmsAsset> assetList = util.importExcel(file.getInputStream());
        String message = amsAssetService.importAmsAsset(assetList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 下载资产台账导入模板
     */
    @RequiresPermissions("asset:asset:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<AmsAsset> util = new ExcelUtil<AmsAsset>(AmsAsset.class);
        return util.importTemplateExcel("资产管理导入模版");
    }

    /**
     * 查看资产台账详情
     */
    @RequiresPermissions("asset:asset:view")
    @GetMapping("/view/{assetId}")
    public String view(@PathVariable("assetId") Long assetId, ModelMap mmap)
    {
        AmsAsset amsAsset = amsAssetService.selectAmsAssetByAssetId(assetId);
        mmap.put("amsAsset", amsAsset);
        // 获取当前资产在台账中的完整生命周期历史流转履历，传递至前端展示页面进行渲染
        mmap.put("lifecycleList", assetLifecycleService.selectByAssetId(assetId));
        return prefix + "/view";
    }

    /**
     * 新增资产台账
     */
    @RequiresPermissions("asset:asset:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        putAssetOptions(mmap);
        return prefix + "/add";
    }

    /**
     * 新增保存资产台账
     */
    @RequiresPermissions("asset:asset:add")
    @Log(title = "资产台账", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmsAsset amsAsset)
    {
        if (!amsAssetService.checkAssetCodeUnique(amsAsset))
        {
            return error("新增资产'" + amsAsset.getAssetName() + "'失败，资产编码已存在");
        }
        amsAsset.setCreateBy(getLoginName());
        return toAjax(amsAssetService.insertAmsAsset(amsAsset));
    }

    /**
     * 修改资产台账
     */
    @RequiresPermissions("asset:asset:edit")
    @GetMapping("/edit/{assetId}")
    public String edit(@PathVariable("assetId") Long assetId, ModelMap mmap)
    {
        AmsAsset amsAsset = amsAssetService.selectAmsAssetByAssetId(assetId);
        mmap.put("amsAsset", amsAsset);
        putAssetOptions(mmap);
        return prefix + "/edit";
    }

    /**
     * 修改保存资产台账
     */
    @RequiresPermissions("asset:asset:edit")
    @Log(title = "资产台账", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmsAsset amsAsset)
    {
        if (!amsAssetService.checkAssetCodeUnique(amsAsset))
        {
            return error("修改资产'" + amsAsset.getAssetName() + "'失败，资产编码已存在");
        }
        amsAsset.setUpdateBy(getLoginName());
        return toAjax(amsAssetService.updateAmsAsset(amsAsset));
    }

    /**
     * 删除资产台账
     */
    @RequiresPermissions("asset:asset:remove")
    @Log(title = "资产台账", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amsAssetService.deleteAmsAssetByAssetIds(ids));
    }

    private void putAssetOptions(ModelMap mmap)
    {
        mmap.put("categoryList", selectEnabledCategoryList());
        mmap.put("warehouseList", selectEnabledWarehouseList());
        mmap.put("locationList", selectEnabledLocationList());
        mmap.put("deptList", selectNormalDeptList());
        mmap.put("userList", selectNormalUserList());
    }

    private List<AmsAssetCategory> selectEnabledCategoryList()
    {
        AmsAssetCategory category = new AmsAssetCategory();
        category.setEnabled(ENABLED_YES);
        return amsAssetCategoryService.selectAmsAssetCategoryList(category);
    }

    private List<AmsWarehouse> selectEnabledWarehouseList()
    {
        AmsWarehouse warehouse = new AmsWarehouse();
        warehouse.setEnabled(ENABLED_YES);
        return amsWarehouseService.selectAmsWarehouseList(warehouse);
    }

    private List<AmsAssetLocation> selectEnabledLocationList()
    {
        AmsAssetLocation location = new AmsAssetLocation();
        location.setEnabled(ENABLED_YES);
        return amsAssetLocationService.selectAmsAssetLocationList(location);
    }

    private List<SysDept> selectNormalDeptList()
    {
        SysDept dept = new SysDept();
        dept.setStatus(UserConstants.DEPT_NORMAL);
        return sysDeptService.selectDeptList(dept);
    }

    private List<SysUser> selectNormalUserList()
    {
        SysUser user = new SysUser();
        user.setStatus(UserConstants.NORMAL);
        return sysUserService.selectUserList(user);
    }
}
