package com.starry.admin.modules.system.controller;


import com.starry.admin.modules.system.entity.SysOperationLogEntity;
import com.starry.admin.modules.system.service.ISysOperationLogService;
import com.starry.admin.modules.system.vo.SysOperationLogVo;
import com.starry.admin.utils.ExcelUtils;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 * 操作日志表Controller
 *
 * @author admin
 */
@RestController
@RequestMapping("/system/operlog")
public class SysOperationLogController {

    @Resource
    private ISysOperationLogService xlOperLogService;

    /**
     * 查询操作日志表列表
     */
    @PreAuthorize("@customSs.hasPermission('system:operlog:list')")
    @GetMapping("/list")
    public R list(SysOperationLogVo sysOperationLogEntity) {
        return R.ok(xlOperLogService.selectXlOperLogList(sysOperationLogEntity));
    }

    /**
     * 获取操作日志表详细信息
     */
    @PreAuthorize("@customSs.hasPermission('system:operlog:query')")
    @GetMapping(value = "/{operId}")
    public R getInfo(@PathVariable("operId") Long operId) {
        return R.ok(xlOperLogService.selectXlOperLogByOperId(operId));
    }

    /**
     * 新增操作日志表
     */
    @PreAuthorize("@customSs.hasPermission('system:operlog:create')")
    @Log(title = "操作日志表", businessType = BusinessType.INSERT)
    @PostMapping
    public R create(@RequestBody SysOperationLogEntity sysOperationLogEntity) {
        boolean success = xlOperLogService.create(sysOperationLogEntity);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改操作日志表
     */
    @PreAuthorize("@customSs.hasPermission('system:operlog:edit')")
    @Log(title = "操作日志表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{operId}")
    public R update(@PathVariable Long operId, @RequestBody SysOperationLogEntity sysOperationLogEntity) {
        sysOperationLogEntity.setOperId(operId);
        boolean success = xlOperLogService.updateXlOperLog(sysOperationLogEntity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除操作日志表
     */
    @PreAuthorize("@customSs.hasPermission('system:operlog:remove')")
    @Log(title = "操作日志表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{operIds}")
    public R remove(@PathVariable Long[] operIds) {
        return R.ok(xlOperLogService.deleteXlOperLogByOperIds(operIds));
    }

    /**
     * 导出操作日志表
     */
    @PreAuthorize("@customSs.hasPermission('system:operlog:export')")
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperationLogVo sysOperationLogEntity) {
        ExcelUtils.exportEasyExcel(response, SysOperationLogEntity.class, xlOperLogService.selectXlOperLogList(sysOperationLogEntity).getRecords(), "系统操作日志表数据");
    }
}
