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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
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.atuScript.service.AtuScriptInfoApiService;
import net.northking.cctp.scriptcase.db.entity.AtuScriptInfo;
import net.northking.cctp.scriptcase.dto.atuScript.atuScriptInfo.*;
import net.northking.cctp.scriptcase.dto.atuScript.atuScriptVersion.AtuScriptVersionQueryDto;
import net.northking.cctp.scriptcase.dto.atuscriptrecord.ScriptInputTableDto;
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;
import java.util.Map;

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

    @Autowired
    private AtuScriptInfoApiService apiService;

    @ApiOperation(value = "保存脚本草稿")
    @RequiredPermissions({"ui-saveScript","ui-updateScript"})
    @PostMapping(value = "/saveScript",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<AtuScriptInfo> saveScript(@RequestBody @Validated AtuScriptInfoAddDto addDto)
    {
        logger.debug("start saveScript");
        ResultWrapper<AtuScriptInfo> wrapper = new ResultWrapper<>();
        AtuScriptInfo info = apiService.saveScript(addDto);
        logger.debug("end saveScript");
        return wrapper.success(info);
    }

    @ApiOperation(value = "保存脚本并发布")
    @RequiredPermissions({"ui-publishScript","ui-updateScript"})
    @PostMapping(value = "/publishScript",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<AtuScriptInfo> publishScript(@RequestBody @Validated AtuScriptInfoAddDto addDto)
    {
        logger.debug(" start publishScript:" + addDto.getId());
        ResultWrapper<AtuScriptInfo> wrapper = new ResultWrapper<>();
        AtuScriptInfo info = apiService.publishScript(addDto);
        logger.debug("end publishScript:" + addDto.getId());
        return wrapper.success(info);
    }

    @ApiOperation(value = "分页查询脚本列表")
    @RequiredPermissions("scriptManagementList")
    @PostMapping(value = "/paging/query/scriptList",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Pagination<AtuScriptInfoDetailDto>> scriptList(@RequestBody QueryByPage<AtuScriptInfoQueryDto> query) {
        logger.debug("start scriptList");
        ResultWrapper<Pagination<AtuScriptInfoDetailDto>> wrapper = new ResultWrapper<>();
        Pagination<AtuScriptInfoDetailDto> result = apiService.pagingQuery(query);
        logger.debug("end scriptList");
        return wrapper.success(result);
    }

    @ApiOperation(value = "删除脚本")
    @RequiredPermissions("ui-deleteScript")
    @PostMapping(value = "/deleteScript/{id}")
    public ResultWrapper<Integer> deleteScript(@PathVariable("id") String id) {
        logger.debug("start deleteScript");
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer count = apiService.deleteScript(id);
        logger.debug("end deleteScript");
        return wrapper.success(count);
    }

    @ApiOperation(value = "批量删除脚本")
    @RequiredPermissions("ui-deleteScriptList")
    @PostMapping(value = "/deleteScriptList",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> deleteScriptList(@RequestBody @Validated AtuScriptInfoDeleteDto dto) {
        logger.debug("start deleteScriptList");
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer count = apiService.deleteScriptList(dto);
        logger.debug("end deleteScriptList");
        return wrapper.success(count);
    }

    @ApiOperation(value = "重命名脚本")
    @RequiredPermissions("ui-renameScript")
    @PostMapping(value = "/renameScript",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> renameScript(@Validated @RequestBody AtuScriptInfoUpdateDto dto) {
        logger.debug("start renameScript");
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer result = apiService.renameScript(dto);
        logger.debug("end renameScript");
        return wrapper.success(result);
    }

    @ApiOperation(value = "移动脚本")
    @RequiredPermissions("ui-moveScript")
    @PostMapping(value = "/moveScript",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> moveScript(@RequestBody @Validated AtuScriptInfoMoveDto dto) {
        logger.info("start moveScript");
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer result = apiService.moveScript(dto);
        logger.info("end moveScript");
        return wrapper.success(result);
    }

    @ApiOperation(value = "复制脚本")
    @RequiredPermissions("ui-copyScript")
    @PostMapping(value = "/copyScript",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> copyScript(@RequestBody @Validated AtuScriptInfoMoveDto dto) {
        logger.info("start copyScript");
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer result = apiService.copyScript(dto);
        logger.info("end copyScript");
        return wrapper.success(result);
    }

    @ApiOperation(value = "回滚历史版本")
    @PostMapping(value = "/rollBackVersion",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> rollBackVersion(@RequestBody @Validated AtuScriptVersionQueryDto queryDto)
    {
        logger.debug("start rollBackVersion");
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer res = apiService.rollBackVersion(queryDto);
        logger.debug("end rollBackVersion");
        return wrapper.success(res);
    }

    @RequiredPermissions("ui-getVersionContent")
    @ApiOperation(value = "根据版本查看脚本详情")
    @PostMapping(value = "/getVersionContent",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<AtuScriptInfoDetailDto> getVersionContent(@RequestBody @Validated AtuScriptVersionQueryDto queryDto)
    {
        logger.debug("start getVersionContent");
        ResultWrapper<AtuScriptInfoDetailDto> wrapper = new ResultWrapper<>();
        AtuScriptInfoDetailDto res = apiService.getVersionContent(queryDto);
        logger.debug("end getVersionContent");
        return wrapper.success(res);
    }

    @ApiOperation(value = "获取脚本详情")
    @RequiredPermissions({"ui-updateScript","ui-saveSceneScript"})
    @GetMapping(value = "/getScriptInfo/{scriptId}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<AtuScriptInfoDetailDto> getScriptInfo(@PathVariable("scriptId") String scriptId, @RequestParam(value = "queryLastVersion", required = false) Boolean queryLastVersion){
        logger.debug("start getScriptInfo");
        ResultWrapper<AtuScriptInfoDetailDto> wrapper = new ResultWrapper<>();
        AtuScriptInfoDetailDto res = apiService.getScriptInfo(scriptId, queryLastVersion != null && queryLastVersion);
        logger.debug("end getScriptInfo");
        return wrapper.success(res);
    }

    @ApiOperation(value = "根据脚本id获取脚本名称")
    @GetMapping(value = "/getScriptName/{scriptId}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> getScriptName(@PathVariable("scriptId") String scriptId){
        logger.debug("start getScriptName");
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String res = apiService.getScriptName(scriptId);
        logger.debug("end getScriptName");
        return wrapper.success(res);
    }

    @ApiOperation(value = "保存更新脚本数据")
    @PostMapping(value = "/saveScriptData",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> saveScriptData(@RequestBody @Validated ScriptInputTableDto dto)
    {
        logger.info("start saveScriptData");
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String url = this.apiService.saveScriptData(dto);
        logger.info("end saveScriptData");
        return wrapper.success(url);
    }

    @ApiOperation(value = "查询脚本详细信息")
    @PostMapping("/getScriptDetailInfo")
    public ResultWrapper<String> getScriptDetailInfo(@RequestBody Map<String, String> params){
        logger.info("start getScriptDetailInfo");
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String result = this.apiService.getScriptDetailInfo(params);
        logger.info("end getScriptDetailInfo");
        return wrapper.success(result);
    }

    @ApiOperation(value = "feign调用：校验计划数据是否正常")
    @PostMapping(value = "/feign/plan/checkData")
    public ResultWrapper<Map<String,Object>> checkPlanByCaseSet(@RequestBody @Validated AtuScriptInfoFeignDto dto){
        ResultWrapper<Map<String,Object>> wrapper = new ResultWrapper<>();
        Map<String, Object> map = this.apiService.checkPlanData(dto);
        return wrapper.success(map);
    }

    @ApiOperation(value = "feign调用：校验脚本类型")
    @PostMapping(value = "/feign/script/checkData")
    public ResultWrapper<AtuScriptInfoResultDto> checkScriptData(@RequestBody @Validated List<String> scriptList){
        ResultWrapper<AtuScriptInfoResultDto> wrapper = new ResultWrapper<>();
        AtuScriptInfoResultDto result = this.apiService.checkScriptList(scriptList);
        return wrapper.success(result);
    }


    @ApiOperation(value = "更新脚本负责人")
    @RequiredPermissions("ui-updateScriptPrincipal")
    @PostMapping(value = "/update/principal")
    public ResultWrapper<Integer> updatePrincipal(@RequestBody PrincipalUpdateDto dto){
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer result = this.apiService.updatePrincipal(dto);
        return wrapper.success(result);
    }

    @ApiOperation(value = "feign调用：查询脚本列表以及脚本类型的判断")
    @PostMapping(value = "/feign/query/ScriptData")
    public PlanScriptPageDto queryScriptByPlan(@RequestBody @Validated PlanPageQueryDto queryDto){
      return this.apiService.queryScriptData(queryDto);
    }


    @ApiOperation(value = "数据列表查询")
    @PostMapping(value = "/feign/queryCode")
    public String queryScriptCode(@RequestBody Map<String,String> scriptMap){
        return this.apiService.queryScriptCode(scriptMap);
    }

    @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 = apiService.advancedQueryOptions(projectId);
        responseVO.setCode(BaseDefineDependencyConstants.SYSTEM_RESPONSE_CODE_SUCCESS);
        responseVO.setData(result);
        responseVO.setMessage("获取成功");
        logger.debug("end advancedQueryOptions");
        return responseVO;
    }

}
