package com.red.circle.console.app.command.admin;

import com.red.circle.console.app.dto.cmd.admin.UserIdCmd;
import com.red.circle.console.infra.database.rds.entity.admin.User;
import com.red.circle.console.infra.database.rds.service.admin.UserService;
import com.red.circle.console.inner.error.ConsoleErrorCode;
import com.red.circle.framework.core.asserts.ResponseAssert;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.stereotype.Component;

/**
 * 重置用户密码.
 *
 * @author pengliang on 2023/5/15
 */
@Component
@RequiredArgsConstructor
public class UserResetPwdCmdExe {

  private final UserService userService;

  public void execute(UserIdCmd cmd) {
    User user = userService.getById(cmd.getId());
    ResponseAssert.notNull(ConsoleErrorCode.USER_NOT_FOUND, user);
    user.setPassword(Md5Crypt.apr1Crypt(user.getLoginName(), user.getLoginName()));
    userService.updateSelectiveById(user);
  }

}
