package com.ssj.service.sys.role.service.impl;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.role.domain.AdminRole;
import com.ssj.bean.sys.role.domain.Role;
import com.ssj.bean.sys.role.domain.RoleMenu;
import com.ssj.dao.sys.role.dao.RoleDao;
import com.ssj.dao.sys.role.dao.RoleQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class RoleServiceImpl  extends BaseServiceImpl<Role, String> implements RoleService {
	
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private RoleQueryDao roleQueryDao;

	@Override
	public PagingAndSortingRepository<Role, String> getDao() {
		return roleDao;
	}

	@Override
	public Page<Map<String, Object>> findForPage(
			Map<String, Object> searchParams, Pageable pageable) {
		return roleQueryDao.findForPage(searchParams, pageable);
	}

	@Override
	public boolean validateRole(String name, String id) {
		Integer i=roleDao.validateRole(name, id);
		if(i>0){
			return false;
		}
		return true;
	}

	@Override
	public List<String> getRoleMenu(String roleId) {
		return roleDao.getRoleMenu(roleId);
	}

	@Override
	public Response addRoleCompetence(String id, String[] menuIds) {
		Response response = new Response();
		roleDao.deleteRoleMenu(id);
		  if(menuIds != null && menuIds.length>0){
			  List<RoleMenu> list =new ArrayList<RoleMenu>();
              for(int x=0; x< menuIds.length; x++){
            	  RoleMenu roleMenu= new  RoleMenu();
            	  roleMenu.setRoleId(id);
            	  roleMenu.setMenuId(menuIds[x]);
            	  roleMenu.setCreateTime(new Date());
            	  list.add(roleMenu);
              }
              roleQueryDao.batchInsertRoleMenu(list);
          }
		  response.success();
		  
		return response;
	}
	@Override
	public Response saveAdminRole(String id, String[] roleIds) {
		Response response = new  Response();
		roleDao.deleteAdminRole(id);
		if(roleIds != null && roleIds.length>0){
			List<AdminRole> list =new ArrayList<AdminRole>();
			for(int x=0; x< roleIds.length; x++){
				AdminRole adminRole= new  AdminRole();
				adminRole.setAdminId(id);
				adminRole.setRoleId(roleIds[x]);
				adminRole.setCreateTime(new Date());
				list.add(adminRole);
			}
			roleQueryDao.batchInsertAdminRole(list);
		}
		response.success();
		
		return response;
	}

	@Override
	public Response deleteRole(String id) {
		Response response = new  Response();
		roleDao.deleteRoleMenu(id);
		this.delete(id);
    	response.success();
		return response;
	}

	@Override
	public List<String> getAdminRole(String adminId) {
		return roleDao.getAdminRole(adminId);
	}


	public Response deleteWxRole(String id) {
		Response response = new  Response();
		roleDao.deleteWxRole(id);
    	response.success();
		return response;
	}

	@Override
	public void saveAdminRole(String adminId,String roleId) {
		    List<String> listRole =roleDao.getAdminRole(adminId);
		    if(listRole.size()==0){
		    	List<AdminRole> list =new ArrayList<AdminRole>();
		    	AdminRole adminRole= new  AdminRole();
		    	adminRole.setAdminId(adminId);
		    	adminRole.setRoleId(roleId);
		    	adminRole.setCreateTime(new Date());
		    	list.add(adminRole);
		    	roleQueryDao.batchInsertAdminRole(list);
		    }
	}

	@Override
	public Page<Map<String, Object>> findForPushPage(Map<String, Object> params, Pageable initPage) {
		
		return roleQueryDao.findForPushPage(params,initPage);
	}
}
