package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysTableSet;
import com.ruoyi.system.domain.SysTableSetQuery;
import com.ruoyi.system.service.ISysTableSetService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 列宽修改Controller
 *
 * @author ruoyi
 * @date 2021-01-30
 */
//@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/set")
public class SysTableSetController extends BaseController {

    @Autowired
    private  ISysTableSetService iSysTableSetService;

    /**
     * 查询列宽修改列表
     */
    @PreAuthorize("@ss.hasPermi('system:set:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTableSet sysTableSet) {
        startPage();
        List<SysTableSet> list = iSysTableSetService.queryList(sysTableSet);
        return getDataTable(list);
    }

    /**
     * 重置当前用户、当前模块的信息
     */
    @PostMapping("/resetModule")
    public AjaxResult resetModule(@RequestBody SysTableSet sysTableSet) {
        if (StringUtils.isEmpty(sysTableSet.getUserId())) {
            return AjaxResult.error("缺少用户信息，请确认");
        }
        if (StringUtils.isEmpty(sysTableSet.getTableName())) {
            return AjaxResult.error("缺少模块信息，请确认");
        }
        return iSysTableSetService.resetModule(sysTableSet);
    }

    /**
     * 导出列宽修改列表
     */
    @PreAuthorize("@ss.hasPermi('system:set:export')")
    @Log(title = "列宽修改", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysTableSet sysTableSet) {
        List<SysTableSet> list = iSysTableSetService.queryList(sysTableSet);
        ExcelUtil<SysTableSet> util = new ExcelUtil<SysTableSet>(SysTableSet.class);
        return util.exportExcel(list, "set");
    }

    /**
     * 新增列宽修改
     */
//    @PreAuthorize("@ss.hasPermi('system:set:add')")
    @Log(title = "列宽修改", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTableSetQuery sysTableSetQuery) {
        if (sysTableSetQuery.getUserId() == null || StringUtils.isEmpty(sysTableSetQuery.getTableName())) {
            return AjaxResult.error("参数缺失");
        }
        return iSysTableSetService.saveTableSet(sysTableSetQuery);
    }

    /**
     * 修改列宽修改
     */
    @PreAuthorize("@ss.hasPermi('system:set:edit')")
    @Log(title = "列宽修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTableSet sysTableSet) {
        return toAjax(iSysTableSetService.updateTableSetMessage(sysTableSet) ? 1 : 0);
    }

    /**
     * 查询列宽修改列表
     *
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('system:set:select')")
    @GetMapping("/select")
    public AjaxResult select(SysTableSet sysTableSet) {
        if (sysTableSet.getUserId() == null || StringUtils.isEmpty(sysTableSet.getTableName())) {
            return AjaxResult.error("参数缺失");
        }
        List<SysTableSet> list = iSysTableSetService.selectTableSet(sysTableSet);
        return AjaxResult.success(list);
    }
}
