package com.zg.project.Inventory.Task.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zg.framework.aspectj.lang.annotation.Log;
import com.zg.framework.aspectj.lang.enums.BusinessType;
import com.zg.project.Inventory.domain.entity.InventoryTask;
import com.zg.project.Inventory.Task.service.IInventoryTaskService;
import com.zg.framework.web.controller.BaseController;
import com.zg.framework.web.domain.AjaxResult;
import com.zg.common.utils.poi.ExcelUtil;
import com.zg.framework.web.page.TableDataInfo;

/**
 * 盘点任务Controller
 * 
 * @author zg
 * @date 2025-06-16
 */
@RestController
@RequestMapping("/Inventory/task")
public class InventoryTaskController extends BaseController
{
    @Autowired
    private IInventoryTaskService inventoryTaskService;

    /**
     * 根据当前登录用户数统计待执行盘点任务数
     * @return
     */
    @PreAuthorize("@ss.hasPermi('Inventory:task:list')")
    @GetMapping("/count")
    public AjaxResult countPending()
    {
        Long userId = getUserId();
        int count = inventoryTaskService.countPendingTaskByUserId(userId);
        return success(count);
    }

    /**
     * 查询盘点任务列表
     */
    @PreAuthorize("@ss.hasPermi('Inventory:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(InventoryTask inventoryTask)
    {
        startPage();
        List<InventoryTask> list = inventoryTaskService.selectInventoryTaskList(inventoryTask);
        return getDataTable(list);
    }

    /**
     * 导出盘点任务列表
     */
    @PreAuthorize("@ss.hasPermi('Inventory:task:export')")
    @Log(title = "盘点任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InventoryTask inventoryTask)
    {
        List<InventoryTask> list = inventoryTaskService.selectInventoryTaskList(inventoryTask);
        ExcelUtil<InventoryTask> util = new ExcelUtil<InventoryTask>(InventoryTask.class);
        util.exportExcel(response, list, "盘点任务数据");
    }

    /**
     * 获取盘点任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('Inventory:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(inventoryTaskService.selectInventoryTaskById(id));
    }

    /**
     * 新增盘点任务
     */
    @PreAuthorize("@ss.hasPermi('Inventory:task:add')")
    @Log(title = "盘点任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InventoryTask inventoryTask)
    {
        return toAjax(inventoryTaskService.insertInventoryTask(inventoryTask));
    }

    /**
     * 修改盘点任务
     */
    @PreAuthorize("@ss.hasPermi('Inventory:task:edit')")
    @Log(title = "盘点任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InventoryTask inventoryTask)
    {
        return toAjax(inventoryTaskService.updateInventoryTask(inventoryTask));
    }

    /**
     * 删除盘点任务
     */
    @PreAuthorize("@ss.hasPermi('Inventory:task:remove')")
    @Log(title = "盘点任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(inventoryTaskService.deleteInventoryTaskByIds(ids));
    }
}
