package com.emr.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.emr.vo.User;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/11/7 14:50
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/11/7 14:50
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Component
public class CommomService {
    @Value("${POWER_URLHEAD}")
    private  String POWER_URLHEAD;
    //获取所属医院用户名集合
    public static String creaters(HttpServletRequest request){
        List<User> userList = (List<User>)request.getSession().getAttribute("USER_LIST");
        String creaters = "";
        for (int i = 0; i < userList.size(); i++) {
            if(userList.get(i).getRoleId() != 0){
                if(i != userList.size() - 1){
                    creaters += "'"+userList.get(i).getUserName() + "',";
                }else{
                    creaters += "'"+userList.get(i).getUserName()+ "'";
                }
            }
        }
        if(creaters.substring(creaters.length()-1,creaters.length()).equals(",")){
            creaters = creaters.substring(0,creaters.length()-1);
        }
        return creaters;
    }

    //获取所属医院用户id
    public static String userIds(HttpServletRequest request){
        List<User> userList = (List<User>)request.getSession().getAttribute("USER_LIST");
        String userIds = "";
        for (int i = 0; i < userList.size(); i++) {
            if(userList.get(i).getRoleId() != 0){
                if(i != userList.size() - 1){
                    userIds += "'"+userList.get(i).getUserId() + "',";
                }else{
                    userIds += "'"+userList.get(i).getUserId()+ "'";
                }
            }
        }
        if(userIds.substring(userIds.length()-1,userIds.length()).equals(",")){
            userIds = userIds.substring(0,userIds.length()-1);
        }
        return userIds;
    }

    public  List<User> getUserList(String userName,HttpServletRequest request){
        List<User> userList = new ArrayList<>();
        try {
            //查询缓存时候存在
            userList = (List<User>)request.getSession().getAttribute("USER_LIST");
            if(null == userList || userList.isEmpty()){
                //调用接口查询
                String resultString = "";
                // 创建uri
                String url = POWER_URLHEAD+"/font/getUserList?userName="+userName;
                // 执行请求
                CloseableHttpResponse response = HttpClients.createDefault().execute(new HttpGet(url));
                // 判断返回状态是否为200
                if (response.getStatusLine().getStatusCode() == 200) {
                    resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
                }
                if(StringUtils.isNoneBlank(resultString)) {
                    JSONObject jsonObject = JSONObject.fromObject(resultString);
                    String extend = jsonObject.getString("extend");
                    JSONObject extendObject = JSONObject.fromObject(extend);
                    String userList1 = extendObject.getString("userList");
                    userList = (List<User>) JSON.parseArray(userList1, User.class);
                }
                //设置进session
                request.getSession().setAttribute("USER_LIST",userList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return userList;
    }
}
