package com.test.test.service.impl;

import com.test.common.core.domain.model.JmeterRequest;
import com.test.common.utils.DateUtils;
import com.test.common.utils.JMeterUtil;
import com.test.test.domain.TestCaseStep;
import com.test.test.domain.qo.TestCaseStepAddQO;
import com.test.test.mapper.TestCaseStepMapper;
import com.test.test.service.ITestCaseStepService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 用例步骤Service业务层处理
 *
 * @author test
 * @date 2025-02-11
 */
@Slf4j
@Service
public class TestCaseStepServiceImpl implements ITestCaseStepService {
    @Resource
    private TestCaseStepMapper testCaseStepMapper;

    /**
     * 查询用例步骤
     *
     * @param id 用例步骤主键
     * @return 用例步骤
     */
    @Override
    public TestCaseStep selectTestCaseStepById(Long id) {
        return testCaseStepMapper.selectTestCaseStepById(id);
    }

    /**
     * 查询用例步骤列表
     *
     * @param testCaseStep 用例步骤
     * @return 用例步骤
     */
    @Override
    public List<TestCaseStep> selectTestCaseStepList(TestCaseStep testCaseStep) {
        testCaseStep.setDelFlag("0");
        List<TestCaseStep> caseStepParentList = testCaseStepMapper.selectTestCaseStepList(testCaseStep);
        for (TestCaseStep testCaseStepParent : caseStepParentList) {
            if (testCaseStepParent.getType() > 2) {
                // 加载子步骤
                TestCaseStep testCaseStepChild = new TestCaseStep();
                testCaseStepChild.setDelFlag("0");
                testCaseStepChild.setParentId(testCaseStepParent.getId());
                List<TestCaseStep> caseStepChildrenList = testCaseStepMapper.selectTestCaseStepList(testCaseStepChild);
                testCaseStepParent.setChildrenList(caseStepChildrenList);
            }
        }
        return caseStepParentList;
    }

    /**
     * 新增用例步骤
     *
     * @param testCaseStep 用例步骤
     * @return 结果
     */
    @Override
    public int insertTestCaseStep(TestCaseStep testCaseStep) {
        testCaseStep.setCreateTime(DateUtils.getNowDate());
        return testCaseStepMapper.insertTestCaseStep(testCaseStep);
    }

    @Override
    @Transactional
    public int insertBatchTestCaseStep(TestCaseStepAddQO qo) {
        testCaseStepMapper.deleteTestCaseStepByCaseId(qo.getCaseId());
        int i = 0;
        for (TestCaseStep testCaseStep1 : qo.getList()) {
            i += insertTestCaseStep(testCaseStep1);
        }
        return i;
    }

    /**
     * 修改用例步骤
     *
     * @param testCaseStep 用例步骤
     * @return 结果
     */
    @Override
    public int updateTestCaseStep(TestCaseStep testCaseStep) {
        testCaseStep.setUpdateTime(DateUtils.getNowDate());
        return testCaseStepMapper.updateTestCaseStep(testCaseStep);
    }

    /**
     * 批量删除用例步骤
     *
     * @param ids 需要删除的用例步骤主键
     * @return 结果
     */
    @Override
    public int deleteTestCaseStepByIds(Long[] ids) {
        return testCaseStepMapper.deleteTestCaseStepByIds(ids);
    }

    /**
     * 删除用例步骤信息
     *
     * @param id 用例步骤主键
     * @return 结果
     */
    @Override
    public int deleteTestCaseStepById(Long id) {
        return testCaseStepMapper.deleteTestCaseStepById(id);
    }

    /**
     * 根据用例id查询用例步骤列表并按照步骤顺序排序
     *
     * @param caseId 用例id
     * @return 用例步骤集合
     */
    @Override
    public List<TestCaseStep> selectTestCaseStepListByCaseId(Long caseId) {
        return testCaseStepMapper.selectTestCaseStepListByCaseId(caseId);
    }

    /**
     * 执行jmeter用例步骤计划
     *
     * @param id             用例步骤主键
     * @param jmeterHomePath jmeter安装路径
     * @return 结果
     */
    @Override
    public Map<String, String> executeJmeterTestCaseStepById(Long id, String jmeterHomePath) {
        TestCaseStep testCaseStep = testCaseStepMapper.selectTestCaseStepById(id);
        if (testCaseStep == null) {
            log.error("根据主键id：{}未查询到用例步骤计划", id);
            return null;
        }
        String url = testCaseStep.getRequestUrl();
        String method = testCaseStep.getRequestMethod().toUpperCase();
        if (!method.equals("GET") && !method.equals("POST")) {
            log.error("不支持的请求方式：{}", method);
            return null;
        }
        if (!url.startsWith("http")) {
            String apiProtocol = testCaseStep.getApiProtocol();
            String appendUrl = apiProtocol + "://" + testCaseStep.getApiHost();
            if (testCaseStep.getApiPort() != null) {
                url = appendUrl + ":" + testCaseStep.getApiPort() + url;
            } else {
                url = appendUrl + url;
            }
        }
        JmeterRequest jmeterRequest = new JmeterRequest();
        jmeterRequest.setId(id);
        jmeterRequest.setUrl(url);
        jmeterRequest.setPort(testCaseStep.getApiPort());
        jmeterRequest.setMethod(testCaseStep.getRequestMethod());
        jmeterRequest.setRequestBody(testCaseStep.getRequestBody());
        jmeterRequest.setRequestParams(testCaseStep.getRequestParams());
        jmeterRequest.setRequestHeader(testCaseStep.getRequestHeader());
        jmeterRequest.setJmeterHomePath(jmeterHomePath);
        log.info("getRequestHeader:{}", jmeterRequest.getRequestHeader());
        Map<String, String> resultMap = JMeterUtil.getJmeterResult(jmeterRequest);

        return resultMap;
    }

}
