package com.sooka.system.controller;

import java.util.List;

import com.sooka.common.annotation.Log;
import com.sooka.common.core.controller.BaseController;
import com.sooka.common.core.domain.AjaxResult;
import com.sooka.common.core.page.TableDataInfo;
import com.sooka.common.enums.BusinessType;
import com.sooka.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sooka.system.domain.TULog;
import com.sooka.system.service.ITULogService;


/**
 * 接口调用日志Controller
 *
 * @author lei
 * @date 2021-07-06
 */
@Controller
@RequestMapping("/system/log")
public class TULogController extends BaseController
{
    private String prefix = "system/log";

    @Autowired
    private ITULogService tULogService;

    @RequiresPermissions("system:log:view")
    @GetMapping()
    public String log()
    {
        return prefix + "/log";
    }

    /**
     * 查询接口调用日志列表
     */
    @RequiresPermissions("system:log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TULog tULog)
    {
        startPage();
        List<TULog> list = tULogService.selectTULogList(tULog);
        return getDataTable(list);
    }

    /**
     * 导出接口调用日志列表
     */
    @RequiresPermissions("system:log:export")
    @Log(title = "接口调用日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TULog tULog)
    {
        List<TULog> list = tULogService.selectTULogList(tULog);
        ExcelUtil<TULog> util = new ExcelUtil<TULog>(TULog.class);
        return util.exportExcel(list, "log");
    }

    /**
     * 新增接口调用日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存接口调用日志
     */
    @RequiresPermissions("system:log:add")
    @Log(title = "接口调用日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TULog tULog)
    {
        return toAjax(tULogService.insertTULog(tULog));
    }

    /**
     * 修改接口调用日志
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        TULog tULog = tULogService.selectTULogById(id);
        mmap.put("tULog", tULog);
        return prefix + "/edit";
    }

    /**
     * 修改保存接口调用日志
     */
    @RequiresPermissions("system:log:edit")
    @Log(title = "接口调用日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TULog tULog)
    {
        return toAjax(tULogService.updateTULog(tULog));
    }

    /**
     * 删除接口调用日志
     */
    @RequiresPermissions("system:log:remove")
    @Log(title = "接口调用日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tULogService.deleteTULogByIds(ids));
    }
}
