package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.dto.DepartMentDto;
import com.ruoyi.system.mapper.DepartmentMapper;
import com.ruoyi.system.service.DepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Department)表服务实现类
 *
 * @author makejava
 * @since 2024-05-27 17:35:59
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentMapper departmentDao;


    @Override
    public List<Map<String, Object>> getDepartments(String departmentName) {

        List<DepartMentDto> departments = departmentDao.getDepartments(departmentName);
        if (departments.isEmpty()){
            return null;
        }
        int departmentId = -123;
        List<DepartMentDto> copy = null;
        List<String> ids = null;
        List<String> names = null;
        List<String> phones = null;
        List<Map<String, Object>> maps = new ArrayList<>();
        int i = 0;
        for (DepartMentDto department : departments) {
            if (!department.getDepartmentId().equals(departmentId)) {
                departmentId = department.getDepartmentId();
                if (i!=0){
                    Map<String, Object> map = new HashMap<>();
                    map.put("DepartMentDto", copy.get(0));
                    map.put("kgyIds", ids);
                    map.put("kgyNames", names);
                    map.put("phones", phones);
                    maps.add(map);
                }
                copy = new ArrayList<>();
                ids = new ArrayList<>();
                names = new ArrayList<>();
                phones = new ArrayList<>();
            }
            department.setAssetNum(departmentDao.getDepartmentAssetNum(department.getDepartmentId()));
            copy.add(department);
            ids.add(Convert.toStr(department.getDutyId()));
            names.add(department.getPersonName());
            phones.add(department.getTelPhone());
            i++;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("DepartMentDto", copy.get(0));
        map.put("kgyIds", ids);
        map.put("kgyNames", names);
        map.put("phones", phones);
        maps.add(map);

        return maps;
    }
}
