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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import com.alibaba.fastjson.JSONObject;
import com.hzbank.testteam.autotest.dependencies.authDependency.constants.AuthDependencyConstants;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.constants.BaseDefineDependencyConstants;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.dto.AdvancedQueryOptions;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.scriptcase.api.scenescript.service.AtuSceneScriptApiService;
import net.northking.cctp.scriptcase.dto.sceneScript.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "场景脚本管理")
@RequestMapping("/v1/scene")
@RestController
public class
SceneScriptCtrl {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(SceneScriptCtrl.class);

    @Autowired
    private AtuSceneScriptApiService atuSceneScriptApiService;

    @ApiOperation(value = "分页查询场景脚本列表")
    @RequiredPermissions("sceneManage")
    @PostMapping(value = "/queryList")
    public ResultWrapper<Pagination<AtuSceneInfoDetailDto>>
        queryList(@RequestBody QueryByPage<AtuSceneScriptQueryDto> query)
    {
        logger.debug("分页查询场景脚本列表入参:" + JSONObject.toJSONString(query));
        ResultWrapper<Pagination<AtuSceneInfoDetailDto>> wrapper = new ResultWrapper<>();
        Pagination<AtuSceneInfoDetailDto> result = atuSceneScriptApiService.pagingQuery(query);
        return wrapper.success(result);
    }

    @ApiOperation(value = "保存场景脚本")
    @PostMapping(value = "/saveScript")
    public ResultWrapper<String> saveScript(@RequestBody @Validated
          AtuSaveSceneScriptDto saveSceneScriptDto) throws Exception
    {
        logger.debug("保存场景脚本入参:" + JSONObject.toJSONString(saveSceneScriptDto));
        ResultWrapper<String> rwResult = new ResultWrapper<String>();
        String result = atuSceneScriptApiService.saveScript(saveSceneScriptDto);
        return rwResult.success(result);
    }

    @RequiredPermissions("ui-saveSceneScript")
    @ApiOperation(value = "保存场景脚本并生成用例")
    @PostMapping(value = "/saveScriptAndCase")
    public ResultWrapper<AtuSaveScriptAndCaseResVo> saveScriptAndCase(@RequestBody @Validated
            AtuSaveSceneScriptAndGenCaseDto saveSceneScriptAndGenCaseDto) throws Exception
    {
        long startTime = System.currentTimeMillis();
        logger.debug("保存场景脚本并生成用例入参:" + JSONObject.toJSONString(saveSceneScriptAndGenCaseDto));
        ResultWrapper<AtuSaveScriptAndCaseResVo> result = new ResultWrapper<AtuSaveScriptAndCaseResVo>();
        logger.info("---------------开始执行逻辑---------------");
        AtuSaveScriptAndCaseResVo vo =
                atuSceneScriptApiService.saveScriptAndCase(saveSceneScriptAndGenCaseDto);
        long endTime = System.currentTimeMillis();
        logger.info("保存场景脚本并生成用例耗时" + (endTime - startTime));
        return result.success(vo);
    }

    @ApiOperation(value = "获取场景脚本数据")
    @PostMapping(value = "/getScript/{scriptId}")
    public ResultWrapper<AtuSceneInfoDetailDto> getScript(@PathVariable("scriptId") String sceneId)
    {
        logger.debug("获取场景脚本数据入参:" + sceneId);
        ResultWrapper<AtuSceneInfoDetailDto> result = new ResultWrapper<AtuSceneInfoDetailDto>();
        AtuSceneInfoDetailDto detailDto = atuSceneScriptApiService.getScript(sceneId);
        return result.success(detailDto);
    }

    @RequiredPermissions("ui-deleteSceneScript")
    @ApiOperation(value = "删除场景脚本")
    @PostMapping(value = "/deleteScript/{id}")
    public ResultWrapper<Integer> deleteScript(@PathVariable("id") String id)
    {
        logger.debug("删除场景脚本入参:" + id);
        ResultWrapper<Integer> result = new ResultWrapper<Integer>();
        int count = atuSceneScriptApiService.deleteScript(id);
        return result.success(count);
    }

    @ApiOperation(value = "feign请求:根据场景脚本id查询节点脚本")
    @PostMapping(value = "/query/scriptByScene")
    public ResultWrapper<Map<String,Object>> querySceneScript(@RequestBody Map<String,Object> queryMap){
        ResultWrapper<Map<String,Object>> wrapper = new ResultWrapper<>();
        Map<String,Object> result = atuSceneScriptApiService.queryScriptData(queryMap);
        return wrapper.success(result);
    }


    @ApiOperation(value = "获取高级查询相关配置数据:创建人，更新人，责任人")
    @GetMapping(value = "/advancedQueryOptions")
    public ResponseVO<AdvancedQueryOptions> advancedQueryOptions(
            @ApiParam(value = "系统ID", required = true, example = "36602691803153920")
            @RequestHeader(AuthDependencyConstants.REQUEST_HEADER_PROJECT_ID) String projectId) {
        logger.debug("start advancedQueryOptions");
        ResponseVO<AdvancedQueryOptions> responseVO = new ResponseVO<>();
        AdvancedQueryOptions result = atuSceneScriptApiService.advancedQueryOptions(projectId);
        responseVO.setCode(BaseDefineDependencyConstants.SYSTEM_RESPONSE_CODE_SUCCESS);
        responseVO.setData(result);
        responseVO.setMessage("获取成功");
        logger.debug("end advancedQueryOptions");
        return responseVO;
    }
}
