package com.template.controller;


import cn.hutool.core.codec.Base64;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson.JSON;
import com.template.api.SmartQrcodeControllerAPI;
import com.template.common.utils.TimeExchange;
import com.template.model.pojo.SmartUser;
import com.template.model.result.CommonResult;
import com.template.model.vo.BaseImageVo;
import com.template.model.vo.QrcodeImageVo;
import com.template.services.SmartUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * <p>
 * 扫码记录 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-01-18
 */
@RestController
public class SmartQrcodeController implements SmartQrcodeControllerAPI {

    @Autowired
    private SmartUserService smartUserService;

    @Override
    public CommonResult generateQrcode(int userId) {
        SmartUser user = smartUserService.getSmartById(userId);
        if(user == null){
            return CommonResult.fail("用户信息无效，无法生成二维码");
        }

        QrcodeImageVo qiv = new QrcodeImageVo();
        qiv.setId(user.getId());
        qiv.setPhone(user.getPhone());
        qiv.setTimestamp(TimeExchange.getTime());

        String qrStr = JSON.toJSONString(qiv);

        BaseImageVo result = new BaseImageVo();
        result.setQrcode(qrStr);

        return CommonResult.ok(result);
    }
}

