package com.cmvr.inspection.service.impl;

import java.util.List;
import java.util.UUID;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmvr.inspection.domain.vo.InspectionWaypointVo;
import com.cmvr.inspection.mapper.InspectionTaskWaypointMapper;
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.inspection.mapper.InspectionWaypointMapper;
import com.cmvr.inspection.domain.InspectionWaypoint;
import com.cmvr.inspection.service.IInspectionWaypointService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 巡检点位Service业务层处理
 * 
 * @author cmvr-iot
 * @since 2026-05-29
 */
@Service
@Transactional
public class InspectionWaypointServiceImpl extends ServiceImpl<InspectionWaypointMapper, InspectionWaypoint> implements IInspectionWaypointService
{
    @Autowired
    private InspectionWaypointMapper inspectionWaypointMapper;

    /** 任务点位关联 mapper */
    @Autowired
    private InspectionTaskWaypointMapper inspectionTaskWaypointMapper;

    /** 检测项配置service */
    @Autowired
    private ITeDetectionItemService teDetectionItemService;

    /**
     * 查询巡检点位
     * 
     * @param id 巡检点位主键
     * @return 巡检点位
     */
    @Override
    public InspectionWaypoint selectInspectionWaypointById(String id)
    {
        return inspectionWaypointMapper.selectInspectionWaypointById(id);
    }

    /**
     * 查询巡检点位列表
     * 
     * @param inspectionWaypoint 巡检点位
     * @return 巡检点位
     */
    @Override
    public List<InspectionWaypointVo> selectInspectionWaypointList(InspectionWaypoint inspectionWaypoint)
    {
        return inspectionWaypointMapper.selectInspectionWaypointVoList(inspectionWaypoint);
    }

    /**
     * 新增巡检点位
     * 
     * @param inspectionWaypoint 巡检点位
     * @return 结果
     */
    @Override
    public int insertInspectionWaypoint(InspectionWaypoint inspectionWaypoint)
    {
        // 生成UUID作为主键
        if (inspectionWaypoint.getId() == null || inspectionWaypoint.getId().isEmpty()) {
            inspectionWaypoint.setId(UUID.randomUUID().toString().replace("-", ""));
        }
        inspectionWaypoint.setCreateTime(DateUtils.getNowDate());
        inspectionWaypoint.setCreateBy(SecurityUtils.getUsername());
        TeDetectionItem teDetectionItem = TeDetectionItem.builder().detectName(inspectionWaypoint.getWaypointName()).groupId(7L).build();
        teDetectionItemService.insertTeDetectionItem(teDetectionItem);
        inspectionWaypoint.setDetectItemId(teDetectionItem.getId());
        return inspectionWaypointMapper.insertInspectionWaypoint(inspectionWaypoint);
    }

    /**
     * 修改巡检点位
     * 
     * @param inspectionWaypoint 巡检点位
     * @return 结果
     */
    @Override
    public int updateInspectionWaypoint(InspectionWaypoint inspectionWaypoint)
    {
        inspectionWaypoint.setUpdateTime(DateUtils.getNowDate());
        inspectionWaypoint.setUpdateBy(SecurityUtils.getUsername());
        return inspectionWaypointMapper.updateInspectionWaypoint(inspectionWaypoint);
    }

    /**
     * 批量删除巡检点位
     * 
     * @param ids 需要删除的巡检点位主键
     * @return 结果
     */
    @Override
    public int deleteInspectionWaypointByIds(String[] ids)
    {
        // 先删除关联表数据
        inspectionTaskWaypointMapper.deleteTaskWaypointByWaypointIds(ids);
        for(String id : ids) {
            // 查询数据后删除绑定的检测项
            InspectionWaypoint inspectionWaypoint = inspectionWaypointMapper.selectInspectionWaypointById(id);
            teDetectionItemService.deleteTeDetectionItemById(inspectionWaypoint.getDetectItemId());
        }
        return inspectionWaypointMapper.deleteInspectionWaypointByIds(ids);
    }
}
