package com.aucma.dms.service.impl;

//import com.aucma.dms.domain.DmsBaseStationProject;
//import com.aucma.dms.mapper.DmsBaseStationProjectMapper;
//import com.aucma.dms.service.IDmsBaseStationProjectService;

import com.aucma.dms.domain.DmsBaseStationProject;
import com.aucma.dms.mapper.DmsBaseStationProjectMapper;
import com.aucma.dms.service.IDmsBaseStationProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 保养部位关联项目信息Service业务层处理
 * 
 * @author zangch
 * @date 2024-09-23
 */
@Service
public class DmsBaseStationProjectServiceImpl implements IDmsBaseStationProjectService
{
    @Autowired
    private DmsBaseStationProjectMapper dmsBaseStationProjectMapper;

    /**
     * 查询保养部位关联项目信息
     * 
     * @param maintStationId 保养部位关联项目信息主键
     * @return 保养部位关联项目信息
     */
    @Override
    public DmsBaseStationProject selectDmsBaseStationProjectByMaintStationId(Long maintStationId)
    {
        return dmsBaseStationProjectMapper.selectDmsBaseStationProjectByMaintStationId(maintStationId);
    }

    /**
     * 查询保养部位关联项目信息列表
     * 
     * @param dmsBaseStationProject 保养部位关联项目信息
     * @return 保养部位关联项目信息
     */
    @Override
    public List<DmsBaseStationProject> selectDmsBaseStationProjectList(DmsBaseStationProject dmsBaseStationProject)
    {
        return dmsBaseStationProjectMapper.selectDmsBaseStationProjectList(dmsBaseStationProject);
    }

    /**
     * 新增保养部位关联项目信息
     * 
     * @param dmsBaseStationProject 保养部位关联项目信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseStationProject(DmsBaseStationProject dmsBaseStationProject)
    {
        return dmsBaseStationProjectMapper.insertDmsBaseStationProject(dmsBaseStationProject);
    }

    /**
     * 修改保养部位关联项目信息
     * 
     * @param dmsBaseStationProject 保养部位关联项目信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseStationProject(DmsBaseStationProject dmsBaseStationProject)
    {
        return dmsBaseStationProjectMapper.updateDmsBaseStationProject(dmsBaseStationProject);
    }

    /**
     * 批量删除保养部位关联项目信息
     * 
     * @param maintStationIds 需要删除的保养部位关联项目信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseStationProjectByMaintStationIds(Long[] maintStationIds)
    {
        return dmsBaseStationProjectMapper.deleteDmsBaseStationProjectByMaintStationIds(maintStationIds);
    }

    /**
     * 更新保养部位关联项目信息信息
     * 
     * @param maintStationId 保养部位关联项目信息
     * @return 结果
     */
    @Override
    public int deleteDmsBaseStationProjectByMaintStationIds(Long maintStationId)
    {
        return dmsBaseStationProjectMapper.deleteDmsBaseStationProjectByMaintStationId(maintStationId);
    }

    /**
     * 删除保养部位关联项目信息信息
     *
     * @param dmsBaseStationProjectList 保养部位关联项目信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int batchUpdateDmsBaseStationProject(List<DmsBaseStationProject> dmsBaseStationProjectList){
        if (dmsBaseStationProjectList == null || dmsBaseStationProjectList.isEmpty()) {
            return 0;
        }
        // 这里先清空部位已有关系，再按当前勾选结果重建，避免历史脏数据或重复关系残留。
        for (DmsBaseStationProject dmsBaseStationProject : dmsBaseStationProjectList) {
            dmsBaseStationProjectMapper.deleteDmsBaseStationProjectByMaintStationId(dmsBaseStationProject.getMaintStationId());
        }
        return insertStationProjectsOneByOne(dmsBaseStationProjectList);
    }

    /**
     * 批量保养部位关联项目信息信息
     *
     * @param dmsBaseStationProjectList 保养部位关联项目信息
     * @return 结果
     */
    @Override
    @Transactional
    public int batchInsertDmsBaseStationProject(List<DmsBaseStationProject> dmsBaseStationProjectList){
        return insertStationProjectsOneByOne(dmsBaseStationProjectList);
    }

    private int insertStationProjectsOneByOne(List<DmsBaseStationProject> dmsBaseStationProjectList)
    {
        if (dmsBaseStationProjectList == null || dmsBaseStationProjectList.isEmpty()) {
            return 0;
        }
        int rows = 0;
        for (DmsBaseStationProject dmsBaseStationProject : dmsBaseStationProjectList) {
            if (dmsBaseStationProject == null || dmsBaseStationProject.getMaintStationId() == null
                    || dmsBaseStationProject.getMaintProjectId() == null) {
                continue;
            }
            // 这里改成事务内逐条插入，是为了规避 Oracle 11/19 在批量 SQL 解析差异下触发 ORA-00933。
            rows += dmsBaseStationProjectMapper.insertDmsBaseStationProject(dmsBaseStationProject);
        }
        return rows;
    }

    /**
     * 查询保养部位关联项目信息信息
     *
     * @param maintProjectId 保养部位关联项目信息
     * @return 结果
     */
    public DmsBaseStationProject selectDmsBaseStationProjectBymaintProjectId(Long maintProjectId){
        return dmsBaseStationProjectMapper.selectDmsBaseStationProjectBymaintProjectId(maintProjectId);
    }
}

