package com.ema.admin.modules.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ema.admin.modules.system.converter.SysDepartmentConverter;
import com.ema.admin.modules.system.converter.SysUserConverter;
import com.ema.admin.modules.system.entity.SysDepartment;
import com.ema.admin.modules.system.entity.SysDepartmentUser;
import com.ema.admin.modules.system.entity.SysUser;
import com.ema.admin.modules.system.mapper.SysDepartmentMapper;
import com.ema.admin.modules.system.service.SysDepartmentService;
import com.ema.admin.modules.system.service.SysDepartmentUserService;
import com.ema.admin.modules.system.service.SysUserService;
import com.ema.admin.modules.system.vo.SysDepartmentQueryVo;
import com.ema.admin.modules.system.vo.SysDepartmentVo;
import com.ema.common.utils.TreeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  部门信息表 服务层实现。
 *
 * @author LIJIAN
 * @since 2026-05-12
 */
@Service
public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper, SysDepartment>  implements SysDepartmentService{

    @Resource
    private SysDepartmentConverter sysDepartmentConverter;
    @Resource
    private SysDepartmentUserService sysDepartmentUserService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysUserConverter sysUserConverter;

    @Override
    public Page<SysDepartment> page(SysDepartmentQueryVo queryVo) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.like(SysDepartment::getDeptCode, queryVo.getDeptCode(), StrUtil.isNotBlank(queryVo.getDeptCode()));
        queryWrapper.like(SysDepartment::getDeptName, queryVo.getDeptName(), StrUtil.isNotBlank(queryVo.getDeptName()));
        queryWrapper.eq(SysDepartment::getParentId, queryVo.getParentId(), queryVo.getParentId() != null);
        queryWrapper.orderBy(SysDepartment::getSort);
        return this.page(new Page<>(queryVo.getPageNumber(), queryVo.getPageSize()), queryWrapper);
    }

    @Override
    public List<SysDepartmentVo> tree() {
        List<SysDepartment> departmentList = this.queryChain()
                .orderBy(SysDepartment::getSort, true).list();
        if (CollUtil.isEmpty(departmentList)) {
            return CollUtil.newArrayList();
        }
        return TreeUtil.buildTree(sysDepartmentConverter.toSysDepartmentVo(departmentList), null);
    }

    @Override
    public List<SysUser> userList(String departmentId) {
        if (StrUtil.isBlank(departmentId)) {
            return Collections.emptyList();
        }
        // 查询部门关联的用户ID列表
        List<SysDepartmentUser> deptUsers = sysDepartmentUserService.queryChain()
                .eq(SysDepartmentUser::getDeptId, departmentId)
                .list();
        if (CollUtil.isEmpty(deptUsers)) {
            return Collections.emptyList();
        }
        List<String> userIds = deptUsers.stream()
                .map(SysDepartmentUser::getUserId)
                .collect(Collectors.toList());
        // 根据用户ID列表查询用户信息
        return sysUserService.queryChain()
                .in(SysUser::getId, userIds)
                .list();
    }

}
