package com.ruoyi.asset.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.Logical;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.AmsInboundOrder;
import com.ruoyi.asset.domain.AmsAsset;
import com.ruoyi.asset.domain.AmsAssetLocation;
import com.ruoyi.asset.domain.AmsWarehouse;
import com.ruoyi.asset.service.IAmsAssetLocationService;
import com.ruoyi.asset.service.IAmsInboundOrderService;
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-10
 */
@Controller
@RequestMapping("/asset/inbound")
public class AmsInboundOrderController extends BaseController
{
    private static final String ENABLED_YES = "Y";

    private String prefix = "asset/inbound";

    @Autowired
    private IAmsInboundOrderService amsInboundOrderService;

    @Autowired
    private IAmsWarehouseService amsWarehouseService;

    @Autowired
    private IAmsAssetLocationService amsAssetLocationService;

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

    /**
     * 查询入库管理列表
     */
    @RequiresPermissions("asset:inbound:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmsInboundOrder amsInboundOrder)
    {
        startPage();
        List<AmsInboundOrder> list = amsInboundOrderService.selectAmsInboundOrderList(amsInboundOrder);
        return getDataTable(list);
    }

    /**
     * 打开待入库资产分页选择页（入库单新增/编辑的明细弹窗选择器）
     * <p>编辑场景会携带 orderId 参数，用于排除当前单据自身占用的资产。</p>
     */
    @RequiresPermissions(value = { "asset:inbound:add", "asset:inbound:edit" }, logical = Logical.OR)
    @GetMapping("/selectAsset")
    public String selectAsset(@RequestParam(value = "orderId", required = false) Long orderId, ModelMap mmap)
    {
        mmap.put("orderId", orderId);
        return prefix + "/selectAsset";
    }

    /**
     * 分页查询未被其他有效草稿占用的待入库资产
     * <p>仅返回 PENDING_INBOUND 且无其他草稿入库单引用的资产，
     * 编辑场景传入 orderId 排除当前单据自身的占用。</p>
     */
    @RequiresPermissions(value = { "asset:inbound:add", "asset:inbound:edit" }, logical = Logical.OR)
    @PostMapping("/availableAssetList")
    @ResponseBody
    public TableDataInfo availableAssetList(AmsAsset amsAsset,
            @RequestParam(value = "orderId", required = false) Long orderId)
    {
        startPage();
        return getDataTable(amsInboundOrderService.selectAvailableInboundAssetList(amsAsset, orderId));
    }

    /**
     * 导出入库管理列表
     */
    @RequiresPermissions("asset:inbound:export")
    @Log(title = "入库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmsInboundOrder amsInboundOrder)
    {
        List<AmsInboundOrder> list = amsInboundOrderService.selectAmsInboundOrderList(amsInboundOrder);
        ExcelUtil<AmsInboundOrder> util = new ExcelUtil<AmsInboundOrder>(AmsInboundOrder.class);
        return util.exportExcel(list, "入库管理数据");
    }

    /**
     * 查看入库管理详情
     */
    @RequiresPermissions("asset:inbound:view")
    @GetMapping("/view/{orderId}")
    public String view(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        AmsInboundOrder amsInboundOrder = amsInboundOrderService.selectAmsInboundOrderByOrderId(orderId);
        mmap.put("amsInboundOrder", amsInboundOrder);
        return prefix + "/view";
    }

    /**
     * 新增入库管理
     */
    @RequiresPermissions("asset:inbound:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        putInboundOptions(mmap);
        return prefix + "/add";
    }

    /**
     * 新增保存入库管理
     */
    @RequiresPermissions("asset:inbound:add")
    @Log(title = "入库管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmsInboundOrder amsInboundOrder)
    {
        amsInboundOrder.setCreateBy(getLoginName());
        return toAjax(amsInboundOrderService.insertAmsInboundOrder(amsInboundOrder));
    }

    /**
     * 修改入库管理
     */
    @RequiresPermissions("asset:inbound:edit")
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        AmsInboundOrder amsInboundOrder = amsInboundOrderService.selectAmsInboundOrderByOrderId(orderId);
        mmap.put("amsInboundOrder", amsInboundOrder);
        putInboundOptions(mmap);
        return prefix + "/edit";
    }

    /**
     * 修改保存入库管理
     */
    @RequiresPermissions("asset:inbound:edit")
    @Log(title = "入库管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmsInboundOrder amsInboundOrder)
    {
        amsInboundOrder.setUpdateBy(getLoginName());
        return toAjax(amsInboundOrderService.updateAmsInboundOrder(amsInboundOrder));
    }

    /**
     * 确认入库
     */
    @RequiresPermissions("asset:inbound:confirm")
    @Log(title = "入库管理", businessType = BusinessType.UPDATE)
    @PostMapping("/confirm/{orderId}")
    @ResponseBody
    public AjaxResult confirm(@PathVariable("orderId") Long orderId)
    {
        return toAjax(amsInboundOrderService.confirmInbound(orderId, getUserId(),
                getSysUser().getUserName(), getLoginName()));
    }

    /**
     * 删除入库管理
     */
    @RequiresPermissions("asset:inbound:remove")
    @Log(title = "入库管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amsInboundOrderService.deleteAmsInboundOrderByOrderIds(ids));
    }

    private void putInboundOptions(ModelMap mmap)
    {
        mmap.put("warehouseList", selectEnabledWarehouseList());

        AmsAssetLocation location = new AmsAssetLocation();
        location.setEnabled(ENABLED_YES);
        mmap.put("locationList", amsAssetLocationService.selectAmsAssetLocationList(location));
    }

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