package com.usky.system.controller.web;


import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.system.controller.web.page.TableDataInfo;
import com.usky.system.domain.SysOperLog;
import com.usky.system.service.ISysOperLogService;
import com.usky.system.service.vo.SysOperLogExportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 操作日志记录
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/operlog")
public class SysOperlogController extends BaseController
{
    @Autowired
    private ISysOperLogService operLogService;

    @GetMapping("/list")
    public ApiResult<TableDataInfo> list(SysOperLog operLog)
    {
        startPage();
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        return ApiResult.success(getDataTable(list));
    }

    /**
     * 操作日志-导出
     *
     * @param response
     * @param operLog
     * @throws IOException
     */
    @Log(title = "操作日志-导出", businessType = BusinessType.EXPORT)
    @PostMapping("listExport")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "exportTitle") String exportTitle,
                       SysOperLog operLog)throws IOException{
        List<SysOperLogExportVO> list = operLogService.selectOperLogListExport(operLog);
        ExcelUtil<SysOperLogExportVO> util = new ExcelUtil<SysOperLogExportVO>(SysOperLogExportVO.class);
        util.exportExcel(response, list, exportTitle, exportTitle);
    }


    @DeleteMapping("/{operIds}")
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    public ApiResult<Void> remove(@PathVariable Long[] operIds)
    {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }


    @DeleteMapping("/clean")
    @Log(title = "操作日志-清空", businessType = BusinessType.CLEAN)
    public ApiResult<Void> clean()
    {
        operLogService.cleanOperLog();
        return ApiResult.success();
    }

    @PostMapping
    public ApiResult<Void> add(@RequestBody SysOperLog operLog)
    {
        operLogService.insertOperlog(operLog);
        return ApiResult.success();
    }
}
