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.common.constant.UserConstants;
import com.ruoyi.asset.domain.AmsAsset;
import com.ruoyi.asset.domain.AmsRepairOperateContext;
import com.ruoyi.asset.domain.AmsRepairOrder;
import com.ruoyi.asset.service.IAmsRepairOrderService;
import com.ruoyi.system.service.ISysUserService;
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
 *
 * 核心职责：提供报修单的新增草稿、修改、逻辑删除、提交申请、确认受理、驳回、开始维修、完成维修等API接口。
 *
 * @author Yangk
 * @date 2026-06-16
 */
@Controller
@RequestMapping("/asset/repair")
public class AmsRepairOrderController extends BaseController
{
    private String prefix = "asset/repair";

    @Autowired
    private IAmsRepairOrderService amsRepairOrderService;

    @Autowired
    private ISysUserService sysUserService;

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

    /**
     * 查询报修维修管理列表
     */
    @RequiresPermissions("asset:repair:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmsRepairOrder amsRepairOrder)
    {
        startPage();
        List<AmsRepairOrder> list = amsRepairOrderService.selectAmsRepairOrderList(amsRepairOrder);
        return getDataTable(list);
    }

    /**
     * 弹出资产选择模态框
     */
    @RequiresPermissions(value = { "asset:repair:add", "asset:repair:edit" }, logical = Logical.OR)
    @GetMapping("/selectAsset")
    public String selectAsset(@RequestParam(value = "repairId", required = false) Long repairId, ModelMap mmap)
    {
        mmap.put("repairId", repairId);
        return prefix + "/selectAsset";
    }

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

    /**
     * 导出报修维修管理列表
     */
    @RequiresPermissions("asset:repair:export")
    @Log(title = "报修维修管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmsRepairOrder amsRepairOrder)
    {
        List<AmsRepairOrder> list = amsRepairOrderService.selectAmsRepairOrderList(amsRepairOrder);
        ExcelUtil<AmsRepairOrder> util = new ExcelUtil<AmsRepairOrder>(AmsRepairOrder.class);
        return util.exportExcel(list, "报修维修管理数据");
    }

    /**
     * 查看报修维修管理详情
     */
    @RequiresPermissions("asset:repair:view")
    @GetMapping("/view/{repairId}")
    public String view(@PathVariable("repairId") Long repairId, ModelMap mmap)
    {
        AmsRepairOrder amsRepairOrder = amsRepairOrderService.selectAmsRepairOrderByRepairId(repairId);
        mmap.put("amsRepairOrder", amsRepairOrder);
        return prefix + "/view";
    }

    /**
     * 新增报修维修管理
     */
    @RequiresPermissions("asset:repair:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        putUserOptions(mmap);
        return prefix + "/add";
    }

    /**
     * 新增保存报修维修管理（草稿）
     */
    @RequiresPermissions("asset:repair:add")
    @Log(title = "报修维修管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmsRepairOrder amsRepairOrder)
    {
        return toAjax(amsRepairOrderService.insertAmsRepairOrder(amsRepairOrder, buildOperateContext()));
    }

    /**
     * 修改报修维修管理
     */
    @RequiresPermissions("asset:repair:edit")
    @GetMapping("/edit/{repairId}")
    public String edit(@PathVariable("repairId") Long repairId, ModelMap mmap)
    {
        AmsRepairOrder amsRepairOrder = amsRepairOrderService.selectAmsRepairOrderByRepairId(repairId);
        mmap.put("amsRepairOrder", amsRepairOrder);
        putUserOptions(mmap);
        return prefix + "/edit";
    }

    /**
     * 修改保存报修维修管理（草稿）
     */
    @RequiresPermissions("asset:repair:edit")
    @Log(title = "报修维修管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmsRepairOrder amsRepairOrder)
    {
        return toAjax(amsRepairOrderService.updateAmsRepairOrder(amsRepairOrder, buildOperateContext()));
    }

    /**
     * 删除报修维修管理（仅限草稿）
     */
    @RequiresPermissions("asset:repair:remove")
    @Log(title = "报修维修管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amsRepairOrderService.deleteAmsRepairOrderByRepairIds(ids));
    }

    /**
     * 提交报修申请
     */
    @RequiresPermissions("asset:repair:submit")
    @Log(title = "报修维修管理", businessType = BusinessType.UPDATE)
    @PostMapping("/submit/{repairId}")
    @ResponseBody
    public AjaxResult submit(@PathVariable("repairId") Long repairId)
    {
        return toAjax(amsRepairOrderService.submitRepair(repairId, buildOperateContext()));
    }

    /**
     * 确认受理报修
     */
    @RequiresPermissions("asset:repair:confirm")
    @Log(title = "报修维修管理", businessType = BusinessType.UPDATE)
    @PostMapping("/confirm/{repairId}")
    @ResponseBody
    public AjaxResult confirm(@PathVariable("repairId") Long repairId)
    {
        return toAjax(amsRepairOrderService.confirmRepair(repairId, buildOperateContext()));
    }

    /**
     * 驳回报修申请
     */
    @RequiresPermissions("asset:repair:reject")
    @Log(title = "报修维修管理", businessType = BusinessType.UPDATE)
    @PostMapping("/reject/{repairId}")
    @ResponseBody
    public AjaxResult reject(@PathVariable("repairId") Long repairId, @RequestParam("rejectReason") String rejectReason)
    {
        return toAjax(amsRepairOrderService.rejectRepair(repairId, rejectReason, buildOperateContext()));
    }

    /**
     * 开始维修弹窗页面
     */
    @RequiresPermissions("asset:repair:start")
    @GetMapping("/start/{repairId}")
    public String start(@PathVariable("repairId") Long repairId, ModelMap mmap)
    {
        AmsRepairOrder amsRepairOrder = amsRepairOrderService.selectAmsRepairOrderByRepairId(repairId);
        mmap.put("amsRepairOrder", amsRepairOrder);
        putUserOptions(mmap);
        return prefix + "/start";
    }

    /**
     * 开始维修保存
     */
    @RequiresPermissions("asset:repair:start")
    @Log(title = "报修维修管理", businessType = BusinessType.UPDATE)
    @PostMapping("/start")
    @ResponseBody
    public AjaxResult startSave(AmsRepairOrder amsRepairOrder)
    {
        return toAjax(amsRepairOrderService.startRepair(amsRepairOrder, buildOperateContext()));
    }

    /**
     * 完成维修弹窗页面
     */
    @RequiresPermissions("asset:repair:finish")
    @GetMapping("/finish/{repairId}")
    public String finish(@PathVariable("repairId") Long repairId, ModelMap mmap)
    {
        AmsRepairOrder amsRepairOrder = amsRepairOrderService.selectAmsRepairOrderByRepairId(repairId);
        mmap.put("amsRepairOrder", amsRepairOrder);
        return prefix + "/finish";
    }

    /**
     * 完成维修保存
     */
    @RequiresPermissions("asset:repair:finish")
    @Log(title = "报修维修管理", businessType = BusinessType.UPDATE)
    @PostMapping("/finish")
    @ResponseBody
    public AjaxResult finishSave(AmsRepairOrder amsRepairOrder)
    {
        return toAjax(amsRepairOrderService.finishRepair(amsRepairOrder, buildOperateContext()));
    }

    private AmsRepairOperateContext buildOperateContext()
    {
        SysUser currentUser = getSysUser();
        return new AmsRepairOperateContext(currentUser.getUserId(), currentUser.getUserName(), getLoginName());
    }

    private void putUserOptions(ModelMap mmap)
    {
        SysUser user = new SysUser();
        user.setStatus(UserConstants.NORMAL);
        mmap.put("userList", sysUserService.selectUserList(user));
        SysUser currentUser = getSysUser();
        mmap.put("defaultReportUserId", currentUser.getUserId());
        mmap.put("defaultReportUserName", currentUser.getUserName());
    }
}
