package com.fs.project.service.impl;

import java.util.List;
import java.util.Objects;

import com.fs.common.config.LoginContextManager;
import com.fs.common.exception.ServiceException;
import com.fs.company.domain.CompanyDept;
import com.fs.company.service.ICompanyDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.project.mapper.ProjectSettingsMapper;
import com.fs.project.domain.ProjectSettings;
import com.fs.project.service.IProjectSettingsService;

/**
 * 项目设置Service业务层处理
 *
 * @author fs
 * @date 2026-01-12
 */
@Service
public class ProjectSettingsServiceImpl implements IProjectSettingsService
{
    @Autowired
    private ProjectSettingsMapper projectSettingsMapper;
    @Autowired
    private ICompanyDeptService companyDeptService;

    /**
     * 查询项目设置
     *
     * @param id 项目设置主键
     * @return 项目设置
     */
    @Override
    public ProjectSettings selectProjectSettingsById(Long id)
    {
        return projectSettingsMapper.selectProjectSettingsById(id);
    }

    /**
     * 查询项目设置列表
     *
     * @param projectSettings 项目设置
     * @return 项目设置
     */
    @Override
    public List<ProjectSettings> selectProjectSettingsList(ProjectSettings projectSettings)
    {
        return projectSettingsMapper.selectProjectSettingsList(projectSettings);
    }

    /**
     * 新增项目设置
     *
     * @param projectSettings 项目设置
     * @return 结果
     */
    @Override
    public int insertProjectSettings(ProjectSettings projectSettings)
    {
        String deptId = projectSettings.getDeptId();
        if (Objects.isNull(deptId) || deptId.isEmpty()) {
            throw new ServiceException("部门/省公司不能为空");
        }

        int count =  projectSettingsMapper.countByDeptId(projectSettings.getDeptId());
        if (count > 0) {
            throw new ServiceException("该部门/省公司已存在配置，不能重复新增");
        }

        projectSettings.setCompanyId(projectSettings.getCompanyId());
        // ② 正常新增
        return projectSettingsMapper.insertProjectSettings(projectSettings);
    }

    /**
     * 修改项目设置
     *
     * @param projectSettings 项目设置
     * @return 结果
     */
    @Override
    public int updateProjectSettings(ProjectSettings projectSettings)
    {
        return projectSettingsMapper.updateProjectSettings(projectSettings);
    }

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

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


    /**
     * 查询指定公司下的项目设置列表
     *
     * @param companyId 公司ID
     * @return 项目设置集合
     */
    @Override
    public List<ProjectSettings> selectProjectSettingsByCompanyId(String companyId)
    {
        List<ProjectSettings> projectSettings = projectSettingsMapper.selectProjectSettingsByCompanyId(companyId);
        for (ProjectSettings prj : projectSettings) {
            CompanyDept companyDept = companyDeptService.selectCompanyDeptById(Long.valueOf(prj.getDeptId()));
            if(companyDept != null) {
                prj.setDeptStr(String.format("%d_%s",companyDept.getDeptId(),companyDept.getDeptName()));
            }
        }
        return projectSettings;
    }
}
