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.AmsAsset;
import com.ruoyi.asset.domain.AmsDisposalOperateContext;
import com.ruoyi.asset.domain.AmsDisposalOrder;
import com.ruoyi.asset.service.IAmsDisposalOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报废管理Controller
 *
 * @author Yangk
 * @date 2026-06-17
 */
@Controller
@RequestMapping("/asset/disposal")
public class AmsDisposalOrderController extends BaseController
{
    private String prefix = "asset/disposal";

    @Autowired
    private IAmsDisposalOrderService amsDisposalOrderService;

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

    /**
     * 查询报废管理列表
     */
    @RequiresPermissions("asset:disposal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmsDisposalOrder amsDisposalOrder)
    {
        startPage();
        List<AmsDisposalOrder> list = amsDisposalOrderService.selectAmsDisposalOrderList(amsDisposalOrder);
        return getDataTable(list);
    }

    /**
     * 弹出选择资产模态框
     */
    @RequiresPermissions(value = { "asset:disposal:add", "asset:disposal: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";
    }

    /**
     * 查询可用于当前报废单选择的资产列表（在库/在用且未被占用）
     */
    @RequiresPermissions(value = { "asset:disposal:add", "asset:disposal:edit" }, logical = Logical.OR)
    @PostMapping("/availableAssetList")
    @ResponseBody
    public TableDataInfo availableAssetList(AmsAsset asset, @RequestParam(value = "orderId", required = false) Long orderId)
    {
        startPage();
        List<AmsAsset> list = amsDisposalOrderService.selectAvailableDisposalAssetList(asset, orderId);
        return getDataTable(list);
    }

    /**
     * 导出报废管理列表
     */
    @RequiresPermissions("asset:disposal:export")
    @Log(title = "报废管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmsDisposalOrder amsDisposalOrder)
    {
        List<AmsDisposalOrder> list = amsDisposalOrderService.selectAmsDisposalOrderList(amsDisposalOrder);
        ExcelUtil<AmsDisposalOrder> util = new ExcelUtil<AmsDisposalOrder>(AmsDisposalOrder.class);
        return util.exportExcel(list, "报废管理数据");
    }

    /**
     * 查看报废管理详情
     */
    @RequiresPermissions("asset:disposal:view")
    @GetMapping("/view/{orderId}")
    public String view(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        AmsDisposalOrder amsDisposalOrder = amsDisposalOrderService.selectAmsDisposalOrderByOrderId(orderId);
        mmap.put("amsDisposalOrder", amsDisposalOrder);
        return prefix + "/view";
    }

    /**
     * 新增报废管理页面
     */
    @RequiresPermissions("asset:disposal:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysUser currentUser = getSysUser();
        mmap.put("applicantId", currentUser.getUserId());
        mmap.put("applicantName", currentUser.getUserName());
        if (currentUser.getDept() != null)
        {
            mmap.put("applyDeptId", currentUser.getDeptId());
            mmap.put("applyDeptName", currentUser.getDept().getDeptName());
        }
        return prefix + "/add";
    }

    /**
     * 新增保存报废管理（保存为草稿）
     */
    @RequiresPermissions("asset:disposal:add")
    @Log(title = "报废管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmsDisposalOrder amsDisposalOrder)
    {
        return toAjax(amsDisposalOrderService.insertAmsDisposalOrder(amsDisposalOrder, buildOperateContext()));
    }

    /**
     * 修改报废管理页面
     */
    @RequiresPermissions("asset:disposal:edit")
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        AmsDisposalOrder amsDisposalOrder = amsDisposalOrderService.selectAmsDisposalOrderByOrderId(orderId);
        mmap.put("amsDisposalOrder", amsDisposalOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存报废管理（保存为草稿）
     */
    @RequiresPermissions("asset:disposal:edit")
    @Log(title = "报废管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmsDisposalOrder amsDisposalOrder)
    {
        return toAjax(amsDisposalOrderService.updateAmsDisposalOrder(amsDisposalOrder, buildOperateContext()));
    }

    /**
     * 删除报废管理（物理删除改为逻辑删除）
     */
    @RequiresPermissions("asset:disposal:remove")
    @Log(title = "报废管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amsDisposalOrderService.deleteAmsDisposalOrderByOrderIds(ids));
    }

    /**
     * 提交报废申请
     */
    @RequiresPermissions("asset:disposal:submit")
    @Log(title = "报废管理", businessType = BusinessType.UPDATE)
    @PostMapping("/submit/{orderId}")
    @ResponseBody
    public AjaxResult submit(@PathVariable("orderId") Long orderId)
    {
        return toAjax(amsDisposalOrderService.submitDisposal(orderId, buildOperateContext()));
    }

    /**
     * 确认报废处置
     */
    @RequiresPermissions("asset:disposal:confirm")
    @Log(title = "报废管理", businessType = BusinessType.UPDATE)
    @PostMapping("/confirm/{orderId}")
    @ResponseBody
    public AjaxResult confirm(@PathVariable("orderId") Long orderId)
    {
        return toAjax(amsDisposalOrderService.confirmDisposal(orderId, buildOperateContext()));
    }

    /**
     * 驳回报废申请
     */
    @RequiresPermissions("asset:disposal:reject")
    @Log(title = "报废管理", businessType = BusinessType.UPDATE)
    @PostMapping("/reject/{orderId}")
    @ResponseBody
    public AjaxResult reject(@PathVariable("orderId") Long orderId, @RequestParam("rejectReason") String rejectReason)
    {
        return toAjax(amsDisposalOrderService.rejectDisposal(orderId, rejectReason, buildOperateContext()));
    }

    private AmsDisposalOperateContext buildOperateContext()
    {
        SysUser currentUser = getSysUser();
        Long deptId = currentUser.getDept() == null ? null : currentUser.getDeptId();
        String deptName = currentUser.getDept() == null ? null : currentUser.getDept().getDeptName();
        return new AmsDisposalOperateContext(currentUser.getUserId(), currentUser.getUserName(), getLoginName(),
                deptId, deptName);
    }
}
