package com.ssm.service.impl.system;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dgtis.sso.model.UserModel;
import com.github.pagehelper.PageHelper;
import com.ssm.mapper.system.SysUserMapper;
import com.ssm.model.system.SysUser;
import com.ssm.model.system.SysUserExample;
import com.ssm.service.system.SysUserService;
import com.ssm.util.page.BeanUtil;
import com.ssm.util.page.PagedResult;

@Service
public class SysUserServiceImpl implements SysUserService{
	
	@Autowired
	private SysUserMapper userMapper;
	
	@Override
	public SysUser findById(int id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public SysUser findByEmployeenoAndPasswod(String employeeno, String password) {
		return userMapper.findByEmployeenoAndPasswod(employeeno, password);
	}

	@Override
	public PagedResult<SysUser> findAllByExample(SysUserExample example, Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNo, pageSize);
		List<SysUser> list = userMapper.selectByExample(example);
		PagedResult<SysUser> pageresult = BeanUtil.toPagedResult(list);
		return pageresult;
	}

	@Override
	public int updateByPrimaryKey(SysUser record) {
		return userMapper.updateByPrimaryKey(record);
	}

	@Override
	public PagedResult<SysUser> findAllByContidion(String username, String employeeno,
			Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNo, pageSize);
		List<SysUser> list = userMapper.selectByCondition(username, employeeno);
		PagedResult<SysUser> pageresult = BeanUtil.toPagedResult(list);
		return pageresult;
	}

	@Override
	public int insertSelective(SysUser record) {
		return userMapper.insertSelective(record);
	}

	@Override
	public int updateByPrimaryKeySelective(SysUser record) {
		return userMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public SysUser findByEmployeeno(String employeeno) {
		return userMapper.findByEmployeeno(employeeno);
	}

	@Override
	public int deleteUserRole(Integer userid) {
		return userMapper.deleteUserRole(userid);
	}

	@Override
	public int insertUserRole(Integer userid, Integer roleid) {
		return userMapper.insertUserRole(userid, roleid);
	}

	@Override
	public int deleteUser(Integer userid) {
		return userMapper.deleteByPrimaryKey(userid);
	}

	@Override
	public int selectPerssion(Integer userid, String menuUrl) {
		return userMapper.selectPerssion(userid, menuUrl);
	}

	@Override
	public List<SysUser> selectAllUser() {
		return userMapper.selectAllUser();
	}

	@Override
	public void insertUserSave(UserModel userModel) {
			SysUser user = new SysUser();
			user.setEmployeeno(userModel.getLoginName());
			user.setPassword(userModel.getPassword());
			user.setMobilenumber(userModel.getLoginName());
			user.setUsername(userModel.getRealName());
			user.setEmailaddress(userModel.getEmali());
			user.setIsdeleted(false);
			user.setIsenabled(false);
			user.setCreateddatetime(new Date());
			userMapper.insertSelective(user);
			
			if(userModel.isAdmin()){
				userMapper.insertUserRole(user.getUserid() ,1);
			} else {
				userMapper.insertUserRole(user.getUserid() ,2);
			}
			
		}

	@Override
	public int selectUserRoleByUserIdAndRoldId(Integer userid, Integer roldid) {
		return userMapper.selectUserRoleByUserIdAndRoldId(userid, roldid);
	}

}
