package com.inspect.system.controller;

import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.InnerAuth;
import com.inspect.common.security.annotation.RequiresPermissions;
import com.inspect.system.base.domain.SysOperateLog;
import com.inspect.system.service.ISysOperateLogService;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/operlog"})
public class SysOperateLogController extends BaseController {
    private final ISysOperateLogService operateLogService;

    public SysOperateLogController(ISysOperateLogService operateLogService) {
        this.operateLogService = operateLogService;
    }

    @RequiresPermissions({"system:operlog:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(SysOperateLog operateLog) {
        this.startPage();
        List<SysOperateLog> list = operateLogService.selectOperateLogList(operateLog);
        return getDataTable(list);
    }

    @RequiresPermissions({"system:operlog:list"})
    @GetMapping({"/auditList"})
    public TableDataInfo auditList(SysOperateLog operateLog) {
        this.startPage();
        List<SysOperateLog> list = operateLogService.selectAuditOperLogList(operateLog);
        return getDataTable(list);
    }

    @Log(
            title = "操作日志",
            businessType = BizType.EXPORT
    )
    @RequiresPermissions({"system:operlog:export"})
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, SysOperateLog operateLog) {
        List<SysOperateLog> list = operateLogService.selectOperateLogList(operateLog);
        ExcelUtil<SysOperateLog> util = new ExcelUtil<>(SysOperateLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @Log(
            title = "操作日志",
            businessType = BizType.DELETE
    )
    @RequiresPermissions({"system:operlog:remove"})
    @PostMapping({"/remove/{operateIds}"})
    public AjaxResult remove(@PathVariable Long[] operateIds) {
        return toAjax(operateLogService.deleteOperateLogByIds(operateIds));
    }

    @RequiresPermissions({"system:operlog:remove"})
    @Log(
            title = "操作日志",
            businessType = BizType.CLEAN
    )
    @PostMapping({"/clean"})
    public AjaxResult clean() {
        operateLogService.cleanOperateLog();
        return AjaxResult.success();
    }

    @InnerAuth
    @PostMapping
    public AjaxResult add(@RequestBody SysOperateLog operateLog) {
        return toAjax(operateLogService.insertOperateLog(operateLog));
    }
}
