package com.goafanti.permission.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.dao.RoleMapper;
import com.goafanti.common.model.Role;
import com.goafanti.permission.bo.RoleBo;
import com.goafanti.permission.service.NewRoleService;

@RestController
@RequestMapping("api/admin")
public class NewRoleApiController extends BaseApiController{
	@Resource
	private NewRoleService newRoleService;
	@Autowired
	RoleMapper				roleMapper;
	
	@RequestMapping(value = "/roles", method = RequestMethod.POST)
	public Result roles(RoleBo role, Integer pageNo,
			Integer pageSize) {
		Result res = new Result();
		res.setData(newRoleService.findRoles(role,pageNo,pageSize));
		return res;
	}
	
	@RequestMapping(value = "/role/rolePermission", method = RequestMethod.POST)
	public Result rolePermission(String id){
		Result res = new Result();
		res.setData(newRoleService.selectRolePList(id));
		return res;
	}
	@RequestMapping(value = "/role", method = RequestMethod.POST)
	public Result insertRole(String data) {
		Result res = new Result();
		JSONObject jo = (JSONObject) JSON.parse(data);
		if (jo != null) {
			JSONArray permissions = jo.getJSONArray("permissions");
			List<String> permisionIds = new ArrayList<>();
			for (int idx = 0; idx < permissions.size(); idx++) {
				String pid = permissions.getString(idx);
					permisionIds.add(pid);
			}
			Role records = jo.toJavaObject(Role.class);
			String newName=roleMapper.selectNameById(records.getId());
			if(newName==null){
				String newid=roleMapper.selectIdByName(records.getRoleName());
				if(newid!=null){
					res.getError().add(buildError("","角色已存在"));
					return res;
				}
			}else{
				String newid=roleMapper.selectIdByNamei(records);
				if(newid!=null){
					res.getError().add(buildError("","角色已存在,请更换名称"));
					return res;
				}
			}
			res.setData(newRoleService.insert(records, permisionIds));
		} else {
			res.getError().add(buildError("", "参数格式不正确"));
		}
		return res;
	}

	@RequestMapping(value = "/role/delete", method = RequestMethod.POST)
	public Result deleteRole(String data) {
		Result res = new Result();
		JSONArray ja = (JSONArray) JSON.parse(data);
		
		if (ja != null && !ja.isEmpty()) {
			List<String> records = new ArrayList<>();
			for (int idx = 0; idx < ja.size(); idx++) {
				String rid = ja.getString(idx);
				if (rid != "1") {
					records.add(rid);
				}
			}
			int zz=roleMapper.selectCountByRoleId(records);
			if(zz>0){
				res.getError().add(buildError("","角色被分配不能删除"));
				return res;
			}
			res.setData(newRoleService.deleteByPrimaryKeys(records));
		} else {
			res.getError().add(buildError("", "参数格式不正确"));
		}
		return res;
	}

	@RequestMapping(value = "/role/bindpermission", method = RequestMethod.POST)
	public Result bindPermission(String rid, String pid) {
		Result res = new Result();
		res.setData(newRoleService.bindPermission(rid, pid));
		return res;
	}

	@RequestMapping(value = "/role/binduser", method = RequestMethod.POST)
	public Result bindUser(String rid, String uid) {
		Result res = new Result();
		res.setData(newRoleService.bindUser(rid, uid));
		return res;
	}

	@RequestMapping(value = "/role/unbindpermission", method = RequestMethod.POST)
	public Result unbindPermission(String rid, String pid) {
		Result res = new Result();
		res.setData(newRoleService.bindPermission(rid, pid));
		return res;
	}

	@RequestMapping(value = "/role/unbinduser", method = RequestMethod.POST)
	public Result unbindUser(String rid, String uid) {
		Result res = new Result();
		res.setData(newRoleService.bindUser(rid, uid));
		return res;
	}
}
