package com.xjrsoft.module.concat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.module.concat.dto.ConcatTreeDto;
import com.xjrsoft.module.concat.service.IXjrUserService;
import com.xjrsoft.module.concat.vo.ConcatTreeVo;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.organization.mapper.DepartmentMapper;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.system.mapper.DictionarydetailMapper;
import com.xjrsoft.module.teacher.entity.XjrUser;
import com.xjrsoft.module.teacher.mapper.XjrUserMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @Author dzx
* @Date: 2024-02-03
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class XjrUserServiceImpl extends MPJBaseServiceImpl<XjrUserMapper, XjrUser> implements IXjrUserService {
    private final XjrUserMapper xjrUserMapper;
    private final DepartmentMapper departmentMapper;
    private final DictionarydetailMapper dictionarydetailMapper;
    @Override
    public List<ConcatTreeVo> getConcatList(ConcatTreeDto dto) {
        List<ConcatTreeVo> voList = new ArrayList<>();

        List<ConcatTreeVo> teacherDeptInfo = xjrUserMapper.getTeacherDeptInfo(dto);
        List<DictionaryDetail> detailList = dictionarydetailMapper.selectList(
            new QueryWrapper<DictionaryDetail>().lambda().eq(DictionaryDetail::getItemId, 2023000000000000004L)
        );
        Map<String, String> dictMap = new HashMap<>();
        for (DictionaryDetail detail : detailList) {
            dictMap.put(detail.getCode(), detail.getName());
        }
        Set<Long> deptIds = new HashSet<>();
        for (ConcatTreeVo concatTreeVo : teacherDeptInfo) {
            deptIds.add(concatTreeVo.getParentId());
        }
        departmentMapper.selectList(
            new QueryWrapper<Department>().lambda()
            .eq(Department::getDeleteMark, DeleteMark.NODELETE.getCode())
        ).forEach((node) -> {
            if(deptIds.contains(node.getId())){
                voList.add(new ConcatTreeVo(){{
                    setId(node.getId());
                    setName(node.getName());
                }});
            }
        });

        for (ConcatTreeVo concatTreeVo : teacherDeptInfo) {
            if(concatTreeVo.getParentId() != null){
                voList.add(new ConcatTreeVo(){{
                    setId(concatTreeVo.getId());
                    setName(concatTreeVo.getName());
                    setParentId(concatTreeVo.getParentId());
                    setGender(dictMap.get(concatTreeVo.getGender()));
                    setMobile(concatTreeVo.getMobile());
                }});
            }
        }

        return voList;
    }
}
