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.constant.UserConstants;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.AmsWarehouse;
import com.ruoyi.asset.service.IAmsWarehouseService;
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.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 仓库管理Controller
 * 
 * @author Yangk
 * @date 2026-06-03
 */
@Controller
@RequestMapping("/asset/warehouse")
public class AmsWarehouseController extends BaseController
{
    private String prefix = "asset/warehouse";

    @Autowired
    private IAmsWarehouseService amsWarehouseService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("asset:warehouse:view")
    @GetMapping()
    public String warehouse(ModelMap mmap)
    {
        mmap.put("deptList", selectNormalDeptList());
        mmap.put("userList", selectNormalUserList());
        return prefix + "/warehouse";
    }

    /**
     * 查询仓库管理列表
     */
    @RequiresPermissions("asset:warehouse:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmsWarehouse amsWarehouse)
    {
        startPage();
        List<AmsWarehouse> list = amsWarehouseService.selectAmsWarehouseList(amsWarehouse);
        return getDataTable(list);
    }

    /**
     * 导出仓库管理列表
     */
    @RequiresPermissions("asset:warehouse:export")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmsWarehouse amsWarehouse)
    {
        List<AmsWarehouse> list = amsWarehouseService.selectAmsWarehouseList(amsWarehouse);
        ExcelUtil<AmsWarehouse> util = new ExcelUtil<AmsWarehouse>(AmsWarehouse.class);
        return util.exportExcel(list, "仓库管理数据");
    }

    /**
     * 查看仓库管理详情
     */
    @RequiresPermissions("asset:warehouse:view")
    @GetMapping("/view/{warehouseId}")
    public String view(@PathVariable("warehouseId") Long warehouseId, ModelMap mmap)
    {
        AmsWarehouse amsWarehouse = amsWarehouseService.selectAmsWarehouseByWarehouseId(warehouseId);
        mmap.put("amsWarehouse", amsWarehouse);
        return prefix + "/view";
    }

    /**
     * 新增仓库管理
     */
    @RequiresPermissions("asset:warehouse:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("deptList", selectNormalDeptList());
        mmap.put("userList", selectNormalUserList());
        return prefix + "/add";
    }

    /**
     * 新增保存仓库管理
     */
    @RequiresPermissions("asset:warehouse:add")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmsWarehouse amsWarehouse)
    {
        if (!amsWarehouseService.checkWarehouseCodeUnique(amsWarehouse))
        {
            return error("新增仓库'" + amsWarehouse.getWarehouseName() + "'失败，仓库编码已存在");
        }
        amsWarehouse.setCreateBy(getLoginName());
        return toAjax(amsWarehouseService.insertAmsWarehouse(amsWarehouse));
    }

    /**
     * 修改仓库管理
     */
    @RequiresPermissions("asset:warehouse:edit")
    @GetMapping("/edit/{warehouseId}")
    public String edit(@PathVariable("warehouseId") Long warehouseId, ModelMap mmap)
    {
        AmsWarehouse amsWarehouse = amsWarehouseService.selectAmsWarehouseByWarehouseId(warehouseId);
        mmap.put("amsWarehouse", amsWarehouse);
        mmap.put("deptList", selectNormalDeptList());
        mmap.put("userList", selectNormalUserList());
        return prefix + "/edit";
    }

    /**
     * 修改保存仓库管理
     */
    @RequiresPermissions("asset:warehouse:edit")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmsWarehouse amsWarehouse)
    {
        if (!amsWarehouseService.checkWarehouseCodeUnique(amsWarehouse))
        {
            return error("修改仓库'" + amsWarehouse.getWarehouseName() + "'失败，仓库编码已存在");
        }
        amsWarehouse.setUpdateBy(getLoginName());
        return toAjax(amsWarehouseService.updateAmsWarehouse(amsWarehouse));
    }

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

    /**
     * 只提供正常部门给仓库选择，避免仓库主数据挂到已停用部门。
     */
    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);
    }
}
