package com.ruoyi.inventory.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.inventory.domain.dto.TakeStockDTO;
import com.ruoyi.inventory.domain.dto.TbAssetInventoryDTO;
import com.ruoyi.system.service.ISysDeptService;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.TbAssetInventory;
import com.ruoyi.inventory.service.ITbAssetInventoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资产盘点记录Controller
 * 
 * @author ydl
 * @date 2023-06-01
 */
@RestController
@RequestMapping("/inventory/inventory")
public class TbAssetInventoryController extends BaseController
{
    @Autowired
    private ITbAssetInventoryService tbAssetInventoryService;


    /**
     * 查询资产盘点记录列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbAssetInventory tbAssetInventory)
    {
        startPage();
        List<TbAssetInventory> list = tbAssetInventoryService.selectTbAssetInventoryList(tbAssetInventory);
        return getDataTable(list);
    }

    /**
     * 查询资产盘点记录列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:list')")
    @GetMapping("/listDTO")
    public TableDataInfo listDTO(TbAssetInventoryDTO dto)
    {
        startPage();
        List<TbAssetInventoryDTO> list = tbAssetInventoryService.selectTbAssetInventoryDTOList(dto);
        return getDataTable(list);
    }

    /**
     * 导出资产盘点记录列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:export')")
    @Log(title = "资产盘点记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAssetInventory tbAssetInventory)
    {
        List<TbAssetInventory> list = tbAssetInventoryService.selectTbAssetInventoryList(tbAssetInventory);
        ExcelUtil<TbAssetInventory> util = new ExcelUtil<TbAssetInventory>(TbAssetInventory.class);
        util.exportExcel(response, list, "资产盘点记录数据");
    }

    /**
     * 获取资产盘点记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbAssetInventoryService.selectTbAssetInventoryById(id));
    }

    /**
     * 获取资产盘点记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:query')")
    @GetMapping(value = "/info/{orderNumber}")
    public AjaxResult getInfo2(@PathVariable("orderNumber") String orderNumber)
    {
        return success(tbAssetInventoryService.selectTbAssetInventoryByNum(orderNumber));
    }

    /**
     * 新增资产盘点记录
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:add')")
    @Log(title = "资产盘点记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAssetInventory tbAssetInventory)
    {
        return toAjax(tbAssetInventoryService.insertTbAssetInventory(tbAssetInventory));
    }

    /**
     * 手持机盘点
     * @param dto
     * @return
     */
    @PostMapping("/takeStock")
    public AjaxResult addByTakeStock(@RequestBody List<TakeStockDTO> dto){
        // int result = tbAssetInventoryService.addByTakeStock(dto);
        int result = tbAssetInventoryService.handheldInventory(dto);
        return toAjax(result);
    }

    /**
     * 修改资产盘点记录
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:edit')")
    @Log(title = "资产盘点记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAssetInventory tbAssetInventory)
    {
        return toAjax(tbAssetInventoryService.updateTbAssetInventory(tbAssetInventory));
    }

    /**
     * 删除资产盘点记录
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:remove')")
    @Log(title = "资产盘点记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbAssetInventoryService.deleteTbAssetInventoryByIds(ids));
    }
}
