package com.jxy.system.service.impl;

import com.jxy.common.core.constant.UserConstants;
import com.jxy.common.core.exception.ServiceException;
import com.jxy.common.core.utils.DateUtils;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.common.redis.service.RedisService;
import com.jxy.common.security.utils.SecurityUtils;
import com.jxy.system.entity.StrategyLoginBody;
import com.jxy.system.entity.StrategyResetPwdBody;
import com.jxy.system.entity.StrategyUser;
import com.jxy.system.entity.StrategyUserDto;
import com.jxy.system.mapper.StrategyMapper;
import com.jxy.system.service.StrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description TODO
 * @Date 2023/1/30 11:32
 * @Author 杜懿
 */
@Service
public class StrategyServiceImpl implements StrategyService {


    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public AjaxResult login(StrategyLoginBody loginBody) {

        String username = loginBody.getUserName();
        String password= loginBody.getPassword();

        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(username, password))
        {
            throw new ServiceException("用户名/密码不能为空");
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            throw new ServiceException("用户密码不在指定范围");
        }

        //todo 可以添加校验验证码的功能

        // 查询用户信息
        StrategyUser user = strategyMapper.selectUserByUserName(username);

        if (StringUtils.isNull(user))
        {
            throw new ServiceException("登录用户：" + username + " 不存在");
        }
        if (!SecurityUtils.matchesPassword(password, user.getPassword()))
        {
            //可能是存储的未加密的密码
            if(!password.equals(user.getPassword())){
                throw new ServiceException("密码错误");
            }else {
                user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
            }
        }

        //修改数据库中的用户登录次数和最后一次登录的ip
        user.setLastLanding(DateUtils.getNowDate());

        StrategyUserDto dto = new StrategyUserDto();
        BeanUtils.copyProperties(user,dto);

        //todo 单独存储用户的登录时间和ip
        strategyMapper.updateUser(user);

        return AjaxResult.success(dto);
    }

    @Override
    public AjaxResult resetPwd(StrategyResetPwdBody resetPwdBody) {

        String username = resetPwdBody.getUserName();
        String oldPassword= resetPwdBody.getOldPassword();
        String newPassword= resetPwdBody.getNewPassword();

        StrategyUser user = strategyMapper.selectUserByUserName(username);
        if (StringUtils.isNull(user))
        {
            throw new ServiceException("用户：" + username + " 不存在");
        }
        if (!SecurityUtils.matchesPassword(oldPassword, user.getPassword()))
        {
            //可能是存储的未加密的密码
            if(!oldPassword.equals(user.getPassword())){
                throw new ServiceException("原密码错误");
            }
        }
        user.setPassword(SecurityUtils.encryptPassword(newPassword));

        int i = strategyMapper.updateUser(user);

        if (i < 1){
            return AjaxResult.error("密码修改失败,请稍后再试");
        }

        return AjaxResult.success();
    }

    @Override
    public void logout(String loginName) {

    }
}
