package cn.fiona.pet.account.service;

import cn.fiona.pet.account.entity.Organize;
import cn.fiona.pet.account.entity.Role;
import cn.fiona.pet.account.entity.User;
import cn.fiona.pet.account.exception.ApiException;
import cn.fiona.pet.account.exception.NotFoundException;
import cn.fiona.pet.account.facade.LoginVO;
import cn.fiona.pet.account.repository.RoleDao;
import cn.fiona.pet.account.repository.UserDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.security.utils.Digests;
import org.springside.modules.utils.Encodes;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author baiqw
 */
@Transactional
public class AccountServiceImpl implements AccountService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    private Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Override
    public String login(LoginVO loginVO) throws ApiException {
        User user = userDao.findByLoginName(loginVO.getName());

        if (null == loginVO){
            throw new InvalidParameterException("输入数据为空!");
        }

        if (null == user) {
            throw new NotFoundException(String.format("[%s]用户未找到!", loginVO.getName()));
        }

        String password = loginVO.getPassword();

        if (StringUtils.isBlank(password)){
            throw new InvalidParameterException("密码不能为空!");
        }

        if (!passwordValidation(password, user)){
            throw new InvalidParameterException("密码错误!");
        }

        String token = user.getId();

        return token;
    }

    @Override
    public boolean validateToken(String token) throws ApiException {
        if (StringUtils.isBlank(token)){
            throw new ApiException("token is null!");
        }

        User user = userDao.findOne(token);
        if (null == user){
            throw new ApiException(String.format("%s not exists!", token));
        }

        return true;
    }

    @Override
    @Transactional
    public User getByToken(String token) throws ApiException {
        User user = userDao.findOne(token);

        if (null == user) {
            throw new ApiException(String.format("%s not exists!", token));
        }

        User userVO = new User();
        userVO.setId(user.getId());
        userVO.setName(user.getName());
        userVO.setLoginName(user.getLoginName());

        return userVO;
    }

    @Override
    public List<User> listByRoleCode(String code) {
        Role role = roleDao.findByCode(code);
        List<User> users = new ArrayList<User>();
        for (User user: role.getUsers()){
            User userVO = new User();
            userVO.setName(user.getName());
            userVO.setId(user.getId());
            userVO.setLoginName(user.getLoginName());
            users.add(userVO);
        }

        return users;
    }

    @Override
    public User createUser(User user) {

        Organize organize =  new Organize();
        organize.setId("9b06d376-44ff-4153-9b31-c29a19b8da29");
        user.setOrganize(organize);

        Set<Role> roleSet = new HashSet<Role>();

        for (Role role: user.getRoles()){
            Role r = roleDao.findByCode(role.getCode());
            roleSet.add(r);
        }

        user.setRoles(roleSet);

        encode(user);

        return userDao.save(user);
    }

    private boolean passwordValidation(String password, User user){

        byte[] salt = Encodes.decodeHex(user.getSalt());

        byte[] hashPassword = Digests.sha1(password.getBytes(), salt, 1024);

        return user.getPassword().equals(Encodes.encodeHex(hashPassword));
    }

    private void encode(User user){

        byte[] salt = Encodes.decodeHex(user.getSalt());

        byte[] hashPassword = Digests.sha1(user.getPlainPassword().getBytes(), salt, 1024);

        user.setPassword(Encodes.encodeHex(hashPassword));
    }


}

@@ -577,73 +577,8 @@
 ed;%0A
-import org.springframework.transaction.annotation.Transactional;%0A
 impo
@@ -673,16 +673,56 @@
 codes;%0A%0A
+import javax.transaction.Transactional;%0A
 import j
@@ -3132,32 +3132,51 @@
 %0A%0A    @Override%0A
+    @Transactional%0A
     public User 
@@ -3434,24 +3434,81 @@
 etRoles())%7B%0A
+            logger.debug(%22add role:%7B%7D%22, role.getCode());%0A
             
@@ -3603,89 +3603,368 @@
     
-user.setRoles(roleSet);%0A%0A        encode(user);%0A%0A        return userDao.save(user)
+if (roleSet.size() == 0)%7B%0A            roleSet.add(roleDao.findByCode(%22doctor%22));%0A        %7D%0A%0A        user.setRoles(roleSet);%0A%0A        encode(user);%0A%0A        userDao.save(user);%0A%0A        User userVO = new User();%0A%0A        userVO.setId(user.getId());%0A        userVO.setName(user.getName());%0A        userVO.setLoginName(user.getLoginName());%0A%0A        return userVO
 ;%0A  

modify account