package com.njcn.csdevice.controller.scheme;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
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.common.utils.LogUtil;
import com.njcn.csdevice.param.WlRecordPageParam;
import com.njcn.csdevice.pojo.param.WlRecordParam;
import com.njcn.csdevice.pojo.po.WlRecord;
import com.njcn.csdevice.pojo.vo.RecordTreeVo;
import com.njcn.csdevice.pojo.vo.RecordVo;
import com.njcn.csdevice.service.IWlRecordService;
import com.njcn.csharmonic.param.CommonStatisticalQueryParam;
import com.njcn.csharmonic.pojo.vo.ThdDataVO;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 方案、测试项表 前端控制器
 * </p>
 *
 * @author xuyang
 * @since 2024-04-01
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/wlRecord")
@Api(tags = "方案测试项管理")
@AllArgsConstructor
public class WlRecordController extends BaseController {

    private final IWlRecordService wlRecordService;

    /**
     * 两层树方案+测试项
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.QUERY)
    @GetMapping("/schemeTree")
    @ApiOperation("两层树方案+测试项")
    public HttpResult<List<RecordTreeVo>> getSchemeTree() {
        String methodDescribe = getMethodDescribe("getSchemeTree");
        LogUtil.njcnDebug(log, "{}", methodDescribe);
        List<RecordTreeVo> result = wlRecordService.getSchemeTree();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据测试项ID查询测试项信息
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.QUERY)
    @GetMapping("/getTestRecordById")
    @ApiOperation("根据测试项ID查询测试项信息")
    @ApiImplicitParam(name = "testRecordId", value = "测试项ID", required = true)
    public HttpResult<RecordVo.RecordItemVo> getTestRecordById(@RequestParam("testRecordId") @Validated String testRecordId) {
        String methodDescribe = getMethodDescribe("getTestRecordById");
        LogUtil.njcnDebug(log, "{}，测试项id为：{}", methodDescribe, testRecordId);
        RecordVo.RecordItemVo result = wlRecordService.getTestRecordById(testRecordId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 新增测试项
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addRecord")
    @ApiOperation("新增测试项")
    @ApiImplicitParam(name = "records", value = "测试项集合", required = true)
    public HttpResult<Boolean> addRecord(@RequestBody @Validated WlRecordParam.AddRecord records) {
        String methodDescribe = getMethodDescribe("addRecord");
        LogUtil.njcnDebug(log, "{}，新增的测试项集合为：{}", methodDescribe, records);
        wlRecordService.addRecord(records);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 新增原始数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addBaseData")
    @ApiOperation("新增原始数据")
    @ApiImplicitParam(name = "wlRecord", value = "装置原始数据", required = true)
    public HttpResult<Boolean> addBaseData(@RequestBody @Validated WlRecord wlRecord) {
        String methodDescribe = getMethodDescribe("addBaseData");
        LogUtil.njcnDebug(log, "{}，装置原始数据为：{}", methodDescribe, wlRecord);
        wlRecordService.addBaseData(wlRecord);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 批量新增原始数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addBaseDataList")
    @ApiOperation("批量新增原始数据")
    @ApiImplicitParam(name = "wlRecord", value = "装置原始数据", required = true)
    public HttpResult<Boolean> addBaseDataList(@RequestBody @Validated List<WlRecord> wlRecord) {
        String methodDescribe = getMethodDescribe("addBaseDataList");
        LogUtil.njcnDebug(log, "{}，装置原始数据为：{}", methodDescribe, wlRecord);
        wlRecordService.addBaseDataList(wlRecord);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 修改测试项
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/updateTestRecord")
    @ApiOperation("修改测试项")
    @ApiImplicitParam(name = "record", value = "测试项对象", required = true)
    public HttpResult<Boolean> updateTestRecord(@RequestBody @Validated WlRecordParam.UpdateRecord record) {
        String methodDescribe = getMethodDescribe("updateTestRecord");
        LogUtil.njcnDebug(log, "{}，修改的测试项对象为：{}", methodDescribe, record);
        wlRecordService.updateTestRecord(record);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 修改测试项日志
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/updateRecordData")
    @ApiOperation("修改测试项日志")
    @ApiImplicitParam(name = "record", value = "测试项日志信息", required = true)
    public HttpResult<Boolean> updateRecordData(@RequestBody @Validated WlRecordParam.UpdateRecord record) {
        String methodDescribe = getMethodDescribe("updateRecordData");
        LogUtil.njcnDebug(log, "{}，修改的测试项日志信息：{}", methodDescribe, record);
        wlRecordService.updateRecordData(record);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 新增或修改方案
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/updateSchemeRecord")
    @ApiOperation("新增或修改方案")
    @ApiImplicitParam(name = "record", value = "方案对象", required = true)
    public HttpResult<Boolean> updateSchemeRecord(@RequestBody @Validated WlRecordParam.UpdateRecord record) {
        String methodDescribe = getMethodDescribe("updateSchemeRecord");
        LogUtil.njcnDebug(log, "{}，新增或修改方案对象为：{}", methodDescribe, record);
        wlRecordService.updateSchemeRecord(record);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 设备列表树
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.QUERY)
    @PostMapping("/queryPage")
    @ApiOperation("设备列表树")
    public HttpResult<Object> queryPage(@Validated @RequestBody WlRecordPageParam wlRecordPageParam) {
        String methodDescribe = getMethodDescribe("queryPage");
        LogUtil.njcnDebug(log, "{}", methodDescribe);
        Object result = wlRecordService.queryPage(wlRecordPageParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 删除测试项或方案
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @DeleteMapping("/deleteRecord")
    @ApiOperation("删除测试项或方案")
    @ApiImplicitParam(name = "id", value = "方案或测试项ID", required = true)
    public HttpResult<Boolean> deleteRecord(@RequestParam @Validated String id) {
        String methodDescribe = getMethodDescribe("deleteRecord");
        LogUtil.njcnDebug(log, "{}，方案或测试项ID为：{}", methodDescribe, id);
        wlRecordService.deleteRecord(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 删除装置测试项
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/deleteItem")
    @ApiOperation("删除装置测试项")
    @ApiImplicitParam(name = "id", value = "装置测试项Id", required = true)
    public HttpResult<Boolean> deleteItem(@RequestParam @Validated String id) {
        String methodDescribe = getMethodDescribe("deleteItem");
        LogUtil.njcnDebug(log, "{}，装置测试项Id为：{}", methodDescribe, id);
        wlRecordService.deleteItem(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 查询测试项时间段实时数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.QUERY)
    @PostMapping("/queryCommonStatisticalByTest")
    @ApiOperation("查询测试项时间段实时数据")
    public HttpResult<List<ThdDataVO>> queryCommonStatisticalByTest(@RequestBody @Validated CommonStatisticalQueryParam commonStatisticalQueryParam) {
        String methodDescribe = getMethodDescribe("queryCommonStatisticalByTest");
        LogUtil.njcnDebug(log, "{}，查询对象为：{}", methodDescribe, commonStatisticalQueryParam);
        List<ThdDataVO> result = wlRecordService.queryCommonStatisticalByTest(commonStatisticalQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 查询装置基础数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.QUERY)
    @PostMapping("/findDevBaseData")
    @ApiOperation("查询装置基础数据")
    @ApiImplicitParam(name = "param", value = "查询条件", required = true)
    public HttpResult<WlRecord> findDevBaseData(@RequestBody @Validated WlRecordParam.Record param) {
        String methodDescribe = getMethodDescribe("findDevBaseData");
        LogUtil.njcnDebug(log, "{}，查询对象为：{}", methodDescribe, param);
        WlRecord record = wlRecordService.findDevBaseData(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, record, methodDescribe);
    }

    /**
     * 根据监测点id查询装置基础数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.QUERY)
    @PostMapping("/findDevBaseDataByLineId")
    @ApiOperation("根据监测点id查询装置基础数据")
    @ApiImplicitParam(name = "param", value = "查询条件", required = true)
    public HttpResult<List<RecordVo>> findDevBaseDataByLineId(@RequestBody @Validated WlRecordParam.lineRecord param) {
        String methodDescribe = getMethodDescribe("findDevBaseDataByLineId");
        LogUtil.njcnDebug(log, "{}，查询对象为：{}", methodDescribe, param);
        List<RecordVo> record = wlRecordService.findDevBaseDataByLineId(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, record, methodDescribe);
    }


    /**
     * 每日处理没有结束时间的测试基础数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/dayDealNoEndTimeEvent")
    @ApiOperation("每日处理没有结束时间的测试基础数据")
    public void dayDealNoEndTimeEvent(@RequestParam("date") String date) {
        String methodDescribe = getMethodDescribe("dayDealNoEndTimeEvent");
        wlRecordService.dayDealNoEndTimeEvent(date);
    }

    /**
     * 根据测试项获取测试项绑定测点数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getWlAssByWlId")
    @ApiOperation("根据测试项获取测试项绑定测点数据")
    public HttpResult<List<WlRecord>> getWlAssByWlId(@RequestParam("wlId") String wlId) {
        String methodDescribe = getMethodDescribe("getWlAssByWlId");
        List<WlRecord> result = wlRecordService.getWlAssByWlId(wlId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}

