package com.yongqi.xinrenli.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.yongqi.xinrenli.domain.vo.DbFileVo;
import com.yongqi.xinrenli.domain.bo.DbFileBo;
import com.yongqi.xinrenli.service.IDbFileService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件
 *
 * @author zyf
 * @date 2023-03-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xinrenli/file")
public class DbFileController extends BaseController {

    private final IDbFileService iDbFileService;

    /**
     * 查询文件列表
     */
    @SaCheckPermission("xinrenli:file:list")
    @GetMapping("/list")
    public TableDataInfo<DbFileVo> list(DbFileBo bo, PageQuery pageQuery) {
        return iDbFileService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文件列表
     */
    @SaCheckPermission("xinrenli:file:export")
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DbFileBo bo, HttpServletResponse response) {
        List<DbFileVo> list = iDbFileService.queryList(bo);
        ExcelUtil.exportExcel(list, "文件", DbFileVo.class, response);
    }

    /**
     * 获取文件详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xinrenli:file:query")
    @GetMapping("/{id}")
    public R<DbFileVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long id) {
        return R.ok(iDbFileService.queryById(id));
    }

    /**
     * 新增文件
     */
    @SaCheckPermission("xinrenli:file:add")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DbFileBo bo) {
        return toAjax(iDbFileService.insertByBo(bo));
    }

    /**
     * 修改文件
     */
    @SaCheckPermission("xinrenli:file:edit")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DbFileBo bo) {
        return toAjax(iDbFileService.updateByBo(bo));
    }

    /**
     * 删除文件
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xinrenli:file:remove")
    @Log(title = "文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iDbFileService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
