package com.fs.company.controller.company;

import com.fs.common.annotation.DataScope;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyOperLog;
import com.fs.company.service.ICompanyOperLogService;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作日志记录
 *

 */
@RestController
@RequestMapping("/monitor/operlog")
public class CompanyOperlogController extends BaseController
{
    @Autowired
    private ICompanyOperLogService operLogService;
    @Autowired
    private TokenService tokenService;


//    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping("/list")
    @DataScope
    public TableDataInfo list(CompanyOperLog operLog)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        operLog.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyOperLog> list = operLogService.selectCompanyOperLogList(operLog);
        return getDataTable(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @GetMapping("/export")
    public AjaxResult export(CompanyOperLog operLog)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        operLog.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyOperLog> list = operLogService.selectCompanyOperLogList(operLog);
        ExcelUtil<CompanyOperLog> util = new ExcelUtil<CompanyOperLog>(CompanyOperLog.class);
        return util.exportExcel(list, "操作日志");
    }

    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds)
    {
        return toAjax(operLogService.deleteCompanyOperLogByIds(operIds));
    }

    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        operLogService.cleanOperLog(loginUser.getCompany().getCompanyId());
        return AjaxResult.success();
    }
}
