package com.ruoyi.asset.controller;

import java.util.List;
import com.ruoyi.asset.domain.AmsAssetCategory;
import com.ruoyi.asset.domain.AmsAssetLocation;
import com.ruoyi.asset.domain.AmsInventoryTask;
import com.ruoyi.asset.domain.AmsInventoryTaskSurplus;
import com.ruoyi.asset.domain.AmsWarehouse;
import com.ruoyi.asset.service.IAmsAssetCategoryService;
import com.ruoyi.asset.service.IAmsAssetLocationService;
import com.ruoyi.asset.service.IAmsInventoryTaskService;
import com.ruoyi.asset.service.IAmsWarehouseService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
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;

/**
 * 盘点任务管理Controller
 *
 * @author Yangk
 */
@Controller
@RequestMapping("/asset/task")
public class AmsInventoryTaskController extends BaseController
{
    private static final String ENABLED_YES = "Y";

    private String prefix = "asset/task";

    @Autowired
    private IAmsInventoryTaskService amsInventoryTaskService;

    @Autowired
    private IAmsWarehouseService amsWarehouseService;

    @Autowired
    private IAmsAssetLocationService amsAssetLocationService;

    @Autowired
    private IAmsAssetCategoryService amsAssetCategoryService;

    @RequiresPermissions("asset:task:view")
    @GetMapping()
    public String task(ModelMap mmap)
    {
        putInventoryOptions(mmap);
        return prefix + "/task";
    }

    @RequiresPermissions("asset:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmsInventoryTask amsInventoryTask)
    {
        startPage();
        List<AmsInventoryTask> list = amsInventoryTaskService.selectAmsInventoryTaskList(amsInventoryTask);
        return getDataTable(list);
    }

    @RequiresPermissions("asset:task:export")
    @Log(title = "盘点任务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmsInventoryTask amsInventoryTask)
    {
        List<AmsInventoryTask> list = amsInventoryTaskService.selectAmsInventoryTaskList(amsInventoryTask);
        return new ExcelUtil<AmsInventoryTask>(AmsInventoryTask.class).exportExcel(list, "盘点任务管理数据");
    }

    @RequiresPermissions("asset:task:view")
    @GetMapping("/view/{taskId}")
    public String view(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        mmap.put("amsInventoryTask", amsInventoryTaskService.selectAmsInventoryTaskByTaskId(taskId));
        return prefix + "/view";
    }

    @RequiresPermissions("asset:task:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        putInventoryOptions(mmap);
        return prefix + "/add";
    }

    @RequiresPermissions("asset:task:add")
    @Log(title = "盘点任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmsInventoryTask amsInventoryTask)
    {
        amsInventoryTask.setCreateBy(getLoginName());
        return toAjax(amsInventoryTaskService.insertAmsInventoryTask(amsInventoryTask,
                getUserId(), getSysUser().getUserName()));
    }

    @RequiresPermissions("asset:task:edit")
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        mmap.put("amsInventoryTask", amsInventoryTaskService.selectAmsInventoryTaskByTaskId(taskId));
        putInventoryOptions(mmap);
        return prefix + "/edit";
    }

    @RequiresPermissions("asset:task:edit")
    @Log(title = "盘点任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmsInventoryTask amsInventoryTask)
    {
        amsInventoryTask.setUpdateBy(getLoginName());
        return toAjax(amsInventoryTaskService.updateAmsInventoryTask(amsInventoryTask));
    }

    /** 下发盘点任务，按当前范围生成资产快照明细。 */
    @RequiresPermissions("asset:task:issue")
    @Log(title = "盘点任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/issue/{taskId}")
    @ResponseBody
    public AjaxResult issue(@PathVariable("taskId") Long taskId)
    {
        return toAjax(amsInventoryTaskService.issueTask(taskId, getLoginName()));
    }

    @RequiresPermissions("asset:task:result")
    @GetMapping("/result/{taskId}")
    public String result(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        mmap.put("amsInventoryTask", amsInventoryTaskService.selectAmsInventoryTaskByTaskId(taskId));
        putInventoryOptions(mmap);
        return prefix + "/result";
    }

    /** 后台录入并提交盘点结果，一期 Web 闭环复用该入口，后续 PDA 只调用同一服务。 */
    @RequiresPermissions("asset:task:result")
    @Log(title = "盘点任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/result/submit")
    @ResponseBody
    public AjaxResult submitResult(AmsInventoryTask amsInventoryTask)
    {
        return toAjax(amsInventoryTaskService.submitResult(amsInventoryTask, getUserId(),
                getSysUser().getUserName(), getLoginName()));
    }

    /** 新增盘盈明细，真实盘盈不写入应盘快照行。 */
    @RequiresPermissions("asset:task:result")
    @Log(title = "盘点任务管理盘盈明细", businessType = BusinessType.INSERT)
    @PostMapping("/surplus/add")
    @ResponseBody
    public AjaxResult addSurplus(AmsInventoryTaskSurplus surplus)
    {
        return toAjax(amsInventoryTaskService.addSurplusItem(surplus, getUserId(),
                getSysUser().getUserName(), getLoginName()));
    }

    /** 删除盘盈明细，仅允许在结果提交前删除。 */
    @RequiresPermissions("asset:task:result")
    @Log(title = "盘点任务管理盘盈明细", businessType = BusinessType.DELETE)
    @PostMapping("/surplus/remove/{surplusId}")
    @ResponseBody
    public AjaxResult removeSurplus(@PathVariable("surplusId") Long surplusId)
    {
        return toAjax(amsInventoryTaskService.deleteSurplusItem(surplusId, getLoginName()));
    }

    /** 确认盘点结果，只确认任务事实，不自动调整资产台账，也不写资产履历。 */
    @RequiresPermissions("asset:task:confirm")
    @Log(title = "盘点任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/confirm/{taskId}")
    @ResponseBody
    public AjaxResult confirm(@PathVariable("taskId") Long taskId)
    {
        return toAjax(amsInventoryTaskService.confirmResult(taskId, getUserId(),
                getSysUser().getUserName(), getLoginName()));
    }

    @RequiresPermissions("asset:task:close")
    @Log(title = "盘点任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/close/{taskId}")
    @ResponseBody
    public AjaxResult close(@PathVariable("taskId") Long taskId)
    {
        return toAjax(amsInventoryTaskService.closeTask(taskId, getLoginName()));
    }

    @RequiresPermissions("asset:task:remove")
    @Log(title = "盘点任务管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amsInventoryTaskService.deleteAmsInventoryTaskByTaskIds(ids));
    }

    private void putInventoryOptions(ModelMap mmap)
    {
        mmap.put("warehouseList", selectEnabledWarehouseList());
        mmap.put("locationList", selectEnabledLocationList());
        mmap.put("categoryList", selectEnabledCategoryList());
    }

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

    private List<AmsAssetLocation> selectEnabledLocationList()
    {
        AmsAssetLocation location = new AmsAssetLocation();
        location.setEnabled(ENABLED_YES);
        return amsAssetLocationService.selectAmsAssetLocationList(location);
    }

    private List<AmsAssetCategory> selectEnabledCategoryList()
    {
        AmsAssetCategory category = new AmsAssetCategory();
        category.setEnabled(ENABLED_YES);
        return amsAssetCategoryService.selectAmsAssetCategoryList(category);
    }
}
