package com.willalp.flow.controller;

import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.flow.domain.FileApproval;
import com.willalp.flow.service.IFileApprovalService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 文件审批业务Controller
 *
 * @author willalp
 * @date 2022-11-30
 */
@RestController
@RequestMapping("/flow/accessory")
public class FileApprovalController extends BaseController
{
    @Resource
    private IFileApprovalService fileApprovalService;

    /**
     * 查询文件审批业务列表
     */
    @PreAuthorize("@ss.hasPermi('flow:accessory:list')")
    @GetMapping("/list")
    public TableDataInfo list(FileApproval fileApproval)
    {
        startPage();
        List<FileApproval> list = fileApprovalService.selectFileApprovalList(fileApproval);
        return getDataTable(list);
    }

    /**
     * 导出文件审批业务列表
     */
    @PreAuthorize("@ss.hasPermi('flow:accessory:export')")
    @Log(title = "文件审批业务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FileApproval fileApproval)
    {
        List<FileApproval> list = fileApprovalService.selectFileApprovalList(fileApproval);
        ExcelUtil<FileApproval> util = new ExcelUtil<FileApproval>(FileApproval.class);
        return util.exportExcel(list, "文件审批业务数据");
    }

    /**
     * 获取文件审批业务详细信息
     */
    @PreAuthorize("@ss.hasPermi('flow:accessory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fileApprovalService.getById(id));
    }

    /**
     * 新增文件审批业务
     */
    @PreAuthorize("@ss.hasPermi('flow:accessory:add')")
    @Log(title = "文件审批业务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FileApproval fileApproval)
    {
        fileApproval.setApplyUserId(getUsername());
        fileApproval.setApplyUserName(getLoginUser().getUser().getNickName());
        fileApproval.setCreateBy(SecurityUtils.getUsername());
        fileApproval.setCreateTime(DateUtils.getNowDate());
        return toAjax(fileApprovalService.save(fileApproval));
    }

    /**
     * 修改文件审批业务
     */
    @PreAuthorize("@ss.hasPermi('flow:accessory:edit')")
    @Log(title = "文件审批业务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FileApproval fileApproval)
    {
        fileApproval.setUpdateBy(SecurityUtils.getUsername());
        fileApproval.setUpdateTime(DateUtils.getNowDate());
        return toAjax(fileApprovalService.updateById(fileApproval));
    }

    /**
     * 删除文件审批业务
     */
    @PreAuthorize("@ss.hasPermi('flow:accessory:remove')")
    @Log(title = "文件审批业务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fileApprovalService.removeByIds(Arrays.asList(ids)));
    }
}
