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.CsHarmonic;
import com.njcn.csharmonic.pojo.vo.HarmonicDetailVO;
import com.njcn.csharmonic.pojo.vo.HarmonicVO;
import com.njcn.csharmonic.service.ICsHarmonicService;
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-13
 */
@Slf4j
@RestController
@RequestMapping("/csHarmonic")
@Api(tags = "稳态事件")
@AllArgsConstructor
public class CsHarmonicController extends BaseController {

    private final ICsHarmonicService csHarmonicService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryHarmonicList")
    @ApiOperation("稳态事件列表(app)")
    @ApiImplicitParam(name = "param", value = "事件查询参数", required = true)
    public HttpResult<Page<HarmonicVO.LineHarmonicDetail>> queryHarmonicList(@RequestBody LineParamDTO param) {
        String methodDescribe = getMethodDescribe("queryHarmonicList");
        Page<HarmonicVO.LineHarmonicDetail> vo = csHarmonicService.queryAppList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, vo, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryHarmonicDetail")
    @ApiOperation("稳态事件详情(app)")
    @ApiImplicitParam(name = "param", value = "事件查询参数", required = true)
    public HttpResult<List<HarmonicDetailVO>> queryHarmonicDetail(@RequestBody LineParamDTO param) {
        String methodDescribe = getMethodDescribe("queryHarmonicDetail");
        List<HarmonicDetailVO> vo = csHarmonicService.queryHarmonicDetail(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, vo, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addList")
    @ApiOperation("批量写入数据")
    @ApiImplicitParam(name = "list", value = "事件查询参数", required = true)
    public HttpResult<Boolean> addList(@RequestBody List<CsHarmonic> list) {
        String methodDescribe = getMethodDescribe("addList");
        boolean result = csHarmonicService.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<CsHarmonic>> queryListByTime(@RequestParam String time) {
        String methodDescribe = getMethodDescribe("queryListByTime");
        LambdaQueryWrapper<CsHarmonic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CsHarmonic::getTime, time);
        List<CsHarmonic> list = csHarmonicService.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<CsHarmonic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CsHarmonic::getTime, time);
        boolean result = csHarmonicService.remove(wrapper);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryAppHarmonicCounts")
    @ApiOperation("查询App稳态事件总数")
    @ApiImplicitParam(name = "param", value = "事件查询参数", required = true)
    public HttpResult<HarmonicVO> queryAppHarmonicCounts(@RequestBody LineParamDTO param) {
        String methodDescribe = getMethodDescribe("queryAppHarmonicCounts");
        HarmonicVO vo = csHarmonicService.queryAppHarmonicCounts(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, vo, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryAppHarmonicLine")
    @ApiOperation("查询App稳态越限监测点")
    @ApiImplicitParam(name = "param", value = "事件查询参数", required = true)
    public HttpResult<Page<HarmonicVO.LineHarmonicOverDays>> queryAppHarmonicLine(@RequestBody LineParamDTO param) {
        String methodDescribe = getMethodDescribe("queryAppHarmonicLine");
        Page<HarmonicVO.LineHarmonicOverDays> result = csHarmonicService.queryAppHarmonicLine(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

}

