package com.zbf.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSONArray;
import com.zbf.generator.util.ColumnUtils;
import com.zbf.web.controller.section.EnhanceDataList;
import com.zbf.web.controller.section.ListColumns;
import liquibase.pro.packaged.S;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.zbf.common.annotation.Log;
import com.zbf.common.core.controller.BaseController;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.core.page.TableDataInfo;
import com.zbf.common.enums.BusinessType;
import com.zbf.common.utils.poi.ExcelUtil;
import com.zbf.system.domain.TBaseStorage;
import com.zbf.system.domain.TBaseStorageLocationRecord;
import com.zbf.system.service.ITBaseStorageService;

/**
 * 仓库管理Controller
 *
 * @author tony
 * @date 2024-06-19
 */
@RestController
@RequestMapping("/system/storage")
public class TBaseStorageController extends BaseController
{
    @Autowired
    private ITBaseStorageService tBaseStorageService;

    /**
     * 查询仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:storage:list')")
    @GetMapping("/list")
    @EnhanceDataList(entityType = TBaseStorage.class)
    public TableDataInfo list(TBaseStorage tBaseStorage)
    {

        List<TBaseStorage> list = tBaseStorageService.selectTBaseStorageList(tBaseStorage);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable; // 直接返回处理后的TableDataInfo对象
    }
    /**
     * 查询仓库管理列表
     */
    @GetMapping("/selectList")
    public AjaxResult selectList(TBaseStorage tBaseStorage)
    {
        tBaseStorage.setDeptId(getDeptId());
        List<TBaseStorage> list = tBaseStorageService.selectTBaseStorageList(tBaseStorage);
        return success(list);
    }

    /**
     * 导出仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:storage:export')")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBaseStorage tBaseStorage)
    {
        List<TBaseStorage> list = tBaseStorageService.selectTBaseStorageList(tBaseStorage);
        ExcelUtil<TBaseStorage> util = new ExcelUtil<TBaseStorage>(TBaseStorage.class);
        util.exportExcel(response, list, "仓库管理数据");
    }

    /**
     * 获取仓库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:storage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tBaseStorageService.selectTBaseStorageById(id));
    }

    /**
     * 新增仓库管理
     */
    @PreAuthorize("@ss.hasPermi('system:storage:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBaseStorage tBaseStorage)
    {
        return toAjax(tBaseStorageService.insertTBaseStorage(tBaseStorage));
    }

    /**
     * 修改仓库管理
     */
    @PreAuthorize("@ss.hasPermi('system:storage:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBaseStorage tBaseStorage)
    {
        return toAjax(tBaseStorageService.updateTBaseStorage(tBaseStorage));
    }

    /**
     * 删除仓库管理
     */
    @PreAuthorize("@ss.hasPermi('system:storage:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tBaseStorageService.deleteTBaseStorageByIds(ids));
    }

    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:storage:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable Long[] ids)
    {
        return toAjax(tBaseStorageService.disabledTBaseStorageByIds(ids));
    }

    /**
     * 禁用
     */
    @PreAuthorize("@ss.hasPermi('system:storage:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable Long[] ids)
    {
        return toAjax(tBaseStorageService.enableTBaseStorageByIds(ids));
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('system:storage:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TBaseStorage tBaseStorage) {
        return toAjax(tBaseStorageService.updateStatus(tBaseStorage));
    }

    /**
     * 查询仓库列表
    */
    @PreAuthorize("@ss.hasPermi('system:storage:list')")
    @GetMapping("/storageAreaLocationList")
    public AjaxResult list(TBaseStorageLocationRecord tBaseStorage)
    {
        tBaseStorage.setDeptId(getDeptId());
        List<TBaseStorageLocationRecord> list = tBaseStorageService.selectStorageAndAreaAndLocation(tBaseStorage);
        return success(list);
    }
    /**
     * 根据当前用的数据权限，查询所管理的仓库信息
     * @param tBaseStorage
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:storage:list')")
    @GetMapping("/getStorageList")
    public AjaxResult getStorageList(TBaseStorage tBaseStorage) {
        tBaseStorage.setDeptId(getDeptId());
        return success(tBaseStorageService.selectTBaseStorageList(tBaseStorage));
    }
}

