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.AmsAssetLocation;
import com.ruoyi.asset.domain.AmsWarehouse;
import com.ruoyi.asset.service.IAmsAssetLocationService;
import com.ruoyi.asset.service.IAmsWarehouseService;
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/location")
public class AmsAssetLocationController extends BaseController
{
    private static final String ENABLED_YES = "Y";

    private String prefix = "asset/location";

    @Autowired
    private IAmsAssetLocationService amsAssetLocationService;

    @Autowired
    private IAmsWarehouseService amsWarehouseService;

    @RequiresPermissions("asset:location:view")
    @GetMapping()
    public String location(ModelMap mmap)
    {
        mmap.put("warehouseList", selectEnabledWarehouseList());
        return prefix + "/location";
    }

    /**
     * 查询资产位置列表
     */
    @RequiresPermissions("asset:location:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmsAssetLocation amsAssetLocation)
    {
        startPage();
        List<AmsAssetLocation> list = amsAssetLocationService.selectAmsAssetLocationList(amsAssetLocation);
        return getDataTable(list);
    }

    /**
     * 导出资产位置列表
     */
    @RequiresPermissions("asset:location:export")
    @Log(title = "资产位置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmsAssetLocation amsAssetLocation)
    {
        List<AmsAssetLocation> list = amsAssetLocationService.selectAmsAssetLocationList(amsAssetLocation);
        ExcelUtil<AmsAssetLocation> util = new ExcelUtil<AmsAssetLocation>(AmsAssetLocation.class);
        return util.exportExcel(list, "资产位置数据");
    }

    /**
     * 查看资产位置详情
     */
    @RequiresPermissions("asset:location:view")
    @GetMapping("/view/{locationId}")
    public String view(@PathVariable("locationId") Long locationId, ModelMap mmap)
    {
        AmsAssetLocation amsAssetLocation = amsAssetLocationService.selectAmsAssetLocationByLocationId(locationId);
        mmap.put("amsAssetLocation", amsAssetLocation);
        return prefix + "/view";
    }

    /**
     * 新增资产位置
     */
    @RequiresPermissions("asset:location:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("warehouseList", selectEnabledWarehouseList());
        return prefix + "/add";
    }

    /**
     * 新增保存资产位置
     */
    @RequiresPermissions("asset:location:add")
    @Log(title = "资产位置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmsAssetLocation amsAssetLocation)
    {
        if (!amsAssetLocationService.checkLocationCodeUnique(amsAssetLocation))
        {
            return error("新增资产位置'" + amsAssetLocation.getLocationName() + "'失败，位置编码已存在");
        }
        amsAssetLocation.setCreateBy(getLoginName());
        return toAjax(amsAssetLocationService.insertAmsAssetLocation(amsAssetLocation));
    }

    /**
     * 修改资产位置
     */
    @RequiresPermissions("asset:location:edit")
    @GetMapping("/edit/{locationId}")
    public String edit(@PathVariable("locationId") Long locationId, ModelMap mmap)
    {
        AmsAssetLocation amsAssetLocation = amsAssetLocationService.selectAmsAssetLocationByLocationId(locationId);
        mmap.put("amsAssetLocation", amsAssetLocation);
        mmap.put("warehouseList", selectEnabledWarehouseList());
        return prefix + "/edit";
    }

    /**
     * 修改保存资产位置
     */
    @RequiresPermissions("asset:location:edit")
    @Log(title = "资产位置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmsAssetLocation amsAssetLocation)
    {
        if (!amsAssetLocationService.checkLocationCodeUnique(amsAssetLocation))
        {
            return error("修改资产位置'" + amsAssetLocation.getLocationName() + "'失败，位置编码已存在");
        }
        amsAssetLocation.setUpdateBy(getLoginName());
        return toAjax(amsAssetLocationService.updateAmsAssetLocation(amsAssetLocation));
    }

    /**
     * 删除资产位置
     */
    @RequiresPermissions("asset:location:remove")
    @Log(title = "资产位置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amsAssetLocationService.deleteAmsAssetLocationByLocationIds(ids));
    }

    /**
     * 位置只能归属启用仓库，页面下拉提前过滤，服务层仍会再次校验。
     */
    private List<AmsWarehouse> selectEnabledWarehouseList()
    {
        AmsWarehouse warehouse = new AmsWarehouse();
        warehouse.setEnabled(ENABLED_YES);
        return amsWarehouseService.selectAmsWarehouseList(warehouse);
    }
}
