package com.qiaoba.module.system.controller;

import com.qiaoba.api.system.entity.dto.ResetUserBasicInfoDto;
import com.qiaoba.api.auth.utils.SecurityUtil;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.module.system.service.SysProfileService;
import com.qiaoba.module.system.service.SysUserPostService;
import com.qiaoba.module.system.service.SysUserRoleService;
import com.qiaoba.module.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 个人信息 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/19 11:25
 */
@RestController
@RequestMapping("/system/user/profile")
@RequiredArgsConstructor
@Tag(name = "用户管理-个人中心")
public class SysProfileController {

    private final SysProfileService sysProfileService;
    private final SysUserService sysUserService;
    private final SysUserRoleService sysUserRoleService;
    private final SysUserPostService sysUserPostService;

    @GetMapping
    @Operation(summary = "基本信息")
    public AjaxResult profile() {
        String userId = SecurityUtil.getLoginUserId();
        AjaxResult ajax = AjaxResult.success(sysUserService.selectById(userId, false));
        ajax.put("roleGroup", sysUserRoleService.selectRoleGroup(userId));
        ajax.put("postGroup", sysUserPostService.selectPostGroup(userId));
        return ajax;
    }

    @PutMapping("/update-avatar")
    @Operation(summary = "更改头像")
    public AjaxResult updateAvatar(@RequestParam("file") MultipartFile file) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("imgUrl", sysProfileService.updateAvatar(file));
        return ajax;
    }

    @PutMapping("/update-password")
    @Operation(summary = "更改密码")
    public AjaxResult updatePassword(String oldPassword, String newPassword) {
        sysProfileService.updatePassword(oldPassword, newPassword);
        return AjaxResult.success("密码更改成功");
    }

    @PutMapping
    @Operation(summary = "更改信息")
    public AjaxResult updateBasicInfo(@RequestBody ResetUserBasicInfoDto dto) {
        sysProfileService.updateBasicInfo(dto);
        return AjaxResult.success("基本信息更改成功");
    }
}
