package com.cmvr.ti.service.impl;


import cn.hutool.core.text.CharPool;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmvr.common.exception.GlobalException;
import com.cmvr.test.enums.RunModeEnum;
import com.cmvr.test.flow.runtime.engine.FlowTaskRuntimeService;
import com.cmvr.test.model.domain.TeTaskConfigInfo;
import com.cmvr.test.model.domain.TeTaskInst;
import com.cmvr.test.model.vo.TeQueryProjectOrchestraItemVO;
import com.cmvr.test.model.vo.TeTaskExecuteProjectVO;
import com.cmvr.test.service.ITeTaskConfigInfoService;
import com.cmvr.ti.mapper.TiProjectMapper;
import com.cmvr.ti.model.domain.TiProject;
import com.cmvr.ti.model.domain.TiVehicleFunction;
import com.cmvr.ti.service.ITiProjectService;
import com.cmvr.ti.service.ITiVehicleFunctionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class TiProjectServiceImpl extends ServiceImpl<TiProjectMapper, TiProject> implements ITiProjectService {

    private final ITeTaskConfigInfoService taskConfigInfoService;
    private final FlowTaskRuntimeService flowTaskRuntimeService;
    private final ITiVehicleFunctionService tiVehicleFunctionService;

    @Override
    public TiProject selectTiProjectByProjectId(String projectId) {
        return this.getById(projectId);
    }

    @Override
    public List<TiProject> selectTiProjectList(TiProject tiProject) {
        LambdaQueryWrapper<TiProject> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotEmpty(tiProject.getProjectName()), TiProject::getProjectName, tiProject.getProjectName())
                .eq(ObjUtil.isNotEmpty(tiProject.getStatus()), TiProject::getStatus, tiProject.getStatus());
        return this.list(wrapper);
    }

    @Override
    public int insertTiProject(TiProject tiProject) {
        // 校验功能ID是否属于同一车型配置
        List<Long> ids = StrUtil.split(tiProject.getTestFuncs(), CharPool.COMMA)
                .stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());

        List<TiVehicleFunction> functions = tiVehicleFunctionService.queryByIds(ids);

        if (functions.size() != ids.size()) {
            throw new GlobalException("存在无效的功能ID");
        }

        if (functions.stream()
                .map(TiVehicleFunction::getVehicleConfigId)
                .distinct()
                .count() != 1) {
            throw new GlobalException("功能ID不属于同一车型配置");
        }

        int insert = this.baseMapper.insert(tiProject);
        TeTaskConfigInfo taskConfigInfo = new TeTaskConfigInfo();
        taskConfigInfo.setRunMode(RunModeEnum.TI_PROJECT.name());
        taskConfigInfo.setId(tiProject.getProjectId());
        taskConfigInfo.setStatus("0");
        taskConfigInfo.setTaskName(StrUtil.format("{}_{}", RunModeEnum.TI_PROJECT, tiProject.getProjectName()));
        taskConfigInfoService.save(taskConfigInfo);
        return insert;
    }

    @Override
    public int updateTiProject(TiProject tiProject) {
        return this.baseMapper.updateById(tiProject);
    }

    @Override
    public int deleteTiProjectByProjectIds(String[] projectIds) {
        taskConfigInfoService.deleteTeTaskConfigInfoByIds(projectIds);
        return this.baseMapper.deleteBatchIds(Arrays.asList(projectIds));
    }

    @Override
    public List<TeQueryProjectOrchestraItemVO> queryExecuteRunParams(String projectId) {
        return Collections.emptyList();
    }

    @Override
    public String executeProject(TeTaskExecuteProjectVO taskExecuteProjectVO) {
        TeTaskExecuteProjectVO normalVO = new TeTaskExecuteProjectVO();
        normalVO.setProjectId(taskExecuteProjectVO.getProjectId());
        normalVO.setRunParams(taskExecuteProjectVO.getRunParams());
        normalVO.setTerminalId(taskExecuteProjectVO.getTerminalId());
        String instId = flowTaskRuntimeService.executeProjectTask(normalVO);
        this.update(
                new LambdaUpdateWrapper<TiProject>()
                        .eq(TiProject::getProjectId, taskExecuteProjectVO.getProjectId())
                        .set(TiProject::getStatus, "0")
        );
        return instId;
    }

    @Override
    public TeTaskInst getTeTaskInst(String projectId) {
        return null;
    }

    @Override
    public String queryExecuteInstId(String projectId) {
        return "";
    }
}
