package com.agileboot.system.log.controller;


import com.agileboot.common.core.core.R;
import com.agileboot.common.core.utils.poi.CustomExcelUtil;
import com.agileboot.common.mybatis.core.page.PageR;
import com.agileboot.system.log.dto.LoginLogQuery;
import com.agileboot.system.log.dto.OperationLogQuery;
import com.agileboot.system.log.service.ILogService;
import com.agileboot.system.log.vo.LoginLogVO;
import com.agileboot.system.log.vo.OperationLogVO;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统访问记录
 */
@RestController
@RequestMapping("/logs")
@Validated
@RequiredArgsConstructor
public class SysLogsController {

    private final ILogService logService;

    /**
     * 登录日志列表
     */
    @GetMapping("/loginLogs")
    public R<PageR<LoginLogVO>> loginInfoList(LoginLogQuery query) {
        PageR<LoginLogVO> page = logService.getLoginInfoList(query);
        return R.ok(page);
    }

    /**
     * 将登录日志导出到excel
     */
    @GetMapping("/loginLogs/excel")
    public void loginInfosExcel(HttpServletResponse response, LoginLogQuery query) {
        PageR<LoginLogVO> page = logService.getLoginInfoList(query);
        CustomExcelUtil.writeToResponse(page.getRows(), LoginLogVO.class, response);
    }

    /**
     * 删除登录日志
     */
    @DeleteMapping("/loginLogs")
    public R<Void> removeLoginInfos(@RequestParam @NotNull @NotEmpty List<Long> ids) {
        logService.deleteLoginInfo(ids);
        return R.ok();
    }

    /**
     * 操作日志列表
     */
    @GetMapping("/operationLogs")
    public R<PageR<OperationLogVO>> operationLogs(OperationLogQuery query) {
        PageR<OperationLogVO> page = logService.getOperationLogList(query);
        return R.ok(page);
    }

    /**
     * 操作日志导出
     */
    @GetMapping("/operationLogs/excel")
    public void operationLogsExcel(HttpServletResponse response, OperationLogQuery query) {
        PageR<OperationLogVO> page = logService.getOperationLogList(query);
        CustomExcelUtil.writeToResponse(page.getRows(), OperationLogVO.class, response);
    }

    /**
     * 删除操作日志
     */
    @DeleteMapping("/operationLogs")
    public R<Void> removeOperationLogs(@RequestParam List<Long> operationIds) {
        logService.deleteOperationLog(operationIds);
        return R.ok();
    }
}
