package me.zhengjie.modules.dm.user.util;

import me.zhengjie.modules.dm.user.service.dto.DmUserDto;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.SpringContextHolder;

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

public class DmUserUtils {

    public static DmUserDto getCurrDmUser(){
        UserService userService = SpringContextHolder.getBean(UserService.class);
        String userId = SecurityUtils.getCurrentUserId();
        //获取账号关联的DM用户
        UserDto userDto = userService.findById(userId);
        DmUserDto dmUser = userDto.getDmUser();
        //测试
        /*DmUserDto dmUser = new DmUserDto();
        dmUser.setId(6L);
        dmUser.setIsApprover(true);*/
        return dmUser;
    }

    /**
     * query查询结果转换为Map对象集合
     * @param queryResult
     * @param columns 字段名称,要与返回的Object数组个数对上
     * @return
     */
    public static List<Map<String,String>> queryResultToMap(List<Object[]> queryResult,String columns){
        List<Map<String,String>> mapList = new ArrayList<>();
        String[] col_arr = columns.split(",");
        for(Object[] arr : queryResult){
            Map<String,String> map = new HashMap<>();
            for(int i = 0; i < arr.length; i++){
                map.put(col_arr[i],String.valueOf(arr[i]));
            }
            mapList.add(map);
        }
        return mapList;
    }
}
