package com.flossom.system.controller;

import com.flossom.common.core.exception.ServiceException;
import com.flossom.system.utils.QrCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/qrcode")
@RefreshScope
public class QrCodeController {

    @Value("${qrcode.url}")
    private String qrcodeUrl;

    @GetMapping("create")
    public void create(HttpServletResponse response, @Param("serial") String serial) throws IOException {
        if (StringUtils.isBlank(serial)) {
            throw new ServiceException("请输入序列号");
        }
        if (StringUtils.isBlank(qrcodeUrl)) {
            throw new ServiceException("请配置二維碼地址");
        }
        QrCodeUtil.buildQrCodeImage(qrcodeUrl + serial, serial, response.getOutputStream());
    }

}
