package vip.xiaonuo.biz.api.wxapp.service;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.IOUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Base64;

@Slf4j
@Service
public class CreateWxaQrCodeService {

    @Resource
    AccessTokenService accessTokenService;

    /**
     * 请求项目预约码
     *
     * @param depotId   项目id
     * @param depotName 项目名称
     */
    @SneakyThrows
    public String createWxaQrCode(String depotId, String depotName) {
        InputStream inseams = HttpRequest.post("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + accessTokenService.getCreateWxaQrCodeToken())
                .header("Content-Type", "application/json")
                .body(JSONObject.toJSONString(MapUtil.builder("path", String.format("pages/appointment/add?depotId=%s&depotGrainName=%s", depotId, depotName)).build()))
                .timeout(15000).execute().bodyStream();
        byte[] bytes = IOUtils.toByteArray(inseams);
        return "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(bytes);
    }

}
