package com.njcn.csharmonic.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.csdevice.pojo.dto.LineParamDTO;
import com.njcn.csharmonic.pojo.po.CsAlarm;
import com.njcn.csharmonic.pojo.vo.AlarmVO;
import com.njcn.csharmonic.service.ICsAlarmService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xy
 * @since 2026-03-16
 */
@Slf4j
@RestController
@RequestMapping("/csAlarm")
@Api(tags = "运行告警事件")
@AllArgsConstructor
public class CsAlarmController extends BaseController {

    private final ICsAlarmService csAlarmService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryAlarmList")
    @ApiOperation("运行告警事件列表(app)")
    @ApiImplicitParam(name = "param", value = "事件查询参数", required = true)
    public HttpResult<Page<AlarmVO>> queryAlarmList(@RequestBody LineParamDTO param) {
        String methodDescribe = getMethodDescribe("queryAlarmList");
        Page<AlarmVO> result = csAlarmService.queryAlarmList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryAlarmDetail")
    @ApiOperation("运行告警事件详情(app)")
    @ApiImplicitParam(name = "param", value = "事件查询参数", required = true)
    public HttpResult<List<AlarmVO.AlarmDetail>> queryAlarmDetail(@RequestBody LineParamDTO.DevParamDTO param) {
        String methodDescribe = getMethodDescribe("queryAlarmDetail");
        List<AlarmVO.AlarmDetail> result = csAlarmService.queryAlarmDetail(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addList")
    @ApiOperation("批量写入数据")
    @ApiImplicitParam(name = "list", value = "参数", required = true)
    public HttpResult<Boolean> addList(@RequestBody List<CsAlarm> list) {
        String methodDescribe = getMethodDescribe("addList");
        boolean result = csAlarmService.saveOrUpdateBatch(list);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryListByTime")
    @ApiOperation("按日期查询数据")
    @ApiImplicitParam(name = "time", value = "时间", required = true)
    public HttpResult<List<CsAlarm>> queryListByTime(@RequestParam String time) {
        String methodDescribe = getMethodDescribe("queryListByTime");
        LambdaQueryWrapper<CsAlarm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CsAlarm::getTime, time);
        List<CsAlarm> list = csAlarmService.list(wrapper);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/deleteListByTime")
    @ApiOperation("按日期删除数据")
    @ApiImplicitParam(name = "time", value = "时间", required = true)
    public HttpResult<Boolean> deleteListByTime(@RequestParam String time) {
        String methodDescribe = getMethodDescribe("deleteListByTime");
        LambdaQueryWrapper<CsAlarm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CsAlarm::getTime, time);
        boolean result = csAlarmService.remove(wrapper);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

}

