package com.example.hospital.patient.wx.api.service.impl;

import cn.hutool.core.map.MapUtil;
import com.example.hospital.patient.wx.api.db.dao.MedicalDeptDao;
import com.example.hospital.patient.wx.api.service.MedicalDeptService;
import org.springframework.stereotype.Service;

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

@Service
public class MedicalDeptServiceImpl implements MedicalDeptService {
    @Resource
    private MedicalDeptDao medicalDeptDao;

    @Override
    public ArrayList<HashMap> searchMedicalDeptList(Map param) {
        ArrayList<HashMap> list = medicalDeptDao.searchMedicalDeptList(param);
        return list;
    }

    @Override
    public HashMap searchDeptAndSub() {
        ArrayList<HashMap> list = medicalDeptDao.searchDeptAndSub();
        LinkedHashMap map=new LinkedHashMap();
        for(HashMap one:list){
            Integer deptId = MapUtil.getInt(one, "deptId");
            Integer subId = MapUtil.getInt(one, "subId");
            String deptName = MapUtil.getStr(one, "deptName");
            String subName = MapUtil.getStr(one, "subName");
            if(map.containsKey(deptName)){
                ArrayList<HashMap> subList =(ArrayList<HashMap>) map.get(deptName);
                subList.add(new HashMap(){{
                    put("subId",subId);
                    put("subName",subName);
                }});
            }
            else{
                map.put(deptName,new ArrayList<>(){{
                    add(new HashMap<>(){{
                        put("subId",subId);
                        put("subName",subName);
                    }});
                }});
            }
        }
        return map;
    }
}
