package com.zhyc.module.base.service.impl;

import com.zhyc.common.exception.ServiceException;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.module.base.domain.BasSheepGroup;
import com.zhyc.module.base.mapper.BasSheepGroupMapper;
import com.zhyc.module.base.service.IBasSheepGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分组管理Service业务层处理
 * 
 * @author wyt
 * @date 2025-07-14
 */
@Service
public class BasSheepGroupServiceImpl implements IBasSheepGroupService
{
    @Autowired
    private BasSheepGroupMapper basSheepGroupMapper;

    /**
     * 查询分组管理
     * 
     * @param groupId 分组管理主键
     * @return 分组管理
     */
    @Override
    public BasSheepGroup selectBasSheepGroupByGroupId(Long groupId)
    {
        return basSheepGroupMapper.selectBasSheepGroupByGroupId(groupId);
    }

    /**
     * 查询分组管理列表
     * 
     * @param basSheepGroup 分组管理
     * @return 分组管理
     */
//    @Override
//    public List<BasSheepGroup> selectBasSheepGroupList(BasSheepGroup basSheepGroup)
//    {
//        return basSheepGroupMapper.selectBasSheepGroupList(basSheepGroup);
//    }
    @Override
    public List<BasSheepGroup> selectBasSheepGroupList(BasSheepGroup basSheepGroup) {
        List<BasSheepGroup> groups = basSheepGroupMapper.selectBasSheepGroupList(basSheepGroup);

        // 处理祖先名称显示格式
        groups.forEach(group -> {
            if (group.getAncestorNames() != null) {
                String formattedNames = group.getAncestorNames().replace(",", " / ");
                group.setAncestorNames(formattedNames);
            }
        });

        return groups;
    }

    /**
     * 新增分组管理
     * 
     * @param basSheepGroup 分组管理
     * @return 结果
     */
    @Override
    public int insertBasSheepGroup(BasSheepGroup basSheepGroup)
    {
        // ✅ 新增唯一性校验
        BasSheepGroup existing = basSheepGroupMapper.selectByParentIdAndGroupName(
                basSheepGroup.getParentId(), basSheepGroup.getGroupName());
        if (existing != null) {
            throw new ServiceException("同一分支下已存在该分组名称");
        }
        basSheepGroup.setCreateTime(DateUtils.getNowDate());
        return basSheepGroupMapper.insertBasSheepGroup(basSheepGroup);
    }

    /**
     * 修改分组管理
     * 
     * @param basSheepGroup 分组管理
     * @return 结果
     */
    @Override
    public int updateBasSheepGroup(BasSheepGroup basSheepGroup)
    {
        // ✅ 新增唯一性校验（排除当前记录）
        BasSheepGroup existing = basSheepGroupMapper.selectByParentIdAndGroupName(
                basSheepGroup.getParentId(), basSheepGroup.getGroupName());
        if (existing != null && !existing.getGroupId().equals(basSheepGroup.getGroupId())) {
            throw new ServiceException("同一分支下已存在该分组名称");
        }
        basSheepGroup.setUpdateTime(DateUtils.getNowDate());
        return basSheepGroupMapper.updateBasSheepGroup(basSheepGroup);
    }

    /**
     * 批量删除分组管理
     * 
     * @param groupIds 需要删除的分组管理主键
     * @return 结果
     */
    @Override
    public int deleteBasSheepGroupByGroupIds(Long[] groupIds)
    {
        return basSheepGroupMapper.deleteBasSheepGroupByGroupIds(groupIds);
    }

    /**
     * 删除分组管理信息
     *
     * @param groupId 分组管理主键
     * @return 结果
     */
    @Override
    public int deleteBasSheepGroupByGroupId(Long groupId)
    {
        return basSheepGroupMapper.deleteBasSheepGroupByGroupId(groupId);
    }

    @Override
    public List<BasSheepGroup> selectLeafNodes() {
        List<BasSheepGroup> leafNodes = basSheepGroupMapper.selectLeafNodes();
        // 如果 ancestorNames 需要格式化，可以复用已有的逻辑
        leafNodes.forEach(group -> {
            if (group.getAncestorNames() != null) {
                group.setAncestorNames(group.getAncestorNames().replace(",", " / "));
            }
        });
        return leafNodes;
    }
}
