package com.rf.psychological.user.rest;

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.user.dao.model.UserFileEntity;
import com.rf.psychological.user.service.UserFileService;
import com.rf.psychological.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:用户头像接口
 * @Author: zsf
 * @Date: 2022/12/26
 */
@Slf4j
@RestController
@RequestMapping("/userFile")
@Api(tags = "用户头像相关接口")
public class UserFileController extends BaseController {

    @Autowired
    private UserFileService fileService;

    @GetMapping("/findByUserId/{userId}")
    @ApiOperation(value = "获取用户头像",notes = "userId:用户id")
    @SafetyProcess
    public Result findByUserId(@PathVariable String userId){
        try {
            UserFileEntity fileEntity = fileService.findByUserId(userId);
            return success(fileEntity,"操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return fail(null,"操作失败");
        }
    }
    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "获取用户头像",notes = "userId:用户id")
    @SafetyProcess
    public Result saveOrUpdate(@RequestBody String jsonParam){
        try {
            JSONObject jsonObject = JSONObject.parseObject(AesEncryptUtils.decrypt(JSONObject.parseObject(jsonParam).getString("data")));
            String userId = jsonObject.getString("userId");
            String path = jsonObject.getString("path");
            if (StringUtils.isEmpty(userId)){
                return fail(null,"用户id不能为空");
            }
            UserFileEntity entity = new UserFileEntity(userId,path);
            entity = fileService.saveOrUpdate(entity);
            return success(entity,"操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return fail(null,"操作失败");
        }
    }
}
