package com.lideeyunji.service.system.controller;

import com.lideeyunji.service.system.config.convert.user.UserConvert;
import com.lideeyunji.service.system.controller.vo.user.profile.UserProfileRespVO;
import com.lideeyunji.service.system.controller.vo.user.profile.UserProfileUpdatePasswordReqVO;
import com.lideeyunji.service.system.controller.vo.user.profile.UserProfileUpdateReqVO;
import com.lideeyunji.service.system.controller.vo.user.user.UserRespVO;
import com.lideeyunji.service.system.entity.AdminUserDO;
import com.lideeyunji.service.system.entity.SocialUserDO;
import com.lideeyunji.service.system.service.*;
import com.lideeyunji.tool.framework.common.enums.UserTypeEnum;
import com.lideeyunji.tool.framework.common.pojo.CommonResult;
import com.lideeyunji.tool.framework.common.util.string.StrUtils;
import com.lideeyunji.tool.framework.permission.core.annotation.DataPermission;
import com.lideeyunji.service.system.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;

import static com.lideeyunji.service.infra.enums.ErrorCodeConstants.FILE_IS_EMPTY;
import static com.lideeyunji.tool.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.lideeyunji.tool.framework.common.pojo.CommonResult.success;
import static com.lideeyunji.tool.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 用户个人中心")
@RestController
@RequestMapping("/system/user/profile")
@Validated
@Slf4j
public class UserProfileController {

    @Resource
    private IAdminUserService userService;
    @Resource
    private IDeptService deptService;
    @Resource
    private IPostService postService;
    @Resource
    private IPermissionService permissionService;
    @Resource
    private IRoleService roleService;
    @Resource
    private ISocialUserService socialService;

    @GetMapping("/get")
    @Operation(tags = "用户管理",summary = "获得登录用户信息")
    @DataPermission(enable = false) // 关闭数据权限，避免只查看自己时，查询不到部门。
    public CommonResult<UserProfileRespVO> getUserProfile() {
        // 获得用户基本信息
        AdminUserDO user = userService.getUser(getLoginUserId());

        Set<Long> deptId = StrUtils.str2setLong(user.getDeptId());

        // 获得社交用户信息
        List<SocialUserDO> socialUsers = socialService.getSocialUserList(user.getId(), UserTypeEnum.ADMIN.getValue());

        UserProfileRespVO vo = UserConvert.INSTANCE.convert(socialUsers,user);

        List<UserRespVO.DeptInfo> deptInfoList = userService.getDeptInfo(user.getId(), deptId);
        List<UserRespVO.IdName> rankInfoList = userService.getRankInfo(user.getId());
        vo.setDeptInfoList(deptInfoList);
        vo.setRankInfoList(rankInfoList);
        return success(vo);
    }

    @PutMapping("/update")
    @Operation(tags = "用户管理",summary = "修改用户个人信息")
    public CommonResult<Boolean> updateUserProfile(@Valid @RequestBody UserProfileUpdateReqVO reqVO) {
        userService.updateUserProfile(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-password")
    @Operation(tags = "用户管理",summary = "修改用户个人密码")
    public CommonResult<Boolean> updateUserProfilePassword(@Valid @RequestBody UserProfileUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(getLoginUserId(), reqVO);
        return success(true);
    }

    @RequestMapping(value = "/update-avatar",
            method = {RequestMethod.POST, RequestMethod.PUT}) // 解决 uni-app 不支持 Put 上传文件的问题
    @Operation(tags = "用户管理",summary = "上传用户个人头像")
    public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw exception(FILE_IS_EMPTY);
        }
        String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream());
        return success(avatar);
    }

}
