package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.mapper.SysDeptMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaEmployeeBo;
import com.ruoyi.oa.domain.vo.OaEmployeeVo;
import com.ruoyi.oa.domain.OaEmployee;
import com.ruoyi.oa.mapper.OaEmployeeMapper;
import com.ruoyi.oa.service.IOaEmployeeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 员工基础信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-23
 */
@RequiredArgsConstructor
@Service
public class OaEmployeeServiceImpl implements IOaEmployeeService {

    private final OaEmployeeMapper baseMapper;
    private final SysDeptMapper deptMapper;

    /**
     * 查询员工基础信息
     */
    @Override
    public OaEmployeeVo queryById(Long employeeId){
        return baseMapper.selectVoById(employeeId);
    }

    /**
     * 查询员工基础信息列表
     */
    @Override
    public TableDataInfo<OaEmployeeVo> queryPageList(OaEmployeeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaEmployee> lqw = buildQueryWrapper(bo);
        Page<OaEmployeeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        // 根据部门id查询部门名称
        if (CollectionUtils.isNotEmpty(result.getRecords())) {
            // 提取所有部门ID
            List<Long> deptIds = result.getRecords().stream()
                    .map(OaEmployeeVo::getDeptId)
                    .filter(Objects::nonNull)
                    .distinct()
                    .collect(Collectors.toList());

            // 批量查询部门信息
            if (!deptIds.isEmpty()) {
                LambdaQueryWrapper<SysDept> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(SysDept::getDeptId, deptIds)
                        .eq(SysDept::getDelFlag, '0'); // 只查询未删除的部门

                List<SysDept> deptList = deptMapper.selectList(queryWrapper);
                Map<Long, String> deptNameMap = deptList.stream()
                        .collect(Collectors.toMap(SysDept::getDeptId, SysDept::getDeptName, (k1, k2) -> k1));

                // 将部门名称设置到对应的VO对象中
                for (OaEmployeeVo vo : result.getRecords()) {
                    if (vo.getDeptId() != null) {
                        vo.setDeptName(deptNameMap.getOrDefault(vo.getDeptId(), "未知部门"));
                    }
                }
            }
        }


        return TableDataInfo.build(result);
    }

    /**
     * 查询员工基础信息列表
     */
    @Override
    public List<OaEmployeeVo> queryList(OaEmployeeBo bo) {
        LambdaQueryWrapper<OaEmployee> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaEmployee> buildQueryWrapper(OaEmployeeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaEmployee> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getEmployeeName()), OaEmployee::getEmployeeName, bo.getEmployeeName());
        lqw.eq(StringUtils.isNotBlank(bo.getCompany()), OaEmployee::getCompany, bo.getCompany());
        return lqw;
    }

    /**
     * 新增员工基础信息
     */
    @Override
    public Boolean insertByBo(OaEmployeeBo bo) {
        OaEmployee add = BeanUtil.toBean(bo, OaEmployee.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEmployeeId(add.getEmployeeId());
        }
        return flag;
    }

    /**
     * 修改员工基础信息
     */
    @Override
    public Boolean updateByBo(OaEmployeeBo bo) {
        OaEmployee update = BeanUtil.toBean(bo, OaEmployee.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OaEmployee entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除员工基础信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
