package com.buy507.mall.service.impl;

import com.buy507.mall.dao.UmsAdminPasswordManagementDao;
import com.buy507.mall.model.UmsAdminPasswordModification;
import com.buy507.mall.service.UmsAdminPasswordManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UmsAdminPasswordManagementServiceImpl implements UmsAdminPasswordManagementService {

    @Autowired
    private UmsAdminPasswordManagementDao dao;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    public int update(UmsAdminPasswordModification pass){
//        String oldPassword = passwordEncoder.encode(pass.getOldPsw());
//        String againNewPassword = passwordEncoder.encode(pass.getAgainNewPsw());
//        pass.setOldPsw(oldPassword);
//        pass.setAgainNewPsw(againNewPassword);
        UserDetails userDetails = userDetailsService.loadUserByUsername(pass.getUsername());
        System.out.println(userDetails.getPassword());
        if(passwordEncoder.matches(pass.getOldPsw() , userDetails.getPassword())){
            return dao.updateAdminPsw(pass);
        }else {
            throw new BadCredentialsException("密码不正确");
        }

    }
}
