package com.mosquito.project.permission;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 部门Service - 部门管理核心业务逻辑
 */
@Service
@Transactional
public class DepartmentService {

    private final DepartmentRepository departmentRepository;

    public DepartmentService(DepartmentRepository departmentRepository) {
        this.departmentRepository = departmentRepository;
    }

    /**
     * 查询所有部门
     */
    @Transactional(readOnly = true)
    public List<SysDepartment> findAll() {
        return departmentRepository.findAll();
    }

    /**
     * 根据ID查询部门
     */
    @Transactional(readOnly = true)
    public Optional<SysDepartment> findById(Long id) {
        return departmentRepository.findById(id);
    }

    /**
     * 根据父部门ID查询
     */
    @Transactional(readOnly = true)
    public List<SysDepartment> findByParentId(Long parentId) {
        return departmentRepository.findByParentId(parentId);
    }

    /**
     * 获取顶级部门列表
     */
    @Transactional(readOnly = true)
    public List<SysDepartment> findRootDepartments() {
        return departmentRepository.findByParentId(null);
    }

    /**
     * 创建部门
     */
    public SysDepartment save(SysDepartment department) {
        if (department.getDeptCode() != null && departmentRepository.existsByDeptCode(department.getDeptCode())) {
            throw new IllegalArgumentException("部门代码已存在: " + department.getDeptCode());
        }
        if (department.getStatus() == null) {
            department.setStatus("ENABLED");
        }
        if (department.getSortOrder() == null) {
            department.setSortOrder(0);
        }
        return departmentRepository.save(department);
    }

    /**
     * 更新部门
     */
    public Optional<SysDepartment> update(Long id, SysDepartment departmentData) {
        return departmentRepository.findById(id)
                .map(existing -> {
                    if (departmentData.getDeptName() != null) {
                        existing.setDeptName(departmentData.getDeptName());
                    }
                    if (departmentData.getParentId() != null) {
                        existing.setParentId(departmentData.getParentId());
                    }
                    if (departmentData.getDeptCode() != null) {
                        existing.setDeptCode(departmentData.getDeptCode());
                    }
                    if (departmentData.getLeaderId() != null) {
                        existing.setLeaderId(departmentData.getLeaderId());
                    }
                    if (departmentData.getSortOrder() != null) {
                        existing.setSortOrder(departmentData.getSortOrder());
                    }
                    if (departmentData.getStatus() != null) {
                        existing.setStatus(departmentData.getStatus());
                    }
                    return departmentRepository.save(existing);
                });
    }

    /**
     * 删除部门
     */
    public void delete(Long id) {
        // 检查是否有子部门
        List<SysDepartment> children = departmentRepository.findByParentId(id);
        if (!children.isEmpty()) {
            throw new IllegalArgumentException("请先删除子部门");
        }
        departmentRepository.deleteById(id);
    }
}
