package com.cmvr.aima.service.impl;

import java.util.List;
import java.util.UUID;
import com.baomidou.mybatisplus.spring.service.impl.ServiceImpl;
import com.cmvr.aima.domain.vo.AimaTestCaseVo;
import com.cmvr.test.model.domain.TeDetectionItem;
import com.cmvr.test.service.ITeDetectionItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cmvr.common.utils.DateUtils;
import com.cmvr.common.utils.SecurityUtils;
import com.cmvr.aima.mapper.AimaTestCaseMapper;
import com.cmvr.aima.domain.AimaTestCase;
import com.cmvr.aima.service.IAimaTestCaseService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 爱玛测试用例Service业务层处理
 * 
 * @author cmvr-iot
 * @since 2026-06-22
 */
@Service
@Transactional
public class AimaTestCaseServiceImpl extends ServiceImpl<AimaTestCaseMapper, AimaTestCase> implements IAimaTestCaseService {
    @Autowired
    private AimaTestCaseMapper aimaTestCaseMapper;

    @Autowired
    private ITeDetectionItemService teDetectionItemService;

    /**
     * 查询爱玛测试用例
     * 
     * @param id 爱玛测试用例主键
     * @return 爱玛测试用例
     */
    @Override
    public AimaTestCase selectAimaTestCaseById(String id) {
        return aimaTestCaseMapper.selectAimaTestCaseById(id);
    }

    /**
     * 查询爱玛测试用例列表
     * 
     * @param aimaTestCase 爱玛测试用例
     * @return 爱玛测试用例
     */
    @Override
    public List<AimaTestCase> selectAimaTestCaseList(AimaTestCase aimaTestCase) {
        return aimaTestCaseMapper.selectAimaTestCaseList(aimaTestCase);
    }

    /**
     * 查询爱玛测试用例视图列表（包含关联信息）
     * 
     * @param aimaTestCase 爱玛测试用例
     * @return 爱玛测试用例视图集合
     */
    @Override
    public List<AimaTestCaseVo> selectAimaTestCaseVoList(AimaTestCase aimaTestCase) {
        return aimaTestCaseMapper.selectAimaTestCaseVoList(aimaTestCase);
    }

    /**
     * 新增爱玛测试用例
     * 
     * @param aimaTestCase 爱玛测试用例
     * @return 结果
     */
    @Override
    public int insertAimaTestCase(AimaTestCase aimaTestCase) {
        // 生成UUID作为主键
        if (aimaTestCase.getId() == null || aimaTestCase.getId().isEmpty()) {
            aimaTestCase.setId(UUID.randomUUID().toString().replace("-", ""));
        }
        aimaTestCase.setCreateTime(DateUtils.getNowDate());
        aimaTestCase.setCreateBy(SecurityUtils.getUsername());
        // 同步新增检测项配置到test模块
        TeDetectionItem teDetectionItem = TeDetectionItem.builder()
                .detectName(aimaTestCase.getCaseName())
                .groupId(7L) // 可以根据实际情况调整分组ID
                .build();
        teDetectionItemService.insertTeDetectionItem(teDetectionItem);
        aimaTestCase.setDetectItemId(teDetectionItem.getId());
        return aimaTestCaseMapper.insertAimaTestCase(aimaTestCase);
    }

    /**
     * 修改爱玛测试用例
     * 
     * @param aimaTestCase 爱玛测试用例
     * @return 结果
     */
    @Override
    public int updateAimaTestCase(AimaTestCase aimaTestCase) {
        aimaTestCase.setUpdateTime(DateUtils.getNowDate());
        aimaTestCase.setUpdateBy(SecurityUtils.getUsername());
        // 同步更新检测项名称
        if (aimaTestCase.getDetectItemId() != null) {
            TeDetectionItem teDetectionItem = teDetectionItemService.selectTeDetectionItemById(aimaTestCase.getDetectItemId());
            if (teDetectionItem != null) {
                teDetectionItem.setDetectName(aimaTestCase.getCaseName());
                teDetectionItemService.updateTeDetectionItem(teDetectionItem);
            }
        }
        return aimaTestCaseMapper.updateAimaTestCase(aimaTestCase);
    }

    /**
     * 批量删除爱玛测试用例
     * 
     * @param ids 需要删除的爱玛测试用例主键
     * @return 结果
     */
    @Override
    public int deleteAimaTestCaseByIds(String[] ids) {
        // 先查询数据，删除绑定的检测项
        for (String id : ids) {
            AimaTestCase aimaTestCase = aimaTestCaseMapper.selectAimaTestCaseById(id);
            if (aimaTestCase != null && aimaTestCase.getDetectItemId() != null) {
                teDetectionItemService.deleteTeDetectionItemById(aimaTestCase.getDetectItemId());
            }
        }
        return aimaTestCaseMapper.deleteAimaTestCaseByIds(ids);
    }

    /**
     * 删除爱玛测试用例信息
     * 
     * @param id 爱玛测试用例主键
     * @return 结果
     */
    @Override
    public int deleteAimaTestCaseById(String id) {
        return aimaTestCaseMapper.deleteAimaTestCaseById(id);
    }
}
