package com.aucma.dms.service.impl;

import com.aucma.common.core.domain.model.LoginUser;
import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.dms.domain.DmsBaseInspectProject;
import com.aucma.dms.mapper.DmsBaseInspectProjectMapper;
import com.aucma.dms.service.IDmsBaseInspectProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 巡检项目信息Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-17
 */
@Service
public class DmsBaseInspectProjectServiceImpl implements IDmsBaseInspectProjectService
{
    @Autowired
    private DmsBaseInspectProjectMapper dmsBaseInspectProjectMapper;

    /**
     * 查询巡检项目信息
     *
     * @param inspectProjectId 巡检项目信息主键
     * @return 巡检项目信息
     */
    @Override
    public DmsBaseInspectProject selectDmsBaseInspectProjectByInspectProjectId(Long inspectProjectId)
    {
        return dmsBaseInspectProjectMapper.selectDmsBaseInspectProjectByInspectProjectId(inspectProjectId);
    }

    /**
     * 查询巡检项目信息列表
     *
     * @param dmsBaseInspectProject 巡检项目信息
     * @return 巡检项目信息
     */
    @Override
    public List<DmsBaseInspectProject> selectDmsBaseInspectProjectList(DmsBaseInspectProject dmsBaseInspectProject)
    {
        return dmsBaseInspectProjectMapper.selectDmsBaseInspectProjectList(dmsBaseInspectProject);
    }

    /**
     * 新增巡检项目信息
     *
     * @param dmsBaseInspectProject 巡检项目信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseInspectProject(DmsBaseInspectProject dmsBaseInspectProject)
    {
        DmsBaseInspectProject InspectProject = new DmsBaseInspectProject();
        InspectProject.setInspectProjectCode(dmsBaseInspectProject.getInspectProjectCode());
        List<DmsBaseInspectProject> DmsBaseInspectProjectList = dmsBaseInspectProjectMapper
                .selectDmsBaseInspectProjectList(InspectProject);
        if (!DmsBaseInspectProjectList.isEmpty()){
            throw new ServiceException("巡检项目编号已存在");
        }
        if (dmsBaseInspectProject.getInspectType()==null){
            dmsBaseInspectProject.setInspectType("1");
        }
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseInspectProject.setIsFlag("1");
        dmsBaseInspectProject.setCreateBy(user.getUserId());
        dmsBaseInspectProject.setCreateTime(DateUtils.getNowDate());
        return dmsBaseInspectProjectMapper.insertDmsBaseInspectProject(dmsBaseInspectProject);
    }

    /**
     * 修改巡检项目信息
     *
     * @param dmsBaseInspectProject 巡检项目信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseInspectProject(DmsBaseInspectProject dmsBaseInspectProject)
    {
        DmsBaseInspectProject InspectProject = new DmsBaseInspectProject();
        InspectProject.setInspectProjectCode(dmsBaseInspectProject.getInspectProjectCode());
        List<DmsBaseInspectProject> DmsBaseInspectProjectList = dmsBaseInspectProjectMapper
                .selectDmsBaseInspectProjectList(InspectProject);
        if (!DmsBaseInspectProjectList.isEmpty() && !DmsBaseInspectProjectList.get(0).getInspectProjectId()
                .equals(dmsBaseInspectProject.getInspectProjectId())){
            throw new ServiceException("巡检项目编号已存在");
        }
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseInspectProject.setUpdateBy(user.getUserId());
        dmsBaseInspectProject.setUpdateTime(DateUtils.getNowDate());
        return dmsBaseInspectProjectMapper.updateDmsBaseInspectProject(dmsBaseInspectProject);
    }

    /**
     * 批量删除巡检项目信息
     *
     * @param inspectProjectIds 需要删除的巡检项目信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseInspectProjectByInspectProjectIds(Long[] inspectProjectIds)
    {
        return dmsBaseInspectProjectMapper.deleteDmsBaseInspectProjectByInspectProjectIds(inspectProjectIds);
    }

    /**
     * 删除巡检项目信息信息
     *
     * @param inspectProjectId 巡检项目信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseInspectProjectByInspectProjectId(Long inspectProjectId)
    {
        return dmsBaseInspectProjectMapper.deleteDmsBaseInspectProjectByInspectProjectId(inspectProjectId);
    }
}
