package com.goafanti.permission.controller;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.dao.PermissionMapper;
import com.goafanti.common.model.Permission;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.permission.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("api/admin")
public class NewPermissionApiController extends BaseApiController {
	@Resource
	private PermissionService permissionService;
	@Autowired
	private PermissionMapper		permissionMapper;
	/**权限管理列表 **/
	@GetMapping(value = "/permissions")
	public Result permissions() {
		Result res = new Result();
		res.setData(permissionService.findPermissionsAll());
		return res;
	}

	/**权限管理最上级新增 **/
	@RequestMapping(value = "/addSupPermission" , method = RequestMethod.POST)
	public Result addSupPermission(Permission record ){
		Result res = new Result();
		if(StringUtils.isBlank(record.getName())){
			res.getError().add(buildError("","权限名不能为空"));
			return res;
		}
		String aut=permissionMapper.selectAutNoByNamei(record.getName());
		if(aut!=null){
			res.getError().add(buildError("","权限已存在"));
			return res;
		}
		res.setData(permissionService.insertn(record));
		return res;
	}
	/**新增上级名称自动生成 **/
	@RequestMapping(value = "/selectSuperIdById" , method = RequestMethod.POST)
	public Permission selectSuperIdById(String id ){
		return permissionService.selectSuperIdById(id);
	}

	/**权限管理新增 **/
	@RequestMapping(value = "/addPermission" , method = RequestMethod.POST)
	public Result addPermission(Permission record ){
		Result res = new Result();
		if(StringUtils.isBlank(record.getName())|| StringUtils.isBlank(record.getUrl())
				|| StringUtils.isBlank(record.getSuperId())){
			res.getError().add(buildError("","名称、路径、上级不能为空"));
			return res;
		}
		String auts=permissionMapper.selectAutNoByNamei(record.getName());
		if(auts!=null){
			res.getError().add(buildError("","权限已存在"));
			return res;
		}
		String supaut=permissionMapper.selectAutNoByName(permissionMapper.selectIdByName(record.getSuperId()));
		if(supaut.length()>=7){
			res.getError().add(buildError("","权限层级超限"));
			return res;
		}
		//*********权限编号判断**************
		record.setSuperId(permissionMapper.selectIdByName(record.getSuperId()));
		String autNo=permissionMapper.selectAutNoByName(record.getSuperId());
		int Count=permissionMapper.selectAutNoCount(record.getSuperId());
		if((Count+1)>99){
			res.getError().add(buildError("","每层最多存在99个子类"));
			return res;
		}
		if(Count<10){
			String aut=autNo+"0"+Count;
			String sid=permissionMapper.selectIdByAutNo(aut);
			while(sid!=null){
				Count=(Count+1)%100;
				if(Count>=10){
					aut=autNo+Count;
				}else{
					aut=autNo+"0"+Count;
				}
				sid=permissionMapper.selectIdByAutNo(aut);
			}
			record.setAutNo(aut);
		}
		if(Count>=10){
			String aut=autNo+Count;
			String sid=permissionMapper.selectIdByAutNo(aut);
			while(sid!=null){
				Count=(Count+1)%100;
				aut=autNo+Count;
				sid=permissionMapper.selectIdByAutNo(aut);
			}
			record.setAutNo(aut);
		}
		res.setData(permissionService.insertSubordinate(record));
		return res;
	}

	/**删除列表信息**/
	@RequestMapping(value = "/deleteById" , method = RequestMethod.POST)
	public Result deleteById(String id){
		Result res = new Result();
		if(StringUtils.isBlank(id)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "id"));
			return res;
		}
		int subordinate=permissionMapper.selectCountBySuperId(id);
		if(subordinate>0){
			res.getError().add(buildError("","存在下级权限不能删除"));
			return res;
		}
		int role_permission=permissionMapper.selectCountByid(id);
		if(role_permission>0){
			res.getError().add(buildError("","权限被分配不能删除"));
			return res;
		}
		res.setData(permissionService.deleteById(id));
		return res;
	}

	/**编辑页面数据读取**/
	@RequestMapping(value = "/selectAllById" , method = RequestMethod.POST)
	public Result selectAllById(String id){
		Result res = new Result();
		res.setData(permissionService.selectAllById(id));
		return res;
	}

	/**上级权限自动补全查询**/
	@RequestMapping(value = "/selectName" , method = RequestMethod.POST)
	public Result selectName(String name){
		Result res = new Result();
		res.setData(permissionService.selectName(name));
		return res;
	}

	/**修改信息**/
	@RequestMapping(value = "/updateById" , method = RequestMethod.POST)
	public Result updateById(Permission record ){
		Result res = new Result();
		if(StringUtils.isBlank(record.getName())|| StringUtils.isBlank(record.getUrl())
				|| StringUtils.isBlank(record.getSuperId())){
			res.getError().add(buildError("","名称、路径、上级不能为空"));
			return res;
		}
		if(permissionService.checkName(record.getName(),record.getId())){
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR,"名称"));
			return res;
		}

		String pname=permissionMapper.selectAutNoByNamei(record.getSuperId());
		if(StringUtils.isBlank(pname)){
			res.getError().add(buildError("","上级权限不匹配"));
			return res;
		}
		String supaut=permissionMapper.selectAutNoByNamei(record.getSuperId());
		if(supaut.length()>=7){
			res.getError().add(buildError("","权限层级超限"));
			return res;
		}
		int subordinate=permissionMapper.selectCountBySuperId(record.getId());
		if(subordinate>0){
			String autNo=permissionMapper.selectAutNoByNamei(record.getSuperId());
			if(autNo.length()>=5){
				res.getError().add(buildError("","权限层级超限"));
				return res;
			}

		}
		//*********权限编号判断**************
		String superName=record.getSuperId();//上级名称
		record.setSuperId(permissionMapper.selectIdByName(record.getSuperId()));
		String autNo=permissionMapper.selectAutNoByName(record.getSuperId());
		int Count=permissionMapper.selectAutNoCount(record.getSuperId());
		if((Count+1)>99){
			res.getError().add(buildError("","每层最多存在99个子类"));
			return res;
		}
		if(Count<10){
			String aut=autNo+"0"+Count;
			String sid=permissionMapper.selectIdByAutNo(aut);
			while(sid!=null){
				Count=(Count+1)%100;
				if(Count>=10){
					aut=autNo+Count;
				}else{
					aut=autNo+"0"+Count;
				}
				sid=permissionMapper.selectIdByAutNo(aut);
			}
			record.setAutNo(aut);
		}
		if(Count>=10){
			String aut=autNo+Count;
			String sid=permissionMapper.selectIdByAutNo(aut);
			while(sid!=null){
				Count=(Count+1)%100;
				aut=autNo+Count;
				sid=permissionMapper.selectIdByAutNo(aut);
			}
			record.setAutNo(aut);
		}
		res.setData(permissionService.updateById(record,superName));
		return res;
	}

	/**
	 * 查询导航
	 * @return
	 */
	@RequestMapping(value = "/selectNavList",method=RequestMethod.GET)
	public Result selectNavList(){
		Result res = new Result();
		res.setData(permissionService.selectNavList());
		return res;
	}

	/**
	 * 查询菜单
	 * @param navId
	 * @return
	 */
	@RequestMapping(value = "/selectMenuList",method=RequestMethod.GET)
	public Result selectMenuList(String navId){
		Result res = new Result();
		if(StringUtils.isBlank(navId)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"导航栏编号"));
			return res;
		}
		res.setData(permissionService.selectMenuList(navId));
		return res;
	}
}
