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.TBaseProvider;
import com.zbf.system.service.ITBaseProviderService;
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 tony
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/system/provider")
public class TBaseProviderController extends BaseController {
    @Autowired
    private ITBaseProviderService tBaseProviderService;

    /**
     * 查询供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:provider:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBaseProvider tBaseProvider) {
        startPage();
        List<TBaseProvider> list = tBaseProviderService.selectTBaseProviderList(tBaseProvider);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setColumns(tBaseProvider.getColumnsJson());
        return dataTable;
    }

    /**
     * 导出供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:provider:export')")
    @Log(title = "供应商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBaseProvider tBaseProvider) {
        List<TBaseProvider> list = tBaseProviderService.selectTBaseProviderList(tBaseProvider);
        ExcelUtil<TBaseProvider> util = new ExcelUtil<TBaseProvider>(TBaseProvider.class);
        util.exportExcel(response, list, "供应商管理数据");
    }

    /**
     * 获取供应商管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:provider:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tBaseProviderService.selectTBaseProviderById(id));
    }

    /**
     * 新增供应商管理
     */
    @PreAuthorize("@ss.hasPermi('system:provider:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBaseProvider tBaseProvider) {
        return toAjax(tBaseProviderService.insertTBaseProvider(tBaseProvider));
    }

    /**
     * 修改供应商管理
     */
    @PreAuthorize("@ss.hasPermi('system:provider:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBaseProvider tBaseProvider) {
        return toAjax(tBaseProviderService.updateTBaseProvider(tBaseProvider));
    }

    /**
     * 删除供应商管理
     */
    @PreAuthorize("@ss.hasPermi('system:provider:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tBaseProviderService.deleteTBaseProviderByIds(ids));
    }

    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:provider:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable Long[] ids) {
        return toAjax(tBaseProviderService.disabledTBaseProviderByIds(ids));
    }

    /**
     * 禁用
     */
    @PreAuthorize("@ss.hasPermi('system:provider:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable Long[] ids) {
        return toAjax(tBaseProviderService.enableTBaseProviderByIds(ids));
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('system:provider:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TBaseProvider tBaseProvider) {
        return toAjax(tBaseProviderService.updateStatus(tBaseProvider));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TBaseProvider> util = new ExcelUtil<TBaseProvider>(TBaseProvider.class);
        util.importTemplateExcel(response, "供应商数据");
    }

    @Log(title = "供应商管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:provider:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TBaseProvider> util = new ExcelUtil<TBaseProvider>(TBaseProvider.class);
        List<TBaseProvider> importList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tBaseProviderService.importData(importList, updateSupport, operName);
        return success(message);
    }
}

