/**
* 文件：RoleController.java
* 版本：1.0.0
* 日期：
* Copyright &reg; 
* All right reserved.
*/
package com.mortals.xhx.base.system.role.web;


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.common.code.UserType;
import com.mortals.framework.model.Context;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.role.model.RoleEntity;
import com.mortals.xhx.base.system.role.model.RoleUserEntity;
import com.mortals.xhx.base.system.role.service.RoleService;
import com.mortals.xhx.base.system.role.service.RoleUserService;
import com.mortals.xhx.common.code.RoleType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: 角色信息</p>
 * <p>Description: RoleController  </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 * @author 
 * @version 1.0.0
 */
@RestController
@RequestMapping("role")
public class RoleController extends BaseCRUDJsonBodyMappingController<RoleService,RoleEntity,Long> {

	@Autowired
	private RoleUserService roleUserService;
	
	public RoleController(){
		super.setFormClass(RoleForm.class);
		super.setModuleDesc("角色信息");
	}

	@Override
	protected void init(Map<String, Object> model, Context context) {
		this.addDict(model,"userType", IBaseEnum.getEnumMap(UserType.class));
		this.addDict(model,"roleType", RoleType.getEnumMap());
		super.init(model, context);
	}
	
	@PostMapping("roleNameByUserId")
    public String getRoleNameByUserId(@RequestBody RoleUserEntity query) {
		try{
			JSONObject ret = new JSONObject();
			RoleUserEntity roleUserEntity = new RoleUserEntity();
			roleUserEntity.setUserId(query.getUserId());
			List<RoleUserEntity> find = roleUserService.find(roleUserEntity, new Context());
			List<RoleEntity> model = new ArrayList<>(); 
			if(find.size() > 0){
				for (RoleUserEntity roleUser : find) {
					RoleEntity entity = service.get(roleUser.getRoleId(), new Context());
					model.add(entity);
				}
			}
			ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
	        ret.put(KEY_RESULT_DATA, model);
	        return ret.toJSONString();
        } catch (Exception e) {
             log.error("查询用户角色错误", e);
             JSONObject ret = new JSONObject();
             ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
             ret.put(KEY_RESULT_MSG, super.convertException(e));
             return ret.toJSONString();
         }
	}
	
}