package com.test.test.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.test.common.utils.DateUtils;
import com.test.common.utils.SecurityUtils;
import com.test.test.domain.UiAutomation;
import com.test.test.domain.UiReport;
import com.test.test.domain.UiSceneStepsReport;
import com.test.test.domain.vo.UiReportVO;
import com.test.test.domain.vo.UiSceneStepsReportVO;
import com.test.test.mapper.UiAutomationMapper;
import com.test.test.mapper.UiReportMapper;
import com.test.test.mapper.UiSceneStepsReportMapper;
import com.test.test.service.IUiReportService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * ui报告Service业务层处理
 * 
 * @author test
 * @date 2025-04-24
 */
@Service
public class UiReportServiceImpl implements IUiReportService
{
    @Resource
    private UiReportMapper uiReportMapper;
    @Resource
    private UiSceneStepsReportMapper uiSceneStepsReportMapper;
    @Resource
    private UiAutomationMapper uiAutomationMapper;

    /**
     * 查询ui报告
     * 
     * @param id ui报告主键
     * @return ui报告
     */
    @Override
    public UiReportVO selectUiReportById(Long id)
    {
        UiReportVO uiReportVO = new UiReportVO();
        UiReport uiReport = uiReportMapper.selectUiReportById(id);
        BeanUtils.copyProperties(uiReport, uiReportVO);
        UiSceneStepsReport uiSceneStepsReport = new UiSceneStepsReport();
        uiSceneStepsReport.setReportId(id);
        //查询报告步骤详情
        List<UiSceneStepsReport> uiSceneStepsReports = uiSceneStepsReportMapper.selectUiSceneStepsReportList(uiSceneStepsReport);
        for (UiSceneStepsReport sceneStepsReport : uiSceneStepsReports) {
            UiSceneStepsReportVO uiSceneStepsReportVO = new UiSceneStepsReportVO();
            BeanUtils.copyProperties(sceneStepsReport, uiSceneStepsReportVO);
            //全部
            uiReportVO.getUiSceneStepsAllReportVOList().add(uiSceneStepsReportVO);
        }
        //执行失败
        List<UiSceneStepsReportVO> errorList = uiReportVO.getUiSceneStepsAllReportVOList().stream().filter(e -> e.getExecutionFlag().equals("2")).collect(Collectors.toList());
        uiReportVO.getUiSceneStepsErrorReportVOList().addAll(errorList);
        //未执行
        List<UiSceneStepsReportVO> NotList = uiReportVO.getUiSceneStepsAllReportVOList().stream().filter(e -> e.getExecutionFlag().equals("0")).collect(Collectors.toList());
        uiReportVO.getUiSceneStepsNotReportVOList().addAll(NotList);
        return uiReportVO;
    }

    /**
     * 查询ui报告列表
     * 
     * @param uiReport ui报告
     * @return ui报告
     */
    @Override
    public List<UiReport> selectUiReportList(UiReport uiReport)
    {
        uiReport.setDelFlag(0);
        return uiReportMapper.selectUiReportList(uiReport);
    }

    /**
     * 新增ui报告
     * 
     * @param automationId ui报告
     * @param triggerMode 触发方式 1手动触发 2批量执行 3定时任务 4 Api调用
     * @return 结果
     */
    @Override
    public Long insertUiReport(Date startTime, Long automationId, String triggerMode, Integer stepsNum)
    {
        UiAutomation automation = uiAutomationMapper.selectUiAutomationById(automationId);
        String name = automation.getName();
        UiReport uiReport = new UiReport();
        uiReport.setName(name + "-" + DateUtils.getTime());
        uiReport.setReportType("场景");
        uiReport.setCreateTime(startTime);
        uiReport.setCreateBy(SecurityUtils.getUsername());
        uiReport.setUpdateBy(SecurityUtils.getUsername());
        uiReport.setTriggerMode(triggerMode);
        uiReport.setDelFlag(0);
        uiReport.setStepsNumber(stepsNum);//步骤数
        uiReport.setStatus(1);//正在执行
        uiReport.setScenesNumber(1);
        uiReportMapper.insertUiReport(uiReport);
        return uiReport.getId();
    }

    /**
     * 修改ui报告
     * 
     * @param uiReport ui报告
     * @return 结果
     */
    @Override
    public int updateUiReport(UiReport uiReport)
    {
        uiReport.setUpdateTime(DateUtils.getNowDate());
        return uiReportMapper.updateUiReport(uiReport);
    }

    /**
     * 批量删除ui报告
     * 
     * @param ids 需要删除的ui报告主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUiReportByIds(Long[] ids)
    {
        //删除报告详情
        uiSceneStepsReportMapper.deleteUiSceneStepsReportByIds(ids);
        return uiReportMapper.deleteUiReportByIds(ids);
    }

    /**
     * 删除ui报告信息
     * 
     * @param id ui报告主键
     * @return 结果
     */
    @Override
    public int deleteUiReportById(Long id)
    {
        return uiReportMapper.deleteUiReportById(id);
    }
}
