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

import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.manage.domain.projectgroup.ProjectGroup;
import com.xinelu.manage.dto.projectgroup.ProjectGroupSaveListDto;
import com.xinelu.manage.mapper.projectgroup.ProjectGroupMapper;
import com.xinelu.manage.service.projectgroup.IProjectGroupService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
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 ProjectGroupServiceImpl implements IProjectGroupService {
    @Resource
    private ProjectGroupMapper projectGroupMapper;

    /**
     * 查询检测项目分组
     *
     * @param id 检测项目分组主键
     * @return 检测项目分组
     */
    @Override
    public ProjectGroup selectProjectGroupById(Long id) {
        return projectGroupMapper.selectProjectGroupById(id);
    }

    /**
     * 查询检测项目分组列表
     *
     * @param projectGroup 检测项目分组
     * @return 检测项目分组
     */
    @Override
    public List<ProjectGroup> selectProjectGroupList(ProjectGroup projectGroup) {
        return projectGroupMapper.selectProjectGroupList(projectGroup);
    }

    /**
     * 新增检测项目分组
     *
     * @param projectGroup 检测项目分组
     * @return 结果
     */
    @Override
    public int insertProjectGroup(ProjectGroup projectGroup) {
        if (StringUtils.isBlank(projectGroup.getGroupName())) {
            throw new ServiceException("请输入检测项目分组名称");
        }
        if (StringUtils.isBlank(projectGroup.getGroupCode())) {
            throw new ServiceException("请输入检测项目分组编码");
        }
        // 查询是否有相同编码的项目组
        List<ProjectGroup> list = projectGroupMapper.hasSameCodeGroup(null, projectGroup.getGroupCode());
        if (CollectionUtils.isNotEmpty(list)) {
            throw new ServiceException("已存在编码为【" + projectGroup.getGroupCode() + "】的分组");
        }
        projectGroup.setCreateTime(LocalDateTime.now());
        projectGroup.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        projectGroup.setDelFlag(0);
        return projectGroupMapper.insertProjectGroup(projectGroup);
    }

    @Override
    public int insertList(ProjectGroupSaveListDto saveDto) {
        if (ObjectUtils.isNotEmpty(saveDto)) {
            List<ProjectGroup> list = saveDto.getList();
            if (CollectionUtils.isNotEmpty(list)) {
                for (ProjectGroup projectGroup : list) {
                    // 查询是否有相同编码的项目组
                    List<ProjectGroup> groupList = projectGroupMapper.hasSameCodeGroup(null, projectGroup.getGroupCode());
                    if (CollectionUtils.isNotEmpty(groupList)) {
                        throw new ServiceException("已存在编码为【" + projectGroup.getGroupCode() + "】的分组");
                    }
                    projectGroup.setCreateTime(LocalDateTime.now());
                    projectGroup.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
                    projectGroup.setDelFlag(0);
                }
                return projectGroupMapper.insertBatch(list);
            } else {
                throw new ServiceException("传输数据错误");
            }
        } else {
            throw new ServiceException("传输数据错误");
        }
    }

    /**
     * 修改检测项目分组
     *
     * @param projectGroup 检测项目分组
     * @return 结果
     */
    @Override
    public int updateProjectGroup(ProjectGroup projectGroup) {
        // 查询是否有相同编码的项目组
        List<ProjectGroup> groupList = projectGroupMapper.hasSameCodeGroup(projectGroup.getId(), projectGroup.getGroupCode());
        if (CollectionUtils.isNotEmpty(groupList)) {
            throw new ServiceException("已存在编码为【" + projectGroup.getGroupCode() + "】的分组");
        }
        projectGroup.setUpdateTime(LocalDateTime.now());
        projectGroup.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        return projectGroupMapper.updateProjectGroup(projectGroup);
    }

    /**
     * 批量删除检测项目分组
     *
     * @param ids 需要删除的检测项目分组主键
     * @return 结果
     */
    @Override
    public int deleteProjectGroupByIds(Long[] ids) {
        return projectGroupMapper.deleteProjectGroupByIds(ids);
    }

    /**
     * 删除检测项目分组信息
     *
     * @param id 检测项目分组主键
     * @return 结果
     */
    @Override
    public int deleteProjectGroupById(Long id) {
        return projectGroupMapper.deleteProjectGroupById(id);
    }
}
