package com.yida.data.system.controller;

import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.system.Log;
import com.yida.data.common.core.utils.ExcelUtil;
import com.yida.data.system.dto.ListLogDTO;
import com.yida.data.system.service.LogService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@Api("日志")
@Slf4j
@RequestMapping(value = "/log")
@RestController
@RequiredArgsConstructor
public class LogController {

    private final LogService logService;

    @GetMapping(value = "/listLog")
    public ResultBean<Page<Log>> findLogPageList(ListLogDTO dto) {
        try {
            return ResultBean.buildSuccess(logService.findLogPageList(dto));
        } catch (Exception e) {
            log.error("查询日志列表数据失败: {}", e.getMessage(), e);
            return ResultBean.buildError("查询失败！");
        }
    }

    @PostMapping("/export")
    public void exportLog(ListLogDTO dto, HttpServletResponse response) {
        Page<Log> list = logService.findLogPageList(dto);
        ExcelUtil.export("日志", Log.class, list.getContent(), response);
    }
}
