package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.QuoteLog;
import com.ruoyi.project.system.service.IQuoteLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 查询日志Controller
 *
 * @author zhu
 * @date 2023-11-07
 */
@RestController
@RequestMapping("/system/log")
public class QuoteLogController extends BaseController {
    @Autowired
    private IQuoteLogService quoteLogService;

    /**
     * 查询查询日志列表
     */
    @PreAuthorize("@ss.hasPermi('system:log:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuoteLog quoteLog) {
        startPage();
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            quoteLog.setUserId(SecurityUtils.getUserId());
        }
        List<QuoteLog> list = quoteLogService.selectQuoteLogList(quoteLog);
        return getDataTable(list);
    }

    /**
     * 导出查询日志列表
     */
    @PreAuthorize("@ss.hasPermi('system:log:export')")
    @Log(title = "查询日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuoteLog quoteLog) {
        List<QuoteLog> list = quoteLogService.selectQuoteLogList(quoteLog);
        ExcelUtil<QuoteLog> util = new ExcelUtil<QuoteLog>(QuoteLog.class);
        util.exportExcel(response, list, "查询日志数据");
    }

    /**
     * 获取查询日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:log:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(quoteLogService.selectQuoteLogById(id));
    }

    /**
     * 新增查询日志
     */
    @PreAuthorize("@ss.hasPermi('system:log:add')")
    @Log(title = "查询日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuoteLog quoteLog) {
        quoteLog.setCreateBy(SecurityUtils.getUsername());
        quoteLog.setUpdateBy(SecurityUtils.getUsername());
        quoteLog.setIp(IpUtils.getIpAddr());
        quoteLog.setOs(ServletUtils.getRequest().getHeader("User-Agent"));
        return toAjax(quoteLogService.insertQuoteLog(quoteLog));
    }

    /**
     * 修改查询日志
     */
    @PreAuthorize("@ss.hasPermi('system:log:edit')")
    @Log(title = "查询日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuoteLog quoteLog) {
        quoteLog.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(quoteLogService.updateQuoteLog(quoteLog));
    }

    /**
     * 删除查询日志
     */
    @PreAuthorize("@ss.hasPermi('system:log:remove')")
    @Log(title = "查询日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(quoteLogService.deleteQuoteLogByIds(ids));
    }
}
