package com.cmvr.inspection.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.ArrayList;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmvr.inspection.domain.InspectionTaskWaypoint;
import com.cmvr.inspection.domain.InspectionWaypoint;
import com.cmvr.inspection.domain.vo.InspectionTaskVo;
import com.cmvr.inspection.mapper.InspectionTaskWaypointMapper;
import com.cmvr.test.model.domain.TeTaskConfigInfo;
import com.cmvr.test.model.vo.TeTaskOrchestraVO;
import com.cmvr.test.service.ITeTaskConfigInfoService;
import com.cmvr.test.service.ITeTaskOrchestrationService;
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.inspection.mapper.InspectionTaskMapper;
import com.cmvr.inspection.domain.InspectionTask;
import com.cmvr.inspection.service.IInspectionTaskService;

/**
 * 巡检任务Service业务层处理
 * 
 * @author cmvr-iot
 * @since 2026-05-29
 */
@Service
public class InspectionTaskServiceImpl extends ServiceImpl<InspectionTaskMapper, InspectionTask> implements IInspectionTaskService
{
    @Autowired
    private InspectionTaskMapper inspectionTaskMapper;

    @Autowired
    private InspectionTaskWaypointMapper inspectionTaskWaypointMapper;

    @Autowired
    private ITeTaskOrchestrationService teTaskOrchestrationService;

    @Autowired
    private ITeTaskConfigInfoService teTaskConfigInfoService;

    /**
     * 查询巡检任务
     * 
     * @param id 巡检任务主键
     * @return 巡检任务
     */
    @Override
    public InspectionTask selectInspectionTaskById(String id)
    {
        return inspectionTaskMapper.selectInspectionTaskById(id);
    }

    /**
     * 查询巡检任务列表
     * 
     * @param inspectionTask 巡检任务
     * @return 巡检任务
     */
    @Override
    public List<InspectionTaskVo> selectInspectionTaskList(InspectionTask inspectionTask)
    {
        return inspectionTaskMapper.selectInspectionTaskVoList(inspectionTask);
    }

    /**
     * 新增巡检任务
     * 
     * @param inspectionTask 巡检任务
     * @return 结果
     */
    @Override
    public int insertInspectionTask(InspectionTask inspectionTask)
    {
        // 生成UUID作为主键
        if (inspectionTask.getId() == null || inspectionTask.getId().isEmpty()) {
            inspectionTask.setId(UUID.randomUUID().toString().replace("-", ""));
        }
        inspectionTask.setCreateTime(DateUtils.getNowDate());
        inspectionTask.setCreateBy(SecurityUtils.getUsername());
        // 同步新增任务
        TeTaskConfigInfo teTaskConfigInfo = TeTaskConfigInfo.builder().taskName(inspectionTask.getTaskName()).build();
        teTaskConfigInfoService.insertTeTaskConfigInfo(teTaskConfigInfo);
        inspectionTask.setTaskConfigId(teTaskConfigInfo.getId());
        return inspectionTaskMapper.insertInspectionTask(inspectionTask);
    }

    /**
     * 修改巡检任务
     * 
     * @param inspectionTask 巡检任务
     * @return 结果
     */
    @Override
    public int updateInspectionTask(InspectionTask inspectionTask)
    {
        inspectionTask.setUpdateTime(DateUtils.getNowDate());
        inspectionTask.setUpdateBy(SecurityUtils.getUsername());
        TeTaskConfigInfo teTaskConfigInfo = teTaskConfigInfoService.selectTeTaskConfigInfoById(inspectionTask.getTaskConfigId());
        teTaskConfigInfo.setTaskName(inspectionTask.getTaskName());
        teTaskConfigInfoService.updateTeTaskConfigInfo(teTaskConfigInfo);
        return inspectionTaskMapper.updateInspectionTask(inspectionTask);
    }

    /**
     * 批量删除巡检任务
     * 
     * @param ids 需要删除的巡检任务主键
     * @return 结果
     */
    @Override
    public int deleteInspectionTaskByIds(String[] ids)
    {
        // 先删除关联表数据
        inspectionTaskWaypointMapper.deleteTaskWaypointByTaskIds(ids);
        // 批量查询巡检任务
        List<InspectionTask> inspectionTasks = this.baseMapper.selectBatchIds(Arrays.asList(ids));
        // 批量删除任务
        teTaskConfigInfoService.deleteTeTaskConfigInfoByIds(inspectionTasks.stream().map(InspectionTask::getTaskConfigId).toArray(String[]::new));
        return inspectionTaskMapper.deleteInspectionTaskByIds(ids);
    }

    /**
     * 查询任务关联的点位列表
     *
     * @param taskId 任务ID
     * @return 点位集合
     */
    @Override
    public List<InspectionWaypoint> selectWaypointsByTaskId(String taskId)
    {
        return inspectionTaskWaypointMapper.selectWaypointsByTaskId(taskId);
    }

    /**
     * 绑定点位到任务
     * 
     * @param taskId 任务ID
     * @param waypointIds 点位ID数组
     * @return 结果
     */
    @Override
    public int bindWaypoints(String taskId, String[] waypointIds)
    {
        // 先删除旧的关联关系
        inspectionTaskWaypointMapper.deleteTaskWaypointByTaskId(taskId);
        if (waypointIds == null || waypointIds.length == 0) {
            return 0;
        }
        // 批量插入新的关联关系
        List<InspectionTaskWaypoint> list = new ArrayList<>();
        for (int i = 0; i < waypointIds.length; i++) {
            InspectionTaskWaypoint tw = new InspectionTaskWaypoint();
            tw.setTaskId(taskId);
            tw.setWaypointId(waypointIds[i]);
            tw.setSortOrder(i + 1);
            list.add(tw);
        }
        inspectionTaskWaypointMapper.batchInsertTaskWaypoint(list);
        // 查询巡检任务,并修改点位数量
        InspectionTask inspectionTask = inspectionTaskMapper.selectInspectionTaskById(taskId);
        inspectionTask.setWaypointCount(waypointIds.length);
        inspectionTaskMapper.updateInspectionTask(inspectionTask);
        // 查询点位
        List<InspectionWaypoint> waypoints = inspectionTaskWaypointMapper.selectWaypointsByTaskId(taskId);
        // 同步任务编排
        TeTaskOrchestraVO teTaskOrchestraVO = TeTaskOrchestraVO.builder().taskId(inspectionTask.getTaskConfigId()).itemIds(waypoints.stream().map(InspectionWaypoint::getDetectItemId).collect(Collectors.toList())).build();
        teTaskOrchestrationService.insertTeTaskOrchestration(teTaskOrchestraVO);
        return 1;
    }

    /**
     * 解绑任务的点位
     * 
     * @param taskId 任务ID
     * @return 结果
     */
    @Override
    public int unbindWaypoints(String taskId)
    {
        return inspectionTaskWaypointMapper.deleteTaskWaypointByTaskId(taskId);
    }
}
