package com.xinelu.manage.service.project.impl;

import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.manage.domain.project.Project;
import com.xinelu.manage.mapper.project.ProjectMapper;
import com.xinelu.manage.service.project.IProjectService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 检测项目Service业务层处理
 *
 * @author haown
 * @date 2024-03-11
 */
@Service
public class ProjectServiceImpl implements IProjectService {
    @Resource
    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) {
        if (StringUtils.isBlank(project.getProjectName())) {
            throw new ServiceException("请输入检测项目名称");
        }
        if (StringUtils.isBlank(project.getProjectCode())) {
            throw new ServiceException("请输入检测项目编码");
        }
        // 查询是否有相同code的检测项目
        List<Project> projects = projectMapper.hasSameCodeProject(null, project.getGroupId(), project.getProjectCode());
        if (CollectionUtils.isNotEmpty(projects)) {
            throw new ServiceException("已存在编码为【" + project.getProjectCode() + "】的检测项目");
        }
        project.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        project.setCreateTime(LocalDateTime.now());
        project.setDelFlag(0);
        return projectMapper.insertProject(project);
    }

    /**
     * 修改检测项目
     *
     * @param project 检测项目
     * @return 结果
     */
    @Override
    public int updateProject(Project project) {
        // 查询是否有相同code的检测项目
        List<Project> projects = projectMapper.hasSameCodeProject(project.getId(), project.getGroupId(), project.getProjectCode());
        if (CollectionUtils.isNotEmpty(projects)) {
            throw new ServiceException("已存在编码为【" + project.getProjectCode() + "】的检测项目");
        }
        project.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        project.setUpdateTime(LocalDateTime.now());
        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);
    }
}
