package cn.iocoder.yudao.module.system.api.face;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.api.face.dto.UserJsonDto;
import cn.iocoder.yudao.module.system.dal.dataobject.face.UserFace;
import cn.iocoder.yudao.module.system.service.face.FaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Service
public class FaceApiImpl implements FaceApi {

    @Autowired
    private FaceService faceService;


    @Override
    public Long addFace(UserJsonDto userJson) {
//        生成雪花id
        long nextId = IdUtil.getSnowflake().nextId();
        CommonResult<Map<String,String>> httpResult = new CommonResult<>();


        // 随机生成一个字符串作为用户ID(测试、实际中禁用)
        UserFace userById = faceService.getUserById(userJson.getUserId());
        if (ObjectUtils.isEmpty(userById)) {
            UserFace user = new UserFace(nextId, userJson.getUserId(), userJson.getPhoneNumber(), userJson.getUserNickname(), userJson.getUrl());
            user.setCarteenId(userJson.getCarteenId());
            faceService.getBaseMapper().insert(user);
        } else {
            nextId = userById.getUserId();
            userById.setUrl(userJson.getUrl());
            faceService.getBaseMapper().updateById(userById);
        }
       return nextId;
    }
}
