/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.base.controller.backend;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.base.entity.BackendUser;
import com.codvision.base.filter.LoginFilter;
import com.codvision.base.service.IBackendUserService;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.commoncore.utils.Md5Utils;
import com.codvision.webcore.annotation.ApiSecret;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 框架后台管理登录
 *
 * @author lingee
 * @date 2023/11/2
 */
@ApiSecret(ignore = true)
@RestController
@RequestMapping("/backend/api/user")
@Api(tags = "框架后台管理-认证模块")
public class BackendUserController {

    @Resource
    private IBackendUserService backendUserService;

    @ApiOperation(value = "登录接口")
    @PostMapping(value = "/login")
    public ResponseEntity<BackendUser> getRegion(HttpServletRequest request,
                                                 @ApiParam(value = "用户名", required = true) @RequestParam String username,
                                                 @ApiParam(value = "密码", required = true) @RequestParam String password) {
        password = Base64Decoder.decodeStr(password);
        BackendUser backendUser = backendUserService.getOne(Wrappers.<BackendUser>lambdaQuery().eq(BackendUser::getUsername, username));
        if (ObjectUtils.isEmpty(backendUser) || !DigestUtil.bcryptCheck(password, backendUser.getPassword())) {
            throw new BusinessException("用户名或密码错误");
        }
        backendUser.setPassword(null);
        request.getSession().setAttribute(LoginFilter.LOGIN_SESSION_KEY, backendUser);
        return new ResponseEntity().ok().data(backendUser);
    }

    @ApiOperation(value = "获取基础用户信息")
    @GetMapping(value = "/getUserInfo")
    public ResponseEntity<BackendUser> getBaseUser(HttpServletRequest request) {
        BackendUser backendUser = (BackendUser) request.getSession().getAttribute(LoginFilter.LOGIN_SESSION_KEY);
        if (ObjectUtils.isEmpty(backendUser)) {
            throw new BusinessException("用户未登录");
        }
        return new ResponseEntity().ok().data(backendUser);
    }

    @ApiOperation(value = "退出登录")
    @GetMapping(value = "/logout")
    public ResponseEntity<String> logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return new ResponseEntity().ok();
    }

    @ApiOperation(value = "修改密码接口")
    @PostMapping(value = "/updatePassword")
    public ResponseEntity<String> updatePassword(
            HttpServletRequest request,
            @ApiParam(value = "旧密码", required = true) @RequestParam String oldPassword,
            @ApiParam(value = "新密码", required = true) @RequestParam String newPassword,
            @ApiParam(value = "重复密码", required = true) @RequestParam String againPassword) {
        BackendUser backendUser = (BackendUser) request.getSession().getAttribute(LoginFilter.LOGIN_SESSION_KEY);
        if (ObjectUtils.isEmpty(backendUser)) {
            throw new BusinessException("用户未登录");
        }

        if (!newPassword.equals(againPassword)) {
            throw new BusinessException("两次输入密码不一致");
        }

        BackendUser curUser = backendUserService.getById(backendUser.getId());
        if (!DigestUtil.bcryptCheck(oldPassword, curUser.getPassword())) {
            throw new BusinessException("旧密码输入错误");
        }

        curUser.setPassword(DigestUtil.bcrypt(newPassword));
        backendUserService.updateById(curUser);
        return new ResponseEntity().ok().message("修改成功");
    }
}
