package com.ruoyi.web.controller.warehouse.basicData;

import com.ruoyi.basicData.domain.TFees;
import com.ruoyi.basicData.service.ITFeesService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.UserConstants;
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.poi.ExcelUtil;
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 2020-12-11
 */
@RestController
@RequestMapping("/basicdata/fees")
public class TFeesController extends BaseController {
    @Autowired
    private ITFeesService tFeesService;

    /**
     * 查询费用信息列表
     */
    @PreAuthorize("@ss.hasPermi('basicdata:fees:list')")
    @GetMapping("/list")
    public TableDataInfo list(TFees tFees) {
        startPage();
        List<TFees> list = tFeesService.selectTFeesList(tFees);
        return getDataTable(list);
    }

    /**
     * 导出费用信息列表
     */
    @PreAuthorize("@ss.hasPermi('basicdata:fees:export')")
    @Log(title = "费用信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TFees tFees) {
        List<TFees> list = tFeesService.selectTFeesList(tFees);
        ExcelUtil<TFees> util = new ExcelUtil<TFees>(TFees.class);
        return util.exportExcel(list, "fees");
    }

    /**
     * 获取费用信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:fees:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return AjaxResult.success(tFeesService.selectTFeesById(fId));
    }

    /**
     * 新增费用信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:fees:add')")
    @Log(title = "费用信息", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody TFees tFees) {
        // 检验编号、名称、 唯一
        if (UserConstants.NOT_UNIQUE.equals(tFeesService.checkUFNoUnique(tFees))) {
            return AjaxResult.error("新增费用'" + tFees.getfNo() + "'失败，编号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tFeesService.checkUFNnameUnique(tFees))) {
            return AjaxResult.error("新增费用'" + tFees.getfName() + "'失败，名称已存在");
        }
        return toAjax(tFeesService.insertTFees(tFees));
    }

    /**
     * 修改费用信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:fees:edit')")
    @Log(title = "费用信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFees tFees) {
        // 检验编号、名称、 唯一
        if (UserConstants.NOT_UNIQUE.equals(tFeesService.checkUFNoUnique(tFees))) {
            return AjaxResult.error("新增费用'" + tFees.getfNo() + "'失败，编号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tFeesService.checkUFNnameUnique(tFees))) {
            return AjaxResult.error("新增费用'" + tFees.getfName() + "'失败，名称已存在");
        }
        return tFeesService.updateTFees(tFees);
    }

    /**
     * 删除费用信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:fees:remove')")
    @Log(title = "费用信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fIds}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return tFeesService.deleteTFeesByIds(fIds);
    }
}
