package common.service.impl;

import common.model.Auth;
import common.model.Code;
import common.model.User;
import common.repository.AuthRepository;
import common.repository.CodeRepository;
import common.repository.UserRepository;
import common.service.UserService;
import dic.RoleEnum;
import dic.RoleTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.CharacterUtils;
import util.CipherUtil;
import util.MailUtils;

import java.util.*;

/**
 * Created by mxq on 2017/12/21.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AuthRepository authRepository;
    @Autowired
    private CodeRepository codeRepository;

    @Override
    public User create(User login, User resource) {

        if(!login.getRole().equals(RoleEnum.MANAGER.getKey())){//非管理员只能创建本部门普通账户
            resource.setRole(login.getRole());
            resource.setRoleType(RoleTypeEnum.COMMON.getKey());
        }
        resource.setPassword(CipherUtil.generatePassword("111111"));
        resource.setCreateTime(new Date());
        resource.setCreateAccount(login.getId());
        User save = userRepository.save(resource);

        //auth
        Auth auth = new Auth();
        auth.setCreateTime(new Date());
        auth.setCreateAccount(login.getId());
        auth.setAuth(resource.getAuth());
        auth.setUser(save.getId());
        authRepository.save(auth);
        return save;
    }

    @Override
    public User update(User login, User resource) {
        if(!login.getRole().equals(RoleEnum.MANAGER.getKey())){//非管理员只能创建本部门普通账户
            resource.setRole(login.getRole());
            resource.setRoleType(RoleTypeEnum.COMMON.getKey());
        }
        resource.setModifyTime(new Date());
        resource.setModifyAccount(login.getId());
        User save = userRepository.save(resource);

        //auth
        Auth auth = new Auth();
        auth.setCreateTime(new Date());
        auth.setCreateAccount(login.getId());
        auth.setAuth(resource.getAuth());
        auth.setUser(save.getId());
        authRepository.save(auth);
        return save;
    }

    @Override
    public List<User> find(User login) {
        List<User> userList = null;
        if(!login.getRole().equals(RoleEnum.MANAGER.getKey())) {
            userList = userRepository.findAll();
        } else if(RoleTypeEnum.MANAGER.getKey().equals(login.getRoleType())){
            userList = userRepository.findOneDepartment(login.getRole());
        }
        return userList;
    }

    @Override
    public User delete(User login, Long id) {
        User one = userRepository.findOne(id);
        one.setModifyTime(new Date());
        one.setModifyAccount(login.getId());
        one.setDelFlag(true);
        return userRepository.save(one);
    }

    @Override
    public User updateName(User login, Long id, String name) {
        User one = userRepository.findOne(id);
        one.setName(name);
        one.setModifyTime(new Date());
        one.setModifyAccount(login.getId());
        return userRepository.save(one);
    }

    @Override
    public User updatePwd(User login, Long id, String pwd) {
        User one = userRepository.findOne(id);
        one.setPassword(CipherUtil.generatePassword(pwd));
        one.setModifyTime(new Date());
        one.setModifyAccount(login.getId());
        return userRepository.save(one);
    }

    @Override
    public User updatePwd(String email, String pwd) {
        User user = userRepository.login(email);
        user.setPassword(CipherUtil.generatePassword(pwd));
        user.setModifyTime(new Date());
        user.setModifyAccount(user.getId());
        return userRepository.save(user);
    }

    @Override
    public Boolean sendCode(String email) {
        String code = CharacterUtils.getRandomString(4);
        List<String> list = new ArrayList<>();
        list.add(email);
        try {
            MailUtils.sendSimpleEmail("验证码", code,list);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        Code c = new Code();
        c.setCreateTime(new Date());
        c.setCode(code);
        c.setEmail(email);
        codeRepository.save(c);
        return true;
    }

    @Override
    public String validCode(String email, String code) {
        Code valid = codeRepository.findValid(email, new Date());
        if(null != valid){
            boolean b = valid.getCode().equals(code);
            if(b){
                return "true";
            }
            return "false";
        }
        return "invalid";
    }
}
