package com.emr.service.ipml;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.emr.util.HttpClientUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.emr.service.PowerUserService;

import java.util.ArrayList;
import java.util.List;

@Service
public class PowerUserServiceImpl implements PowerUserService {

    @Value("${powerGetUserList}")
    private String powerGetUserList;


    @Override
    public JSONArray getPowerUserList(String type) {
        JSONObject jsonObject = HttpClientUtils.httpGet(powerGetUserList);
        JSONArray users = new JSONArray();
        String code = jsonObject.getString("code");
        if (code.equals("100")) {
            JSONObject data = jsonObject.getJSONObject("extend");
//            return data.toString()
            JSONArray userList = data.getJSONArray("userList");
            if (type.equals("1")) {
                return userList;
            } else {
                for (int i = 0; i < userList.size(); i++) {
                    JSONObject temp = userList.getJSONObject(i);
                    String roleId = temp.getString("roleId");
                    if (roleId.equals("22") || roleId.equals("20")) {
                        users.add(temp);
                    }
                }
                return users;
            }
        }
        return null;
    }
}
