package com.cool.store.service.enterprise.impl;

import com.cool.store.constants.CommonConstants;
import com.cool.store.dto.login.ModifyPasswordDTO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.SmsCodeTypeEnum;
import com.cool.store.mapper.EnterpriseUserMapper;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.enterprise.EnterpriseUserService;
import com.cool.store.utils.Md5Utils;
import com.cool.store.utils.RedisUtilPool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户
 * </p>
 *
 * @author wangff
 * @since 2026/3/2
 */
@Service(value = "enterpriseUserService")
@Slf4j
public class EnterpriseUserServiceImpl implements EnterpriseUserService {
    @Resource
    private RedisUtilPool redisUtilPool;
    @Resource
    private EnterpriseUserMapper enterpriseUserMapper;

    @Override
    public ResponseResult modifyPassword(ModifyPasswordDTO param) {
        String smsCodeKey = SmsCodeTypeEnum.MODIFY_PWD + ":" + param.getMobile();
        String smsCode = param.getSmsCode();
        String codeValue = redisUtilPool.getString(smsCodeKey);
        if (StringUtils.isBlank(codeValue)) {
            return ResponseResult.fail(ErrorCodeEnum.SMS_CODE_EXPIRE);
        }
        if (!smsCode.equals(codeValue)) {
            return ResponseResult.fail(ErrorCodeEnum.SMS_CODE_ERROR);
        }
        enterpriseUserMapper.modifyPasswordByMobile(param.getMobile(), Md5Utils.md5(param.getPassword() + CommonConstants.USER_AUTH_KEY));
        redisUtilPool.delKey(smsCodeKey);
        return ResponseResult.success();
    }
}
