package com.aucma.dms.service.impl;

//import com.hw.common.core.exception.ServiceException;
//import com.hw.common.core.utils.DateUtils;
//import com.aucma.dms.domain.*;
//import com.aucma.dms.mapper.*;
//import com.aucma.dms.service.*;

import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.dms.domain.DmsBaseMaintProject;
import com.aucma.dms.domain.DmsBaseStationProject;
import com.aucma.dms.mapper.DmsBaseMaintProjectMapper;
import com.aucma.dms.service.IDmsBaseMaintProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 保养项目信息Service业务层处理
 * 
 * @author zangch
 * @date 2024-09-23
 */
@Service
public class DmsBaseMaintProjectServiceImpl implements IDmsBaseMaintProjectService
{
    @Autowired
    private DmsBaseMaintProjectMapper dmsBaseMaintProjectMapper;
    @Autowired
    private DmsBaseStationProjectServiceImpl dmsBaseStationProjectServiceImpl;

    /**
     * 查询保养项目信息
     * 
     * @param maintProjectId 保养项目信息主键
     * @return 保养项目信息
     */
    @Override
    public DmsBaseMaintProject selectDmsBaseMaintProjectByMaintProjectId(Long maintProjectId)
    {
        return dmsBaseMaintProjectMapper.selectDmsBaseMaintProjectByMaintProjectId(maintProjectId);
    }

    /**
     * 查询保养项目信息列表
     * 
     * @param dmsBaseMaintProject 保养项目信息
     * @return 保养项目信息
     */
    @Override
    public List<DmsBaseMaintProject> selectDmsBaseMaintProjectList(DmsBaseMaintProject dmsBaseMaintProject)
    {
        return dmsBaseMaintProjectMapper.selectDmsBaseMaintProjectList(dmsBaseMaintProject);
    }

    /**
     * 新增保养项目信息
     * 
     * @param dmsBaseMaintProject 保养项目信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseMaintProject(DmsBaseMaintProject dmsBaseMaintProject)
    {
        // 设置默认值：ACTIVE_FLAG 为 null 时默认为 0
        if (dmsBaseMaintProject.getActiveFlag() == null) {
            dmsBaseMaintProject.setActiveFlag("0");
        }
        dmsBaseMaintProject.setCreateTime(DateUtils.getNowDate());
        return dmsBaseMaintProjectMapper.insertDmsBaseMaintProject(dmsBaseMaintProject);
    }

    /**
     * 修改保养项目信息
     * 
     * @param dmsBaseMaintProject 保养项目信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseMaintProject(DmsBaseMaintProject dmsBaseMaintProject)
    {
        dmsBaseMaintProject.setUpdateTime(DateUtils.getNowDate());
        return dmsBaseMaintProjectMapper.updateDmsBaseMaintProject(dmsBaseMaintProject);
    }

    /**
     * 批量删除保养项目信息
     * 
     * @param maintProjectIds 需要删除的保养项目信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseMaintProjectByMaintProjectIds(Long[] maintProjectIds)
    {
        //删除前先根据maintProjectId监测stationproject表中是否存在数据，若存在就抛出异常不能删除
        for(Long maintProjectId : maintProjectIds){
            DmsBaseStationProject dmsBaseStationProject = new DmsBaseStationProject();
            dmsBaseStationProject.setMaintProjectId(maintProjectId);
            List<DmsBaseStationProject> dmsBaseStationProjectList = dmsBaseStationProjectServiceImpl
                    .selectDmsBaseStationProjectList(dmsBaseStationProject);
            if(dmsBaseStationProjectList.size() > 0){
                throw new ServiceException("其中的保养项目信息已绑定到保养部位，不能删除");
            }
        }
        return dmsBaseMaintProjectMapper.deleteDmsBaseMaintProjectByMaintProjectIds(maintProjectIds);
    }

    /**
     * 删除保养项目信息信息
     * 
     * @param maintProjectId 保养项目信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseMaintProjectByMaintProjectId(Long maintProjectId)
    {

        //删除前先根据maintProjectId监测stationproject表中是否存在数据，若存在就抛出异常不能删除
        DmsBaseStationProject dmsBaseStationProject = dmsBaseStationProjectServiceImpl
                .selectDmsBaseStationProjectBymaintProjectId(maintProjectId);
        if(dmsBaseStationProject != null){
            throw new ServiceException("该保养项目信息已绑定到保养部位，不能删除");
        }

        return dmsBaseMaintProjectMapper.deleteDmsBaseMaintProjectByMaintProjectId(maintProjectId);
    }

    public List<DmsBaseMaintProject> selectProjectMenuByStationId(Long maintStationId)
    {
        return dmsBaseMaintProjectMapper.selectProjectMenuByStationId(maintStationId);
    }
}
