package com.schoolinout.web.controller.system;

import com.schoolinout.common.annotation.Log;
import com.schoolinout.common.core.controller.BaseController;
import com.schoolinout.common.core.domain.AjaxResult;
import com.schoolinout.common.core.page.TableDataInfo;
import com.schoolinout.common.enums.BusinessType;
import com.schoolinout.common.utils.poi.ExcelUtil;
import com.schoolinout.system.domain.AccessLog;
import com.schoolinout.system.domain.dto.InOutLogDto;
import com.schoolinout.system.service.IAccessLogService;
import io.swagger.v3.oas.annotations.tags.Tag;
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 sakura
 * @date 2024-01-03
 */
@Tag(name = "学生进出记录接口")
@RestController
@RequestMapping("/system/access_log")
public class AccessLogController extends BaseController {
    @Autowired
    private IAccessLogService accessLogService;


    /**
     * 查询学生进出学校记录
     */
    @PostMapping("/student/inout/list")
    public AjaxResult searchStudentInOutLog(@RequestBody InOutLogDto dto) {
        return success(accessLogService.queryStudentInOutLog(dto));
    }

    /**
     * 查询进出学校记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:access_log:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccessLog accessLog) {
        startPage();
        List<AccessLog> list = accessLogService.selectAccessLogList(accessLog);
        return getDataTable(list);
    }

    /**
     * 导出进出学校记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:access_log:export')")
    @Log(title = "进出学校记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccessLog accessLog) {
        List<AccessLog> list = accessLogService.selectAccessLogList(accessLog);
        ExcelUtil<AccessLog> util = new ExcelUtil<AccessLog>(AccessLog.class);
        util.exportExcel(response, list, "进出学校记录数据");
    }

    /**
     * 获取进出学校记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:access_log:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(accessLogService.selectAccessLogById(id));
    }

    /**
     * 新增进出学校记录
     */
    @PreAuthorize("@ss.hasPermi('system:access_log:add')")
    @Log(title = "进出学校记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AccessLog accessLog) {
        return toAjax(accessLogService.insertAccessLog(accessLog));
    }

    /**
     * 修改进出学校记录
     */
    @PreAuthorize("@ss.hasPermi('system:access_log:edit')")
    @Log(title = "进出学校记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccessLog accessLog) {
        return toAjax(accessLogService.updateAccessLog(accessLog));
    }

    /**
     * 删除进出学校记录
     */
    @PreAuthorize("@ss.hasPermi('system:access_log:remove')")
    @Log(title = "进出学校记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(accessLogService.deleteAccessLogByIds(ids));
    }
}
