//package com.yeejoin.amos.boot.module.jcs.biz.controller;
//
//import java.io.IOException;
//import java.lang.reflect.Field;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Map;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.core.io.ByteArrayResource;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.http.ResponseEntity;
//import org.springframework.util.LinkedMultiValueMap;
//import org.springframework.util.MultiValueMap;
//import org.springframework.util.ObjectUtils;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.client.RestTemplate;
//import org.springframework.web.multipart.MultipartFile;
//import org.typroject.tyboot.core.foundation.enumeration.UserType;
//import org.typroject.tyboot.core.foundation.utils.Bean;
//import org.typroject.tyboot.core.restful.doc.TycloudOperation;
//import org.typroject.tyboot.core.restful.utils.ResponseHelper;
//import org.typroject.tyboot.core.restful.utils.ResponseModel;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.google.common.collect.Maps;
//import com.yeejoin.amos.boot.biz.common.bo.CompanyBo;
//import com.yeejoin.amos.boot.biz.common.bo.DepartmentBo;
//import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
//import com.yeejoin.amos.boot.biz.common.bo.RoleBo;
//import com.yeejoin.amos.boot.biz.common.controller.BaseController;
//import com.yeejoin.amos.boot.module.jcs.biz.service.impl.RemoteSecurityService;
//import com.yeejoin.amos.component.feign.model.FeignClientResult;
//import com.yeejoin.amos.feign.privilege.model.AgencyUserModel;
//import com.yeejoin.amos.feign.privilege.model.CompanyModel;
//import com.yeejoin.amos.feign.privilege.model.DepartmentModel;
//import com.yeejoin.amos.feign.privilege.model.RoleModel;
//import com.yeejoin.amos.feign.systemctl.model.DictionarieValueModel;
//
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//
///**
// * 获取用户信息
// *
// * @author gaodongdong
// */
//@RestController
//@RequestMapping(value = "/user")
//@Api(tags = "用户信息api")
//public class UserController extends BaseController {
//
//	private final Logger logger = LoggerFactory.getLogger(UserController.class);
//
//	@Autowired
//	private RemoteSecurityService remoteSecurityService;		
//	@Autowired
//	private RestTemplate restTemplate;
//	
//	@Value("${security.systemctl.name}")
//	private String systemctl;
//	private static final String appType = "APP";
//	
//	/**
//	 * 获取公司选择信息
//	 */
//	@ApiOperation(value = "获取公司选择信息", notes = "获取公司选择信息")
//	@GetMapping(value = "/selectInfo")
//	 @TycloudOperation(needAuth = true, ApiLevel = UserType.AGENCY)
//	public JSONObject selectInfo() {
//		try {
//			JSONObject result = new JSONObject();
//			AgencyUserModel user = getUserInfo();
//			if (user != null) {
//				List<CompanyModel> list = user.getCompanys();
//				Map<Long, List<DepartmentModel>> mapDepartments = user.getCompanyDepartments();
//				Map<String, List<Map<String, Object>>> mapdate = new HashMap<String, List<Map<String, Object>>>();
//				List<Map> listCompanyModel = new ArrayList();
//				if (list != null && list.size() > 0) {
//					for (CompanyModel article : list) {
//						Map<String, Object> map = objectToMap(article);
//						map.put("companySeq", map.get("sequenceNbr") + "");
//						map.put("parentId", map.get("parentId") + "");
//						listCompanyModel.add(map);
//						long key = Long.valueOf(map.get("sequenceNbr").toString());
//						List<DepartmentModel> listdep = mapDepartments.get(key);
//						List<Map<String, Object>> dep = new ArrayList();
//						for (DepartmentModel departmentModel : listdep) {
//							if (departmentModel != null) {
//								Map<String, Object> mapo = objectToMap(departmentModel);
//								mapo.put("sequenceNbr", mapo.get("sequenceNbr").toString());
//								dep.add(mapo);
//							}
//						}
//						mapdate.put(map.get("sequenceNbr").toString(), dep);
//					}
//				}
//				Map<String, Object> mapRoles = objectToMap(user.getOrgRoles());
//
//				result.put("companys", listCompanyModel);
//				result.put("orgRoles", user.getOrgRoles());
//				result.put("companyDepartments", mapdate);
//				return result;
//			} else {
//				throw new RuntimeException("请重新登录");
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			logger.error("获取公司选择信息异常", e);
//			throw new RuntimeException(e.getMessage());
//		}
//	}
//
//	
//	// 对象转map
//
//	public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
//		Map<String, Object> map = new HashMap<String, Object>();
//		if (obj != null) {
//			Class<?> clazz = obj.getClass();
//			for (Field field : clazz.getDeclaredFields()) {
//				field.setAccessible(true);
//				String fieldName = field.getName();
//				Object value = nvl(field.get(obj));
//
//				if (fieldName.equals("sequenceNbr")) {
//					map.put(fieldName, value + "");
//				} else if (fieldName.equals("companySeq")) {
//					map.put(fieldName, value + "");
//				} else if (fieldName.equals("children")) {
//					map.put(fieldName, value);
//				} else {
//					map.put(fieldName, value);
//				}
//
//			}
//			return map;
//		}
//		return map;
//	}
//
//	public static Object nvl(Object param) {
//		return param != null ? param : "";
//	}
//
//	/**
//	 * 保存登陆用户选择公司信息
//	 */
//	@ApiOperation(value = "保存登陆用户选择公司信息", notes = "保存登陆用户选择公司信息")
//	@PostMapping(value = "/save/curCompany")
//	 @TycloudOperation(needAuth = true, ApiLevel = UserType.AGENCY)
//	public JSONObject saveCurCompany(
//			@ApiParam(value = "当前登陆用户所选单位机构编号", required = true) @RequestBody ReginParams selectUserInfo) {
//		try {
//			AgencyUserModel user = getUserInfo();
//			CompanyBo company = new CompanyBo();
//			DepartmentBo department = new DepartmentBo();
//			RoleBo role = new RoleBo();
//			if(ObjectUtils.isEmpty(selectUserInfo.getCompany())){
//				CompanyModel companyM = user.getCompanys().get(0);
//				Bean.copyExistPropertis(companyM,company);
//				
//				Map<Long, List<DepartmentModel>> mapDepartments = user.getCompanyDepartments();
//				DepartmentModel departmentM = mapDepartments.get(companyM.getSequenceNbr()).get(0);
//				Bean.copyExistPropertis(departmentM,department);
//				Map<Long, List<RoleModel>> roles = user.getOrgRoles();
//				RoleModel roleM = roles.get(departmentM.getSequenceNbr()).get(0);
//				
//				Bean.copyExistPropertis(roleM,role);
//				selectUserInfo.setCompany(company);
//				selectUserInfo.setDepartment(department);
//				selectUserInfo.setRole(role);
//			}else{
//				company = selectUserInfo.getCompany();
//				role = selectUserInfo.getRole();
//				department = selectUserInfo.getDepartment();
//			}
//			ReginParams reginParams = new ReginParams();
//			reginParams.setCompany(company);
//			reginParams.setRole(role);
//			reginParams.setDepartment(department);
//			saveSelectedOrgInfo(reginParams);
//			//保存用户信息
//			saveUser(user);
//		
//			return buildCurCompany(selectUserInfo, user);
//		} catch (Exception e) {
//			e.printStackTrace();
//			logger.error("保存登陆用户选择公司信息异常", e);
//			throw new RuntimeException("系统繁忙，请稍后再试");
//		}
//	}
//	private JSONObject buildCurCompany(ReginParams selectUserInfo, AgencyUserModel user) {
//		JSONObject result = new JSONObject();
//		result.put("userId", user.getUserId());
//		result.put("realName", user.getRealName());
//		result.put("userMobile", user.getMobile());
//		result.put("userName", user.getUserName());
//		result.put("email", user.getEmail());
//
//		ResponseModel secResponse = remoteSecurityService
//				.searchPermissionTree(selectUserInfo.getRole().getSequenceNbr(), appType);
//		List<JSONObject> listp = new ArrayList<>();
//		if (secResponse.getStatus()==HttpStatus.OK.value() && secResponse.getResult() != null) {
//			JSONArray arr = JSON.parseArray(JSONArray.toJSONString(secResponse.getResult()));
//			Map<String, JSONObject> map = Maps.newHashMap();
//
//			if (arr != null && arr.size() > 0) {
//				JSONObject obj = arr.getJSONObject(0);
//				if (obj != null) {
//					JSONArray childrens = obj.getJSONArray("children");
//					if (childrens != null && childrens.size() > 0) {
//						for (int i = 0; i < childrens.size(); i++) {
//							JSONObject child = childrens.getJSONObject(i);
//							// map.put(child.getString("path"), child);
//							listp.add(child);
//						}
//					}
//				}
//
//				// result.put("userPower", map.get("app"));
//			}
//		}
//		result.put("userPower", JSON.toJSON(listp));
//		result.put("companyModel", selectUserInfo.getCompany());
//		result.put("departmentModel", selectUserInfo.getDepartment());
//		result.put("roleModel", selectUserInfo.getRole());
//		return result;
//	}
//
//	
//
//	/**
//	 * 
//	 * 获取字典
//	 * 
//	 */
//
//	@ApiOperation(value = "查询指定的字典信息", notes = "查询指定的字典信息")
//	@GetMapping(value = "listDictionaryByDictCode/{dictCode}")
//	 @TycloudOperation(needAuth = true, ApiLevel = UserType.AGENCY)
//	public List<DictionarieValueModel> listDictionaryByDictCode(@PathVariable String dictCode) {
//		try {
//			List<DictionarieValueModel> list = remoteSecurityService.listDictionaryByDictCode(dictCode);
//
//			return list;
//		} catch (Exception e) {
//
//			throw new RuntimeException();
//		}
//	}
//
//	/**
//	 * 
//	 * 
//	 * 上传图片到平台
//	 * 
//	 **/
//	@ApiOperation(httpMethod = "POST", value = "文件上传(<font color='blue'>release</font>)", notes = "文件上传")
//	@RequestMapping(value = "/upload-file", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//	 @TycloudOperation(needAuth = true, ApiLevel = UserType.AGENCY)
//	public ResponseModel uploadFile(@RequestParam("file") MultipartFile file) {
//
//		FeignClientResult<Map<String, String>> date = remoteSecurityService.fileImage(file);
//		Map<String, String> map1 = new HashMap<>();
//		if (date != null) {
//			Map<String, String> map = date.getResult();
//			Iterator<String> it = map.keySet().iterator();
//
//			while (it.hasNext()) {
//				map1.put("url", it.next());
//			}
//
//		}
//		return ResponseHelper.buildResponse(map1);
//	}
//
//	@ApiOperation(httpMethod = "POST", value = "文件上传(<font color='blue'>release</font>)", notes = "文件上传")
//	@RequestMapping(value = "/upload-files", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//	 @TycloudOperation(needAuth = true, ApiLevel = UserType.AGENCY)
//	public ResponseModel uploadFile(@RequestParam("files") MultipartFile[] files) throws IOException {
//
//		// 设置请求头
//		HttpHeaders headers = new HttpHeaders();
//		MediaType type = MediaType.parseMediaType("multipart/form-data");
//		headers.setContentType(type);
//		headers.set("product", getProduct());
//		headers.set("token", getToken());
//		headers.set("appKey", getAppKey());
//		// 设置请求体，注意是LinkedMultiValueMap
//		MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
//		List<Object> fileList = new ArrayList<>();
//		for (MultipartFile file : files) {
//			ByteArrayResource byteArrayResource = new ByteArrayResource(file.getBytes()) {
//				@Override
//				public String getFilename() throws IllegalStateException {
//					return file.getOriginalFilename();
//				}
//			};
//			fileList.add(byteArrayResource);
//		}
//
//		form.put("files", fileList);
//
//		// 用HttpEntity封装整个请求报文
//		HttpEntity<MultiValueMap<String, Object>> files1 = new HttpEntity<>(form, headers);
//
//		ResponseEntity<String> responseEntity = restTemplate.exchange(
//				"http://" + systemctl + "/systemctl/v1/filestorage/image", HttpMethod.POST, files1, String.class);
//
//		JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
//
//		Map<String, String> map1 = new HashMap<>();
//		if (jsonObject != null) {
//			Map<String, String> map = JSON.parseObject(jsonObject.get("result").toString(), Map.class);
//			Iterator<String> it = map.keySet().iterator();
//			String url = "";
//			while (it.hasNext()) {
//				url = url + it.next() + ",";
//			}
//			map1.put("url", url);
//		}
//		return ResponseHelper.buildResponse(map1);
//	}
//
//}