package com.chuanxia.mcp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuanxia.mcp.bean.enums.ResultStatusEnum;
import com.chuanxia.mcp.bean.model.SystemUser;
import com.chuanxia.mcp.bean.vo.Result;
import com.chuanxia.mcp.exception.ResultException;
import com.chuanxia.mcp.mapper.UserMapper;
import com.chuanxia.mcp.service.UserService;
import com.chuanxia.mcp.util.Md5Util;
import com.chuanxia.mcp.util.UidUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Administrator
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, SystemUser> implements UserService {
    private final UserMapper userMapper;
    private final Md5Util md5Util;
    private final UidUtil uidUtil;

    @Override
    public Result login(SystemUser user) {
        LambdaQueryWrapper<SystemUser> query = Wrappers.lambdaQuery();
        String md5 = md5Util.getMd5(user.getPassword());
        query.eq(SystemUser::getUsername, user.getUsername());
        SystemUser result = userMapper.selectOne(query);
        if (result == null) {
            throw new ResultException(ResultStatusEnum.USERNAME_ERROR);
        }
        query.eq(SystemUser::getPassword, md5);
        result = userMapper.selectOne(query);
        if (result == null) {
            throw new ResultException(ResultStatusEnum.PASSWORD_ERROR);
        }
        String token = uidUtil.getUUID();
        result.setToken(token);
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_MONTH, 1);
        Date tokenValidityTime = instance.getTime();
        result.setTokenValidityTime(tokenValidityTime);
        userMapper.updateById(result);
        return Result.success(token);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result register(SystemUser user) {
        LambdaQueryWrapper<SystemUser> query = Wrappers.lambdaQuery();
        query.eq(SystemUser::getUsername, user.getUsername());
        String password = user.getPassword();
        if (StringUtils.isBlank(password)) {
            throw new ResultException(500, "密码不能为空");
        }
        String realName = user.getRealName();
        if (StringUtils.isBlank(realName)) {
            throw new ResultException(500, "真实姓名不能为空");
        }
        if (userMapper.selectOne(query) != null) {
            throw new ResultException(ResultStatusEnum.USERNAME_ALREXISTS);
        }
        String md5 = md5Util.getMd5(password);
        user.setPassword(md5);
        userMapper.insert(user);
        return Result.success();
    }

    @Override
    public SystemUser getLoginUser() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            throw new ResultException(500, "获取当前登录用户失败");
        }
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        LambdaQueryWrapper<SystemUser> query = Wrappers.lambdaQuery();
        query.eq(SystemUser::getToken, token);
        SystemUser systemUser = userMapper.selectOne(query);
        if (systemUser == null) {
            throw new ResultException(500, "获取当前用户信息为空");
        }
        return systemUser;
    }
}

