package com.emr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;
import com.emr.entity.Archive_Master_Following;
import com.emr.entity.Archive_Master_Vo;
import com.emr.entity.Power_User;

import javax.servlet.http.HttpServletRequest;

import com.emr.service.Archive_Master_FollowingService;
import com.emr.service.PowerUserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Controller
public class PowerUserController {

    @Autowired
    private PowerUserService powerUserService;
    @Autowired
    private Archive_Master_FollowingService archiveMasterFollowingService;

    @RequestMapping("getPowerUserList")
    @ResponseBody
    public Object getPowerUserList(HttpServletRequest request, String type) {
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        String handleName = user.getUserPosition();//姓名
        request.setAttribute("handleName", handleName);
        JSONArray powerUserList = powerUserService.getPowerUserList(type);
        if (powerUserList == null) {
            return "ERROR";
        } else {
            return powerUserList;
        }
    }


    @RequestMapping("getPowerUserList2")
    @ResponseBody
    public Object getPowerUserList2(HttpServletRequest request, String type) {
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        String handleName = user.getUserPosition();//姓名
        request.setAttribute("handleName", handleName);
        StringBuffer sb = new StringBuffer();
        StringBuffer sb1 = new StringBuffer();
        JSONArray powerUserList = powerUserService.getPowerUserList(type);
        for (int i = 0; powerUserList.size() > i; i++) {
            JSONObject jsonObject = powerUserList.getJSONObject(i);
            String  roleName = jsonObject.getString("name");
                if (sb.length() != 0) {
                    sb.append(",");
                }
                if (!roleName.equals("") && !roleName.equals(null)) {
                sb.append("'" + roleName + "'");
                }
            String  userName = jsonObject.getString("userName");
            if (sb1.length() != 0) {
                sb1.append(",");
            }
            if (!roleName.equals("")&& !roleName.equals(null)) {
                sb1.append("'" + userName + "'");
            }
        }
        String roleName = sb.toString() ;
        String userName = sb1.toString() ;
        List<Archive_Master_Following> nameList = archiveMasterFollowingService.getHandleName(roleName,userName);
            if (nameList == null) {
                return "ERROR";
            } else {
                return nameList;
            }


    }
}