package com.hnshituo.core.auth.service.impl;

import java.util.List;

import market.Com.vo.ResultModel;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.mapper.UserRoleMapper;
import com.hnshituo.core.auth.service.UserRoleService;
import com.hnshituo.core.auth.vo.UserRole;

@RemoteService(path = "UserRoleService")
public class UserRoleServiceImpl extends BaseServiceImpl<UserRole, Integer> implements UserRoleService {
	public List<UserRole> doQuery(UserRole userRole){
		List<UserRole> list=this.getMapper(UserRoleMapper.class).doQuery(userRole.getUserId());
		return list;
	}

	public List<UserRole> doQueryUser(String role) {
		List<UserRole> list=this.getMapper(UserRoleMapper.class).doQueryUser(role);
		return list;
	}
	//新增
	public ResultModel doAdd(UserRole userRole){
		ResultModel rm=new ResultModel();
		try{
//			int roleId = userRole.getRoleId();
//			UserRole role = new UserRole();
//			role.setRoleId(roleId);
//			List<UserRole> roleList = this.getMapper(UserRoleMapper.class).find(role, 0, 0);
//			if(roleList.size() > 0){
//				for(UserRole ur : roleList){
//					this.getMapper(UserRoleMapper.class).doDelete(ur);
//				}
//			}
			
				for(UserRole userRoles:userRole.getUserRoleList()){
					if(userRoles.getRoleId()==null||"".equals(userRoles.getRoleId()))
						userRoles.setRoleId(userRole.getRoleId());
					if(this.getMapper(UserRoleMapper.class).count(userRoles) <= 0) {
						int count =this.getMapper(UserRoleMapper.class).findMaxId();
						userRoles.setId(count+1);
						this.getMapper(UserRoleMapper.class).insert(userRoles);
					}	
			}
//			User user = new User();
//			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
//			user.setId(userId);
//			user.setUpdate_time(new Date());
//			this.getMapper(UserMapper.class).update(user);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
		}
		return rm;
	}

	public ResultModel doDelete(UserRole userRole){
		ResultModel rm=new ResultModel();
		try{
			this.getMapper(UserRoleMapper.class).doDelete(userRole);
//			User user = new User();
//			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
//			user.setId(userId);
//			user.setUpdate_time(new Date());
//			this.getMapper(UserMapper.class).update(user);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("删除失败"+e.getMessage());
		}
		return rm;
	}

	public void deleteByRoleId(Integer roleId) {
		this.getMapper(UserRoleMapper.class).deleteByRoleId(roleId);
	}
}
