package com.yushu.web.controller.monitor;

import com.yushu.common.annotation.Log;
import com.yushu.common.core.controller.BaseController;
import com.yushu.common.core.domain.AjaxResult;
import com.yushu.common.core.page.TableDataInfo;
import com.yushu.common.enums.BusinessType;
import com.yushu.system.domain.SysErrorLog;
import com.yushu.system.service.ISysErrorLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 错误日志Controller
 * 
 * @author yushu
 */
@RestController
@RequestMapping("/monitor/errorLog")
public class SysErrorLogController extends BaseController {
    
    @Autowired
    private ISysErrorLogService errorLogService;

    /**
     * 查询错误日志列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:errorLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysErrorLog errorLog) {
        startPage();
        List<SysErrorLog> list = errorLogService.selectErrorLogList(errorLog);
        return getDataTable(list);
    }

    /**
     * 获取错误日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:errorLog:query')")
    @GetMapping("/{errorId}")
    public AjaxResult getInfo(@PathVariable Long errorId) {
        return success(errorLogService.selectErrorLogById(errorId));
    }

    /**
     * 处理错误（标记为已处理/忽略）
     */
    @PreAuthorize("@ss.hasPermi('monitor:errorLog:resolve')")
    @Log(title = "错误日志", businessType = BusinessType.UPDATE)
    @PutMapping("/resolve")
    public AjaxResult resolve(@RequestBody ResolveRequest request) {
        return toAjax(errorLogService.resolveError(request.getErrorId(), request.getStatus(), request.getRemark()));
    }

    /**
     * 批量处理错误
     */
    @PreAuthorize("@ss.hasPermi('monitor:errorLog:resolve')")
    @Log(title = "错误日志", businessType = BusinessType.UPDATE)
    @PutMapping("/batchResolve")
    public AjaxResult batchResolve(@RequestBody BatchResolveRequest request) {
        return toAjax(errorLogService.batchResolve(request.getErrorIds(), request.getStatus(), request.getRemark()));
    }

    /**
     * 删除错误日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:errorLog:remove')")
    @Log(title = "错误日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{errorIds}")
    public AjaxResult remove(@PathVariable Long[] errorIds) {
        return toAjax(errorLogService.deleteErrorLogByIds(errorIds));
    }

    /**
     * 清空错误日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:errorLog:clean')")
    @Log(title = "错误日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        errorLogService.cleanErrorLog();
        return success();
    }

    /**
     * 获取未处理错误数量
     */
    @GetMapping("/unresolved/count")
    public AjaxResult countUnresolved() {
        return success(errorLogService.countUnresolved());
    }

    // 请求体类
    public static class ResolveRequest {
        private Long errorId;
        private String status;
        private String remark;

        public Long getErrorId() { return errorId; }
        public void setErrorId(Long errorId) { this.errorId = errorId; }
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
        public String getRemark() { return remark; }
        public void setRemark(String remark) { this.remark = remark; }
    }

    public static class BatchResolveRequest {
        private Long[] errorIds;
        private String status;
        private String remark;

        public Long[] getErrorIds() { return errorIds; }
        public void setErrorIds(Long[] errorIds) { this.errorIds = errorIds; }
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
        public String getRemark() { return remark; }
        public void setRemark(String remark) { this.remark = remark; }
    }
}
