package com.zbf.web.controller.system;

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.TBasePallet;
import com.zbf.system.domain.TBaseStorageAreaLocation;
import com.zbf.system.service.ITBasePalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 托盘Controller
 *
 * @author sunits
 * @date 2024-06-16
 */
@RestController
@RequestMapping("/system/pallet")
public class TBasePalletController extends BaseController {
    @Autowired
    private ITBasePalletService tBasePalletService;

    /**
     * 查询托盘列表
     */
    @PreAuthorize("@ss.hasPermi('system:pallet:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBasePallet tBasePallet) {
        startPage();
        List<TBasePallet> list = tBasePalletService.selectTBasePalletList(tBasePallet);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setColumns(tBasePallet.getColumnsJson());
        return dataTable;
    }

    /**
     * 导出托盘列表
     */
    @PreAuthorize("@ss.hasPermi('system:pallet:export')")
    @Log(title = "托盘", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBasePallet tBasePallet) {
        List<TBasePallet> list = tBasePalletService.selectTBasePalletList(tBasePallet);
        ExcelUtil<TBasePallet> util = new ExcelUtil<TBasePallet>(TBasePallet.class);
        util.exportExcel(response, list, "托盘数据");
    }

    /**
     * 获取托盘详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pallet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tBasePalletService.selectTBasePalletById(id));
    }

    /**
     * 新增托盘
     */
    @PreAuthorize("@ss.hasPermi('system:pallet:add')")
    @Log(title = "托盘", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBasePallet tBasePallet) {
        return toAjax(tBasePalletService.insertTBasePallet(tBasePallet));
    }

    /**
     * 修改托盘
     */
    @PreAuthorize("@ss.hasPermi('system:pallet:edit')")
    @Log(title = "托盘", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBasePallet tBasePallet) {
        return toAjax(tBasePalletService.updateTBasePallet(tBasePallet));
    }

    /**
     * 删除托盘
     */
    @PreAuthorize("@ss.hasPermi('system:pallet:remove')")
    @Log(title = "托盘", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tBasePalletService.deleteTBasePalletByIds(ids));
    }

    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:pallet:remove')")
    @Log(title = "托盘", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable Long[] ids) {
        return toAjax(tBasePalletService.disabledTBasePalletByIds(ids));
    }

    /**
     * 禁用
     */
    @PreAuthorize("@ss.hasPermi('system:pallet:edit')")
    @Log(title = "托盘", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable Long[] ids) {
        return toAjax(tBasePalletService.enableTBasePalletByIds(ids));
    }

    /**
     * 更改禁用状态
     */
    @Log(title = "托盘管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus/{ids}")
    public AjaxResult changeStatus(@PathVariable Long[] ids,@RequestBody TBasePallet tBasePallet) {
        return toAjax(tBasePalletService.changeStatus(ids,tBasePallet));
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TBasePallet> util = new ExcelUtil<TBasePallet>(TBasePallet.class);
        util.importTemplateExcel(response, "托盘数据");
    }

    @Log(title = "托盘", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:pallet:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TBasePallet> util = new ExcelUtil<TBasePallet>(TBasePallet.class);
        List<TBasePallet> importList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tBasePalletService.importData(importList, updateSupport, operName);
        return success(message);
    }
}

