package cn.iocoder.lyzsys.module.demo.service.project;

import cn.iocoder.lyzsys.framework.common.pojo.PageResult;
import cn.iocoder.lyzsys.framework.common.util.object.BeanUtils;
import cn.iocoder.lyzsys.module.demo.controller.admin.project.vo.ProjectPageReqVO;
import cn.iocoder.lyzsys.module.demo.controller.admin.project.vo.ProjectSaveReqVO;
import cn.iocoder.lyzsys.module.demo.dal.dataobject.project.ProjectDO;
import cn.iocoder.lyzsys.module.demo.dal.mysql.project.ProjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static cn.iocoder.lyzsys.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.lyzsys.module.demo.enums.ErrorCodeConstants.*;

/**
 * 项目管理 Service 实现类
 *
 * @author lyzsys
 */
@Service
@Validated
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    @Override
    public Long createProject(ProjectSaveReqVO createReqVO) {
        // 校验项目编号的唯一性
        validateProjectCodeUnique(null, createReqVO.getProjectCode());

        // 插入项目
        ProjectDO project = BeanUtils.toBean(createReqVO, ProjectDO.class);
        projectMapper.insert(project);
        return project.getId();
    }

    @Override
    public void updateProject(ProjectSaveReqVO updateReqVO) {
        // 校验项目存在
        validateProjectExists(updateReqVO.getId());
        // 校验项目编号的唯一性
        validateProjectCodeUnique(updateReqVO.getId(), updateReqVO.getProjectCode());

        // 更新项目
        ProjectDO updateObj = BeanUtils.toBean(updateReqVO, ProjectDO.class);
        projectMapper.updateById(updateObj);
    }

    @Override
    public void deleteProject(Long id) {
        // 校验项目存在
        validateProjectExists(id);
        // 删除项目
        projectMapper.deleteById(id);
    }

    @Override
    public void deleteProjectList(List<Long> ids) {
        // 校验项目存在
        ids.forEach(this::validateProjectExists);
        // 批量删除
        projectMapper.deleteBatchIds(ids);
    }

    @Override
    public PageResult<ProjectDO> getProjectPage(ProjectPageReqVO pageReqVO) {
        return projectMapper.selectPage(pageReqVO);
    }

    @Override
    public ProjectDO getProject(Long id) {
        return projectMapper.selectById(id);
    }

    // ==================== 校验方法 ====================

    private void validateProjectExists(Long id) {
        if (projectMapper.selectById(id) == null) {
            throw exception(PROJECT_NOT_EXISTS);
        }
    }

    private void validateProjectCodeUnique(Long id, String projectCode) {
        ProjectDO project = projectMapper.selectByProjectCode(projectCode);
        if (project == null) {
            return;
        }
        // 如果 id 为空，说明是新增，项目编号已存在则报错
        if (id == null) {
            throw exception(PROJECT_CODE_DUPLICATE);
        }
        // 如果有 id，说明是修改，判断是否是自己
        if (!project.getId().equals(id)) {
            throw exception(PROJECT_CODE_DUPLICATE);
        }
    }

}
