package com.goafanti.permission.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.PermissionMapper;
import com.goafanti.common.dao.RolePermissionMapper;
import com.goafanti.common.dao.UserMapper;
import com.goafanti.common.dao.UserRoleMapper;
import com.goafanti.common.model.Permission;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.permission.service.PermissionService;
import com.goafanti.permission.bo.Menu;
import com.goafanti.permission.bo.Navigation;
import com.goafanti.permission.bo.PermissionBo;
import com.goafanti.permission.bo.SubMenu;

@Service
public class PermissionServiceImpl extends BaseMybatisDao<PermissionMapper> implements PermissionService {

	@Autowired
	PermissionMapper		permissionMapper;
	@Autowired
	UserMapper				userMapper;
	@Autowired
	RolePermissionMapper	rolePermissionMapper;
	@Autowired
	UserRoleMapper			userRoleMapper;

	@Override
	public int insert(List<Permission> records) {
		return permissionMapper.insertBatch(records);
	}

	@Override
	public Set<String> findPermissionByUserId(String userId) {
		return permissionMapper.findPermissionByUserId(userId);
	}

	@Override
	public List<Permission> findPermissions() {
		return permissionMapper.findAll();
	}

	@Override
	public int deleteByPrimaryKeys(List<String> ids) {
		rolePermissionMapper.deleteByPermissionId(ids);
		return permissionMapper.deleteByPrimaryKeys(ids);
	}

	@Override
	public int insertn(Permission record) {
		String id = UUID.randomUUID().toString();
		Date now = new Date();
		record.setId(id);
		record.setUrl(record.getUrl());
		record.setName(record.getName());
		String createId=permissionMapper.selectAdminNameById(TokenManager.getAdminId());
		record.setCreateId(createId);
		record.setCreateTime(now);
		record.setUpdateTime(now);
		record.setDeletedSign("0");
		int Count=permissionMapper.selectAutNoCount("PM");
		if(Count<10){
			String aut="PM0"+Count;
			String sid=permissionMapper.selectIdByAutNo(aut);
			while(sid!=null){
				Count=Count+1;				
				if(Count>=10){
					aut="PM"+Count;	
				}else{
					aut="PM0"+Count;
				}
				sid=permissionMapper.selectIdByAutNo(aut);			
			}
			record.setAutNo(aut);
		}
		if(Count>=10){
			String aut="PM"+Count;
			String sid=permissionMapper.selectIdByAutNo(aut);
			while(sid!=null){
				Count=Count+1;				
				aut="PM"+Count;
				sid=permissionMapper.selectIdByAutNo(aut);				
			}
			record.setAutNo(aut);
		}
		record.setHierarchies("1");//1第一层
		record.setStatus("0");//0正常 1解除
		record.setShowOrder("0");//0
		record.setSuperId(permissionMapper.selectIdByName("平台超级模块"));//顶级为PM	
		permissionMapper.insertn(record);
		return 1;
	}

	@Override
	public Permission selectSuperIdById(String id) {
		return permissionMapper.selectSuperIdById(id);
	}

	@Override
	public int insertSubordinate(Permission record) {
		String id = UUID.randomUUID().toString();
		Date now = new Date();
		record.setId(id);
		record.setUrl(record.getUrl());
		record.setName(record.getName());
		String createId=permissionMapper.selectAdminNameById(TokenManager.getAdminId());
		record.setCreateId(createId);
		record.setCreateTime(now);
		record.setUpdateTime(now);
		record.setDeletedSign("0");
		record.setHierarchies("0");//0下层
		record.setStatus("0");//0正常 1解除
		record.setShowOrder("0");//正常
		permissionMapper.insertn(record);
		return 1;
	}

	@Override
	public int deleteById(String id) {
		permissionMapper.deleteById(id);
		return 1;
	}

	@Override
	public Permission selectAllById(String id) {
		Permission permission=permissionMapper.selectAllById(id);
		permission.setSuperId(permissionMapper.selectNameById(permission.getSuperId()));
		return permissionMapper.selectAllById(id);
	}

	@Override
	public List<Permission> selectName(String name) {
		return permissionMapper.selectName(name);
	}

	@Override
	public int updateById(Permission record,String superName) {
		Date now=new Date();
		record.setUpdateTime(now);
		if(superName.equals("平台超级模块")){
			record.setHierarchies("1");
		}else{
			record.setHierarchies("0");
		}
		String autNo;
		permissionMapper.updateById(record);
		//*******下级编号修改
		int n=0;
		List<Permission> Nos =permissionMapper.selectIDNBySuperId(record.getId());
		for(Permission i:Nos){			
			autNo=permissionMapper.selectAutNoByName(i.getSuperId());
			if(n<10){
				String aut=autNo+"0"+n;			
				if(n>=10){
					aut=autNo+n;	
				}else{
					aut=autNo+"0"+n;
				}	
				i.setAutNo(aut);
			}
			if(n>=10){
				String aut=autNo+n;			
				aut=autNo+n;	
				i.setAutNo(aut);
			}
			permissionMapper.updateByNumber(i);
			n++;
			
			//*******下下级编号修改
			int m=0;
			List<Permission> Noss =permissionMapper.selectIDNBySuperId(i.getId());
			for(Permission o:Noss){			
				autNo=permissionMapper.selectAutNoByName(o.getSuperId());
				if(m<10){
					String aut=autNo+"0"+m;			
					if(m>=10){
						aut=autNo+m;	
					}else{
						aut=autNo+"0"+m;
					}	
					o.setAutNo(aut);
				}
				if(m>=10){
					String aut=autNo+m;			
					aut=autNo+m;	
					o.setAutNo(aut);
				}
				permissionMapper.updateByNumber(o);
				m++;
				//*******下下级编号修改
				int z=0;
				List<Permission> Nosss =permissionMapper.selectIDNBySuperId(o.getId());
				for(Permission p:Nosss){			
					autNo=permissionMapper.selectAutNoByName(p.getSuperId());
					if(z<10){
						String aut=autNo+"0"+z;			
						if(z>=10){
							aut=autNo+z;	
						}else{
							aut=autNo+"0"+z;
						}	
						p.setAutNo(aut);
					}
					if(z>=10){
						String aut=autNo+z;			
						aut=autNo+z;	
						p.setAutNo(aut);
					}
					permissionMapper.updateByNumber(p);
					z++;
				}
			}
			
		}
		return permissionMapper.updateById(record);
	}

	@Override
	public Map<String,Object> findPermissionsAll() {
		Map<String,Object> params = new HashMap<String, Object>();
		List<PermissionBo> one=permissionMapper.selectOneAll();	
		for (PermissionBo i:one){
			i.setKey(i.getId());
			List<PermissionBo> two=permissionMapper.selectAllBySuperId(i.getId());
			for(PermissionBo o:two){
				o.setChildren(permissionMapper.selectAllBySuperId(o.getId()));
				o.setKey(o.getId());
				List<PermissionBo> three=permissionMapper.selectAllBySuperId(o.getId());
				for(PermissionBo p:three){
					p.setChildren(permissionMapper.selectAllBySuperId(p.getId()));
					p.setKey(p.getId());					
				}
				o.setChildren(three);
			}
			i.setChildren(two);
		}
		params.put("one", one);
		return params;
	}
	
	@Override
	public List<Navigation> selectNavList(){
		String aid = TokenManager.hasRole(AFTConstants.SUPERADMIN) ? null : TokenManager.getAdminId();
		return permissionMapper.selectNavList(aid);
	}

	@Override
	public List<Menu> selectMenuList(String superId) {
		String aid = TokenManager.hasRole(AFTConstants.SUPERADMIN) ? null : TokenManager.getAdminId();
		List<SubMenu> subMenus = permissionMapper.selectMenuList(superId,aid);
		List<Menu> menus = new ArrayList<Menu>();
		try {
			Menu m = null;
			for(SubMenu sm: subMenus){
				m = new Menu();
				BeanUtils.copyProperties(m, sm);
				menus.add(m);
			}
			if(menus != null && menus.size()>0){
				for(Menu menu: menus){
					subMenus = permissionMapper.selectMenuList(menu.getId(),aid);
					menu.setSubMenus(subMenus);
				}
			}
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return menus;
	}

	@Override
	public String selectTopNavBySubUrl(String subUrl) {
		return permissionMapper.selectTopNavBySubUrl(subUrl);
	}

	@Override
	public boolean checkName(String name,String id) {
		int i=permissionMapper.selectIdByNameCount(name,id);
		if (i>0) {
			return true;
		}
		return false;
	}

}
