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

import com.starry.admin.modules.system.module.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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 操作日志表Controller
 *
 * @author admin
 */
@Api(tags = "操作日志管理", description = "系统操作日志相关接口")
@RestController
@RequestMapping("/system/operlog")
public class SysOperationLogController {

    @Resource
    private ISysOperationLogService xlOperLogService;

    /**
     * 查询操作日志表列表
     */
    @ApiOperation(value = "查询操作日志列表", notes = "分页查询系统操作日志列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = SysOperationLogEntity.class, responseContainer = "Page")})
    @PreAuthorize("@customSs.hasPermission('system:operlog:list')")
    @GetMapping("/list")
    public R list(@ApiParam(value = "查询条件") SysOperationLogVo sysOperationLogEntity) {
        return R.ok(xlOperLogService.selectXlOperLogList(sysOperationLogEntity));
    }

    /**
     * 获取操作日志表详细信息
     */
    @ApiOperation(value = "获取操作日志详情", notes = "根据ID获取操作日志详细信息")
    @ApiImplicitParam(name = "operId", value = "日志ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = SysOperationLogEntity.class)})
    @PreAuthorize("@customSs.hasPermission('system:operlog:query')")
    @GetMapping(value = "/{operId}")
    public R getInfo(@PathVariable("operId") Long operId) {
        return R.ok(xlOperLogService.selectXlOperLogByOperId(operId));
    }

    /**
     * 新增操作日志表
     */
    @ApiOperation(value = "新增操作日志", notes = "手动创建系统操作日志记录")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "添加失败")})
    @PreAuthorize("@customSs.hasPermission('system:operlog:create')")
    @Log(title = "操作日志表", businessType = BusinessType.INSERT)
    @PostMapping
    public R create(
            @ApiParam(value = "操作日志信息", required = true) @RequestBody SysOperationLogEntity sysOperationLogEntity) {
        boolean success = xlOperLogService.create(sysOperationLogEntity);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改操作日志表
     */
    @ApiOperation(value = "修改操作日志", notes = "根据ID修改操作日志信息")
    @ApiImplicitParam(name = "operId", value = "日志ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "修改失败")})
    @PreAuthorize("@customSs.hasPermission('system:operlog:edit')")
    @Log(title = "操作日志表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{operId}")
    public R update(@PathVariable Long operId,
            @ApiParam(value = "操作日志信息", required = true) @RequestBody SysOperationLogEntity sysOperationLogEntity) {
        sysOperationLogEntity.setOperId(operId);
        boolean success = xlOperLogService.updateXlOperLog(sysOperationLogEntity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除操作日志表
     */
    @ApiOperation(value = "删除操作日志", notes = "根据ID批量删除操作日志")
    @ApiImplicitParam(name = "operIds", value = "日志ID数组", required = true, dataType = "Long[]", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = Boolean.class)})
    @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));
    }

    /**
     * 导出操作日志表
     */
    @ApiOperation(value = "导出操作日志", notes = "导出系统操作日志表数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PreAuthorize("@customSs.hasPermission('system:operlog:export')")
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,
            @ApiParam(value = "查询条件") SysOperationLogVo sysOperationLogEntity) {
        ExcelUtils.exportEasyExcel(response, SysOperationLogEntity.class,
                xlOperLogService.selectXlOperLogList(sysOperationLogEntity).getRecords(), "系统操作日志表数据");
    }
}
