package com.aucma.dms.service.impl;

//import com.aucma.dms.domain.DmsInspectRouteDetail;
//import com.aucma.dms.mapper.DmsInspectRouteDetailMapper;
//import com.aucma.dms.service.IDmsInspectRouteDetailService;

import com.aucma.dms.domain.DmsInspectRouteDetail;
import com.aucma.dms.mapper.DmsInspectRouteDetailMapper;
import com.aucma.dms.service.IDmsInspectRouteDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 巡检线路明细Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-16
 */
@Service
public class DmsInspectRouteDetailServiceImpl implements IDmsInspectRouteDetailService
{
    @Autowired
    private DmsInspectRouteDetailMapper dmsInspectRouteDetailMapper;

    /**
     * 查询巡检线路明细
     *
     * @param routeDetailId 巡检线路明细主键
     * @return 巡检线路明细
     */
    @Override
    public DmsInspectRouteDetail selectDmsInspectRouteDetailByRouteDetailId(Long routeDetailId)
    {
        return dmsInspectRouteDetailMapper.selectDmsInspectRouteDetailByRouteDetailId(routeDetailId);
    }

    /**
     * 查询巡检线路明细列表
     *
     * @param dmsInspectRouteDetail 巡检线路明细
     * @return 巡检线路明细
     */
    @Override
    public List<DmsInspectRouteDetail> selectDmsInspectRouteDetailList(DmsInspectRouteDetail dmsInspectRouteDetail)
    {
        return dmsInspectRouteDetailMapper.selectDmsInspectRouteDetailList(dmsInspectRouteDetail);
    }

    /**
     * 新增巡检线路明细
     *
     * @param dmsInspectRouteDetail 巡检线路明细
     * @return 结果
     */
    @Override
    public int insertDmsInspectRouteDetail(DmsInspectRouteDetail dmsInspectRouteDetail)
    {
        // 避免外部传入固定主键导致序列失效，强制使用序列生成
        dmsInspectRouteDetail.setRouteDetailId(null);
        fillMachineIdIfAbsent(dmsInspectRouteDetail);
        return dmsInspectRouteDetailMapper.insertDmsInspectRouteDetail(dmsInspectRouteDetail);
    }

    /**
     * 修改巡检线路明细
     *
     * @param dmsInspectRouteDetail 巡检线路明细
     * @return 结果
     */
    @Override
    public int updateDmsInspectRouteDetail(DmsInspectRouteDetail dmsInspectRouteDetail)
    {
        fillMachineIdIfAbsent(dmsInspectRouteDetail);
        return dmsInspectRouteDetailMapper.updateDmsInspectRouteDetail(dmsInspectRouteDetail);
    }

    /**
     * 批量删除巡检线路明细
     *
     * @param routeDetailIds 需要删除的巡检线路明细主键
     * @return 结果
     */
    @Override
    public int deleteDmsInspectRouteDetailByRouteDetailIds(Long[] routeDetailIds)
    {
        return dmsInspectRouteDetailMapper.deleteDmsInspectRouteDetailByRouteDetailIds(routeDetailIds);
    }

    /**
     * 删除巡检线路明细信息
     *
     * @param routeDetailId 巡检线路明细主键
     * @return 结果
     */
    @Override
    public int deleteDmsInspectRouteDetailByRouteDetailId(Long routeDetailId)
    {
        return dmsInspectRouteDetailMapper.deleteDmsInspectRouteDetailByRouteDetailId(routeDetailId);
    }

    @Override
    public List<DmsInspectRouteDetail> selectInspectRouteListByInspectRouteId(Long inspectRouteId) {
        return dmsInspectRouteDetailMapper.selectInspectRouteListByInspectRouteId(inspectRouteId);
    }

    /**
     * 巡检线路明细前置校准：machineId 为空时回填 deviceId，确保满足数据库非空约束。
     */
    private void fillMachineIdIfAbsent(DmsInspectRouteDetail dmsInspectRouteDetail) {
        if (dmsInspectRouteDetail == null) {
            return;
        }
        if (dmsInspectRouteDetail.getMachineId() == null && dmsInspectRouteDetail.getDeviceId() != null) {
            dmsInspectRouteDetail.setMachineId(dmsInspectRouteDetail.getDeviceId());
        }
    }
}
