package com.ygtx.worklog.service.impl;

import java.util.List;
import com.ygtx.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ygtx.worklog.mapper.ProjectMapper;
import com.ygtx.worklog.domain.Project;
import com.ygtx.worklog.service.IProjectService;
import com.ygtx.common.exception.ServiceException;

/**
 * 项目Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@Service
public class ProjectServiceImpl implements IProjectService
{
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询项目
     *
     * @param id 项目主键
     * @return 项目
     */
    @Override
    public Project selectProjectById(Long id)
    {
        return projectMapper.selectProjectById(id);
    }

    /**
     * 查询项目列表
     *
     * @param project 项目
     * @return 项目
     */
    @Override
    public List<Project> selectProjectList(Project project)
    {
        return projectMapper.selectProjectList(project);
    }

    /**
     * 新增项目
     *
     * @param project 项目
     * @return 结果
     */
    @Override
    public int insertProject(Project project)
    {
        // 检查项目编号是否已存在
        Project existProject = projectMapper.selectProjectByCode(project.getProjectCode());
        if (existProject != null)
        {
            throw new ServiceException("项目编号已存在");
        }

        project.setCreateTime(DateUtils.getNowDate());
        return projectMapper.insertProject(project);
    }

    /**
     * 修改项目
     *
     * @param project 项目
     * @return 结果
     */
    @Override
    public int updateProject(Project project)
    {
        // 检查项目编号是否与其他项目重复
        Project existProject = projectMapper.selectProjectByCode(project.getProjectCode());
        if (existProject != null && !existProject.getId().equals(project.getId()))
        {
            throw new ServiceException("项目编号已存在");
        }

        project.setUpdateTime(DateUtils.getNowDate());
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目
     *
     * @param ids 需要删除的项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectByIds(Long[] ids)
    {
        return projectMapper.deleteProjectByIds(ids);
    }

    /**
     * 删除项目信息
     *
     * @param id 项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectById(Long id)
    {
        return projectMapper.deleteProjectById(id);
    }
}
