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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import com.alibaba.fastjson.JSONObject;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.constants.BaseDefineDependencyConstants;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.dto.TagDto;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.scriptcase.api.atuScript.service.AtuScriptTagApiService;
import net.northking.cctp.scriptcase.dto.atuScript.atuScriptTag.AtuScriptTagAddDto;
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/scriptTag")
@RestController
public class ScriptTagCtrl {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(ScriptTagCtrl.class);

    @Autowired
    private AtuScriptTagApiService apiService;

    @ApiOperation(value = "给脚本列表批量打标签")
    @RequiredPermissions("ui-addScriptTagList")
    @PostMapping(value = "/addScriptTagList",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<Integer> addScriptTagList(@RequestBody @Validated AtuScriptTagAddDto addDto) {
        logger.debug("start addScriptTagList, param:{}", JSONObject.toJSONString(addDto));
        ResponseVO<Integer> responseVO = apiService.addScriptTagList(addDto);
        logger.debug("end addScriptTagList, count:{}", responseVO.getData());
        return responseVO;
    }

    @ApiOperation(value = "根据脚本id移除对应脚本标签")
    @DeleteMapping(value = "/deleteScriptTagByScriptId",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<Integer> deleteScriptTagByScriptId(@RequestParam String scriptId, @RequestParam Long tagId) {
        logger.debug("start deleteScriptTag, scriptId:{}, tagId:{}", scriptId, tagId);
        ResponseVO<Integer> responseVO = new ResponseVO<>();
        Integer result = apiService.deleteScriptTag(scriptId, tagId);
        logger.debug("end deleteScriptTag, num:{}", result);
        responseVO.setCode(BaseDefineDependencyConstants.SYSTEM_RESPONSE_CODE_SUCCESS);
        responseVO.setData(result);
        return responseVO;
    }

    @ApiOperation(value = "获取脚本引用的标签列表")
    @RequiredPermissions( {"ui-queryScriptTag","ui-queryScriptTags"})
    @GetMapping("/queryScriptTag/{scriptId}")
    public ResponseVO<List<TagDto>> queryScriptTag(@PathVariable("scriptId") String scriptId) {
        logger.debug("start queryScriptTag, scriptId:{}", scriptId);
        ResponseVO<List<TagDto>> responseVO = new ResponseVO<>();
        List<TagDto> res = apiService.queryScriptTag(scriptId);
        logger.debug("end queryScriptTag");
        responseVO.setCode(BaseDefineDependencyConstants.SYSTEM_RESPONSE_CODE_SUCCESS);
        responseVO.setData(res);
        return responseVO;
    }

}
