package com.zcloud.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcloud.common.exception.ZException;
import com.zcloud.common.validator.Assert;
import com.zcloud.modules.app.dao.UserDao;
import com.zcloud.modules.app.entity.UserEntity;
import com.zcloud.modules.app.form.LoginForm;
import com.zcloud.modules.app.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

	@Override
	public UserEntity queryByMobile(String mobile) {
		return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("mobile", mobile));
	}

	@Override
	public String login(LoginForm form) {
		UserEntity user = queryByMobile(form.getUsername());
		Assert.isNull(user, "手机号或密码错误");

		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
			throw new ZException("手机号或密码错误");
		}

		return user.getUserId();
	}
}
