package com.tuoheng.admin.service.user.update;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.user.EditUserPasswordCodeEnum;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.request.user.UserEditPwdRequest;
import com.tuoheng.admin.service.third.oidc.OidcService;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.CommonUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 修改用户密码业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-16
 */
@Slf4j
@Service
public class UpdateUserPasswordService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OidcService oidcService;

    /**
     * 修改用户密码数据
     *
     * @return
     */
    public JsonResult editPassword(UserEditPwdRequest request) {
        log.info("进入修改用户密码业务接口");
        User user = CurrentUserUtil.getUserInfo();
        JsonResult result = this.check(request, user.getId());
        if (0 != result.getCode()) {
            log.info("修改用户密码业务接口：校验失败：{}", result.getMsg());
            return result;
        }
        user.setPassword(CommonUtils.password(request.getNewPasswordFirst()));
        Integer rowCount = userMapper.updateById(user);
        if (rowCount <= 0) {
            return JsonResult.error(EditUserPasswordCodeEnum.EDIT_IS_FAILED.getCode(), EditUserPasswordCodeEnum.EDIT_IS_FAILED.getMsg());
        }
        // 修改oidc用户密码
        result = oidcService.editUserPassword(user, request.getNewPasswordFirst());
        if (0 != result.getCode()) {
            log.info("修改用户密码业务接口：修改oidc用户密码失败：{}", result.getMsg());
            return result;
        }
        return JsonResult.success();
    }

    /**
     * 检查参数
     *
     * @param request
     * @param userId
     * @return
     */
    private JsonResult check(UserEditPwdRequest request, String userId) {
        log.info("修改用户密码，进入参数校验");
        if (StringUtils.isEmpty(request.getOldPassword())) {
            return JsonResult.error(EditUserPasswordCodeEnum.OLD_PASSWORD_IS_NULL.getCode(), EditUserPasswordCodeEnum.OLD_PASSWORD_IS_NULL.getMsg());
        }
        if (StringUtils.isEmpty(request.getNewPasswordFirst())) {
            return JsonResult.error(EditUserPasswordCodeEnum.NEW_PASSWORD_IS_NULL.getCode(), EditUserPasswordCodeEnum.NEW_PASSWORD_IS_NULL.getMsg());
        }
        if (StringUtils.isEmpty(request.getNewPasswordSecond())) {
            return JsonResult.error(EditUserPasswordCodeEnum.NEW_PASSWORD_IS_NULL.getCode(), EditUserPasswordCodeEnum.NEW_PASSWORD_IS_NULL.getMsg());
        }

        if (!request.getNewPasswordFirst().equals(request.getNewPasswordSecond())) {
            return JsonResult.error(EditUserPasswordCodeEnum.TWO_PASSWORDS_NOT_MATCH.getCode(), EditUserPasswordCodeEnum.TWO_PASSWORDS_NOT_MATCH.getMsg());
        }

        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getId, userId)
                .eq(User::getMark, MarkEnum.VALID.getCode()));

        if (ObjectUtil.isNull(user)) {
            return JsonResult.error(EditUserPasswordCodeEnum.USER_IS_NOT_EXIST.getCode(), EditUserPasswordCodeEnum.USER_IS_NOT_EXIST.getMsg());
        }
        if (!user.getPassword().equals(CommonUtils.password(request.getOldPassword()))) {
            return JsonResult.error(EditUserPasswordCodeEnum.OLD_PASSWORD_INCORRECT.getCode(), EditUserPasswordCodeEnum.OLD_PASSWORD_INCORRECT.getMsg());
        }
        return JsonResult.success();
    }

}
