package com.njcn.system.controller;

import cn.hutool.core.collection.CollectionUtil;
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.common.utils.LogUtil;
import com.njcn.system.pojo.param.MxGraphParam;
import com.njcn.system.pojo.po.MxGraph;
import com.njcn.system.service.IMxGraphService;
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;
import java.util.Objects;

/**
 * pqs
 * 组态管理
 * @author cdf
 * @date 2022/1/26
 */
@RestController
@Slf4j
@Api(tags = "组态管理")
@RequestMapping("/king")
@AllArgsConstructor
public class MxGraphController extends BaseController {

    private final IMxGraphService iMxGraphService;

    /**
     * 新增组态
     * @author cdf
     * @date 2022/1/26
     */
    @OperateInfo(info = LogEnum.SYSTEM_MEDIUM)
    @PostMapping("/addKingView")
    @ApiOperation("新增组态")
    @ApiImplicitParam(name = "mxGraphParam", value = "组件实体", required = true)
    HttpResult addKingView(@RequestBody @Validated MxGraphParam mxGraphParam){
        String methodDescribe = getMethodDescribe("addKingView");
        boolean result = iMxGraphService.addKingView(mxGraphParam);
        LogUtil.njcnDebug(log, "{}，组态实体：{}", methodDescribe, mxGraphParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 查询
     * @author cdf
     * @date 2022/1/26
     */
    @OperateInfo(info = LogEnum.SYSTEM_MEDIUM)
    @GetMapping("/getKingViewList")
    @ApiOperation("查询组态")
    HttpResult<Object> getKingViewList(){
        String methodDescribe = getMethodDescribe("getKingViewList");
        List<MxGraph> result = iMxGraphService.getKingViewList();
        if (CollectionUtil.isNotEmpty(result)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, result, methodDescribe);
        }
    }

    /**
     * 通过id查询
     * @author cdf
     * @date 2022/1/26
     */
    @OperateInfo(info = LogEnum.SYSTEM_MEDIUM)
    @GetMapping("/getKingViewById")
    @ApiOperation("查询组态")
    HttpResult<Object> getKingViewById(@RequestParam("id") String id){
        String methodDescribe = getMethodDescribe("getKingViewById");
        MxGraph result = iMxGraphService.getKingViewById(id);
        if (Objects.nonNull(result)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, result, methodDescribe);
        }
    }



}
