package com.njcn.gather.tool.addledger.controller;

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.LogUtil;
import com.njcn.gather.tool.addledger.pojo.param.AddDeviceUnitSaveParam;
import com.njcn.gather.tool.addledger.pojo.param.AddLedgerEngineeringSaveParam;
import com.njcn.gather.tool.addledger.pojo.param.AddLedgerEquipmentSaveParam;
import com.njcn.gather.tool.addledger.pojo.param.AddLedgerLineSaveParam;
import com.njcn.gather.tool.addledger.pojo.param.AddLedgerProjectSaveParam;
import com.njcn.gather.tool.addledger.pojo.po.AddOverlimitPO;
import com.njcn.gather.tool.addledger.pojo.vo.AddDeviceUnitVO;
import com.njcn.gather.tool.addledger.pojo.vo.AddLedgerDetailVO;
import com.njcn.gather.tool.addledger.pojo.vo.AddLedgerTreeNodeVO;
import com.njcn.gather.tool.addledger.service.AddLedgerService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据台账配置接口。
 */
@Validated
@Slf4j
@Api(tags = "数据台账配置")
@RestController
@RequestMapping("/addLedger")
@RequiredArgsConstructor
public class AddLedgerController extends BaseController {

    private final AddLedgerService addLedgerService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询台账树")
    @GetMapping("/tree")
    public HttpResult<List<AddLedgerTreeNodeVO>> tree(@RequestParam(value = "keyword", required = false) String keyword) {
        String methodDescribe = getMethodDescribe("tree");
        LogUtil.njcnDebug(log, "{}，开始查询台账树，keyword={}", methodDescribe, keyword);
        List<AddLedgerTreeNodeVO> result = addLedgerService.tree(keyword);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询台账节点详情")
    @GetMapping("/detail")
    public HttpResult<AddLedgerDetailVO> detail(@RequestParam("id") String id, @RequestParam("level") Integer level) {
        String methodDescribe = getMethodDescribe("detail");
        LogUtil.njcnDebug(log, "{}，开始查询台账节点详情，id={}, level={}", methodDescribe, id, level);
        AddLedgerDetailVO result = addLedgerService.detail(id, level);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @ApiOperation("新增或保存工程")
    @PostMapping("/engineering/save")
    public HttpResult<AddLedgerDetailVO> saveEngineering(@RequestBody @Validated AddLedgerEngineeringSaveParam param) {
        String methodDescribe = getMethodDescribe("saveEngineering");
        LogUtil.njcnDebug(log, "{}，开始保存工程，id={}", methodDescribe, param.getId());
        AddLedgerDetailVO result = addLedgerService.saveEngineering(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @ApiOperation("新增或保存项目")
    @PostMapping("/project/save")
    public HttpResult<AddLedgerDetailVO> saveProject(@RequestBody @Validated AddLedgerProjectSaveParam param) {
        String methodDescribe = getMethodDescribe("saveProject");
        LogUtil.njcnDebug(log, "{}，开始保存项目，id={}", methodDescribe, param.getId());
        AddLedgerDetailVO result = addLedgerService.saveProject(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @ApiOperation("新增或保存设备")
    @PostMapping("/equipment/save")
    public HttpResult<AddLedgerDetailVO> saveEquipment(@RequestBody @Validated AddLedgerEquipmentSaveParam param) {
        String methodDescribe = getMethodDescribe("saveEquipment");
        LogUtil.njcnDebug(log, "{}，开始保存设备，id={}", methodDescribe, param.getId());
        AddLedgerDetailVO result = addLedgerService.saveEquipment(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询设备单位")
    @GetMapping("/equipment/unit")
    public HttpResult<AddDeviceUnitVO> getDeviceUnit(@RequestParam("devId") String devId) {
        String methodDescribe = getMethodDescribe("getDeviceUnit");
        LogUtil.njcnDebug(log, "{}，开始查询设备单位，devId={}", methodDescribe, devId);
        AddDeviceUnitVO result = addLedgerService.getDeviceUnit(devId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("保存设备单位")
    @PostMapping("/equipment/unit/save")
    public HttpResult<AddDeviceUnitVO> saveDeviceUnit(@RequestBody @Validated AddDeviceUnitSaveParam param) {
        String methodDescribe = getMethodDescribe("saveDeviceUnit");
        LogUtil.njcnDebug(log, "{}，开始保存设备单位，devId={}", methodDescribe, param.getDevId());
        AddDeviceUnitVO result = addLedgerService.saveDeviceUnit(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @ApiOperation("新增或保存测点")
    @PostMapping("/line/save")
    public HttpResult<AddLedgerDetailVO> saveLine(@RequestBody @Validated AddLedgerLineSaveParam param) {
        String methodDescribe = getMethodDescribe("saveLine");
        LogUtil.njcnDebug(log, "{}，开始保存测点，lineId={}", methodDescribe, param.getLineId());
        AddLedgerDetailVO result = addLedgerService.saveLine(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询监测点限值")
    @GetMapping("/line/overlimit")
    public HttpResult<AddOverlimitPO> getLineOverlimit(@RequestParam("lineId") String lineId) {
        String methodDescribe = getMethodDescribe("getLineOverlimit");
        LogUtil.njcnDebug(log, "{}，开始查询监测点限值，lineId={}", methodDescribe, lineId);
        AddOverlimitPO result = addLedgerService.getLineOverlimit(lineId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询设备可用线路号")
    @GetMapping("/line/availableLineNos")
    public HttpResult<List<Integer>> availableLineNos(@RequestParam("deviceId") String deviceId,
                                                       @RequestParam(value = "lineId", required = false) String lineId) {
        String methodDescribe = getMethodDescribe("availableLineNos");
        LogUtil.njcnDebug(log, "{}，开始查询可用线路号，deviceId={}, lineId={}", methodDescribe, deviceId, lineId);
        List<Integer> result = addLedgerService.availableLineNos(deviceId, lineId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
    @ApiOperation("删除台账节点")
    @DeleteMapping("/node")
    public HttpResult<Boolean> deleteNode(@RequestParam("id") String id, @RequestParam("level") Integer level) {
        String methodDescribe = getMethodDescribe("deleteNode");
        LogUtil.njcnDebug(log, "{}，开始删除台账节点，id={}, level={}", methodDescribe, id, level);
        boolean result = addLedgerService.deleteNode(id, level);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}
