package com.fs.project.service.impl;

import java.util.List;

import com.fs.audit.exception.BusinessException;
import com.fs.common.utils.DateUtils;
import com.fs.company.domain.CompanyRole;
import com.fs.company.mapper.CompanyRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.project.mapper.ProjectPermissionMapper;
import com.fs.project.domain.ProjectPermission;
import com.fs.project.service.IProjectPermissionService;

/**
 * 项目权限Service业务层处理
 * 
 * @author fs
 * @date 2025-12-30
 */
@Service
public class ProjectPermissionServiceImpl implements IProjectPermissionService 
{
    @Autowired
    private ProjectPermissionMapper projectPermissionMapper;

    /**
     * 查询项目权限
     * 
     * @param id 项目权限主键
     * @return 项目权限
     */
    @Override
    public ProjectPermission selectProjectPermissionById(Long id)
    {
        return projectPermissionMapper.selectProjectPermissionById(id);
    }

    /**
     * 查询项目权限列表
     * 
     * @param projectPermission 项目权限
     * @return 项目权限
     */
    @Override
    public List<ProjectPermission> selectProjectPermissionList(ProjectPermission projectPermission)
    {
        return projectPermissionMapper.selectProjectPermissionList(projectPermission);
    }

    /**
     * 新增项目权限
     *
     * @param projectPermission 项目权限
     * @return 结果
     */
    @Override
    public int insertProjectPermission(ProjectPermission projectPermission) {
        Long companyId = projectPermission.getCompanyId();
        String projectType = projectPermission.getProjectType();
        // 先检查是否已存在相同的记录
        ProjectPermission exist = projectPermissionMapper.selectByUniqueKey(
                companyId, projectType, projectPermission.getDeleted() != null ? projectPermission.getDeleted() : 0);

        if (exist != null) {
            // 已存在相同记录，抛出业务异常或执行更新
            throw new BusinessException("该公司已存在相同的项目权限，无法重复添加");
        }

        // 2. 设置创建时间
        projectPermission.setCreateTime(DateUtils.getNowDate());

        // 3. 确保 deleted 有默认值
        if (projectPermission.getDeleted() == null) {
            projectPermission.setDeleted(0); // 默认未删除
        }

        // 4. 执行插入
        return projectPermissionMapper.insertProjectPermission(projectPermission);
    }

    /**
     * 修改项目权限
     * 
     * @param projectPermission 项目权限
     * @return 结果
     */
    @Override
    public int updateProjectPermission(ProjectPermission projectPermission)
    {

        projectPermission.setUpdateTime(DateUtils.getNowDate());
        return projectPermissionMapper.updateProjectPermission(projectPermission);
    }

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

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