package com.blueland.ebpm.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueland.bpm.identity.RoleService;
import com.blueland.bpm.identity.model.Userrole;

@RestController
@RequestMapping({ "/role" })
public class RoleController {
	@Autowired
	private RoleService roleService;
	
	/**
	 * 批量添加用户
	 * @param userroles
	 * @return
	 */
	@RequestMapping(value = { "/addUsers" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST }, produces = { "application/json;charset=UTF-8" })
	public Map<String, Object> addUsers(@RequestBody List<Userrole> userroles) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Userrole> userroleMeassages = new ArrayList();
		List<String> addSuccessUsers = new ArrayList();
		String addSuccessUser = "";
		List<String> addErrorMessages = new ArrayList();
		List<String> addErrorUsers = new ArrayList();
		String addErrorUser = "";
		String addEerrorMessage = "";
		for (Userrole userrole : userroles) {
			Userrole userroleMeassage = userRoleMessage(userrole);
			if(userroleMeassage == userrole){
				Boolean addFlag = false;
				try{
				addFlag = roleService.addUsers2Role(userrole);
				}catch(Exception e){
					addErrorUser = userrole.getUserId();
					addErrorUsers.add(addErrorUser);
					addEerrorMessage = e.getMessage();
					addErrorMessages.add("add user "+userrole.getUserId()+" to "+ userrole.getRoleId() + " error"+addEerrorMessage);
					e.printStackTrace();
				}
				if(true == addFlag){
					addSuccessUser = userrole.getUserId();
					addSuccessUsers.add(addSuccessUser);
				}else{
					addErrorUser = userrole.getUserId();
					addErrorUsers.add(addErrorUser);
				}
			}else{
				userroleMeassages.add(userroleMeassage);
//				addEerrorMessage = userrole.getUserId();
//				addErrorMessages.add(addSuccessMessage);
				addErrorUser = userrole.getUserId();
				addErrorUsers.add(addErrorUser);
			}
		}
		map.put("addSuccessUsers", addSuccessUsers);
		map.put("addErrorMeassage", addErrorMessages);
		map.put("addErrorReason", userroleMeassages);
		map.put("addErrorUsers", addErrorUsers);
		//Gson gson = new Gson();
        //String postData = gson.toJson(map);
        if(addErrorMessages.size() == 0 && userroleMeassages.size() == 0){
        	//Map<String, Object> statusMap = new HashMap<String, Object>();
        	map.clear();
        	map.put("status", "success");
        	//postData = gson.toJson(statusMap);
        }else{
        	map.put("status", "failure");
        	//postData = gson.toJson(map);
        }
		return map;
	}
	

	
	/**
	 * 批量删除用户
	 * @param userroles
	 * @return
	 */
	@RequestMapping(value = { "/delUsers" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST }, produces = { "application/json;charset=UTF-8" })
	public Map<String, Object> delUsers(@RequestBody List<Userrole> userroles) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Userrole> userroleMeassages = new ArrayList();
		List<String> delSuccessUsers = new ArrayList();
		String delSuccessUser = "";
		List<String> delEerrorMessages = new ArrayList();
		String delEerrorMessage = "";
		List<String> delEerrorUsers = new ArrayList();
		String delEerrorUser = "";
		for (Userrole userrole : userroles) {
			Userrole userroleMeassage = userRoleMessage(userrole);
			if(userroleMeassage == userrole){
				int addFlag = 0;
				try{
				addFlag = roleService.delUsers2Role(userrole);
				}catch(Exception e){
					delEerrorUser = userrole.getUserId();
					delEerrorUsers.add(delEerrorUser);
					delEerrorMessage = e.getMessage();
					delEerrorMessages.add("delete user "+userrole.getUserId()+" to "+ userrole.getRoleId() + " error"+delEerrorMessage);
					e.printStackTrace();
				}
				if(1 == addFlag){
					delSuccessUser = userrole.getUserId();
					delSuccessUsers.add(delSuccessUser);
				}else{
					delEerrorUser = userrole.getUserId();
					delEerrorUsers.add(delEerrorUser);
				}
			}else{
				userroleMeassages.add(userroleMeassage);
				delSuccessUser = userrole.getUserId();
				delSuccessUsers.add(delSuccessUser);
			}
		}
		map.put("delSuccessUsers", delSuccessUsers);
		map.put("delErrorMeassage", delEerrorMessages);
		map.put("delErrorReason", userroleMeassages);
		map.put("delEerrorUsers", delEerrorUsers);
		//Gson gson = new Gson();
        //String postData = gson.toJson(map);
        if(delEerrorMessages.size() == 0 && userroleMeassages.size() == 0){
        	map.clear();
        	map.put("status", "success");
        }else{
        	map.put("status", "failure");
        	//postData = gson.toJson(map);
        }
		return map;
	}
	
	public Userrole userRoleMessage(Userrole userrole){
		//Userrole userroleMeassage = new Userrole();
		if(null != userrole){
			if(!StringUtils.isNotEmpty(userrole.getUserCode())){
				userrole.setUserCode("isNull");
			}
			if(!StringUtils.isNotEmpty(userrole.getUserId())){
				userrole.setUserId("isNull");
			}
			if(!StringUtils.isNotEmpty(userrole.getUserName())){
				userrole.setUserName("isNull");
			}
			if(!StringUtils.isNotEmpty(userrole.getOrgId())){
				userrole.setOrgId("isNull");
			}
			if(!StringUtils.isNotEmpty(userrole.getOrgName())){
				userrole.setOrgName("isNull");
			}
			if(!StringUtils.isNotEmpty(userrole.getRoleId())){
				userrole.setRoleId("isNull");
			}
			if(!StringUtils.isNotEmpty(userrole.getRoleAppId())){
				userrole.setRoleAppId("isNull");
			}			
		}
		return userrole;
	}
}
