package com.cim.idm.controller;

import com.alibaba.fastjson.JSONObject;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.response.BaseResponse;
import com.cim.idm.response.RespGenerator;
import com.cim.idm.service.Impl.QueryServiceImpl;
import com.cim.idm.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.RestController;

import java.util.List;
import java.util.Map;

@RestController
@EnableAutoConfiguration
@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private QueryServiceImpl queryServiceImpl;

	@RequestMapping(value = "/getRoleOfPermission", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> getRoleOfPermission(@RequestBody Map<String,Object> param)
	{
		PageResult<Map<String,Object>> mapList = new PageResult<Map<String,Object>>();
		String queryId = param.get("queryId").toString();
		String version = param.get("version").toString();
		int pageNum = (int) param.get("pageNum");
		int pageSize = (int) param.get("pageSize");
		Map bindMap = (Map) param.get("params");
		JSONObject head = new JSONObject(true);
		try {
			mapList = queryServiceImpl.getQueryResultPage(queryId, version, bindMap, pageNum, pageSize);
			head.put("roleList", mapList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		BaseResponse returnOK = RespGenerator.returnOK(head);
        return returnOK;
	}
	
	@RequestMapping(value = "/getRole", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> getRole(@RequestBody Map<String,Object> param)
	{
		PageResult<Map<String,Object>> mapList = new PageResult<Map<String,Object>>();
		String queryId = param.get("queryId").toString();
		String version = param.get("version").toString();
		int pageNum = (int) param.get("pageNum");
		int pageSize = (int) param.get("pageSize");
		Map bindMap = (Map) param.get("params");
		JSONObject head = new JSONObject(true);
		try {
			mapList = queryServiceImpl.getQueryResultPage(queryId, version, bindMap, pageNum, pageSize);
			head.put("roleList", mapList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		BaseResponse returnOK = RespGenerator.returnOK(head);
        return returnOK;
	}
	
	@RequestMapping(value = "/getOrg", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> getOrg(@RequestBody Map<String,Object> param)
	{
		PageResult<Map<String,Object>> mapList = new PageResult<Map<String,Object>>();
		String queryId = param.get("queryId").toString();
		String version = param.get("version").toString();
		int pageNum = (int) param.get("pageNum");
		int pageSize = (int) param.get("pageSize");
		Map bindMap = (Map) param.get("params");
		JSONObject head = new JSONObject(true);
		try {
			mapList = queryServiceImpl.getQueryResultPage(queryId, version, bindMap, pageNum, pageSize);
			head.put("orgList", mapList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		BaseResponse returnOK = RespGenerator.returnOK(head);
        return returnOK;
	}
	
	@RequestMapping(value = "/addRole", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> addRole(@RequestBody Map<String,Object> param){
				
		String siteName = (String)param.get("siteName");
		String userGroupName = (String)param.get("userGroupName");
		String description = (String)param.get("description");
		String remark = (String)param.get("remark");
		String accessFactory = (String)param.get("accessFactory");
		String status = (String)param.get("status");
		String createUser = (String)param.get("createUser");
		String createTime = (String)param.get("createTime");
		String lastUpdateUser = (String)param.get("lastUpdateUser");
		String lastUpdateTime = (String)param.get("lastUpdateTime");
		
		String checkDuplicateSql = "SELECT COUNT(*) FROM USERGROUP WHERE USERGROUPNAME = ?";
		Long count = IDMFrameServiceProxy.getSqlTemplate().queryForObject(checkDuplicateSql, Long.class, userGroupName);
		if(count > 0) {
			BaseResponse returnError = RespGenerator.returnError("该数据已存在!");
			return returnError;
		}
		
		String sql = "INSERT INTO USERGROUP (SITENAME, USERGROUPNAME, DESCRIPTION, REMARK, ACCESSFACTORY, " +  
                "STATUS, CREATEUSER, CREATETIME, LASTUPDATEUSER, LASTUPDATETIME) " +  
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";  

		Object[] args = new Object[]{siteName, userGroupName, description, remark, accessFactory, status,  
				createUser, createTime, createTime, lastUpdateTime};  

		IDMFrameServiceProxy.getSqlTemplate().update(sql, args);
		BaseResponse returnOK = RespGenerator.returnOK(param);
        return returnOK;

	}
	
	@RequestMapping(value = "/saveRoleMenu", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> saveRoleMenu(@RequestBody Map<String,Object> param){
				
		String userGroupName = (String)param.get("userGroupName");
		List menuIdList = (List)param.get("menuIdList");
		
		
		String deleteSql = "DELETE FROM USERGROUPMENU WHERE USERGROUPNAME = ?";
		IDMFrameServiceProxy.getSqlTemplate().update(deleteSql, userGroupName);
		
		for(Object menuId : menuIdList) {
			String sql = "INSERT INTO USERGROUPMENU (USERGROUPNAME, MENUID) " +  "VALUES (?, ?)";  
			Object[] args = new Object[]{userGroupName, menuId};  
			IDMFrameServiceProxy.getSqlTemplate().update(sql, args);
		}
		BaseResponse returnOK = RespGenerator.returnOK(param);
        return returnOK;

	}
	
	@RequestMapping(value = "/editRole", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> editRole(@RequestBody Map<String,Object> param){
		
		String siteName = (String)param.get("siteName");
		String userGroupName = (String)param.get("userGroupName");
		String description = (String)param.get("description");
		String remark = (String)param.get("remark");
		String accessFactory = (String)param.get("accessFactory");
		String status = (String)param.get("status");
		String createUser = (String)param.get("createUser");
		String createTime = (String)param.get("createTime");
		String lastUpdateUser = (String)param.get("lastUpdateUser");
		String lastUpdateTime = (String)param.get("lastUpdateTime");

		String sql = "UPDATE USERGROUP " +  
                "SET SITENAME = ?, DESCRIPTION = ?, REMARK = ?, ACCESSFACTORY = ?, " +  
                "STATUS = ?, CREATEUSER = ?, CREATETIME = ?, LASTUPDATEUSER = ?, LASTUPDATETIME = ? " +  
                "WHERE USERGROUPNAME = ?";  

		Object[] args = new Object[]{siteName, description, remark, accessFactory, status,  
				createUser, createTime, createTime, lastUpdateTime, userGroupName}; 
		
		IDMFrameServiceProxy.getSqlTemplate().update(sql, args);
		BaseResponse returnOK = RespGenerator.returnOK(param);
        return returnOK;
        
	}
	
	@RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> deleteRole(@RequestBody Map<String,Object> param)
	{
		String userGroupName = (String)param.get("userGroupName");
		String verifySql = "SELECT COUNT(*) FROM USERGROUPLINK WHERE USERGROUPNAME = ?";
		Long count = IDMFrameServiceProxy.getSqlTemplate().queryForObject(verifySql, Long.class, userGroupName);
		
		if(count > 0) {
			BaseResponse returnError = RespGenerator.returnError("该角色已被用户绑定，请先解绑!");
			return returnError;
		}
		IDMFrameServiceProxy.getTransactionManager().beginTransaction();
		try {
			String sql1 = "DELETE FROM USERGROUP WHERE USERGROUPNAME = ?";
			IDMFrameServiceProxy.getSqlTemplate().update(sql1, userGroupName);
			
			String sql2 = "DELETE FROM USERGROUPMENU WHERE USERGROUPNAME = ?";
			IDMFrameServiceProxy.getSqlTemplate().update(sql2, userGroupName);
			
			IDMFrameServiceProxy.getTransactionManager().commitTransaction();
		}catch(Exception e) {
			IDMFrameServiceProxy.getTransactionManager().rollbackTransaction();
		}
		
		BaseResponse returnOK = RespGenerator.returnOK(param);
        return returnOK;
	}
	
	
	

}