package net.northking.cctp.executePlan.api.third.devops;

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.executePlan.api.third.common.ResponseEntity;
import net.northking.cctp.executePlan.api.third.devops.dto.DevopsDoExecplanParamDto;
import net.northking.cctp.executePlan.api.third.devops.dto.DevopsExecplanInfoResultDto;
import net.northking.cctp.executePlan.api.third.devops.dto.DevopsProExecplanInfoDto;
import net.northking.cctp.executePlan.api.third.devops.service.DevopsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>Title:  </p>
 * <p>Description: </p>
 *
 * @Author yujinfa
 * @Version V1.0
 * @Date
 * <p>Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved.</p>
 */

@RequestMapping("/pub")
@RestController
public class DevopsPubCtrl {

    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(DevopsPubCtrl.class);

    @Autowired
    private DevopsService devopsService;

    @ApiOperation(value = "获取系统下执行计划集合")
    @PostMapping(value = "/execplan/getExecplanListByProjectId",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity getExecplanListByProjectId(
            @RequestBody  DevopsDoExecplanParamDto doExecplanParamDto)
    {
        logger.info("start projectIds: params =======> " + JSONUtil.toJsonStr(doExecplanParamDto.getProjectIds()));
        //校验数据
        List<String> projectList = new ArrayList<>(1);
        if (Objects.isNull(doExecplanParamDto.getProjectIds()) || "".equals(doExecplanParamDto.getProjectIds())) {
            return new ResponseEntity("200","请求参数为空",null);
        } else {
            if (doExecplanParamDto.getProjectIds().contains(",")) {
                projectList = Arrays.asList(doExecplanParamDto.getProjectIds().split(","));
            } else {
                projectList.add(doExecplanParamDto.getProjectIds());
            }
        }
        List<DevopsProExecplanInfoDto> result = new ArrayList<>();
        try {
            result = devopsService.getExecplanListByProjectId(projectList);
        }catch (Exception e){
            logger.info("获取系统下执行计划集合接口 异常信息"+e.getMessage());
            return new ResponseEntity("500","自动化测试平台查询异常！"+e.getMessage(),null);
        }
        logger.info("end projectIds"+result.toString());
        return new ResponseEntity("200","查询成功！",result);
    }

    @ApiOperation(value = "Devops调用入口")
    @PostMapping(value = "/execplan/doExecplanByThirdPlatforn",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity doExecplanByThirdPlatforn(
            @RequestBody DevopsDoExecplanParamDto doExecplanParamDto)
    {
        logger.info("start projectIds: params =======> " + JSONUtil.toJsonStr(doExecplanParamDto));
        //校验数据
        if(Objects.isNull(doExecplanParamDto)
                 ||Objects.isNull(doExecplanParamDto.getExecplanId())
                 ||Objects.isNull(doExecplanParamDto.getChannel())
                 ||Objects.isNull(doExecplanParamDto.getDevopsExecUuid())
                 ||Objects.isNull(doExecplanParamDto.getExecMan())){
            logger.info("调用时间"+new Date().toString()+"Devops调用参数为空,执行失败");
            return new ResponseEntity("200","Devops调用传参错误",null);
        }
        try {
            devopsService.doExecplanByThirdPlatforn(doExecplanParamDto);
        }catch (Exception e){
            logger.info("Devops调用入口接口 异常信息"+e.getMessage());
            return new ResponseEntity("500","测试平台调用异常！"+e.getMessage(),null);
        }
        logger.info("调用成功！");
        return new ResponseEntity("200","正在执行中...",null);
    }


    @ApiOperation(value = "获取本次执行计划详情")
    @PostMapping(value = "/execplan/getExecplanResultInfo",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity getExecplanResultInfo(
            @RequestBody DevopsDoExecplanParamDto doExecplanParamDto)
    {
        logger.info("start doExecplanParamDto: params =======> " + JSONUtil.toJsonStr(doExecplanParamDto));
        //校验数据
        if(Objects.isNull(doExecplanParamDto)||Objects.isNull(doExecplanParamDto.getDevopsExecUuid())){
            logger.info("调用时间"+new Date().toString()+"Devops查询参数为空");
            return new ResponseEntity("200","Devops调用传参错误",null);
        }
        DevopsExecplanInfoResultDto devopsExecplanInfoResultDto;
        try {
             devopsExecplanInfoResultDto = devopsService.getExecplanResultInfo(doExecplanParamDto);
        }catch (Exception e){
            logger.info("获取本次执行计划详情接口 异常信息"+e.getMessage());
            return new ResponseEntity("500","测试平台调用异常！"+e.getMessage(),null);
        }
        logger.info("调用成功！"+ JSONUtil.toJsonStr(devopsExecplanInfoResultDto) );
        return new ResponseEntity("200","测试平台调用成功！",devopsExecplanInfoResultDto);
    }

}
