package net.northking.cctp.scriptcase.api.atuScript;

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.scriptcase.api.atuScript.service.AtuScriptGroupApiService;
import net.northking.cctp.scriptcase.dto.atuScript.atuScriptGroup.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "脚本目录树管理")
@RequestMapping("/v1/scriptTree")
@RestController
public class ScriptTreeCtrl {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(ScriptTreeCtrl.class);

    @Autowired
    private AtuScriptGroupApiService apiService;

    @ApiOperation(value = "新增脚本目录")
    @RequiredPermissions("ui-addScriptTree")
    @PostMapping(value = "/addScriptTree",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> addScriptTree(@RequestBody @Validated AtuScriptGroupAddDto addDto)
    {
        logger.debug("start addScriptTree, param:{}", JSONObject.toJSONString(addDto));
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String uuid = apiService.add(addDto);
        logger.debug("end addScriptTree, treeId:{}", uuid);
        return wrapper.success(uuid);
    }

    @ApiOperation(value = "编辑脚本目录")
    @RequiredPermissions("ui-updateScriptTree")
    @PutMapping(value = "/updateScriptTree",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> updateScriptTree(@RequestBody @Validated AtuScriptGroupUpdateDto dto)
    {
        logger.debug("start updateScriptTree, param:{}", JSONObject.toJSONString(dto));
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer result = apiService.updateScriptTree(dto);
        logger.debug("end updateScriptTree, result:{}", result);
        return wrapper.success(result);
    }

    @ApiOperation(value = "删除脚本目录")
    @RequiredPermissions("ui-deleteScriptTree")
    @DeleteMapping(value = "/deleteScriptTree/{id}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> deleteScriptTree(@PathVariable String id) {
        logger.debug("start deleteScriptTree, id:{}", id);
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer result = apiService.deleteScriptTree(id);
        logger.debug("end deleteScriptTree, num:{}", result);
        return wrapper.success(result);
    }

    @ApiOperation(value = "获取子目录列表")
    @PostMapping(
            value = "/queryChildrenTree",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<List<AtuScriptGroupDetailDto>> queryChildrenTree(
            @RequestBody @Validated AtuScriptGroupQueryDto dto)
    {
        logger.debug("start queryChildrenTree, param:{}", JSONObject.toJSONString(dto));
        ResultWrapper<List<AtuScriptGroupDetailDto>> wrapper = new ResultWrapper<>();
        List<AtuScriptGroupDetailDto> res = apiService.queryChildrenTree(dto);
        logger.debug("end queryChildrenTree");
        return wrapper.success(res);
    }

    @ApiOperation(value = "条件查询目录树")
    @PostMapping(
            value = "/queryScriptTree",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<List<AtuScriptGroupDetailDto>> queryScriptTree(
            @RequestBody @Validated AtuScriptGroupQueryDto dto)
    {
        logger.debug("start queryScriptTree, param:{}", JSONObject.toJSONString(dto));
        ResultWrapper<List<AtuScriptGroupDetailDto>> wrapper = new ResultWrapper<>();
        List<AtuScriptGroupDetailDto> res = apiService.queryScriptTree(dto);
        logger.debug("end queryScriptTree");
        return wrapper.success(res);
    }

    @ApiOperation(value = "拖拽树目录")
    @PostMapping(
            value = "/moveScriptTree",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> moveScriptTree(
            @RequestBody @Validated AtuScriptGroupMoveDto dto)
    {
        logger.debug("start moveScriptTree, param:{}", JSONObject.toJSONString(dto));
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer num = apiService.moveScriptTree(dto);
        logger.debug("end moveScriptTree");
        return wrapper.success(num);
    }

    @ApiOperation(value = "条件查询已发布脚本列表")
    @PostMapping(
            value = "/getPublishScriptList",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<List<PublishScriptDetailDto>> getPublishScriptList(
            @RequestBody @Validated PublishScriptQueryDto dto)
    {
        logger.debug("start getPublishScriptList, param:{}", JSONObject.toJSONString(dto));
        ResultWrapper<List<PublishScriptDetailDto>> wrapper = new ResultWrapper<>();
        List<PublishScriptDetailDto> res = apiService.getPublishScriptList(dto);
        logger.debug("end getPublishScriptList");
        return wrapper.success(res);
    }
}
